PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/msf/core/exploit/capture.rb

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 557 lines | 432 code | 68 blank | 57 comment | 84 complexity | ba0c90e7ef8d53fb7e164bd0c3d96633 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, GPL-2.0, MIT
  1. # -*- coding: binary -*-
  2. module Msf
  3. ###
  4. #
  5. # This module provides methods for sending and receiving
  6. # raw packets. It should be preferred over the soon-to-be
  7. # deprecated Rex::Socket::Ip and Msf::Exploite::Remote::Ip
  8. # mixins.
  9. #
  10. # Please see the pcaprub documentation for more information
  11. # on how to use capture objects.
  12. #
  13. ###
  14. module Exploit::Capture
  15. #
  16. # Initializes an instance of an exploit module that captures traffic
  17. #
  18. def initialize(info = {})
  19. super
  20. register_options(
  21. [
  22. OptPath.new('PCAPFILE', [false, 'The name of the PCAP capture file to process']),
  23. OptString.new('INTERFACE', [false, 'The name of the interface']),
  24. OptString.new('FILTER', [false, 'The filter string for capturing traffic']),
  25. OptInt.new('SNAPLEN', [true, 'The number of bytes to capture', 65535]),
  26. OptInt.new('TIMEOUT', [true, 'The number of seconds to wait for new data', 500]),
  27. Opt::RHOST
  28. ], Msf::Exploit::Capture
  29. )
  30. register_advanced_options(
  31. [
  32. OptInt.new('UDP_SECRET', [true, 'The 32-bit cookie for UDP probe requests.', 1297303091]),
  33. OptAddress.new('GATEWAY', [false, 'The gateway IP address. This will be used rather than a random remote address for the UDP probe, if set.']),
  34. OptInt.new('NETMASK', [false, 'The local network mask. This is used to decide if an address is in the local network.', 24]),
  35. ], Msf::Exploit::Capture
  36. )
  37. require 'packetfu'
  38. begin
  39. require 'pcaprub'
  40. @pcaprub_loaded = true
  41. rescue ::Exception => e
  42. @pcaprub_loaded = false
  43. @pcaprub_error = e
  44. end
  45. end
  46. def stats_recv(pcap=self.capture)
  47. return(0) if not pcap
  48. pcap.stats['recv']
  49. end
  50. def stats_drop(pcap=self.capture)
  51. return(0) if not pcap
  52. pcap.stats['drop']
  53. end
  54. def stats_ifdrop(pcap=self.capture)
  55. return(0) if not pcap
  56. pcap.stats['ifdrop']
  57. end
  58. #
  59. # Opens a handle to the specified device
  60. #
  61. def open_pcap(opts={})
  62. check_pcaprub_loaded
  63. if RUBY_PLATFORM == "i386-mingw32"
  64. if opts['INTERFACE'] or datastore['INTERFACE']
  65. dev = opts['INTERFACE'] || datastore['INTERFACE']
  66. if is_interface?(dev)
  67. dev = get_interface_guid(dev)
  68. end
  69. end
  70. else
  71. dev = opts['INTERFACE'] || datastore['INTERFACE'] || nil
  72. end
  73. len = (opts['SNAPLEN'] || datastore['SNAPLEN'] || 65535).to_i
  74. tim = (opts['TIMEOUT'] || datastore['TIMEOUT'] || 0).to_i
  75. fil = opts['FILTER'] || datastore['FILTER']
  76. arp = opts['ARPCAP'] || true
  77. # Look for a PCAP file
  78. cap = datastore['PCAPFILE'] || ''
  79. if(not cap.empty?)
  80. if(not File.exists?(cap))
  81. raise RuntimeError, "The PCAP file #{cap} could not be found"
  82. end
  83. self.capture = ::Pcap.open_offline(cap)
  84. else
  85. dev ||= ::Pcap.lookupdev
  86. unless RUBY_PLATFORM == "i386-mingw32"
  87. system("ifconfig", dev, "up")
  88. end
  89. self.capture = ::Pcap.open_live(dev, len, true, tim)
  90. if arp
  91. self.arp_capture = ::Pcap.open_live(dev, 512, true, tim)
  92. preamble = datastore['UDP_SECRET'].to_i
  93. arp_filter = "arp[6:2] = 2 or (udp[8:4] = #{preamble})"
  94. self.arp_capture.setfilter(arp_filter)
  95. end
  96. end
  97. if (not self.capture)
  98. raise RuntimeError, "Could not start the capture process"
  99. elsif (arp and !self.arp_capture and cap.empty?)
  100. raise RuntimeError, "Could not start the ARP capture process"
  101. end
  102. self.capture.setfilter(fil) if fil
  103. end
  104. def close_pcap
  105. return if not self.capture
  106. self.capture = nil
  107. self.arp_capture = nil
  108. GC.start()
  109. end
  110. def capture_extract_ies(raw)
  111. set = {}
  112. ret = 0
  113. idx = 0
  114. len = 0
  115. while (idx < raw.length)
  116. len = raw[idx+1]
  117. return set if not len
  118. set[ raw[idx] ] ||= []
  119. set[ raw[idx] ].push(raw[idx + 2, len])
  120. idx += len + 2
  121. end
  122. return set
  123. end
  124. #
  125. # This monstrosity works around a series of bugs in the interrupt
  126. # signal handling of Ruby 1.9
  127. #
  128. def each_packet
  129. return if not capture
  130. begin
  131. @capture_count = 0
  132. reader = framework.threads.spawn("PcapReceiver", false) do
  133. capture.each do |pkt|
  134. yield(pkt)
  135. @capture_count += 1
  136. end
  137. end
  138. reader.join
  139. rescue ::Exception
  140. raise $!
  141. ensure
  142. reader.kill if reader.alive?
  143. end
  144. @capture_count
  145. end
  146. # Injects a packet on the wire. For all injection-related functions, it's
  147. # on the module to open up a capture device first (this way, we don't
  148. # needlessly spawn new capture devices).
  149. def inject(pkt="",pcap=self.capture)
  150. check_pcaprub_loaded
  151. if not pcap
  152. raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)"
  153. else
  154. pcap.inject(pkt.to_s) # Can be a PacketFu Packet object or a pre-packed string
  155. end
  156. end
  157. # Injects an Ethernet packet with an optional payload. The payload
  158. # may be a regular PacketFu packet, an EthHeader, or a string.
  159. def inject_eth(args={})
  160. eth_daddr = args[:eth_daddr] || "ff:ff:ff:ff:ff:ff"
  161. eth_saddr = args[:eth_saddr] || "00:00:00:00:00:00"
  162. eth_type = args[:eth_type] || 0x0800 # IP default
  163. payload = args[:payload]
  164. pcap = args[:pcap] || self.capture
  165. p = PacketFu::EthPacket.new
  166. p.eth_daddr = eth_daddr
  167. p.eth_saddr = eth_saddr
  168. p.eth_proto = eth_type
  169. if payload
  170. if payload.kind_of? PacketFu::EthPacket
  171. p.payload = payload.eth_header.body
  172. elsif payload.kind_of? PacketFu::EthHeader
  173. p.payload = payload.body
  174. else
  175. p.payload = payload.to_s
  176. end
  177. end
  178. inject p.to_s,pcap
  179. end
  180. def inject_pcap(pcap_file, filter=nil, delay = 0, pcap=self.capture)
  181. check_pcaprub_loaded
  182. if not pcap
  183. raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)"
  184. end
  185. if(not File.exists?(pcap_file))
  186. raise RuntimeError, "The PCAP file #{pcap_file} could not be found"
  187. end
  188. if(pcap_file.empty?)
  189. raise RuntimeError, "The PCAP file #{pcap_file} is empty"
  190. end
  191. capture_file = ::Pcap.open_offline(pcap_file)
  192. capture_file.setfilter(filter) if filter
  193. while (pkt = capture_file.next) do
  194. pcap.inject(pkt)
  195. Kernel.select(nil, nil, nil, (delay * 1.0)/1000)
  196. end
  197. GC.start
  198. end
  199. # Capture_sendto is intended to replace the old Rex::Socket::Ip.sendto method. It requires
  200. # a payload and a destination address. To send to the broadcast address, set bcast
  201. # to true (this will guarantee that packets will be sent even if ARP doesn't work
  202. # out).
  203. def capture_sendto(payload="", dhost=nil, bcast=false, dev=nil)
  204. raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)" unless self.capture
  205. raise RuntimeError, "Must specify a host to sendto" unless dhost
  206. dev ||= datastore['INTERFACE']
  207. dst_mac,src_mac = lookup_eth(dhost,dev)
  208. if dst_mac == nil and not bcast
  209. return false
  210. end
  211. inject_eth(:payload => payload, :eth_daddr => dst_mac, :eth_saddr => src_mac)
  212. end
  213. # The return value either be a PacketFu::Packet object, or nil
  214. def inject_reply(proto=:udp,pcap=self.capture)
  215. reply = nil
  216. to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
  217. if not pcap
  218. raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)"
  219. else
  220. begin
  221. ::Timeout.timeout(to) do
  222. pcap.each do |r|
  223. packet = PacketFu::Packet.parse(r)
  224. next unless packet.proto.map {|x| x.downcase.to_sym}.include? proto
  225. reply = packet
  226. break
  227. end
  228. end
  229. rescue ::Timeout::Error
  230. end
  231. end
  232. return reply
  233. end
  234. # This ascertains the correct Ethernet addresses one should use to
  235. # ensure injected IP packets actually get where they are going, and
  236. # manages the self.arp_cache hash. It always uses self.arp_capture
  237. # do inject and capture packets, and will always first fire off a
  238. # UDP packet using the regular socket to learn the source host's
  239. # and gateway's mac addresses.
  240. def lookup_eth(addr=nil,iface=nil)
  241. raise RuntimeError, "Could not access the capture process." if not self.arp_capture
  242. self.arp_cache ||= {}
  243. self.dst_cache ||= {}
  244. return self.dst_cache[addr] if self.dst_cache[addr]
  245. if !self.arp_cache[Rex::Socket.source_address(addr)]
  246. probe_gateway(addr)
  247. end
  248. src_mac = self.arp_cache[Rex::Socket.source_address(addr)]
  249. if should_arp?(addr)
  250. dst_mac = self.arp_cache[addr] || arp(addr)
  251. else
  252. dst_mac = self.arp_cache[:gateway]
  253. end
  254. self.dst_cache[addr] = [dst_mac,src_mac]
  255. end
  256. def probe_gateway(addr)
  257. dst_host = (datastore['GATEWAY'] || IPAddr.new((rand(16777216) + 2969567232), Socket::AF_INET).to_s)
  258. dst_port = rand(30000)+1024
  259. preamble = [datastore['UDP_SECRET']].pack("N")
  260. secret = "#{preamble}#{Rex::Text.rand_text(rand(0xff)+1)}"
  261. UDPSocket.open.send(secret,0,dst_host,dst_port)
  262. begin
  263. to = (datastore['TIMEOUT'] || 1500).to_f / 1000.0
  264. ::Timeout.timeout(to) do
  265. while(my_packet = inject_reply(:udp,self.arp_capture))
  266. if my_packet.payload == secret
  267. dst_mac = self.arp_cache[:gateway] = my_packet.eth_daddr
  268. src_mac = self.arp_cache[Rex::Socket.source_address(addr)] = my_packet.eth_saddr
  269. return [dst_mac,src_mac]
  270. else
  271. next
  272. end
  273. end
  274. end
  275. rescue ::Timeout::Error
  276. # Well, that didn't work (this common on networks where there's no gatway, like
  277. # VMWare network interfaces. We'll need to use a fake source hardware address.
  278. self.arp_cache[Rex::Socket.source_address(addr)] = "00:00:00:00:00:00"
  279. end
  280. end
  281. # A pure-Ruby ARP exchange. It uses self.arp_capture to send and recv
  282. # packets, rather than self.capture.
  283. def arp(target_ip=nil)
  284. return self.arp_cache[target_ip] if self.arp_cache[target_ip]
  285. return self.arp_cache[:gateway] unless should_arp? target_ip
  286. source_ip = Rex::Socket.source_address(target_ip)
  287. raise RuntimeError, "Could not access the capture process." if not self.arp_capture
  288. p = arp_packet(target_ip,source_ip)
  289. inject_eth(:eth_type => 0x0806,
  290. :payload => p,
  291. :pcap => self.arp_capture,
  292. :eth_saddr => self.arp_cache[Rex::Socket.source_address(target_ip)]
  293. )
  294. begin
  295. to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
  296. ::Timeout.timeout(to) do
  297. while(my_packet = inject_reply(:arp,self.arp_capture))
  298. if my_packet.arp_saddr_ip == target_ip
  299. self.arp_cache[target_ip] = my_packet.eth_saddr
  300. return self.arp_cache[target_ip]
  301. else
  302. next
  303. end
  304. end
  305. end
  306. rescue ::Timeout::Error
  307. end
  308. end
  309. # Creates a full ARP packet, mainly for use with inject_eth()
  310. def arp_packet(target_ip=nil,source_ip=nil)
  311. p = PacketFu::ARPPacket.new
  312. p.arp_opcode = 1
  313. p.arp_daddr_ip = target_ip || datastore['RHOST']
  314. p.arp_saddr_ip = source_ip || datastore['LHOST']
  315. my_eth = self.arp_cache[Rex::Socket.source_address(target_ip)]
  316. p.arp_saddr_mac = my_eth || "00:00:00:00:00:00"
  317. return p
  318. end
  319. # Allow modules to reset their arp caches arbitrarily.
  320. def expire_arpcache
  321. self.arp_cache = {}
  322. end
  323. # For compatabilty with Msf::Exploit::Remote::Ip
  324. def rhost
  325. datastore['RHOST']
  326. end
  327. def check_pcaprub_loaded
  328. if not @pcaprub_loaded
  329. print_status("The Pcaprub module is not available: #{@pcaprub_error}")
  330. raise RuntimeError, "Pcaprub not available"
  331. else
  332. true
  333. end
  334. end
  335. def lookupnet
  336. check_pcaprub_loaded
  337. dev = datastore['INTERFACE'] || ::Pcap.lookupdev
  338. mask = datastore['NETMASK'] || 24
  339. begin
  340. my_net = IPAddr.new("#{Pcap.lookupnet(dev).first}/#{mask}")
  341. rescue RuntimeError => e
  342. @pcaprub_error = e
  343. print_status("Cannot stat device: #{@pcaprub_error}")
  344. raise RuntimeError, "Pcaprub error: #{@pcaprub_error}"
  345. end
  346. return my_net
  347. end
  348. def should_arp?(ip)
  349. @mydev ||= datastore['INTERFACE'] || ::Pcap.lookupdev
  350. @mymask ||= datastore['NETMASK'] || 24
  351. @mynet ||= lookupnet
  352. @mynet.include?(IPAddr.new(ip))
  353. end
  354. attr_accessor :capture, :arp_cache, :arp_capture, :dst_cache
  355. #Netifaces code
  356. # netifaces code is not available in pcaprub 0.9.2 and prior,
  357. # which is going to be installed in a lot of places. Modules
  358. # which want it should check explicitly for it. TODO: Bug upstream
  359. # to release it for real in 0.9.3
  360. def netifaces_implemented?
  361. @pcaprub_loaded and
  362. Pcap.respond_to?(:lookupaddrs) and
  363. Pcap.respond_to?(:interfaces) and
  364. Pcap.respond_to?(:addresses)
  365. end
  366. def list_interfaces
  367. check_pcaprub_loaded
  368. Pcap.interfaces
  369. end
  370. def is_interface?(dev)
  371. check_pcaprub_loaded
  372. if RUBY_PLATFORM == "i386-mingw32"
  373. if dev =~ /\\Device\\NPF_\{[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}/
  374. return Pcap.interfaces.include?(dev)
  375. elsif dev.to_s =~ /^[0-9]{1,2}$/
  376. if (dev.to_i <= Pcap.interfaces.length) and (dev.to_i >= 0)
  377. return true
  378. else
  379. return false
  380. end
  381. else
  382. return false
  383. end
  384. else
  385. return Pcap.interfaces.include?(dev)
  386. end
  387. end
  388. # This function is usefull only on windows where pcaprub use the GUID
  389. def get_interface_guid(dev)
  390. check_pcaprub_loaded
  391. if RUBY_PLATFORM == "i386-mingw32"
  392. if dev.to_s =~ /^[0-9]{1,2}$/
  393. if is_interface?(dev)
  394. Pcap.interfaces[(dev.to_i) - 1]
  395. else
  396. return dev
  397. end
  398. else
  399. return dev
  400. end
  401. else #Non windows
  402. return dev
  403. end
  404. end
  405. def get_mac(dev)
  406. check_pcaprub_loaded
  407. dev = get_interface_guid(dev)
  408. addrs = Pcap.addresses(dev)
  409. raise RuntimeError, "Interface #{dev} does not exist" if !addrs
  410. raise RuntimeError, "Can not get mac address for interface #{dev}" if !addrs[Pcap::AF_LINK][0]['addr']
  411. addrs[Pcap::AF_LINK][0]['addr']
  412. end
  413. def get_ipv4_addr_count(dev)
  414. check_pcaprub_loaded
  415. dev = get_interface_guid(dev)
  416. addrs = Pcap.addresses(dev)
  417. raise RuntimeError, "Interface #{dev} does not exist" if !addrs
  418. addrs[Pcap::AF_INET].length
  419. end
  420. def get_ipv4_addr(dev, num=0)
  421. check_pcaprub_loaded
  422. dev = get_interface_guid(dev)
  423. addrs = Pcap.addresses(dev)
  424. raise RuntimeError, "Interface #{dev} do not exists" if !addrs
  425. raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
  426. raise RuntimeError, "Can not get the IPv4 address for interface #{dev}" if !addrs[Pcap::AF_INET][num]['addr']
  427. addrs[Pcap::AF_INET][num]['addr']
  428. end
  429. def get_ipv4_netmask(dev, num=0)
  430. check_pcaprub_loaded
  431. dev = get_interface_guid(dev)
  432. addrs = Pcap.addresses(dev)
  433. raise RuntimeError, "Interface #{dev} do not exists" if !addrs
  434. raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
  435. raise RuntimeError, "Can not get IPv4 netmask for interface #{dev}" if !addrs[Pcap::AF_INET][num]['netmask']
  436. addrs[Pcap::AF_INET][num]['netmask']
  437. end
  438. def get_ipv4_broadcast(dev, num=0)
  439. check_pcaprub_loaded
  440. dev = get_interface_guid(dev)
  441. addrs = Pcap.addresses(dev)
  442. raise RuntimeError, "Interface #{dev} do not exists" if !addrs
  443. raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
  444. raise RuntimeError, "Can not get IPv4 broadcast address for interface #{dev}" if !addrs[Pcap::AF_INET][num]['broadcast']
  445. addrs[Pcap::AF_INET][num]['broadcast']
  446. end
  447. def get_ipv6_addr_count(dev)
  448. check_pcaprub_loaded
  449. dev = get_interface_guid(dev)
  450. raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
  451. addrs = Pcap.addresses(dev)
  452. raise RuntimeError, "Interface #{dev} do not exists" if !addrs
  453. addrs[Pcap::AF_INET6].length
  454. end
  455. # NOTE: IPv6 is not implemented on Windows
  456. def get_ipv6_addr(dev, num=0)
  457. check_pcaprub_loaded
  458. dev = get_interface_guid(dev)
  459. raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
  460. addrs = Pcap.addresses(dev)
  461. raise RuntimeError, "Interface #{dev} do not exists" if !addrs
  462. raise RuntimeError, "Interface #{dev} do not have an ipv6 address at position #{num}" if addrs[Pcap::AF_INET6].length < num + 1
  463. raise RuntimeError, "Can not get ipv6 address for interface #{dev}" if !addrs[Pcap::AF_INET6][num]['addr']
  464. addrs[Pcap::AF_INET6][num]['addr'].gsub(/%(.)*$/,'')
  465. end
  466. def get_ipv6_netmask(dev, num=0)
  467. check_pcaprub_loaded
  468. dev = get_interface_guid(dev)
  469. raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
  470. addrs = Pcap.addresses(dev)
  471. raise RuntimeError, "Interface #{dev} do not exists" if !addrs
  472. raise RuntimeError, "Interface #{dev} do not have an ipv6 address at position #{num}" if addrs[Pcap::AF_INET6].length < num + 1
  473. raise RuntimeError, "Can not get ipv6 netmask address for interface #{dev}" if !addrs[Pcap::AF_INET6][num]['netmask']
  474. addrs[Pcap::AF_INET6][num]['netmask']
  475. end
  476. # Protocol-specific encoding/decoding methods until more
  477. # application protos get into PacketFu proper
  478. # Intended to be used as the payload to an ICMP echo request's payload
  479. def capture_icmp_echo_pack(id=nil,seq=nil,payload=nil)
  480. id ||= rand(0x10000)
  481. seq ||= rand(0x10000)
  482. [id,seq,payload.to_s].pack("nna*")
  483. end
  484. # Decodes and ICMP echo request or response.
  485. def capture_icmp_echo_unpack(data)
  486. data.unpack("nna*")
  487. end
  488. end
  489. end