zeek/scripts/policy/protocols/smtp/detect-suspicious-orig.zeek
Benjamin Bannier d5fd29edcd Prefer explicit construction to coercion in record initialization
While we support initializing records via coercion from an expression
list, e.g.,

    local x: X = [$x1=1, $x2=2];

this can sometimes obscure the code to readers, e.g., when assigning to
value declared and typed elsewhere. The language runtime has a similar
overhead since instead of just constructing a known type it needs to
check at runtime that the coercion from the expression list is valid;
this can be slower than just writing the readible code in the first
place, see #4559.

With this patch we use explicit construction, e.g.,

    local x = X($x1=1, $x2=2);
2025-07-11 16:28:37 -07:00

52 lines
1.4 KiB
Text

@load base/frameworks/notice/main
@load base/protocols/smtp/main
module SMTP;
export {
redef enum Notice::Type += {
Suspicious_Origination
};
## Places where it's suspicious for mail to originate from represented
## as all-capital, two character country codes (e.g., US). It requires
## Zeek to be built with GeoIP support.
option suspicious_origination_countries: set[string] = {};
option suspicious_origination_networks: set[subnet] = {};
}
event log_smtp(rec: Info)
{
local ip: addr;
local loc: geo_location;
if ( rec?$x_originating_ip )
{
ip = rec$x_originating_ip;
loc = lookup_location(ip);
if ( (loc?$country_code &&
loc$country_code in suspicious_origination_countries) ||
ip in suspicious_origination_networks )
{
NOTICE(Notice::Info($note=Suspicious_Origination,
$msg=fmt("An email originated from %s (%s).",
loc?$country_code ? loc$country_code : "", ip),
$id=rec$id));
}
}
if ( rec?$path )
{
ip = rec$path[|rec$path|-1];
loc = lookup_location(ip);
if ( (loc?$country_code &&
loc$country_code in suspicious_origination_countries) ||
ip in suspicious_origination_networks )
{
NOTICE(Notice::Info($note=Suspicious_Origination,
$msg=fmt("Based up Received headers, email originated from %s (%s).", loc?$country_code ? loc$country_code : "", ip),
$id=rec$id));
}
}
}