Hello,
I am planning to apply the summary statistics framework to filter and aggregate the Ethernet fields: Mac Addresses from my packet capture file, .pcapng. I attempted certain approaches but getting errors. As a newcomer in the field, maybe I am missing certain conditions that I need to fulfill.
@load base/frameworks/sumstats
module Ethernet;
# Defining a record for logging MAC address statistics
type EtherMacStats: record {
ts: time; # Timestamp of the summary event
mac: string; # The MAC address (source or destination)
direction: string; # Direction of the MAC address (src or dst)
count: int; # Count of packets associated with the MAC address
};
# Initializing the summary statistics framework
event zeek_init() {
Log::create_stream("ether_mac_stats", [$columns=[ts, mac, direction, count]]);
}
# Handlinge Ethernet frames
event packet(f: fa_file, p: packet) {
if ( p$payload$ethernet?$src && p$payload$ethernet?$dst ) {
local src_mac = fmt("%s", p$payload$ethernet$src);
local dst_mac = fmt("%s", p$payload$ethernet$dst);
# Observe source MAC address
SumStats::observe("src_mac_count", src_mac);
# Observe destination MAC address
SumStats::observe("dst_mac_count", dst_mac);
}
}
# Creating summary statistics for MAC addresses
event SumStats::create_summarizer() {
# Summarize source MAC addresses
SumStats::create([
$name = "src_mac_count",
$epoch = 1 sec, # Interval of 1 second
$reducers = [SumStats::SUM],
$threshold = 1, # Report if count is 1 or more
]);
# Summarize destination MAC addresses
SumStats::create([
$name = "dst_mac_count",
$epoch = 1 sec, # Interval of 1 second
$reducers = [SumStats::SUM],
$threshold = 1, # Report if count is 1 or more
]);
}
# Handle the results of the summarization
event SumStats::result(name: string, key: any, result: double, num: double) {
if ( name == "src_mac_count" ) {
Log::write("ether_mac_stats", [$ts=network_time(), $mac=key, $direction="src", $count=int(result)]);
}
else if ( name == "dst_mac_count" ) {
Log::write("ether_mac_stats", [$ts=network_time(), $mac=key, $direction="dst", $count=int(result)]);
}
}