class LTools::Nmap

Public Class Methods

portscan(targets, ports: nil, mode: 'S', speed: 5, xml_report: nil, append: nil) click to toggle source

TODO 非正常退出的情况, Root权限问题

# File lib/tools/nmap.rb, line 7
def self.portscan(targets, ports: nil, mode: 'S', speed: 5, xml_report: nil, append: nil)
  cmd = "nmap -Pn -n -T#{speed} -s#{mode}"
  cmd << ToolOpts.build {|opts|
    opts.on('-p', ports)
    opts.on('-oX', xml_report){ |file|
      next file unless file == :auto
      @nmap_xml_report = Tempfile.new('ltools-nmap-xml-report')
      @nmap_xml_report.close
      @nmap_xml_report.to_path
    }
  }
  cmd << self.targets_cmd(targets)
  cmd << ' ' << append if append
  new cmd
end
targets_cmd(targets) click to toggle source
# File lib/tools/nmap.rb, line 23
def self.targets_cmd(targets)
  case targets
  when Array
    if targets.size < 8
      targets.join ' '
    else
      infile = Tempfile.new('ltools-nmap-')
      infile.puts targets
      infile.close
      "-iL #{infile.to_path}"
    end
  when String
    File.file?(targets) ? "-iL #{targets}" : targets
  end
end

Public Instance Methods

xml_report() click to toggle source
# File lib/tools/nmap.rb, line 39
def xml_report
  require 'nmap/xml'
  if index = @cmd.index('-oX')
    file = @cmd[index+1]
    ::Nmap::XML.new(file)
  end
end