Skip to content

Commit a53a68c

Browse files
committed
Refactor db_nmap and fix the save option
1 parent 23df66b commit a53a68c

File tree

1 file changed

+35
-55
lines changed
  • lib/msf/ui/console/command_dispatcher

1 file changed

+35
-55
lines changed

lib/msf/ui/console/command_dispatcher/db.rb

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ class Db
1414
require 'tempfile'
1515

1616
include Msf::Ui::Console::CommandDispatcher
17-
18-
# TODO: Not thrilled about including this entire module for just store_local.
19-
include Msf::Auxiliary::Report
20-
2117
include Metasploit::Credential::Creation
2218

2319
#
@@ -1752,15 +1748,14 @@ def cmd_db_nmap(*args)
17521748
return unless active?
17531749
::ActiveRecord::Base.connection_pool.with_connection {
17541750
if (args.length == 0)
1755-
print_status("Usage: db_nmap [nmap options]")
1751+
print_status("Usage: db_nmap [--save | [--help | -h]] [nmap options]")
17561752
return
17571753
end
1758-
save = false
17591754
arguments = []
17601755
while (arg = args.shift)
17611756
case arg
1762-
when 'save'
1763-
save = active?
1757+
when '--save'
1758+
save = true
17641759
when '--help', '-h'
17651760
cmd_db_nmap_help
17661761
return
@@ -1778,55 +1773,47 @@ def cmd_db_nmap(*args)
17781773
return
17791774
end
17801775

1781-
fd = Tempfile.new('dbnmap')
1782-
fd.binmode
1783-
1784-
fo = Tempfile.new('dbnmap')
1785-
fo.binmode
1786-
1787-
# When executing native Nmap in Cygwin, expand the Cygwin path to a Win32 path
1788-
if(Rex::Compat.is_cygwin and nmap =~ /cygdrive/)
1789-
# Custom function needed because cygpath breaks on 8.3 dirs
1790-
tout = Rex::Compat.cygwin_to_win32(fd.path)
1791-
fout = Rex::Compat.cygwin_to_win32(fo.path)
1792-
arguments.push('-oX', tout)
1793-
arguments.push('-oN', fout)
1794-
else
1795-
arguments.push('-oX', fd.path)
1796-
arguments.push('-oN', fo.path)
1797-
end
1776+
fd = Rex::Quickfile.new(['msf-db-nmap-', '.xml'], Msf::Config.local_directory)
17981777

17991778
begin
1800-
nmap_pipe = ::Open3::popen3([nmap, 'nmap'], *arguments)
1801-
temp_nmap_threads = []
1802-
temp_nmap_threads << framework.threads.spawn("db_nmap-Stdout", false, nmap_pipe[1]) do |np_1|
1803-
np_1.each_line do |nmap_out|
1804-
next if nmap_out.strip.empty?
1805-
print_status("Nmap: #{nmap_out.strip}")
1806-
end
1779+
# When executing native Nmap in Cygwin, expand the Cygwin path to a Win32 path
1780+
if(Rex::Compat.is_cygwin and nmap =~ /cygdrive/)
1781+
# Custom function needed because cygpath breaks on 8.3 dirs
1782+
tout = Rex::Compat.cygwin_to_win32(fd.path)
1783+
arguments.push('-oX', tout)
1784+
else
1785+
arguments.push('-oX', fd.path)
18071786
end
18081787

1809-
temp_nmap_threads << framework.threads.spawn("db_nmap-Stderr", false, nmap_pipe[2]) do |np_2|
1810-
np_2.each_line do |nmap_err|
1811-
next if nmap_err.strip.empty?
1812-
print_status("Nmap: '#{nmap_err.strip}'")
1788+
begin
1789+
nmap_pipe = ::Open3::popen3([nmap, 'nmap'], *arguments)
1790+
temp_nmap_threads = []
1791+
temp_nmap_threads << framework.threads.spawn("db_nmap-Stdout", false, nmap_pipe[1]) do |np_1|
1792+
np_1.each_line do |nmap_out|
1793+
next if nmap_out.strip.empty?
1794+
print_status("Nmap: #{nmap_out.strip}")
1795+
end
1796+
end
1797+
1798+
temp_nmap_threads << framework.threads.spawn("db_nmap-Stderr", false, nmap_pipe[2]) do |np_2|
1799+
np_2.each_line do |nmap_err|
1800+
next if nmap_err.strip.empty?
1801+
print_status("Nmap: '#{nmap_err.strip}'")
1802+
end
18131803
end
1814-
end
18151804

1816-
temp_nmap_threads.map {|t| t.join rescue nil}
1817-
nmap_pipe.each {|p| p.close rescue nil}
1818-
rescue ::IOError
1819-
end
1805+
temp_nmap_threads.map {|t| t.join rescue nil}
1806+
nmap_pipe.each {|p| p.close rescue nil}
1807+
rescue ::IOError
1808+
end
18201809

1821-
fo.close(true)
1822-
framework.db.import_nmap_xml_file(:filename => fd.path)
1810+
framework.db.import_nmap_xml_file(:filename => fd.path)
18231811

1824-
if save
1825-
fd.rewind
1826-
saved_path = report_store_local("nmap.scan.xml", "text/xml", fd.read, "nmap_#{Time.now.utc.to_i}")
1827-
print_status("Saved NMAP XML results to #{saved_path}")
1812+
print_status("Saved NMAP XML results to #{fd.path}") if save
1813+
ensure
1814+
fd.close
1815+
fd.unlink unless save
18281816
end
1829-
fd.close(true)
18301817
}
18311818
end
18321819

@@ -1869,13 +1856,6 @@ def cmd_db_nmap_tabs(str, words)
18691856
tabs
18701857
end
18711858

1872-
#
1873-
# Store some locally-generated data as a file, similiar to store_loot.
1874-
#
1875-
def report_store_local(ltype=nil, ctype=nil, data=nil, filename=nil)
1876-
store_local(ltype,ctype,data,filename)
1877-
end
1878-
18791859
#
18801860
# Database management
18811861
#

0 commit comments

Comments
 (0)