forked from arkime/arkime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmolochwise.bro
194 lines (170 loc) · 4.84 KB
/
molochwise.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
@load base/frameworks/files
@load base/frameworks/notice
@load frameworks/files/hash-all-files
module MolochWise;
export {
redef enum Notice::Type += {
Match
};
## File types to attempt matching
const match_file_types = /application\/x-dosexec/ |
/application\/vnd.ms-cab-compressed/ |
/application\/pdf/ |
/application\/x-shockwave-flash/ |
/application\/x-java-applet/ |
/application\/jar/ |
/video\/mp4/ &redef;
# Set in local.bro with: redef MolochWise:base_url = "http://wise:80801";
const base_url = "" &redef;
const lookup_timeout: interval = 30min &redef;
const lookup_count = 20 &redef;
}
global wise_matched: table[string] of string_vec &create_expire=lookup_timeout;
global wise_lookingup: set[string] &create_expire=1min;
global wise_next_lookups: table[string] of set[string] = table();
function wiseAvailable(key: string): bool
{
return when (key in wise_matched) {
return T;
}
timeout 30 sec {
return F;
}
}
function process_ip(key: string)
{
local matches = wise_matched[key];
if (matches[0] == "BRONONE") {
return;
}
for (match_index in matches) {
local parts = split_string1(matches[match_index], /\tBROSUB\t/);
local n: Notice::Info = Notice::Info($note=Match,
$msg=fmt("WISE hit on %s by %s", key, parts[0]),
$sub=parts[1]);
NOTICE(n);
}
}
function process_md5(key: string, fi: Notice::FileInfo)
{
local matches = wise_matched[key];
if (matches[0] == "BRONONE") {
return;
}
for (match_index in matches) {
local parts = split_string1(matches[match_index], /\tBROSUB\t/);
local n: Notice::Info = Notice::Info($note=Match,
$msg=fmt("WISE hit on %s by %s", key, parts[0]),
$sub=parts[1]);
Notice::populate_file_info2(fi, n);
NOTICE(n);
}
}
function requestWise()
{
print "MOLOCH", |wise_matched|, |wise_lookingup|, |wise_next_lookups["ip"]|, |wise_next_lookups["md5"]|;
for (wtype in wise_next_lookups) {
if (|wise_next_lookups[wtype]| == 0) {
next;
}
local data = "";
for (key in wise_next_lookups[wtype]) {
delete(wise_next_lookups[wtype][key]);
add(wise_lookingup[key]);
if (data == "") {
data += key;
} else {
data += "," + key;
}
}
local url = fmt("%s/bro/%s?items=%s", base_url, wtype, data);
local req: ActiveHTTP::Request = ActiveHTTP::Request($url=url, $method="GET");
when (local res = ActiveHTTP::request(req)) {
if (|res| > 0) {
if (res?$body) {
local lines = split_string(res$body, /\tBRONEXT\t/);
for (line_index in lines) {
local parts = split_string1(lines[line_index], /\tBROIS\t/);
wise_matched[parts[0]] = split_string(parts[1], /\tBROMORE\t/);
delete(wise_lookingup[parts[0]]);
}
}
}
return;
}
}
}
function wise_lookup_md5(key: string, fi: Notice::FileInfo)
{
if (key in wise_matched) {
process_md5(key, fi);
} else if (key in wise_lookingup || key in wise_next_lookups["md5"]) {
when (wiseAvailable(key)) {
process_md5(key, fi);
}
timeout 30 sec {
}
} else {
add(wise_next_lookups["md5"][key]);
if (|wise_next_lookups["md5"]| > lookup_count) {
requestWise();
}
when (wiseAvailable(key)) {
process_md5(key, fi);
}
timeout 30 sec {
}
}
}
function wise_lookup_ip(ip: addr)
{
local key = fmt("%s", ip);
if (key in wise_matched) {
process_ip(key);
} else if (key in wise_lookingup || key in wise_next_lookups["ip"]) {
when (wiseAvailable(key)) {
process_ip(key);
}
timeout 30 sec {
}
} else {
add(wise_next_lookups["ip"][key]);
if (|wise_next_lookups["ip"]| > lookup_count) {
requestWise();
}
when (wiseAvailable(key)) {
process_ip(key);
}
timeout 30 sec {
}
}
}
event connection_established(c: connection)
{
if ( c$orig$state == TCP_ESTABLISHED &&
c$resp$state == TCP_ESTABLISHED ) {
wise_lookup_ip(c$id$orig_h);
wise_lookup_ip(c$id$resp_h);
}
}
event file_hash(f: fa_file, kind: string, hash: string)
{
if ( kind == "md5" && f$info?$mime_type && match_file_types in f$info$mime_type) {
wise_lookup_md5(hash, Notice::create_file_info(f));
}
flush_all();
}
# Make sure nothing is waiting more then 5 seconds
event wisetimer()
{
if (|wise_next_lookups["md5"]| > 0 || |wise_next_lookups["ip"]| > 0) {
requestWise();
}
schedule 5sec {wisetimer()};
}
event bro_init()
{
wise_next_lookups["md5"] = set();
wise_next_lookups["ip"] = set();
schedule 5sec {wisetimer()};
}