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

/lib/msf/core/exploit/ipv6.rb

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 324 lines | 227 code | 45 blank | 52 comment | 40 complexity | f143c5c047c5e4902178c3ba801218d1 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, GPL-2.0, MIT
  1. # -*- coding: binary -*-
  2. ##
  3. # $Id$
  4. ##
  5. module Msf
  6. ###
  7. #
  8. # This module provides common tools for IPv6
  9. #
  10. ###
  11. module Exploit::Remote::Ipv6
  12. #
  13. # Initializes an instance of an exploit module that captures traffic
  14. #
  15. def initialize(info = {})
  16. super
  17. register_options(
  18. [
  19. OptString.new('INTERFACE', [false, 'The name of the interface']),
  20. OptString.new("SMAC", [ false, "The source MAC address"]),
  21. OptAddress.new("SHOST", [ false, "The source IPv6 address" ] ),
  22. OptInt.new("TIMEOUT", [ true, "Timeout when waiting for host response.", 5])
  23. ], Msf::Exploit::Remote::Ipv6
  24. )
  25. begin
  26. require 'pcaprub'
  27. @pcaprub_loaded = true
  28. rescue ::Exception => e
  29. @pcaprub_loaded = false
  30. @pcaprub_error = e
  31. end
  32. end
  33. #
  34. # Shortcut method for resolving our local interface name
  35. #
  36. def ipv6_interface(opts={})
  37. opts['INTERFACE'] || datastore['INTERFACE'] || ::Pcap.lookupdev
  38. end
  39. #
  40. # Shortcut method for determining our link-local address
  41. #
  42. def ipv6_link_address(opts={})
  43. Rex::Socket.ipv6_link_address(ipv6_interface(opts))
  44. end
  45. #
  46. # Shortcut method for determining our MAC address
  47. #
  48. def ipv6_mac(opts={})
  49. Rex::Socket.ipv6_mac(ipv6_interface(opts))
  50. end
  51. #
  52. # Opens a pcaprub capture interface to inject packets, and sniff ICMPv6 packets
  53. #
  54. def open_icmp_pcap(opts = {})
  55. check_pcaprub_loaded
  56. dev = ipv6_interface(opts)
  57. len = 65535
  58. tim = 0
  59. @ipv6_icmp6_capture = ::Pcap.open_live(dev, len, true, tim)
  60. @ipv6_icmp6_capture.setfilter("icmp6")
  61. end
  62. #
  63. # Close the capture interface
  64. #
  65. def close_icmp_pcap()
  66. check_pcaprub_loaded
  67. return if not @ipv6_icmp6_capture
  68. @ipv6_icmp6_capture = nil
  69. GC.start()
  70. end
  71. #
  72. # Send out a ICMPv6 neighbor solicitation, and
  73. # return the associated MAC address
  74. #
  75. def solicit_ipv6_mac(dhost, opts = {})
  76. check_pcaprub_loaded
  77. dhost_intf = dhost + '%' + ipv6_interface(opts)
  78. smac = opts['SMAC'] || datastore['SMAC'] || ipv6_mac
  79. shost = opts['SHOST'] || datastore['SHOST'] || Rex::Socket.source_address(dhost_intf)
  80. timeout = opts['TIMEOUT'] || datastore['TIMEOUT'] || 3
  81. open_icmp_pcap()
  82. p2 = PacketFu::IPv6Packet.new
  83. p2.eth_saddr = smac
  84. p2.eth_daddr = ipv6_soll_mcast_mac(dhost)
  85. p2.ipv6_saddr = shost
  86. p2.ipv6_daddr = ipv6_soll_mcast_addr6(dhost)
  87. p2.ipv6_hop = 255
  88. p2.ipv6_next = 0x3a
  89. p2.payload = ipv6_neighbor_solicitation(
  90. IPAddr.new(dhost).to_i,
  91. p2.eth_src
  92. )
  93. p2.ipv6_len = p2.payload.size
  94. ipv6_checksum!(p2)
  95. @ipv6_icmp6_capture.inject(p2.to_s)
  96. # Wait for a response
  97. max_epoch = ::Time.now.to_i + timeout
  98. while(::Time.now.to_i < max_epoch)
  99. pkt_bytes = @ipv6_icmp6_capture.next()
  100. next if not pkt_bytes
  101. pkt = PacketFu::Packet.parse(pkt_bytes) rescue nil
  102. next unless pkt
  103. next unless pkt.is_ipv6?
  104. next unless pkt.ipv6_next == 0x3a
  105. next unless pkt.payload
  106. next if pkt.payload.empty?
  107. next unless pkt.payload[0,1] == "\x88" # Neighbor advertisement
  108. if(IPAddr.new(pkt.ipv6_daddr).to_i == IPAddr.new(shost).to_i and
  109. IPAddr.new(pkt.ipv6_saddr).to_i == IPAddr.new(dhost).to_i)
  110. ipv6opts = pkt.payload[24,pkt.payload.size]
  111. next unless ipv6opts
  112. parsed_opts = ipv6_parse_options(ipv6opts)
  113. parsed_opts.each do |opt|
  114. if opt[0] == 2
  115. addr = PacketFu::EthHeader.str2mac(opt.last)
  116. close_icmp_pcap()
  117. return(addr)
  118. end
  119. end
  120. close_icmp_pcap
  121. return(pkt.eth_saddr)
  122. end
  123. end
  124. close_icmp_pcap
  125. return nil
  126. end
  127. #
  128. # Send a ICMPv6 Echo Request, and wait for the
  129. # associated ICMPv6 Echo Response
  130. #
  131. def ping6(dhost, opts={})
  132. check_pcaprub_loaded
  133. dhost_intf = dhost + '%' + ipv6_interface(opts)
  134. smac = opts['SMAC'] || datastore['SMAC'] || ipv6_mac
  135. shost = opts['SHOST'] || datastore['SHOST'] || Rex::Socket.source_address(dhost_intf)
  136. dmac = opts['DMAC'] || solicit_ipv6_mac(dhost)
  137. timeout = opts['TIMEOUT'] || datastore['TIMEOUT']
  138. wait = opts['WAIT']
  139. if(wait.eql?(nil))
  140. wait = true
  141. end
  142. dmac.eql?(nil) and return false
  143. open_icmp_pcap()
  144. # Create ICMPv6 Request
  145. p = PacketFu::IPv6Packet.new
  146. p.eth_saddr = smac
  147. p.eth_daddr = dmac
  148. p.ipv6_saddr = shost
  149. p.ipv6_daddr = dhost
  150. p.ipv6_next = 0x3a
  151. icmp_id = rand(65000)
  152. icmp_seq = 1
  153. icmp_payload = Rex::Text.rand_text(8)
  154. p.payload = ipv6_icmpv6_echo_request(icmp_id,icmp_seq,icmp_payload)
  155. p.ipv6_len = p.payload.to_s.size
  156. ipv6_checksum!(p)
  157. @ipv6_icmp6_capture.inject(p.to_s)
  158. if(wait.eql?(true))
  159. print_status("Waiting for ping reply...")
  160. print_line("")
  161. # Wait for a response
  162. max_epoch = ::Time.now.to_i + timeout
  163. while(::Time.now.to_i < max_epoch)
  164. pkt = @ipv6_icmp6_capture.next()
  165. next if not pkt
  166. response_pkt = PacketFu::Packet.parse(pkt) rescue nil
  167. next unless response_pkt
  168. next unless response_pkt.is_ipv6?
  169. next unless response_pkt.payload
  170. next if response_pkt.payload.empty?
  171. next unless response_pkt.payload[0,1] == "\x81" # Echo reply
  172. if( response_pkt.ipv6_daddr == p.ipv6_saddr and
  173. response_pkt.ipv6_saddr == p.ipv6_daddr and
  174. response_pkt.ipv6_daddr == p.ipv6_saddr and
  175. response_pkt.payload[4,2] == p.payload[4,2] and # Id
  176. response_pkt.payload[6,2] == p.payload[6,2] # Seq
  177. )
  178. close_icmp_pcap()
  179. return(true)
  180. end
  181. end # End while
  182. end
  183. close_icmp_pcap()
  184. return(false)
  185. end
  186. #
  187. # Helper methods that haven't made it upstream yet. Mostly packet data
  188. # packers, also a checksum calculator.
  189. #
  190. def ipv6_icmpv6_echo_request(id,seq,data)
  191. type = 0x80
  192. code = 0
  193. checksum = 0
  194. id ||= rand(0x10000)
  195. seq ||= rand(0x10000)
  196. [type,code,checksum,id,seq,data].pack("CCnnna*")
  197. end
  198. # Simple tlv parser
  199. def ipv6_parse_options(data)
  200. pos = 0
  201. opts = []
  202. while pos < data.size
  203. type, len = data[pos,2].unpack("CC")
  204. this_opt = [type,len]
  205. this_opt << data[pos+2, (pos-2 + (len * 8))]
  206. opts << this_opt
  207. pos += this_opt.pack("CCa*").size
  208. end
  209. opts
  210. end
  211. # From Jon Hart's Racket::L3::Misc#linklocaladdr(), which
  212. # is from Daniele Bellucci
  213. def ipv6_linklocaladdr(mac)
  214. mac = mac.split(":")
  215. mac[0] = (mac[0].to_i(16) ^ (1 << 1)).to_s(16)
  216. ["fe80", "", mac[0,2].join, mac[2,2].join("ff:fe"), mac[4,2].join].join(":")
  217. end
  218. # From Jon Hart's Racket::L3::Misc#soll_mcast_addr6(),
  219. # which is from DDniele Belluci
  220. def ipv6_soll_mcast_addr6(addr)
  221. h = addr.split(':')[-2, 2]
  222. m = []
  223. m << 'ff'
  224. m << (h[0].to_i(16) & 0xff).to_s(16)
  225. m << ((h[1].to_i(16) & (0xff << 8)) >> 8).to_s(16)
  226. m << (h[1].to_i(16) & 0xff).to_s(16)
  227. 'ff02::1:' + [m[0,2].join, m[2,2].join].join(':')
  228. end
  229. # From Jon Hart's Racket::L3::Misc#soll_mcast_mac()
  230. def ipv6_soll_mcast_mac(addr)
  231. h = addr.split(':')[-2, 2]
  232. m = []
  233. m << 'ff'
  234. m << (h[0].to_i(16) & 0xff).to_s(16)
  235. m << ((h[1].to_i(16) & (0xff << 8)) >> 8).to_s(16)
  236. m << (h[1].to_i(16) & 0xff).to_s(16)
  237. '33:33:' + m.join(':')
  238. end
  239. # Usual ghetto strategy from PacketFu
  240. def ipv6_checksum!(pkt)
  241. check_data = pkt.headers.last[:ipv6_src].to_s.unpack("n8")
  242. check_data << pkt.headers.last[:ipv6_dst].to_s.unpack("n8")
  243. check_data << pkt.ipv6_len
  244. check_data << [0,58]
  245. check_payload = pkt.payload.size % 2 == 0 ? pkt.payload : pkt.payload + "\x00"
  246. check_data << check_payload.unpack("n*")
  247. check_data.flatten!
  248. checksum = check_data.inject(0) {|sum,x| sum += x}
  249. checksum = checksum % 0xffff
  250. checksum = 0xffff - checksum
  251. checksum == 0 ? 0xffff : checksum
  252. pkt.payload[2,2] = [checksum].pack("n")
  253. pkt
  254. end
  255. # Takes a neighbor and smac as arguments, The Neighbor
  256. # value must be an int, while the smac must be a string.
  257. # Very rudimentary and temporary.
  258. def ipv6_neighbor_solicitation(neigh,smac)
  259. target = neigh.to_s(16).scan(/../).map {|x| x.to_i(16)}.pack("C*")
  260. type = 135
  261. code = 0
  262. checksum = 0
  263. reserved = 0
  264. opt_type = 1
  265. opt_len = 1
  266. [type, code, checksum, reserved,
  267. target, opt_type, opt_len, smac
  268. ].pack("CCnNa16CCa6")
  269. end
  270. def check_pcaprub_loaded
  271. unless @pcaprub_loaded
  272. print_status("The Pcaprub module is not available: #{@pcaprub_error}")
  273. raise RuntimeError, "Pcaprub not available"
  274. else
  275. true
  276. end
  277. end
  278. end
  279. end