PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/auxiliary/scanner/portscan/ack.rb

https://bitbucket.org/cfield/metasploit-framework
Ruby | 138 lines | 99 code | 28 blank | 11 comment | 6 complexity | bf0ed42187d3acd388803d40b1cad870 MD5 | raw file
  1. ##
  2. # This file is part of the Metasploit Framework and may be subject to
  3. # redistribution and commercial restrictions. Please see the Metasploit
  4. # web site for more information on licensing and terms of use.
  5. # http://metasploit.com/
  6. ##
  7. require 'msf/core'
  8. class Metasploit3 < Msf::Auxiliary
  9. include Msf::Exploit::Capture
  10. include Msf::Auxiliary::Scanner
  11. include Msf::Auxiliary::Report
  12. def initialize
  13. super(
  14. 'Name' => 'TCP ACK Firewall Scanner',
  15. 'Description' => %q{
  16. Map out firewall rulesets with a raw ACK scan. Any
  17. unfiltered ports found means a stateful firewall is
  18. not in place for them.
  19. },
  20. 'Author' => 'kris katterjohn',
  21. 'License' => MSF_LICENSE
  22. )
  23. register_options([
  24. OptString.new('PORTS', [true, "Ports to scan (e.g. 22-25,80,110-900)", "1-10000"]),
  25. OptInt.new('TIMEOUT', [true, "The reply read timeout in milliseconds", 500]),
  26. OptInt.new('BATCHSIZE', [true, "The number of hosts to scan per set", 256]),
  27. OptString.new('INTERFACE', [false, 'The name of the interface'])
  28. ], self.class)
  29. deregister_options('FILTER','PCAPFILE')
  30. end
  31. # No IPv6 support yet
  32. def support_ipv6?
  33. false
  34. end
  35. def run_batch_size
  36. datastore['BATCHSIZE'] || 256
  37. end
  38. def run_batch(hosts)
  39. open_pcap
  40. pcap = self.capture
  41. ports = Rex::Socket.portspec_crack(datastore['PORTS'])
  42. if ports.empty?
  43. print_error("Error: No valid ports specified")
  44. return
  45. end
  46. to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
  47. # Spread the load across the hosts
  48. ports.each do |dport|
  49. hosts.each do |dhost|
  50. shost, sport = getsource(dhost)
  51. pcap.setfilter(getfilter(shost, sport, dhost, dport))
  52. begin
  53. probe = buildprobe(shost, sport, dhost, dport)
  54. capture_sendto(probe, dhost)
  55. reply = probereply(pcap, to)
  56. next if not reply
  57. print_status(" TCP UNFILTERED #{dhost}:#{dport}")
  58. #Add Report
  59. report_note(
  60. :host => dhost,
  61. :proto => 'tcp',
  62. :port => dport,
  63. :type => "TCP UNFILTERED #{dhost}:#{dport}",
  64. :data => "TCP UNFILTERED #{dhost}:#{dport}"
  65. )
  66. rescue ::Exception
  67. print_error("Error: #{$!.class} #{$!}")
  68. end
  69. end
  70. end
  71. close_pcap
  72. end
  73. def getfilter(shost, sport, dhost, dport)
  74. # Look for associated RSTs
  75. "tcp and (tcp[13] & 0x04) != 0 and " +
  76. "src host #{dhost} and src port #{dport} and " +
  77. "dst host #{shost} and dst port #{sport}"
  78. end
  79. def getsource(dhost)
  80. # srcip, srcport
  81. [ Rex::Socket.source_address(dhost), rand(0xffff - 1025) + 1025 ]
  82. end
  83. def buildprobe(shost, sport, dhost, dport)
  84. p = PacketFu::TCPPacket.new
  85. p.ip_saddr = shost
  86. p.ip_daddr = dhost
  87. p.tcp_sport = sport
  88. p.tcp_ack = rand(0x100000000)
  89. p.tcp_flags.ack = 1
  90. p.tcp_dport = dport
  91. p.tcp_win = 3072
  92. p.recalc
  93. p
  94. end
  95. def probereply(pcap, to)
  96. reply = nil
  97. begin
  98. Timeout.timeout(to) do
  99. pcap.each do |r|
  100. pkt = PacketFu::Packet.parse(r)
  101. next unless pkt.is_tcp?
  102. reply = pkt
  103. break
  104. end
  105. end
  106. rescue Timeout::Error
  107. end
  108. return reply
  109. end
  110. end