Quickstart
This quickstart shows how to process a log dataset (we use data from the AIT Log Data Set V2.0).
Tutorials
We developed three tutorials that show how to work with DetectMate using a few straightforward examples:
- Run individually: Execute each component separately within a script.
- Run basic pipeline: Execute all types of components together in a simple pipeline.
- Run expert pipeline: Execute all types of components using a Redis connector and multi-source logs as the input.
Before you start
Before starting, follow the next steps in your work directory:
-
Copy the logs from in here and the matching templates from here.
-
Install DetectMateLibrary dependency:
pip install detectmatelibrary
Run individually
In the first example, we will execute the components one by one to better understand their functionality. You can follow the tutorial by copying and pasting the different code sections into your script.
0. Define configuration
First, we need to set up all the variables, such as the paths and the parameters used by the DetectMate methods. As shown below, all the methods can be initialized within the same dictionary. We will explain all the steps bellow.
# Define your PATH to get the data
template_path = f"{PATH}audit_templates.txt"
log_path = f"{PATH}audit.log"
config_dict = {
"parsers": {
"TreeMatcher": {
"method_type": "tree_matcher",
"params": {
"path_templates": template_path,
"log_format": "type=<Type> msg=audit(<Time>): <Content>",
}
}
},
"detectors": {
"NewValueDetector": {
"method_type": "new_value_detector",
"auto_config": True,
"params": {
"data_use_configure": 1000,
"data_use_training": 1,
}
},
"CharsetDetector": {
"method_type": "charset_detector",
"auto_config": True,
"params": {
"data_use_configure": 1000,
"data_use_training": 1,
}
}
},
"alert_aggregators": {
"TimeDeltaAggregator": {
"method_type": "time_delta_aggregator",
"delta": 4.0,
}
}
}
In this example, we will work with audit log data. The code below shows what the logs look like before they are processed.
from detectmatelibrary.helper.from_to import From
print_one = True # just print one time the output
for log in From.log(None, log_path, do_process=False):
print(log)
break # Remove the break to see more raw logs
1. Parse the logs
By default, the logs are not parsed. To parse them, we first initialize a parser and then process the log lines.
from detectmatelibrary.parsers.tree_matcher import TemplateCppTreeMatcher
print(parser := TemplateCppTreeMatcher(name="TreeMatcher", config=config_dict))
parsed_logs = []
print_one = True # just print one time the output
for parsed_log in From.log(parser, log_path):
if print_one:
print(parsed_log)
print_one = False
parsed_logs.append(parsed_log)
2. Run the detectors
After parsing the logs, we can run the detectors to identify anomalies. In this example, we’ll use two detectors.
from detectmatelibrary.detectors.charset_detector import CharsetDetector
from detectmatelibrary.detectors.new_value_detector import NewValueDetector
print(detector1 := CharsetDetector(name="CharsetDetector", config=config_dict))
print(detector2 := NewValueDetector(name="NewValueDetector", config=config_dict))
alert_logs = []
print_one = True # just print one time the output
for parsed_log in parsed_logs:
if (alert1 := detector1.process(parsed_log)) is not None:
alert_logs.append(alert1)
if (alert2 := detector2.process(parsed_log)) is not None:
alert_logs.append(alert2)
if print_one:
print(alert2)
print_one = False
3. Run the alert aggregation
Finally, to combine all the alerts collected from the various detectors, an alert aggregation method is executed. This is the final output of DetectMate.
from detectmatelibrary.alert_aggregation.time_delta import TimeDeltaAggregation
print(
agg := TimeDeltaAggregation(name="TimeDeltaAggregator", config=config_dict)
)
print_one = True # just print one time the output
for alert in alert_logs:
if (agg_alert := agg.process(alert)) is not None:
if print_one:
print(agg_alert)
print_one = False
Run basic pipeline
Although you can run each method separately to gain more control over the individual steps, it isn’t required. You can also execute everything together as a single pipeline. Next, we will run the methods described earlier using the generic DetectMateLibray pipeline.
1. Initialize the pipeline
First, we initialze the pipeline.
from detectmatelibrary.pipeline import PipelineConfig, Pipeline
config = PipelineConfig(
do_parsing=True, # Use the parser
do_aggregation=True, # Use the aggregator
parsers=[{
"parsers": {
"TreeMatcher": {
"method_type": "tree_matcher",
"params": {
"path_templates": template_path,
"log_format": "type=<Type> msg=audit(<Time>): <Content>",
}
}
}
}],
n_workers=1, # You can add parallelization for the detectors
detectors=[
{
"detectors": {
"NewValueDetector": {
"method_type": "new_value_detector",
"auto_config": True,
"params": {
"data_use_configure": 1000,
"data_use_training": 1,
}
}
},
},
{
"detectors": {
"CharsetDetector": {
"method_type": "charset_detector",
"auto_config": True,
"params": {
"data_use_configure": 1000,
"data_use_training": 1,
}
},
},
}
],
aggregator={
"alert_aggregators": {
"TimeDeltaAggregator": {
"method_type": "time_delta_aggregator",
"delta": 4.0,
}
}
}
)
pipeline = Pipeline(config=config)
2. Run the pipeline
Finally, we run the pipeline.
print_one = True # just print one time the output
for agg_alert in From.log(pipeline, log_path):
if agg_alert is not None and print_one:
print(agg_alert)
print_one = False
Run expert pipeline
The earlier pipeline processes only a single log data source, and intermediate schemas are discarded, while any output is saved permanently. In this example, two log sources will run at the same time, and the detector and alert-aggregation outputs will be stored in a Redis service.
For this use case, we also need to download other types of logs, including downloading and adding these Apache logs.
0. Prepare logs
First, we will prepare the data. In this example, we will simply upload the Audit and Apache datasets and ensure they have distinct log sources.
from detectmatelibrary.helper.from_to import From
# Define your PATH to get the data
template_path = f"{PATH}audit_templates.txt"
log_path = f"{PATH}audit.log"
apache_path = f"{PATH}apache.log"
all_logs = []
for apache_log in From.log(None, apache_path, do_process=False):
apache_log["logID"] = "Apache_" + apache_log["logID"]
apache_log["logSource"] = "Apache_logs"
all_logs.append(apache_log)
for audit_log in From.log(None, log_path, do_process=False):
audit_log["logID"] = "Audit_" + audit_log["logID"]
audit_log["logSource"] = "Audit_logs"
all_logs.append(audit_log)
1. Prepare Redis
In this example, we’ll set up a Redis service using Docker Compose. Create a Docker Compose file named compose.yaml and add the following services.
services:
redis:
image: redis:7-alpine
container_name: redis
restart: unless-stopped
ports:
- "6379:6379"
command: ["redis-server", "--appendonly", "yes"]
volumes:
- redis_data:/data
redisinsight:
image: redis/redisinsight:latest
container_name: redisinsight
restart: unless-stopped
depends_on:
- redis
ports:
- "5540:5540"
environment:
# Database 0
RI_REDIS_HOST_0: redis
RI_REDIS_PORT_0: "6379"
RI_REDIS_ALIAS_0: local-db0
RI_REDIS_DB_0: "0"
# Database 1
RI_REDIS_HOST_1: redis
RI_REDIS_PORT_1: "6379"
RI_REDIS_ALIAS_1: local-db1
RI_REDIS_DB_1: "1"
# Database 2
RI_REDIS_HOST_2: redis
RI_REDIS_PORT_2: "6379"
RI_REDIS_ALIAS_2: local-db2
RI_REDIS_DB_2: "2"
volumes:
- redisinsight_data:/data
volumes:
redis_data:
redisinsight_data:
Run it using the command below. After it starts up, you’ll be able to access the Redis dataset and the dashboard.
docker compose up -d
To reach the dashboard, launch your preferred web browser and navigate to the following URL:
http://localhost:5540/
3. Initialize the pipeline
Now that the data and services are in place, we can return to our script and implement our pipeline. Previously, we relied on a dictionary to initialize the methods within the pipeline. In this case, we’ll use the Config classes instead. We will run two parsers—one for each log source—and four detectors, again two per source.
To link where the data should go, we first use the log_source argument in the parsers to specify which type of logs to process. For detectors, the parser argument is used to indicate which parser to pull data from.
We will store the alert data so that dataset 0 contains Audit alerts, dataset 1 contains Apache alerts, and dataset 2 contains all aggregated alerts.
from detectmatelibrary.parsers.tree_matcher import TemplateCppTreeMatcherConfig
from detectmatelibrary.parsers.autoparser import AutoParserConfig
from detectmatelibrary.detectors.new_value_detector import NewValueDetectorConfig
from detectmatelibrary.detectors.charset_detector import CharsetDetectorConfig
from detectmatelibrary.alert_aggregation.logid_count import LogIDCountAggegationConfig
from detectmatelibrary.pipeline import PipelineConfig, Pipeline
config = PipelineConfig(
do_parsing=True,
do_aggregation=True,
n_workers=1,
parsers=[
TemplateCppTreeMatcherConfig(
path_templates=template_path,
log_format="type=<Type> msg=audit(<Time>): <Content>",
log_source="Audit_logs",
),
AutoParserConfig(
fix_type="Apache",
log_source="Apache_logs",
data_use_training=1,
),
],
detectors=[
NewValueDetectorConfig(
data_use_configure=1000,
data_use_training=1,
auto_config=True,
parser="TreeMatcher_from_Audit_logs",
connector_name="Redis",
connector_url="http://localhost:6379:0",
),
CharsetDetectorConfig(
data_use_configure=1000,
data_use_training=1,
auto_config=True,
parser="TreeMatcher_from_Audit_logs",
connector_name="Redis",
connector_url="http://localhost:6379:0",
),
NewValueDetectorConfig(
data_use_configure=100,
data_use_training=1,
auto_config=True,
parser="AutoParser_from_Apache_logs",
connector_name="Redis",
connector_url="http://localhost:6379:1",
),
CharsetDetectorConfig(
data_use_configure=500,
data_use_training=1,
auto_config=True,
parser="AutoParser_from_Apache_logs",
connector_name="Redis",
connector_url="http://localhost:6379:1",
),
],
aggregator=LogIDCountAggegationConfig(
max_count=2,
connector_name="Redis",
connector_url="http://localhost:6379:2",
)
)
pipeline = Pipeline(config=config)
4. Run pipeline
Let’s execute the full pipeline to retrieve the data.
from tqdm import tqdm
for log in tqdm(all_logs):
pipeline.process(log)
5. Get the data
To retrieve the data, we can simply initialize the schemas using the various IDs.
import detectmatelibrary.schemas as sc
# Get an alert from Audit
sc.set_connector(connector="Redis", url="http://localhost:6379:0")
print(sc.DetectorSchema.from_id("CharsetDetector_from_TreeMatcher_from_Audit_logs_15"))
# Get an alert from Apache
sc.set_connector(connector="Redis", url="http://localhost:6379:1")
print(sc.DetectorSchema.from_id(
"NewValueDetector_from_AutoParser_from_Apache_logs_377"
))
# Get an aggreate alert
sc.set_connector(connector="Redis", url="http://localhost:6379:2")
print(sc.AggregateSchema.from_id("LogIDCountAggregator_15"))
Or check the dashboard:
http://localhost:5540/