Class: LTools::Nmap

Inherits:
Tool
  • Object
show all
Defined in:
lib/tools/nmap.rb

Instance Attribute Summary

Attributes inherited from Tool

#cmd, #stderr, #stdin, #stdout, #verbose

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tool

check?, #command, inherited, #initialize, #io_inherit!, #start, #stop, #wait

Constructor Details

This class inherits a constructor from LTools::Tool

Class Method Details

.portscan(targets, ports: nil, mode: 'S', speed: 5, xml_report: nil, append: nil) ⇒ Object

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#xml_reportObject



39
40
41
42
43
44
45
# 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