-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsmtp-url.bro
182 lines (149 loc) · 4.79 KB
/
smtp-url.bro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
##! A script for handling URLs in SMTP traffic. This script does
##! two things. It logs URLs discovered in SMTP traffic. It
##! also records them in a bloomfilter and looks for them to be
##! visited through HTTP requests.
##!
##! Authors: Aashish Sharma <[email protected]>
##! Seth Hall <[email protected]>
@load base/utils/urls
module SMTP_URL;
export {
redef enum Log::ID += { Links_LOG };
type Info: record {
## When the email was seen.
ts: time &log;
## Unique ID for the connection.
uid: string &log;
## Connection details.
id: conn_id &log;
## Depth of the email into the SMTP exchange.
trans_depth: count &log;
## The host field extracted from the discovered URL.
host: string &log &optional;
## URL that was discovered.
url: string &log &optional;
};
redef enum Notice::Type += {
## A link discovered in an email appears to have been clicked.
Link_in_Email_Clicked,
## An email was seen in email that matched the pattern in
## `SMTP_URL::suspicious_urls`
Suspicious_URL,
## Certain file extensions in email links can be watched for
## with the pattern in `SMTP_URL::suspicious_file_extensions`
Suspicious_File_Extension,
## URL with a dotted IP address seen in an email.
Dotted_URL
};
const suspicious_file_extensions = /\.([rR][aA][rR]|[eE][xX][eE]|[zZ][iI][pP])$/ &redef;
const suspicious_urls = /googledocs?/ &redef;
const ignore_file_types = /\.([gG][iI][fF]|[pP][nN][gG]|[jJ][pP][gG]|[xX][mM][lL]|[jJ][pP][eE]?[gG]|[cC][sS][sS])$/ &redef;
## The following
const ignore_mail_originators: set[subnet] = { } &redef;
const ignore_mailfroms = /bro@|alerts|reports/ &redef;
const ignore_notification_emails = {"[email protected]", "[email protected]"} &redef;
const ignore_site_links = /http:\/\/.*\.example\.com\/|http:\/\/.*\.example\.net/ &redef;
}
# The bloomfilter that stores all of the links seen in email.
global mail_links_bf: opaque of bloomfilter;
redef record connection += {
smtp_url: Info &optional;
};
event bro_init() &priority=5
{
# initialize the bloomfilter
mail_links_bf = bloomfilter_basic_init(0.00000001, 10000000, "SMTP_URL");
Log::create_stream(Links_LOG, [$columns=Info]);
}
function extract_host(name: string): string
{
local split_on_slash = split(name, /\//);
return split_on_slash[3];
}
function log_smtp_urls(c: connection, url: string)
{
c$smtp_url = Info($ts = c$smtp$ts,
$uid = c$uid,
$id = c$id,
$trans_depth = c$smtp$trans_depth,
$host = extract_host(url),
$url = url);
Log::write(SMTP_URL::Links_LOG, c$smtp_url);
}
event SMTP_URL::email_data(f: fa_file, data: string)
{
# Grab the connection.
local c: connection;
for ( cid in f$conns )
{
c = f$conns[cid];
break;
}
if( c$smtp?$mailfrom && ignore_mailfroms in c$smtp$mailfrom )
{
return;
}
if ( c$smtp?$to )
{
for ( to in c$smtp$to )
{
if ( to in ignore_notification_emails )
return;
}
}
local mail_info = Files::describe(f);
local urls = find_all_urls(data);
for ( link in urls )
{
#local link = sub(a,/(http|https):\/\//,"");
#local _bf_lookup = bloomfilter_lookup(mail_links_bf, link);
#if (link !in mail_links_bf && ignore_file_types !in link )
#if ((_bf_lookup == 0) && ignore_file_types !in link )
if ( ignore_file_types !in link )
{
bloomfilter_add(mail_links_bf, link);
log_smtp_urls(c, link);
if ( suspicious_file_extensions in link )
{
NOTICE([$note = Suspicious_File_Extension,
$msg = fmt("Suspicious file extension embedded in URL %s from %s", link, c$id$orig_h),
$sub = mail_info,
$conn = c]);
}
if ( suspicious_urls in link )
{
NOTICE([$note = Suspicious_URL,
$msg = fmt("Suspicious text embedded in URL %s from %s", link, c$smtp$uid),
$sub = mail_info,
$conn = c]);
}
if ( /([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}.*/ in link )
{
NOTICE([$note = Dotted_URL,
$msg = fmt("Embedded IP address in URL %s from %s", link, c$id$orig_h),
$sub = mail_info,
$conn = c]);
}
}
}
}
event file_over_new_connection(f: fa_file, c: connection, is_orig: bool)
{
if ( f$source == "SMTP" && c?$smtp &&
c$id$orig_h !in ignore_mail_originators )
{
Files::add_analyzer(f, Files::ANALYZER_DATA_EVENT, [$stream_event=SMTP_URL::email_data]);
}
}
event http_message_done(c: connection, is_orig: bool, stat: http_message_stat) &priority=-3
{
local str = HTTP::build_url_http(c$http);
if ( bloomfilter_lookup(SMTP_URL::mail_links_bf, str) > 0 &&
ignore_file_types !in str &&
ignore_site_links !in str)
{
NOTICE([$note=SMTP_URL::Link_in_Email_Clicked,
$msg=fmt("URL %s ", str),
$conn=c]);
}
}