sethhall / bro_scripts

Analysis scripts for the Bro Intrusion Detection System

This URL has Read+Write access

bro_scripts / smtp-ext.bro
100644 395 lines (334 sloc) 11.568 kb
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
@load global-ext
@load smtp
 
type smtp_ext_session_info: record {
msg_id: string &default="";
in_reply_to: string &default="";
helo: string &default="";
mailfrom: string &default="";
rcptto: set[string];
date: string &default="";
from: string &default="";
to: set[string];
reply_to: string &default="";
subject: string &default="";
x_originating_ip: string &default="";
received_from_originating_ip: string &default="";
first_received: string &default="";
second_received: string &default="";
last_reply: string &default=""; # last message the server sent to the client
files: set[string];
path: string &default="";
is_webmail: bool &default=F; # This is not being set yet.
agent: string &default="";
 
# These are used during processing and are likely less useful.
current_header: string &default="";
in_received_from_headers: bool &default=F;
received_finished: bool &default=F;
};
 
function default_smtp_ext_session_info(): smtp_ext_session_info
{
local tmp: set[string] = set();
local tmp2: set[string] = set();
local tmp3: set[string] = set();
return [$rcptto=tmp, $to=tmp2, $files=tmp3];
}
 
# Define the generic smtp-ext event that can be handled from other scripts
global smtp_ext: event(id: conn_id, si: smtp_ext_session_info);
 
module SMTP;
 
export {
redef enum Notice += {
# Thrown when a local host receives a reply mentioning an smtp block list
SMTP_BL_Error_Message,
# Thrown when the local address is seen in the block list error message
SMTP_BL_Blocked_Host,
# When mail seems to originate from a suspicious location
SMTP_Suspicious_Origination,
};
 
# Direction to capture the full "Received from" path. (from the Hosts enum)
# RemoteHosts - only capture the path until an internal host is found.
# LocalHosts - only capture the path until the external host is discovered.
# AllHosts - capture the entire path.
const mail_path_capture: Hosts = LocalHosts &redef;
 
# Places where it's suspicious for mail to originate from.
# this requires all-capital letter, two character country codes (e.x. US)
const suspicious_origination_countries: set[string] = {} &redef;
const suspicious_origination_networks: set[subnet] = {} &redef;
 
# This matches content in SMTP error messages that indicate some
# block list doesn't like the connection/mail.
const smtp_bl_error_messages =
/spamhaus\.org\//
| /sophos\.com\/security\//
| /spamcop\.net\/bl/
| /cbl\.abuseat\.org\//
| /sorbs\.net\//
| /bsn\.borderware\.com\//
| /mail-abuse\.com\//
| /bbl\.barracudacentral\.com\//
| /psbl\.surriel\.com\//
| /antispam\.imp\.ch\//
| /dyndns\.com\/.*spam/
| /rbl\.knology\.net\//
| /intercept\.datapacket\.net\// &redef;
 
global conn_info: table[conn_id] of smtp_ext_session_info
&read_expire=5mins
&redef;
 
}
 
# Examples for how to handle notices from this script.
# (define these in a local script)...
#redef notice_policy += {
# # Send email if a local host is on an SMTP watch list
# [$pred(n: notice_info) =
# { return (n$note == SMTP::SMTP_BL_Blocked_Host && is_local_addr(n$conn$id$orig_h)); },
# $result = NOTICE_EMAIL],
#};
 
function find_address_in_smtp_header(header: string): string
{
local ips = find_ip_addresses(header);
if ( |ips| > 1 )
return ips[2];
else if ( |ips| > 0 )
return ips[1];
else
return "";
}
 
function end_smtp_extended_logging(c: connection)
{
local id = c$id;
local conn_log = conn_info[id];
 
local loc: geo_location;
local ip: addr;
if ( conn_log$x_originating_ip != "" )
{
ip = to_addr(conn_log$x_originating_ip);
loc = lookup_location(ip);
 
if ( loc$country_code in suspicious_origination_countries ||
ip in suspicious_origination_networks )
{
NOTICE([$note=SMTP_Suspicious_Origination,
$msg=fmt("An email originated from %s (%s).", loc$country_code, ip),
$sub=fmt("Subject: %s", conn_log$subject),
$conn=c]);
}
}
 
if ( conn_log$received_from_originating_ip != "" &&
conn_log$received_from_originating_ip != conn_log$x_originating_ip )
{
ip = to_addr(conn_log$received_from_originating_ip);
loc = lookup_location(ip);
 
if ( loc$country_code in suspicious_origination_countries ||
ip in suspicious_origination_networks )
{
NOTICE([$note=SMTP_Suspicious_Origination,
$msg=fmt("An email originated from %s (%s).", loc$country_code, ip),
$sub=fmt("Subject: %s", conn_log$subject),
$conn=c]);
}
}
 
# Throw the event for other scripts to handle
event smtp_ext(id, conn_log);
 
delete conn_info[id];
}
 
