Skip to content

Commit 36e2953

Browse files
authored
Land rapid7#19211, update db manager to no longer default updated_at to created_at value
2 parents a92e37e + cf97b36 commit 36e2953

File tree

32 files changed

+153
-100
lines changed

32 files changed

+153
-100
lines changed

lib/msf/core/db_manager/cred.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def report_auth_info(opts={})
211211

212212
# Update the timestamp
213213
if cred.changed?
214-
msf_import_timestamps(opts,cred)
214+
msf_assign_timestamps(opts, cred)
215215
cred.save!
216216
end
217217

lib/msf/core/db_manager/host.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def report_host(opts)
274274
host_state_changed(host, ostate) if host.state != ostate
275275

276276
if host.changed?
277-
msf_import_timestamps(opts, host)
277+
msf_assign_timestamps(opts, host)
278278
host.save!
279279
end
280280
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid

lib/msf/core/db_manager/import.rb

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -480,23 +480,84 @@ def import_filetype_detect(data)
480480
raise Msf::DBImportError.new("Could not automatically determine file type")
481481
end
482482

483-
# Handles timestamps from Metasploit Express/Pro imports.
484-
def msf_import_timestamps(opts,obj)
483+
def msf_import_service(opts)
484+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
485+
report_service(normalised_import_timestamp_opts)
486+
end
487+
488+
def msf_import_vuln(opts)
489+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
490+
report_vuln(normalised_import_timestamp_opts)
491+
end
492+
493+
def msf_import_note(opts)
494+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
495+
report_note(normalised_import_timestamp_opts)
496+
end
497+
498+
def msf_import_host(opts)
499+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
500+
report_host(normalised_import_timestamp_opts)
501+
end
502+
503+
def msf_import_task(opts)
504+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
505+
report_task(normalised_import_timestamp_opts)
506+
end
507+
508+
def msf_import_user(opts)
509+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
510+
report_user(normalised_import_timestamp_opts)
511+
end
512+
513+
def msf_import_loot(opts)
514+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
515+
report_loot(normalised_import_timestamp_opts)
516+
end
517+
518+
def msf_import_web_site(opts)
519+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
520+
report_web_site(normalised_import_timestamp_opts)
521+
end
522+
523+
def msf_import_web_page(opts)
524+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
525+
report_web_page(normalised_import_timestamp_opts)
526+
end
527+
528+
def msf_import_web_vuln(opts)
529+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
530+
report_web_vuln(normalised_import_timestamp_opts)
531+
end
532+
533+
def msf_import_artifact(opts)
534+
normalised_import_timestamp_opts = msf_normalise_import_timestamps(opts)
535+
report_artifact(normalised_import_timestamp_opts)
536+
end
537+
538+
# Assigns created_at and updated_at time stamps to an object.
539+
def msf_assign_timestamps(opts,obj)
485540
obj.created_at = opts["created_at"] if opts["created_at"]
486541
obj.created_at = opts[:created_at] if opts[:created_at]
487-
obj.updated_at = opts["updated_at"] ? opts["updated_at"] : obj.created_at
488-
obj.updated_at = opts[:updated_at] ? opts[:updated_at] : obj.created_at
489-
return obj
542+
obj.updated_at = opts["updated_at"] if opts["updated_at"]
543+
obj.updated_at = opts[:updated_at] if opts[:updated_at]
544+
end
545+
546+
# Handles timestamps from Metasploit Express/Pro imports.
547+
def msf_normalise_import_timestamps(opts)
548+
opts[:created_at] ||= (opts["created_at"] || ::Time.now.utc)
549+
opts[:updated_at] ||= (opts["updated_at"] || opts[:created_at])
550+
opts
490551
end
491552

492553
def report_import_note(wspace,addr)
493554
if @import_filedata.kind_of?(Hash) && @import_filedata[:filename] && @import_filedata[:filename] !~ /msfe-nmap[0-9]{8}/
494-
report_note(
495-
:workspace => wspace,
496-
:host => addr,
497-
:type => 'host.imported',
498-
:data => @import_filedata.merge(:time=> Time.now.utc)
499-
)
555+
msf_import_note(
556+
:workspace => wspace,
557+
:host => addr,
558+
:type => 'host.imported',
559+
:data => @import_filedata.merge(:time=> Time.now.utc)
560+
)
500561
end
501562
end
502563

lib/msf/core/db_manager/import/gpp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def import_gpp_xml(args = {}, &block)
2727
end
2828

