/lib/packetfu/packetfu/protos/ipv6.rb

https://bitbucket.org/technopunk2099/metasploit-framework · Ruby · 251 lines · 152 code · 31 blank · 68 comment · 12 complexity · 975a938c94476f13cb69e4a4d44c6300 MD5 · raw file

  1. # -*- coding: binary -*-
  2. module PacketFu
  3. # AddrIpv6 handles addressing for IPv6Header
  4. #
  5. # ==== Header Definition
  6. #
  7. # Int32 :a1
  8. # Int32 :a2
  9. # Int32 :a3
  10. # Int32 :a4
  11. class AddrIpv6 < Struct.new(:a1, :a2, :a3, :a4)
  12. include StructFu
  13. def initialize(args={})
  14. super(
  15. Int32.new(args[:a1]),
  16. Int32.new(args[:a2]),
  17. Int32.new(args[:a3]),
  18. Int32.new(args[:a4]))
  19. end
  20. # Returns the address in string format.
  21. def to_s
  22. self.to_a.map {|x| x.to_s}.join
  23. end
  24. # Returns the address as a fairly ginormous integer.
  25. def to_i
  26. (a1.to_i << 96) + (a2.to_i << 64) + (a3.to_i << 32) + a4.to_i
  27. end
  28. # Returns the address as a colon-delimited hex string.
  29. def to_x
  30. IPAddr.new(self.to_i, Socket::AF_INET6).to_s
  31. end
  32. # Reads in a string and casts it as an IPv6 address
  33. def read(str)
  34. force_binary(str)
  35. return self if str.nil?
  36. self[:a1].read str[0,4]
  37. self[:a2].read str[4,4]
  38. self[:a3].read str[8,4]
  39. self[:a4].read str[12,4]
  40. self
  41. end
  42. # Reads in a colon-delimited hex string and casts it as an IPv6 address.
  43. def read_x(str)
  44. addr = IPAddr.new(str).to_i
  45. self[:a1]=Int32.new(addr >> 96)
  46. self[:a2]=Int32.new((addr & 0x00000000ffffffff0000000000000000) >> 64)
  47. self[:a3]=Int32.new((addr & 0x0000000000000000ffffffff00000000) >> 32)
  48. self[:a4]=Int32.new(addr & 0x000000000000000000000000ffffffff)
  49. self
  50. end
  51. end
  52. # IPv6Header is complete IPv6 struct, used in IPv6Packet.
  53. #
  54. # ==== Header Definition
  55. #
  56. # Fixnum (4 bits) :ipv6_v Default: 6 # Versiom
  57. # Fixnum (8 bits) :ipv6_class Defualt: 0 # Class
  58. # Fixnum (20 bits) :ipv6_label Defualt: 0 # Label
  59. # Int16 :ipv6_len Default: calc # Payload length
  60. # Int8 :ipv6_next # Next Header
  61. # Int8 :ipv6_hop Default: 0xff # Hop limit
  62. # AddrIpv6 :ipv6_src
  63. # AddrIpv6 :ipv6_dst
  64. # String :body
  65. class IPv6Header < Struct.new(:ipv6_v, :ipv6_class, :ipv6_label,
  66. :ipv6_len, :ipv6_next, :ipv6_hop,
  67. :ipv6_src, :ipv6_dst, :body)
  68. include StructFu
  69. def initialize(args={})
  70. super(
  71. (args[:ipv6_v] || 6),
  72. (args[:ipv6_class] || 0),
  73. (args[:ipv6_label] || 0),
  74. Int16.new(args[:ipv6_len]),
  75. Int8.new(args[:ipv6_next]),
  76. Int8.new(args[:ipv6_hop] || 0xff),
  77. AddrIpv6.new.read(args[:ipv6_src] || ("\x00" * 16)),
  78. AddrIpv6.new.read(args[:ipv6_dst] || ("\x00" * 16)),
  79. StructFu::String.new.read(args[:body])
  80. )
  81. end
  82. # Returns the object in string form.
  83. def to_s
  84. bytes_v_class_label = [(self.ipv6_v << 28) +
  85. (self.ipv6_class << 20) +
  86. self.ipv6_label].pack("N")
  87. bytes_v_class_label + (self.to_a[3,6].map {|x| x.to_s}.join)
  88. end
  89. # Reads a string to populate the object.
  90. def read(str)
  91. force_binary(str)
  92. return self if str.nil?
  93. self[:ipv6_v] = str[0,1].unpack("C").first >> 4
  94. self[:ipv6_class] = (str[0,2].unpack("n").first & 0x0ff0) >> 4
  95. self[:ipv6_label] = str[0,4].unpack("N").first & 0x000fffff
  96. self[:ipv6_len].read(str[4,2])
  97. self[:ipv6_next].read(str[6,1])
  98. self[:ipv6_hop].read(str[7,1])
  99. self[:ipv6_src].read(str[8,16])
  100. self[:ipv6_dst].read(str[24,16])
  101. self[:body].read(str[40,str.size]) if str.size > 40
  102. self
  103. end
  104. # Setter for the version (usually, 6).
  105. def ipv6_v=(i); self[:ip_v] = i.to_i; end
  106. # Getter for the version (usually, 6).
  107. def ipv6_v; self[:ipv6_v].to_i; end
  108. # Setter for the traffic class.
  109. def ipv6_class=(i); self[:ip_class] = i.to_i; end
  110. # Getter for the traffic class.
  111. def ipv6_class; self[:ipv6_class].to_i; end
  112. # Setter for the flow label.
  113. def ipv6_label=(i); self[:ip_label] = i.to_i; end
  114. # Getter for the flow label.
  115. def ipv6_label; self[:ipv6_label].to_i; end
  116. # Setter for the payload length.
  117. def ipv6_len=(i); typecast i; end
  118. # Getter for the payload length.
  119. def ipv6_len; self[:ipv6_len].to_i; end
  120. # Setter for the next protocol header.
  121. def ipv6_next=(i); typecast i; end
  122. # Getter for the next protocol header.
  123. def ipv6_next; self[:ipv6_next].to_i; end
  124. # Setter for the hop limit.
  125. def ipv6_hop=(i); typecast i; end
  126. # Getter for the hop limit.
  127. def ipv6_hop; self[:ipv6_hop].to_i; end
  128. # Setter for the source address.
  129. def ipv6_src=(i); typecast i; end
  130. # Getter for the source address.
  131. def ipv6_src; self[:ipv6_src].to_i; end
  132. # Setter for the destination address.
  133. def ipv6_dst=(i); typecast i; end
  134. # Getter for the destination address.
  135. def ipv6_dst; self[:ipv6_dst].to_i; end
  136. # Calculates the payload length.
  137. def ipv6_calc_len
  138. self[:ipv6_len] = body.to_s.length
  139. end
  140. # Recalculates the calculatable fields for this object.
  141. def ipv6_recalc(arg=:all)
  142. case arg
  143. when :ipv6_len
  144. ipv6_calc_len
  145. when :all
  146. ipv6_recalc(:len)
  147. end
  148. end
  149. # Get the source address in a more readable form.
  150. def ipv6_saddr
  151. self[:ipv6_src].to_x
  152. end
  153. # Set the source address in a more readable form.
  154. def ipv6_saddr=(str)
  155. self[:ipv6_src].read_x(str)
  156. end
  157. # Get the destination address in a more readable form.
  158. def ipv6_daddr
  159. self[:ipv6_dst].to_x
  160. end
  161. # Set the destination address in a more readable form.
  162. def ipv6_daddr=(str)
  163. self[:ipv6_dst].read_x(str)
  164. end
  165. # Readability aliases
  166. alias :ipv6_src_readable :ipv6_saddr
  167. alias :ipv6_dst_readable :ipv6_daddr
  168. end # class IPv6Header
  169. # IPv6Packet is used to construct IPv6 Packets. They contain an EthHeader and an IPv6Header, and in
  170. # the distant, unknowable future, will take interesting IPv6ish payloads.
  171. #
  172. # This mostly complete, but not very useful. It's intended primarily as an example protocol.
  173. #
  174. # == Parameters
  175. #
  176. # :eth
  177. # A pre-generated EthHeader object.
  178. # :ip
  179. # A pre-generated IPHeader object.
  180. # :flavor
  181. # TODO: Sets the "flavor" of the IPv6 packet. No idea what this will look like, haven't done much IPv6 fingerprinting.
  182. # :config
  183. # A hash of return address details, often the output of Utils.whoami?
  184. class IPv6Packet < Packet
  185. attr_accessor :eth_header, :ipv6_header
  186. def self.can_parse?(str)
  187. return false unless EthPacket.can_parse? str
  188. return false unless str.size >= 54
  189. return false unless str[12,2] == "\x86\xdd"
  190. true
  191. end
  192. def read(str=nil,args={})
  193. raise "Cannot parse `#{str}'" unless self.class.can_parse?(str)
  194. @eth_header.read(str)
  195. @ipv6_header.read(str[14,str.size])
  196. @eth_header.body = @ipv6_header
  197. super(args)
  198. self
  199. end
  200. def initialize(args={})
  201. @eth_header = (args[:eth] || EthHeader.new)
  202. @ipv6_header = (args[:ipv6] || IPv6Header.new)
  203. @eth_header.eth_proto = 0x86dd
  204. @eth_header.body=@ipv6_header
  205. @headers = [@eth_header, @ipv6_header]
  206. super
  207. end
  208. # Peek provides summary data on packet contents.
  209. def peek(args={})
  210. peek_data = ["6 "]
  211. peek_data << "%-5d" % self.to_s.size
  212. peek_data << "%-31s" % self.ipv6_saddr
  213. peek_data << "-> "
  214. peek_data << "%-31s" % self.ipv6_daddr
  215. peek_data << " N:"
  216. peek_data << self.ipv6_next.to_s(16)
  217. peek_data.join
  218. end
  219. end
  220. end