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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
# File 'lib/doroParser.rb', line 60
def analyze_bintraffic(pcaps)
dns_list = Hash.new
hosts = []
@insertdb.begin_t
pcaps.each do |dump|
!dump['sample'].nil? && !dump['hash'].nil? && !dump['pcapr_id'].nil? or next
LOGGER_PARSER.info "PARSER", "Analyzing file: ".yellow + dump['sample']
LOGGER_PARSER.info "PARSER", "Analyzing pcaprid: ".yellow + dump['pcapr_id'].gsub(/\s+/, "")
LOGGER_PARSER.debug "PARSER", "Analyzing dump: ".yellow + dump['hash'].gsub(/\s+/, "") if VERBOSE
downloadir = "#{DoroSettings.env[:analysis_dir]}/#{dump['anal_id']}/downloads"
begin
xtractr = Doroxtractr.create "http://#{DoroSettings.pcapr[:host]}:#{DoroSettings.pcapr[:port]}/pcaps/1/pcap/#{dump['pcapr_id'].gsub(/\s+/, "")}"
rescue => e
LOGGER_PARSER.fatal "PARSER", "Can't create a XTRACTR instance, try with nextone"
LOGGER_PARSER.debug "PARSER", "#{$!}"
LOGGER_PARSER.debug "PARSER", e
next
end
LOGGER_PARSER.info "PARSER", "Scanning network flows and searching for unknown host IPs".yellow
xtractr.flows.each { |flow|
begin
flowdeep = xtractr.flows("flow.id:#{flow.id}")
if flow.dport == 135 or flow.dport == 445
LOGGER_PARSER.info "PARSER", "Netbios connections, skipping flow" unless NONETBIOS
next
end
title = flow.title[0..200].gsub(/'/,"")
localip = xtractr.flows.first.src.address
localnet = IPAddr.new(localip)
multicast = IPAddr.new("224.0.0.0/4")
unless(@insertdb.select("host_ips", "ip", flow.dst.address).one? || hosts.include?(flow.dst.address))
LOGGER_PARSER.info "PARSER", "Analyzing #{flow.dst.address}".yellow
hosts << flow.dst.address
dest = flow.dst.address
unless(localnet.include?(flow.dst.address) || multicast.include?(flow.dst.address))
geo = Geoinfo.new(flow.dst.address.to_s)
geoval = ["default", geo.coord, geo.country, geo.city, geo.updated, geo.asn]
LOGGER_PARSER.debug "GEO", "Geo-values for #{flow.dst.address.to_s}: " + geo.country + " " + geo.city + " " + geo.coord if VERBOSE
if geo.coord != "null"
LOGGER_PARSER.debug "DB", " Inserting geo values for #{flow.dst.address.to_s} : #{geo.country}".blue if VERBOSE
@insertdb.insert("geoinfo",geoval)
geoval = "currval('dorothy.geoinfo_id_seq')"
else
LOGGER_PARSER.warn "DB", " No Geovalues found for #{flow.dst.address.to_s}".red if VERBOSE
geoval = "null"
end
else
LOGGER_PARSER.warn "PARSER", "#{flow.dst.address} skipped while searching for GeoInfo (it's a local network))".yellow
geoval = 'null'
dest = localip
end
hostname = (dns_list[dest].nil? ? "null" : dns_list[dest])
hostval = [dest, geoval, "null", "null", true, "null", "null", get_time, "default", hostname]
if !@insertdb.insert("host_ips",hostval)
LOGGER_PARSER.debug "DB", " Skipping flow #{flow.id}: #{flow.src.address} > #{flow.dst.address}" if VERBOSE
next
end
else
LOGGER_PARSER.debug "PARSER", "Host already #{flow.dst.address} known, skipping..." if VERBOSE
end
flowvals = [flow.src.address, flow.dst.address, flow.sport, flow.dport, flow.bytes, dump['hash'], flow.packets, "default", flow.proto, flow.service.name, title, "null", flow.duration, flow.time, flow.id ]
if !@insertdb.insert("flows",flowvals)
LOGGER_PARSER.info "PARSER", "Skipping flow #{flow.id}: #{flow.src.address} > #{flow.dst.address}"
next
end
LOGGER_PARSER.debug("DB", "Inserting flow #{flow.id} - #{flow.title}".blue) if VERBOSE
flowid = "currval('dorothy.connections_id_seq')"
service = flow.service.name
case flow.proto
when 6 then
streamdata = xtractr.streamdata(flow.id)
case service
when "HTTP" then
http = DoroHttp.new(flowdeep)
if http.method =~ /GET|POST/
LOGGER_PARSER.info "HTTP", "FOUND an HTTP request".white
LOGGER_PARSER.info "HTTP", "HTTP #{http.method}".white + " #{http.uri}".yellow
t = http.uri.split('/')
filename = (t[t.length - 1].nil? ? "noname-#{flow.id}" : t[t.length - 1])
if http.method =~ /POST/
role_values = [CCDROP, flow.dst.address]
@insertdb.insert("host_roles", role_values ) unless @insertdb.select("host_roles", "role", role_values[0], "host_ip", role_values[1]).one?
http.data = xtractr.flowcontent(flow.id)
LOGGER_PARSER.debug "DB", "POST DATA SAVED IN THE DB"
end
if http.contype =~ /application/ LOGGER_PARSER.info "HTTP", "FOUND an Application Type".white
LOGGER_PARSER.debug "DB", " Inserting #{filename} downloaded file info" if VERBOSE
flowdeep.each do |flow|
flow.contents.each do |c|
LOGGER_PARSER.debug("DB", "Inserting downloaded http file info from #{flow.dst.address.to_s}".blue) if VERBOSE
downvalues = [ DoroFile.sha2(c.body), flowid, downloadir, filename ]
@insertdb.insert("downloads", downvalues )
role_values = [CCSUPPORT, flow.dst.address]
@insertdb.insert("host_roles", role_values ) unless @insertdb.select("host_roles", "role", role_values[0], "host_ip", role_values[1]).one?
LOGGER_PARSER.debug "HTTP", "Saving downloaded file into #{downloadir}".white if VERBOSE
c.save("#{downloadir}/#{filename}")
end
end
end
httpvalues = "default, '#{http.method.downcase}', '#{http.uri}', #{http.size}, #{http.ssl}, #{flowid}, E'#{Insertdb.escape_bytea(http.data)}' "
LOGGER_PARSER.debug "DB", " Inserting http data info from #{flow.dst.address.to_s}".blue if VERBOSE
@insertdb.raw_insert("http_data", httpvalues)
else
LOGGER_PARSER.warn "HTTP", "Not a regular HTTP traffic on flow #{flow.id}".yellow
LOGGER_PARSER.info "PARSER", "Trying to guess if it is IRC".white
if Parser.guess(streamdata.inspect).class.inspect =~ /IRC/
ircvalues = search_irc(streamdata)
ircvalues.each do |ircvalue|
LOGGER_PARSER.debug "DB", " Inserting IRC DATA info from #{flow.dst.address.to_s}".blue if VERBOSE
@insertdb.raw_insert("irc_data", ircvalue )
role_values = [CCIRC, flow.dst.address]
@insertdb.insert("host_roles", role_values ) unless @insertdb.select("host_roles", "role", role_values[0], "host_ip", role_values[1]).one?
end
else
LOGGER_PARSER.info "PARSER", "NO-IRC".red
end
end
when "SMTP" then
LOGGER_PARSER.info "SMTP", "FOUND an SMTP request..".white
streamdata.each do |m|
mailfrom = 'null'
mailto = 'null'
mailcontent = 'null'
mailsubject = 'null'
mailhcmd = 'null'
mailhcont = 'null'
rdata = ['null', 'null']
case m[1]
when 0
if Parser::SMTP.(m[0])
@email = Parser::SMTP.new(m[0])
LOGGER_PARSER.info "SMTP", "[A]".white + @email.hcmd + " " + @email.hcont
if Parser::SMTP.hasbody?(m[0])
@email.body = Parser::SMTP.body(m[0])
mailto = @email.body.to
mailfrom = @email.body.from
mailsubject = @email.body.subject.gsub(/'/,"") end
end
when 1
rdata = Parser::SMTP.response(m[0]) if Parser::SMTP.response(m[0])
LOGGER_PARSER.info "SMTP", "[R]".white + rdata[0] + " " + rdata[1]
rdata[0] = 'null' if rdata[0].empty?
rdata[1] = 'null' if rdata[1].empty?
end
mailvalues = [mailfrom, mailto, mailsubject, mailcontent, "default", flowid, mailhcmd, mailhcont, rdata[0], rdata[1].gsub(/'/,"")] @insertdb.insert("emails", mailvalues )
end
when "FTP" then
LOGGER_PARSER.info "FTP", "FOUND an FTP request".white
when "TCP" then
LOGGER_PARSER.info "TCP", "FOUND GENERIC TCP TRAFFIC - may be a netbios scan".white
LOGGER_PARSER.info "PARSER", "Trying see if it is IRC traffic".white
if Parser.guess(streamdata.inspect).class.inspect =~ /IRC/
ircvalues = search_irc(streamdata)
ircvalues.each do |ircvalue|
LOGGER_PARSER.debug "DB", " Inserting IRC DATA info from #{flow.dst.address.to_s}".blue if VERBOSE
@insertdb.raw_insert("irc_data", ircvalue )
role_values = [CCIRC, flow.dst.address]
@insertdb.insert("host_roles", role_values ) unless @insertdb.select("host_roles", "role", CCIRC, "host_ip", flow.dst.address).one?
end
end
else
LOGGER_PARSER.info "PARSER", "Unknown traffic, try see if it is IRC traffic"
if Parser.guess(streamdata.inspect).class.inspect =~ /IRC/
ircvalues = search_irc(streamdata)
ircvalues.each do |ircvalue|
LOGGER_PARSER.debug "DB", " Inserting IRC DATA info from #{flow.dst.address.to_s}".blue if VERBOSE
@insertdb.raw_insert("irc_data", ircvalue )
role_values = [CCIRC, flow.dst.address]
@insertdb.insert("host_roles", role_values ) unless @insertdb.select("host_roles", "role", CCIRC, "host_ip", flow.dst.address).one?
end
end
end
when 17 then
case service
when "DNS" then
@i = 0
@p = []
flowdeep.each do |flow|
flow.each do |pkt|
@p[@i] = pkt.payload
@i = @i + 1
end
end
@p.each do |d|
begin
dns = DoroDNS.new(d)
dnsvalues = ["default", dns.name, dns.cls_i.inspect, dns.qry?, dns.ttl, flowid, dns.address.to_s, dns.data, dns.type_i.inspect]
LOGGER_PARSER.debug "DB", " Inserting DNS data from #{flow.dst.address.to_s}".blue if VERBOSE
unless @insertdb.insert("dns_data", dnsvalues )
LOGGER_PARSER.error "DB", " Error while Inserting DNS data".blue
nex
end
dnsid = @insertdb.find_seq("dns_id_seq").first['currval']
if dns.qry?
LOGGER_PARSER.info "DNS", "DNS Query:".white + " #{dns.name}".yellow + " class #{dns.cls_i} type #{dns.type_i}"
else
dns_list.merge!( dns.address.to_s => dnsid)
LOGGER_PARSER.info "DNS", "DNS Answer:".white + " #{dns.name}".yellow + " class #{dns.cls} type #{dns.type} ttl #{dns.ttl} " + "#{dns.address}".yellow
end
rescue => e
LOGGER_PARSER.error "DB", "Something went wrong while adding a DNS entry into the DB (packet malformed?) - The packet will be skipped"
LOGGER_PARSER.debug "DB", "#{$!}" if VERBOSE
LOGGER_PARSER.debug "DB", e if VERBOSE
end
end
end
when 1 then
else
LOGGER_PARSER.warn "PARSER", "Unknown protocol: #{flow.id} -- Proto #{flow.proto}".yellow
end
rescue => e
LOGGER_PARSER.error "PARSER", "Error while analyzing flow #{flow.id}"
LOGGER_PARSER.debug "PARSER", "#{e.inspect} BACKTRACE: #{e.backtrace}"
LOGGER_PARSER.info "PARSER", "Flow #{flow.id} will be skipped"
next
end
}
@insertdb.set_analyzed(dump['hash'])
@insertdb.commit
@insertdb.close
end
end
|