event smtp_reply(c: connection, is_orig: bool, code: count, cmd: string,
                 msg: string, cont_resp: bool)
{
local id = c$id;
# This continually overwrites, but we want the last reply, so this actually works fine.
if ( (code != 421 && code >= 400) &&
id in conn_info )
{
conn_info[id]$last_reply = fmt("%d %s", code, msg);
 
# Raise a notice when an SMTP error about a block list is discovered.
if ( smtp_bl_error_messages in msg )
{
local note = SMTP_BL_Error_Message;
local message = fmt("%s received an error message mentioning an SMTP block list", c$id$orig_h);
 
# Determine if the originator's IP address is in the message.
local ips = find_ip_addresses(msg);
local text_ip = "";
if ( |ips| > 0 && to_addr(ips[1]) == c$id$orig_h )
{
note = SMTP_BL_Blocked_Host;
message = fmt("%s is on an SMTP block list", c$id$orig_h);
}
 
NOTICE([$note=note,
$conn=c,
$msg=message,
$sub=msg]);
}
}
}
 
event smtp_request(c: connection, is_orig: bool, command: string, arg: string) &priority=-5
{
local id = c$id;
if ( id !in smtp_sessions )
return;
 
# In case this is not the first message in a session
if ( ((/^[mM][aA][iI][lL]/ in command && /^[fF][rR][oO][mM]:/ in arg) ) &&
id in conn_info )
{
local tmp_helo = conn_info[id]$helo;
end_smtp_extended_logging(c);
conn_info[id] = default_smtp_ext_session_info();
conn_info[id]$helo = tmp_helo;
}
 
if ( id !in conn_info )
conn_info[id] = default_smtp_ext_session_info();
local conn_log = conn_info[id];
 
if ( /^([hH]|[eE]){2}[lL][oO]/ in command )
conn_log$helo = arg;
 
if ( /^[rR][cC][pP][tT]/ in command && /^[tT][oO]:/ in arg )
add conn_log$rcptto[split1(arg, /:[[:blank:]]*/)[2]];
 
if ( /^[mM][aA][iI][lL]/ in command && /^[fF][rR][oO][mM]:/ in arg )
{
local partially_done = split1(arg, /:[[:blank:]]*/)[2];
conn_log$mailfrom = split1(partially_done, /[[:blank:]]/)[1];
}
}
 