2929
# Store entire file as loot, including metadata
30-
report_loot(
30+
msf_import_loot(
3131
workspace: wspace,
3232
path: args[:filename],
3333
name: File.basename(args[:filename]),

lib/msf/core/db_manager/import/ip360/v3.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ def import_ip360_xml_v3(args={}, &block)
8383
host_hash[:name] = hname.to_s.strip if hname
8484
host_hash[:mac] = mac.to_s.strip.upcase if mac
8585

86-
hobj = report_host(host_hash)
86+
hobj = msf_import_host(host_hash)
8787

8888
yield(:os, os) if block
8989
if os
90-
report_note(
90+
msf_import_note(
9191
:workspace => wspace,
9292
:task => args[:task],
9393
:host => hobj,
@@ -131,15 +131,15 @@ def import_ip360_xml_v3(args={}, &block)
131131
# IP360 v3 svc
132132
def handle_ip360_v3_svc(wspace,hobj,port,proto,hname,task=nil)
133133
addr = hobj.address
134-
report_host(:workspace => wspace, :host => hobj, :state => Msf::HostState::Alive, :task => task)
134+
msf_import_host(:workspace => wspace, :host => hobj, :state => Msf::HostState::Alive, :task => task)
135135

136136
info = { :workspace => wspace, :host => hobj, :port => port, :proto => proto, :task => task }
137137
if hname != "unknown" and hname[-1,1] != "?"
138138
info[:name] = hname
139139
end
140140

141141
if port.to_i != 0
142-
report_service(info)
142+
msf_import_service(info)
143143
end
144144
end
145145

@@ -153,7 +153,7 @@ def handle_ip360_v3_vuln(wspace,hobj,port,proto,hname,vulnid,vulnname,cves,bids,
153153
end
154154

155155
if port.to_i != 0
156-
report_service(info)
156+
msf_import_service(info)
157157
end
158158

159159
refs = []
@@ -181,6 +181,6 @@ def handle_ip360_v3_vuln(wspace,hobj,port,proto,hname,vulnid,vulnname,cves,bids,
181181
vuln[:proto] = proto
182182
end
183183

184-
report_vuln(vuln)
184+
msf_import_vuln(vuln)
185185
end
186186
end

lib/msf/core/db_manager/import/libpcap.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def import_libpcap(args={}, &block)
3535
unless( bl.include?(saddr) || rfc3330_reserved(saddr))
3636
yield(:address,saddr) if block and !seen_hosts.keys.include?(saddr)
3737
unless seen_hosts[saddr]
38-
report_host(
38+
msf_import_host(
3939
:workspace => wspace,
4040
:host => saddr,
4141
:state => Msf::HostState::Alive,
@@ -48,7 +48,7 @@ def import_libpcap(args={}, &block)
4848
unless( bl.include?(daddr) || rfc3330_reserved(daddr))
4949
yield(:address,daddr) if block and !seen_hosts.keys.include?(daddr)
5050
unless seen_hosts[daddr]
51-
report_host(
51+
msf_import_host(
5252
:workspace => wspace,
5353
:host => daddr,
5454
:state => Msf::HostState::Alive,
@@ -63,7 +63,7 @@ def import_libpcap(args={}, &block)
6363
pkt.tcp_src < 1024 # If it's a low port, assume it's a proper service.
6464
if seen_hosts[saddr]
6565
unless seen_hosts[saddr].include? [pkt.tcp_src,"tcp"]
66-
report_service(
66+
msf_import_service(
6767
:workspace => wspace, :host => saddr,
6868
:proto => "tcp", :port => pkt.tcp_src,
6969
:state => Msf::ServiceState::Open,
@@ -79,7 +79,7 @@ def import_libpcap(args={}, &block)
7979
[saddr,daddr].each do |xaddr|
8080
if seen_hosts[xaddr]
8181
unless seen_hosts[xaddr].include? [pkt.udp_src,"udp"]
82-
report_service(
82+
msf_import_service(
8383
:workspace => wspace, :host => xaddr,
8484
:proto => "udp", :port => pkt.udp_src,
8585
:state => Msf::ServiceState::Open,
@@ -93,7 +93,7 @@ def import_libpcap(args={}, &block)
9393
elsif pkt.udp_src < 1024 # Probably a service
9494
if seen_hosts[saddr]
9595
unless seen_hosts[saddr].include? [pkt.udp_src,"udp"]
96-
report_service(
96+
msf_import_service(
9797
:workspace => wspace, :host => saddr,
9898
:proto => "udp", :port => pkt.udp_src,
9999
:state => Msf::ServiceState::Open,
@@ -142,7 +142,7 @@ def inspect_single_packet_http(pkt,wspace,args)
142142
if pkt.payload =~ /^HTTP\x2f1\x2e[01]/n
143143
http_server_match = pkt.payload.match(/\nServer:\s+([^\r\n]+)[\r\n]/n)
144144
if http_server_match.kind_of?(MatchData) and http_server_match[1]
145-
report_service(
145+
msf_import_service(
146146
:workspace => wspace,
147147
:host => pkt.ip_saddr,
148148
:port => pkt.tcp_src,
@@ -172,7 +172,7 @@ def inspect_single_packet_http(pkt,wspace,args)
172172
# this point, we'll just believe everything the packet says -- validation ought
173173
# to come later.
174174
user,pass = b64_cred.unpack("m*").first.split(/:/,2)
175-
report_service(
175+
msf_import_service(
176176
:workspace => wspace,
177177
:host => pkt.ip_daddr,
178178
:port => pkt.tcp_dst,

lib/msf/core/db_manager/import/metasploit_framework/xml.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def import_msf_note_element(note, allow_yaml, note_data={})
8585
note_data[datum.gsub("-","_")] = nils_for_nulls(note.at(datum).text.to_s.strip)
8686
end
8787
}
88-
report_note(note_data)
88+
msf_import_note(note_data)
8989
end
9090

9191
# Imports web_form element using Msf::DBManager#report_web_form.
@@ -294,7 +294,7 @@ def parse_web_site(web, wspace, allow_yaml, &block)
294294
end
295295
}
296296

297-
report_web_site(info)
297+
msf_import_web_site(info)
298298
yield(:web_site, "#{info[:host]}:#{info[:port]} (#{info[:vhost]})") if block
299299
end
300300

@@ -331,7 +331,7 @@ def parse_host(host, wspace, blacklist, allow_yaml, btag, args, &block)
331331
end
332332
}
333333
host_address = host_data[:host].dup # Preserve after report_host() deletes
334-
hobj = report_host(host_data)
334+
hobj = msf_import_host(host_data)
335335

336336
host.xpath("host_details/host_detail").each do |hdet|
337337
hdet_data = {}
@@ -371,7 +371,7 @@ def parse_host(host, wspace, blacklist, allow_yaml, btag, args, &block)
371371
end
372372
end
373373
}
374-
report_service(service_data)
374+
msf_import_service(service_data)
375375
end
376376

377377
host.xpath('notes/note').each do |note|
@@ -417,7 +417,7 @@ def parse_host(host, wspace, blacklist, allow_yaml, btag, args, &block)
417417
end
418418
end
419419

420-
vobj = report_vuln(vuln_data)
420+
vobj = msf_import_vuln(vuln_data)
421421

422422
vuln.xpath("notes/note").each do |note|
423423
note_data = {}

lib/msf/core/db_manager/import/metasploit_framework/zip.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def parse_zip_task(task, wspace, bl, allow_yaml, btag, args, basedir, host_info,
144144
if ::File.exist?(new_task)
145145
::File.unlink new_task # Delete it, and don't report it.
146146
else
147-
report_task(task_info) # It's new, so report it.
147+
msf_import_task(task_info) # It's new, so report it.
148148
end
149149
::FileUtils.copy(task_info[:orig_path], new_task)
150150
yield(:msf_task, new_task) if block

lib/msf/core/db_manager/import/nessus.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def handle_nessus(wspace, hobj, port, nasl, plugin_name, severity, data,task=nil
3030
if name and name != "unknown" and name[-1,1] != "?"
3131
info[:name] = name
3232
end
33-
report_service(info)
33+
msf_import_service(info)
3434

3535
if nasl.nil? || nasl.empty? || nasl == 0 || nasl == "0"
3636
return
@@ -78,6 +78,6 @@ def handle_nessus(wspace, hobj, port, nasl, plugin_name, severity, data,task=nil
7878
:refs => refs,
7979
:task => task,
8080
}
81-
report_vuln(vuln_info)
81+
msf_import_vuln(vuln_info)
8282
end
8383
end

lib/msf/core/db_manager/import/nessus/nbe.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def import_nessus_nbe(args={}, &block)
4545
yield(:address,addr) if block
4646
end
4747

48-
hobj_map[ addr ] ||= report_host(:host => addr, :workspace => wspace, :task => args[:task])
48+
hobj_map[ addr ] ||= msf_import_host(:host => addr, :workspace => wspace, :task => args[:task])
4949

5050
# Match the NBE types with the XML severity ratings
5151
case type
@@ -61,7 +61,7 @@ def import_nessus_nbe(args={}, &block)
6161
end
6262
if nasl == "11936"
6363
os = data.match(/The remote host is running (.*)\\n/)[1]
64-
report_note(
64+
msf_import_note(
6565
:workspace => wspace,
6666
:task => args[:task],
6767
:host => hobj_map[ addr ],

0 commit comments

Comments
 (0)