PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/auxiliary/scanner/portscan/xmas.rb

https://github.com/rapid7/metasploit-framework
Ruby | 151 lines | 113 code | 27 blank | 11 comment | 7 complexity | e9c7f4ba0c70bb5656a55fcee088b05c MD5 | raw file
  1. ##
  2. # This module requires Metasploit: https://metasploit.com/download
  3. # Current source: https://github.com/rapid7/metasploit-framework
  4. ##
  5. class MetasploitModule < Msf::Auxiliary
  6. include Msf::Exploit::Capture
  7. include Msf::Auxiliary::Scanner
  8. include Msf::Auxiliary::Report
  9. def initialize
  10. super(
  11. 'Name' => 'TCP "XMas" Port Scanner',
  12. 'Description' => %q{
  13. Enumerate open|filtered TCP services using a raw
  14. "XMas" scan; this sends probes containing the FIN,
  15. PSH and URG flags.
  16. },
  17. 'Author' => 'kris katterjohn',
  18. 'License' => MSF_LICENSE
  19. )
  20. register_options([
  21. OptString.new('PORTS', [true, "Ports to scan (e.g. 22-25,80,110-900)", "1-10000"]),
  22. OptInt.new('TIMEOUT', [true, "The reply read timeout in milliseconds", 500]),
  23. OptInt.new('BATCHSIZE', [true, "The number of hosts to scan per set", 256]),
  24. OptInt.new('DELAY', [true, "The delay between connections, per thread, in milliseconds", 0]),
  25. OptInt.new('JITTER', [true, "The delay jitter factor (maximum value by which to +/- DELAY) in milliseconds.", 0]),
  26. OptString.new('INTERFACE', [false, 'The name of the interface'])
  27. ])
  28. deregister_options('FILTER','PCAPFILE')
  29. end
  30. # No IPv6 support yet
  31. def support_ipv6?
  32. false
  33. end
  34. def run_batch_size
  35. datastore['BATCHSIZE'] || 256
  36. end
  37. def run_batch(hosts)
  38. open_pcap
  39. pcap = self.capture
  40. ports = Rex::Socket.portspec_crack(datastore['PORTS'])
  41. if ports.empty?
  42. raise Msf::OptionValidateError.new(['PORTS'])
  43. end
  44. jitter_value = datastore['JITTER'].to_i
  45. if jitter_value < 0
  46. raise Msf::OptionValidateError.new(['JITTER'])
  47. end
  48. delay_value = datastore['DELAY'].to_i
  49. if delay_value < 0
  50. raise Msf::OptionValidateError.new(['DELAY'])
  51. end
  52. to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
  53. # we copy the hosts because some may not be reachable and need to be ejected
  54. host_queue = hosts.dup
  55. # Spread the load across the hosts
  56. ports.each do |dport|
  57. host_queue.each do |dhost|
  58. shost, sport = getsource(dhost)
  59. pcap.setfilter(getfilter(shost, sport, dhost, dport))
  60. begin
  61. probe = buildprobe(shost, sport, dhost, dport)
  62. # Add the delay based on JITTER and DELAY if needs be
  63. add_delay_jitter(delay_value,jitter_value)
  64. unless capture_sendto(probe, dhost)
  65. host_queue.delete(dhost)
  66. next
  67. end
  68. reply = probereply(pcap, to)
  69. next if reply # Got a RST back
  70. print_status(" TCP OPEN|FILTERED #{dhost}:#{dport}")
  71. # Add Report
  72. report_note(
  73. :host => dhost,
  74. :proto => 'tcp',
  75. :port => dport,
  76. :type => "TCP OPEN|FILTERED #{dhost}:#{dport}",
  77. :data => "TCP OPEN|FILTERED #{dhost}:#{dport}"
  78. )
  79. rescue ::Exception
  80. print_error("Error: #{$!.class} #{$!}")
  81. end
  82. end
  83. end
  84. close_pcap
  85. end
  86. def getfilter(shost, sport, dhost, dport)
  87. # Look for associated RSTs
  88. "tcp and (tcp[13] & 0x04) != 0 and " +
  89. "src host #{dhost} and src port #{dport} and " +
  90. "dst host #{shost} and dst port #{sport}"
  91. end
  92. def getsource(dhost)
  93. # srcip, srcport
  94. [ Rex::Socket.source_address(dhost), rand(0xffff - 1025) + 1025 ]
  95. end
  96. def buildprobe(shost, sport, dhost, dport)
  97. p = PacketFu::TCPPacket.new
  98. p.ip_saddr = shost
  99. p.ip_daddr = dhost
  100. p.tcp_sport = sport
  101. p.tcp_flags.fin = 1
  102. p.tcp_flags.urg = 1
  103. p.tcp_flags.psh = 1
  104. p.tcp_dport = dport
  105. p.tcp_win = 3072
  106. p.recalc
  107. p
  108. end
  109. def probereply(pcap, to)
  110. reply = nil
  111. begin
  112. Timeout.timeout(to) do
  113. pcap.each do |r|
  114. pkt = PacketFu::Packet.parse(r)
  115. next unless pkt.is_tcp?
  116. reply = pkt
  117. break
  118. end
  119. end
  120. rescue Timeout::Error
  121. end
  122. return reply
  123. end
  124. end