event smtp_data(c: connection, is_orig: bool, data: string) &priority=-5
{
local id = c$id;
 
if ( id !in conn_info )
return;
    
if ( !smtp_sessions[id]$in_header )
{
if ( /^[cC][oO][nN][tT][eE][nN][tT]-[dD][iI][sS].*[fF][iI][lL][eE][nN][aA][mM][eE]/ in data )
{
data = sub(data, /^.*[fF][iI][lL][eE][nN][aA][mM][eE]=/, "");
add conn_info[id]$files[data];
}
return;
}
 
local conn_log = conn_info[id];
# This is to fully construct headers that will tend to wrap around.
if ( /^[[:blank:]]/ in data )
{
data = sub(data, /^[[:blank:]]/, "");
if ( conn_log$current_header == "message-id" )
conn_log$msg_id += data;
else if ( conn_log$current_header == "received" )
conn_log$first_received += data;
else if ( conn_log$in_reply_to == "in-reply-to" )
conn_log$in_reply_to += data;
else if ( conn_log$current_header == "subject" )
conn_log$subject += data;
else if ( conn_log$current_header == "from" )
conn_log$from += data;
else if ( conn_log$current_header == "reply-to" )
conn_log$reply_to += data;
else if ( conn_log$current_header == "agent" )
conn_log$agent += data;
return;
}
conn_log$current_header = "";
 
if ( /^[mM][eE][sS][sS][aA][gG][eE]-[iI][dD]:[[:blank:]]*./ in data )
{
conn_log$msg_id = split1(data, /:[[:blank:]]*/)[2];
conn_log$current_header = "message-id";
}
 
else if ( /^[rR][eE][cC][eE][iI][vV][eE][dD]:[[:blank:]]*./ in data )
{
conn_log$second_received = conn_log$first_received;
conn_log$first_received = split1(data, /:[[:blank:]]*/)[2];
# Fill in the second value in case there is only one hop in the message.
if ( conn_log$second_received == "" )
conn_log$second_received = conn_log$first_received;
 
conn_log$current_header = "received";
}
 
else if ( /^[iI][nN]-[rR][eE][pP][lL][yY]-[tT][oO]:[[:blank:]]*./ in data )
{
conn_log$in_reply_to = split1(data, /:[[:blank:]]*/)[2];
conn_log$current_header = "in-reply-to";
}
 
else if ( /^[dD][aA][tT][eE]:[[:blank:]]*./ in data )
{
conn_log$date = split1(data, /:[[:blank:]]*/)[2];
conn_log$current_header = "date";
}
 
else if ( /^[fF][rR][oO][mM]:[[:blank:]]*./ in data )
{
conn_log$from = split1(data, /:[[:blank:]]*/)[2];
conn_log$current_header = "from";
}
 
else if ( /^[tT][oO]:[[:blank:]]*./ in data )
{
add conn_log$to[split1(data, /:[[:blank:]]*/)[2]];
conn_log$current_header = "to";
}
 
else if ( /^[rR][eE][pP][lL][yY]-[tT][oO]:[[:blank:]]*./ in data )
{
conn_log$reply_to = split1(data, /:[[:blank:]]*/)[2];
conn_log$current_header = "reply-to";
}
 
else if ( /^[sS][uU][bB][jJ][eE][cC][tT]:[[:blank:]]*./ in data )
{
conn_log$subject = split1(data, /:[[:blank:]]*/)[2];
conn_log$current_header = "subject";
}
 
else if ( /^[xX]-[oO][rR][iI][gG][iI][nN][aA][tT][iI][nN][gG]-[iI][pP]:[[:blank:]]*./ in data )
{
local addresses = find_ip_addresses(data);
if ( |addresses| > 0 )
conn_log$x_originating_ip = addresses[1];
else
conn_log$x_originating_ip = data;
conn_log$current_header = "x-originating-ip";
}
 
else if ( /^[xX]-[mM][aA][iI][lL][eE][rR]:[[:blank:]]*./ |
/^[uU][sS][eE][rR]-[aA][gG][eE][nN][tT]:[[:blank:]]*./ in data )
{
conn_log$agent = split1(data, /:[[:blank:]]*/)[2];
conn_log$current_header = "agent";
}
 
}
 
# This event handler builds the "Received From" path by reading the
# headers in the mail
event smtp_data(c: connection, is_orig: bool, data: string)
{
local id = c$id;
 
if ( id !in conn_info ||
id !in smtp_sessions ||
!smtp_sessions[id]$in_header )
return;
 
local conn_log = conn_info[id];
 
# If we've decided that we're done watching the received headers, we're done.
if ( conn_log$received_finished )
return;
 
if ( /^[rR][eE][cC][eE][iI][vV][eE][dD]:/ in data )
conn_log$in_received_from_headers = T;
else if ( /^[[:blank:]]/ !in data )
conn_log$in_received_from_headers = F;
 
if ( conn_log$in_received_from_headers ) # currently seeing received from headers
{
local text_ip = find_address_in_smtp_header(data);
 
if ( text_ip == "" )
return;
 
local ip = to_addr(text_ip);
 
# I don't care if mail bounces around on localhost
if ( ip == 127.0.0.1 ) return;
 
# This overwrites each time.
conn_log$received_from_originating_ip = text_ip;
 
local ellipsis = "";
if ( !resp_matches_hosts(ip, mail_path_capture) &&
ip !in private_address_space )
{
ellipsis = "... ";
conn_log$received_finished=T;
}
 
if (conn_log$path == "")
conn_log$path = fmt("%s%s -> %s -> %s", ellipsis, ip, id$orig_h, id$resp_h);
else
conn_log$path = fmt("%s%s -> %s", ellipsis, ip, conn_log$path);
}
else if ( !smtp_sessions[id]$in_header && !conn_log$received_finished )
conn_log$received_finished=T;
}
 
event connection_finished(c: connection) &priority=5
{
if ( c$id in conn_info )
end_smtp_extended_logging(c);
}
 
event connection_state_remove(c: connection) &priority=5
{
if ( c$id in conn_info )
end_smtp_extended_logging(c);
}