Skip to content

Commit c453bde

Browse files
committed
Land rapid7#3125, fix zip imports
2 parents 0c4b71c + 33ca577 commit c453bde

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/msf/core/db.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,21 +2932,26 @@ def import(args={}, &block)
29322932
def import_filetype_detect(data)
29332933

29342934
if data and data.kind_of? Zip::ZipFile
2935-
raise DBImportError.new("The zip file provided is empty.") if data.entries.empty?
2935+
if data.entries.empty?
2936+
raise DBImportError.new("The zip file provided is empty.")
2937+
end
2938+
29362939
@import_filedata ||= {}
29372940
@import_filedata[:zip_filename] = File.split(data.to_s).last
29382941
@import_filedata[:zip_basename] = @import_filedata[:zip_filename].gsub(/\.zip$/,"")
29392942
@import_filedata[:zip_entry_names] = data.entries.map {|x| x.name}
2940-
begin
2941-
@import_filedata[:zip_xml] = @import_filedata[:zip_entry_names].grep(/^(.*)_[0-9]+\.xml$/).first || raise
2942-
@import_filedata[:zip_wspace] = @import_filedata[:zip_xml].to_s.match(/^(.*)_[0-9]+\.xml$/)[1]
2943-
@import_filedata[:type] = "Metasploit ZIP Report"
2944-
return :msf_zip
2945-
rescue ::Interrupt
2946-
raise $!
2947-
rescue ::Exception
2948-
raise DBImportError.new("The zip file provided is not a Metasploit ZIP report")
2943+
2944+
xml_files = @import_filedata[:zip_entry_names].grep(/^(.*)\.xml$/)
2945+
2946+
# TODO This check for our zip export should be more extensive
2947+
if xml_files.empty?
2948+
raise DBImportError.new("The zip file provided is not a Metasploit Zip Export")
29492949
end
2950+
2951+
@import_filedata[:zip_xml] = xml_files.first
2952+
@import_filedata[:type] = "Metasploit Zip Export"
2953+
2954+
return :msf_zip
29502955
end
29512956

29522957
if data and data.kind_of? PacketFu::PcapFile
@@ -3624,16 +3629,13 @@ def import_msf_zip(args={}, &block)
36243629
end
36253630
}
36263631

3627-
36283632
data.entries.each do |e|
36293633
target = ::File.join(@import_filedata[:zip_tmp],e.name)
3630-
::File.unlink target if ::File.exists?(target) # Yep. Deleted.
36313634
data.extract(e,target)
36323635
if target =~ /^.*.xml$/
36333636
target_data = ::File.open(target, "rb") {|f| f.read 1024}
36343637
if import_filetype_detect(target_data) == :msf_xml
36353638
@import_filedata[:zip_extracted_xml] = target
3636-
#break
36373639
end
36383640
end
36393641
end

0 commit comments

Comments
 (0)