packetfu 1.1.5 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. data/.document +5 -2
  2. data/.gitignore +1 -0
  3. data/LICENSE.txt +1 -1
  4. data/bench/after-2012-07-28.txt +25 -0
  5. data/bench/before-2012-07-28.txt +25 -0
  6. data/bench/benchit.rb +68 -0
  7. data/bench/calc_delta.rb +17 -0
  8. data/bench/octets.rb +22 -0
  9. data/bench/octets_after.txt +8 -0
  10. data/bench/octets_after_refactor.txt +8 -0
  11. data/bench/octets_before.txt +8 -0
  12. data/lib/packetfu.rb +8 -3
  13. data/lib/packetfu/packet.rb +2 -2
  14. data/lib/packetfu/pcap.rb +20 -4
  15. data/lib/packetfu/protos/arp.rb +7 -160
  16. data/lib/packetfu/protos/arp/header.rb +160 -0
  17. data/lib/packetfu/protos/arp/mixin.rb +38 -0
  18. data/lib/packetfu/protos/eth.rb +5 -247
  19. data/lib/packetfu/protos/eth/header.rb +247 -0
  20. data/lib/packetfu/protos/eth/mixin.rb +20 -0
  21. data/lib/packetfu/protos/hsrp.rb +13 -123
  22. data/lib/packetfu/protos/hsrp/header.rb +120 -0
  23. data/lib/packetfu/protos/hsrp/mixin.rb +31 -0
  24. data/lib/packetfu/protos/icmp.rb +10 -97
  25. data/lib/packetfu/protos/icmp/header.rb +93 -0
  26. data/lib/packetfu/protos/icmp/mixin.rb +17 -0
  27. data/lib/packetfu/protos/ip.rb +7 -295
  28. data/lib/packetfu/protos/ip/header.rb +335 -0
  29. data/lib/packetfu/protos/ip/mixin.rb +43 -0
  30. data/lib/packetfu/protos/ipv6.rb +7 -191
  31. data/lib/packetfu/protos/ipv6/header.rb +190 -0
  32. data/lib/packetfu/protos/ipv6/mixin.rb +31 -0
  33. data/lib/packetfu/protos/tcp.rb +13 -939
  34. data/lib/packetfu/protos/tcp/ecn.rb +42 -0
  35. data/lib/packetfu/protos/tcp/flags.rb +83 -0
  36. data/lib/packetfu/protos/tcp/header.rb +307 -0
  37. data/lib/packetfu/protos/tcp/hlen.rb +40 -0
  38. data/lib/packetfu/protos/tcp/mixin.rb +48 -0
  39. data/lib/packetfu/protos/tcp/option.rb +323 -0
  40. data/lib/packetfu/protos/tcp/options.rb +106 -0
  41. data/lib/packetfu/protos/tcp/reserved.rb +42 -0
  42. data/lib/packetfu/protos/udp.rb +12 -110
  43. data/lib/packetfu/protos/udp/header.rb +107 -0
  44. data/lib/packetfu/protos/udp/mixin.rb +23 -0
  45. data/lib/packetfu/utils.rb +24 -24
  46. data/lib/packetfu/version.rb +1 -1
  47. data/packetfu.gemspec +2 -2
  48. data/test/test_ip.rb +0 -19
  49. data/test/test_octets.rb +18 -21
  50. data/test/test_tcp.rb +10 -0
  51. data/test/test_udp.rb +17 -0
  52. metadata +79 -50
@@ -0,0 +1,160 @@
1
+ module PacketFu
2
+ # ARPHeader is a complete ARP struct, used in ARPPacket.
3
+ #
4
+ # ARP is used to discover the machine address of nearby devices.
5
+ #
6
+ # See http://www.networksorcery.com/enp/protocol/arp.htm for details.
7
+ #
8
+ # ==== Header Definition
9
+ #
10
+ # Int16 :arp_hw Default: 1 # Ethernet
11
+ # Int16 :arp_proto, Default: 0x8000 # IP
12
+ # Int8 :arp_hw_len, Default: 6
13
+ # Int8 :arp_proto_len, Default: 4
14
+ # Int16 :arp_opcode, Default: 1 # 1: Request, 2: Reply, 3: Request-Reverse, 4: Reply-Reverse
15
+ # EthMac :arp_src_mac # From eth.rb
16
+ # Octets :arp_src_ip # From ip.rb
17
+ # EthMac :arp_dst_mac # From eth.rb
18
+ # Octets :arp_dst_ip # From ip.rb
19
+ # String :body
20
+ class ARPHeader < Struct.new(:arp_hw, :arp_proto, :arp_hw_len,
21
+ :arp_proto_len, :arp_opcode,
22
+ :arp_src_mac, :arp_src_ip,
23
+ :arp_dst_mac, :arp_dst_ip,
24
+ :body)
25
+ include StructFu
26
+
27
+ def initialize(args={})
28
+ src_mac = args[:arp_src_mac] || (args[:config][:eth_src] if args[:config])
29
+ src_ip_bin = args[:arp_src_ip] || (args[:config][:ip_src_bin] if args[:config])
30
+
31
+ super(
32
+ Int16.new(args[:arp_hw] || 1),
33
+ Int16.new(args[:arp_proto] ||0x0800),
34
+ Int8.new(args[:arp_hw_len] || 6),
35
+ Int8.new(args[:arp_proto_len] || 4),
36
+ Int16.new(args[:arp_opcode] || 1),
37
+ EthMac.new.read(src_mac),
38
+ Octets.new.read(src_ip_bin),
39
+ EthMac.new.read(args[:arp_dst_mac]),
40
+ Octets.new.read(args[:arp_dst_ip]),
41
+ StructFu::String.new.read(args[:body])
42
+ )
43
+ end
44
+
45
+ # Returns the object in string form.
46
+ def to_s
47
+ self.to_a.map {|x| x.to_s}.join
48
+ end
49
+
50
+ # Reads a string to populate the object.
51
+ def read(str)
52
+ force_binary(str)
53
+ return self if str.nil?
54
+ self[:arp_hw].read(str[0,2])
55
+ self[:arp_proto].read(str[2,2])
56
+ self[:arp_hw_len].read(str[4,1])
57
+ self[:arp_proto_len].read(str[5,1])
58
+ self[:arp_opcode].read(str[6,2])
59
+ self[:arp_src_mac].read(str[8,6])
60
+ self[:arp_src_ip].read(str[14,4])
61
+ self[:arp_dst_mac].read(str[18,6])
62
+ self[:arp_dst_ip].read(str[24,4])
63
+ self[:body].read(str[28,str.size])
64
+ self
65
+ end
66
+
67
+ # Setter for the ARP hardware type.
68
+ def arp_hw=(i); typecast i; end
69
+ # Getter for the ARP hardware type.
70
+ def arp_hw; self[:arp_hw].to_i; end
71
+ # Setter for the ARP protocol.
72
+ def arp_proto=(i); typecast i; end
73
+ # Getter for the ARP protocol.
74
+ def arp_proto; self[:arp_proto].to_i; end
75
+ # Setter for the ARP hardware type length.
76
+ def arp_hw_len=(i); typecast i; end
77
+ # Getter for the ARP hardware type length.
78
+ def arp_hw_len; self[:arp_hw_len].to_i; end
79
+ # Setter for the ARP protocol length.
80
+ def arp_proto_len=(i); typecast i; end
81
+ # Getter for the ARP protocol length.
82
+ def arp_proto_len; self[:arp_proto_len].to_i; end
83
+ # Setter for the ARP opcode.
84
+ def arp_opcode=(i); typecast i; end
85
+ # Getter for the ARP opcode.
86
+ def arp_opcode; self[:arp_opcode].to_i; end
87
+ # Setter for the ARP source MAC address.
88
+ def arp_src_mac=(i); typecast i; end
89
+ # Getter for the ARP source MAC address.
90
+ def arp_src_mac; self[:arp_src_mac].to_s; end
91
+ # Getter for the ARP source IP address.
92
+ def arp_src_ip=(i); typecast i; end
93
+ # Setter for the ARP source IP address.
94
+ def arp_src_ip; self[:arp_src_ip].to_s; end
95
+ # Setter for the ARP destination MAC address.
96
+ def arp_dst_mac=(i); typecast i; end
97
+ # Setter for the ARP destination MAC address.
98
+ def arp_dst_mac; self[:arp_dst_mac].to_s; end
99
+ # Setter for the ARP destination IP address.
100
+ def arp_dst_ip=(i); typecast i; end
101
+ # Getter for the ARP destination IP address.
102
+ def arp_dst_ip; self[:arp_dst_ip].to_s; end
103
+
104
+ # Set the source MAC address in a more readable way.
105
+ def arp_saddr_mac=(mac)
106
+ mac = EthHeader.mac2str(mac)
107
+ self[:arp_src_mac].read(mac)
108
+ self.arp_src_mac
109
+ end
110
+
111
+ # Get a more readable source MAC address.
112
+ def arp_saddr_mac
113
+ EthHeader.str2mac(self[:arp_src_mac].to_s)
114
+ end
115
+
116
+ # Set the destination MAC address in a more readable way.
117
+ def arp_daddr_mac=(mac)
118
+ mac = EthHeader.mac2str(mac)
119
+ self[:arp_dst_mac].read(mac)
120
+ self.arp_dst_mac
121
+ end
122
+
123
+ # Get a more readable source MAC address.
124
+ def arp_daddr_mac
125
+ EthHeader.str2mac(self[:arp_dst_mac].to_s)
126
+ end
127
+
128
+ # Set a more readable source IP address.
129
+ def arp_saddr_ip=(addr)
130
+ self[:arp_src_ip].read_quad(addr)
131
+ end
132
+
133
+ # Get a more readable source IP address.
134
+ def arp_saddr_ip
135
+ self[:arp_src_ip].to_x
136
+ end
137
+
138
+ # Set a more readable destination IP address.
139
+ def arp_daddr_ip=(addr)
140
+ self[:arp_dst_ip].read_quad(addr)
141
+ end
142
+
143
+ # Get a more readable destination IP address.
144
+ def arp_daddr_ip
145
+ self[:arp_dst_ip].to_x
146
+ end
147
+
148
+ # Readability aliases
149
+
150
+ alias :arp_src_mac_readable :arp_saddr_mac
151
+ alias :arp_dst_mac_readable :arp_daddr_mac
152
+ alias :arp_src_ip_readable :arp_saddr_ip
153
+ alias :arp_dst_ip_readable :arp_daddr_ip
154
+
155
+ def arp_proto_readable
156
+ "0x%04x" % arp_proto
157
+ end
158
+
159
+ end # class ARPHeader
160
+ end
@@ -0,0 +1,38 @@
1
+ module PacketFu
2
+ # This Mixin simplifies access to the ARPHeaders. Mix this in with your
3
+ # packet interface, and it will add methods that essentially delegate to
4
+ # the 'arp_header' method (assuming that it is a ARPHeader object)
5
+ module ARPHeaderMixin
6
+ def arp_hw=(v); self.arp_header.arp_hw= v; end
7
+ def arp_hw; self.arp_header.arp_hw; end
8
+ def arp_proto=(v); self.arp_header.arp_proto= v; end
9
+ def arp_proto; self.arp_header.arp_proto; end
10
+ def arp_hw_len=(v); self.arp_header.arp_hw_len= v; end
11
+ def arp_hw_len; self.arp_header.arp_hw_len; end
12
+ def arp_proto_len=(v); self.arp_header.arp_proto_len= v; end
13
+ def arp_proto_len; self.arp_header.arp_proto_len; end
14
+ def arp_opcode=(v); self.arp_header.arp_opcode= v; end
15
+ def arp_opcode; self.arp_header.arp_opcode; end
16
+ def arp_src_mac=(v); self.arp_header.arp_src_mac= v; end
17
+ def arp_src_mac; self.arp_header.arp_src_mac; end
18
+ def arp_src_ip=(v); self.arp_header.arp_src_ip= v; end
19
+ def arp_src_ip; self.arp_header.arp_src_ip; end
20
+ def arp_dst_mac=(v); self.arp_header.arp_dst_mac= v; end
21
+ def arp_dst_mac; self.arp_header.arp_dst_mac; end
22
+ def arp_dst_ip=(v); self.arp_header.arp_dst_ip= v; end
23
+ def arp_dst_ip; self.arp_header.arp_dst_ip; end
24
+ def arp_saddr_mac=(v); self.arp_header.arp_saddr_mac= v; end
25
+ def arp_saddr_mac; self.arp_header.arp_saddr_mac; end
26
+ def arp_daddr_mac=(v); self.arp_header.arp_daddr_mac= v; end
27
+ def arp_daddr_mac; self.arp_header.arp_daddr_mac; end
28
+ def arp_saddr_ip=(v); self.arp_header.arp_saddr_ip= v; end
29
+ def arp_saddr_ip; self.arp_header.arp_saddr_ip; end
30
+ def arp_daddr_ip=(v); self.arp_header.arp_daddr_ip= v; end
31
+ def arp_daddr_ip; self.arp_header.arp_daddr_ip; end
32
+ def arp_src_mac_readable; self.arp_header.arp_src_mac_readable; end
33
+ def arp_dst_mac_readable; self.arp_header.arp_dst_mac_readable; end
34
+ def arp_src_ip_readable; self.arp_header.arp_src_ip_readable; end
35
+ def arp_dst_ip_readable; self.arp_header.arp_dst_ip_readable; end
36
+ def arp_proto_readable; self.arp_header.arp_proto_readable; end
37
+ end
38
+ end
@@ -1,251 +1,7 @@
1
- module PacketFu
2
-
3
- # EthOui is the Organizationally Unique Identifier portion of a MAC address, used in EthHeader.
4
- #
5
- # See the OUI list at http://standards.ieee.org/regauth/oui/oui.txt
6
- #
7
- # ==== Header Definition
8
- #
9
- # Fixnum :b0
10
- # Fixnum :b1
11
- # Fixnum :b2
12
- # Fixnum :b3
13
- # Fixnum :b4
14
- # Fixnum :b5
15
- # Fixnum :local
16
- # Fixnum :multicast
17
- # Int16 :oui, Default: 0x1ac5 :)
18
- class EthOui < Struct.new(:b5, :b4, :b3, :b2, :b1, :b0, :local, :multicast, :oui)
19
-
20
- # EthOui is unusual in that the bit values do not enjoy StructFu typing.
21
- def initialize(args={})
22
- args[:local] ||= 0
23
- args[:oui] ||= 0x1ac # :)
24
- args.each_pair {|k,v| args[k] = 0 unless v}
25
- super(args[:b5], args[:b4], args[:b3], args[:b2],
26
- args[:b1], args[:b0], args[:local], args[:multicast],
27
- args[:oui])
28
- end
29
-
30
- # Returns the object in string form.
31
- def to_s
32
- byte = 0
33
- byte += 0b10000000 if b5.to_i == 1
34
- byte += 0b01000000 if b4.to_i == 1
35
- byte += 0b00100000 if b3.to_i == 1
36
- byte += 0b00010000 if b2.to_i == 1
37
- byte += 0b00001000 if b1.to_i == 1
38
- byte += 0b00000100 if b0.to_i == 1
39
- byte += 0b00000010 if local.to_i == 1
40
- byte += 0b00000001 if multicast.to_i == 1
41
- [byte,oui].pack("Cn")
42
- end
43
-
44
- # Reads a string to populate the object.
45
- def read(str)
46
- force_binary(str)
47
- return self if str.nil?
48
- if 1.respond_to? :ord
49
- byte = str[0].ord
50
- else
51
- byte = str[0]
52
- end
53
- self[:b5] = byte & 0b10000000 == 0b10000000 ? 1 : 0
54
- self[:b4] = byte & 0b01000000 == 0b01000000 ? 1 : 0
55
- self[:b3] = byte & 0b00100000 == 0b00100000 ? 1 : 0
56
- self[:b2] = byte & 0b00010000 == 0b00010000 ? 1 : 0
57
- self[:b1] = byte & 0b00001000 == 0b00001000 ? 1 : 0
58
- self[:b0] = byte & 0b00000100 == 0b00000100 ? 1 : 0
59
- self[:local] = byte & 0b00000010 == 0b00000010 ? 1 : 0
60
- self[:multicast] = byte & 0b00000001 == 0b00000001 ? 1 : 0
61
- self[:oui] = str[1,2].unpack("n").first
62
- self
63
- end
64
-
65
- end
66
-
67
- # EthNic is the Network Interface Controler portion of a MAC address, used in EthHeader.
68
- #
69
- # ==== Header Definition
70
- #
71
- # Fixnum :n1
72
- # Fixnum :n2
73
- # Fixnum :n3
74
- #
75
- class EthNic < Struct.new(:n0, :n1, :n2)
76
-
77
- # EthNic does not enjoy StructFu typing.
78
- def initialize(args={})
79
- args.each_pair {|k,v| args[k] = 0 unless v}
80
- super(args[:n0], args[:n1], args[:n2])
81
- end
82
-
83
- # Returns the object in string form.
84
- def to_s
85
- [n0,n1,n2].map {|x| x.to_i}.pack("C3")
86
- end
87
-
88
- # Reads a string to populate the object.
89
- def read(str)
90
- force_binary(str)
91
- return self if str.nil?
92
- self[:n0], self[:n1], self[:n2] = str[0,3].unpack("C3")
93
- self
94
- end
95
-
96
- end
97
-
98
- # EthMac is the combination of an EthOui and EthNic, used in EthHeader.
99
- #
100
- # ==== Header Definition
101
- #
102
- # EthOui :oui # See EthOui
103
- # EthNic :nic # See EthNic
104
- class EthMac < Struct.new(:oui, :nic)
105
-
106
- def initialize(args={})
107
- super(
108
- EthOui.new.read(args[:oui]),
109
- EthNic.new.read(args[:nic]))
110
- end
111
-
112
- # Returns the object in string form.
113
- def to_s
114
- "#{self[:oui]}#{self[:nic]}"
115
- end
116
-
117
- # Reads a string to populate the object.
118
- def read(str)
119
- force_binary(str)
120
- return self if str.nil?
121
- self.oui.read str[0,3]
122
- self.nic.read str[3,3]
123
- self
124
- end
125
-
126
- end
127
-
128
- # EthHeader is a complete Ethernet struct, used in EthPacket.
129
- # It's the base header for all other protocols, such as IPHeader,
130
- # TCPHeader, etc.
131
- #
132
- # For more on the construction on MAC addresses, see
133
- # http://en.wikipedia.org/wiki/MAC_address
134
- #
135
- # TODO: Need to come up with a good way of dealing with vlan
136
- # tagging. Having a usually empty struct member seems weird,
137
- # but there may not be another way to do it if I want to preserve
138
- # the Eth-ness of vlan-tagged 802.1Q packets. Also, may as well
139
- # deal with 0x88a8 as well (http://en.wikipedia.org/wiki/802.1ad)
140
- #
141
- # ==== Header Definition
142
- #
143
- # EthMac :eth_dst # See EthMac
144
- # EthMac :eth_src # See EthMac
145
- # Int16 :eth_proto, Default: 0x8000 # IP 0x0800, Arp 0x0806
146
- # String :body
147
- class EthHeader < Struct.new(:eth_dst, :eth_src, :eth_proto, :body)
148
- include StructFu
149
-
150
- def initialize(args={})
151
- super(
152
- EthMac.new.read(args[:eth_dst]),
153
- EthMac.new.read(args[:eth_src]),
154
- Int16.new(args[:eth_proto] || 0x0800),
155
- StructFu::String.new.read(args[:body])
156
- )
157
- end
158
-
159
- # Setter for the Ethernet destination address.
160
- def eth_dst=(i); typecast(i); end
161
- # Getter for the Ethernet destination address.
162
- def eth_dst; self[:eth_dst].to_s; end
163
- # Setter for the Ethernet source address.
164
- def eth_src=(i); typecast(i); end
165
- # Getter for the Ethernet source address.
166
- def eth_src; self[:eth_src].to_s; end
167
- # Setter for the Ethernet protocol number.
168
- def eth_proto=(i); typecast(i); end
169
- # Getter for the Ethernet protocol number.
170
- def eth_proto; self[:eth_proto].to_i; end
171
-
172
- # Returns the object in string form.
173
- def to_s
174
- self.to_a.map {|x| x.to_s}.join
175
- end
176
-
177
- # Reads a string to populate the object.
178
- def read(str)
179
- force_binary(str)
180
- return self if str.nil?
181
- self[:eth_dst].read str[0,6]
182
- self[:eth_src].read str[6,6]
183
- self[:eth_proto].read str[12,2]
184
- self[:body].read str[14,str.size]
185
- self
186
- end
187
-
188
- # Converts a readable MAC (11:22:33:44:55:66) to a binary string.
189
- # Readable MAC's may be split on colons, dots, spaces, or underscores.
190
- #
191
- # irb> PacketFu::EthHeader.mac2str("11:22:33:44:55:66")
192
- #
193
- # #=> "\021\"3DUf"
194
- def self.mac2str(mac)
195
- if mac.split(/[:\x2d\x2e\x5f]+/).size == 6
196
- ret = mac.split(/[:\x2d\x2e\x20\x5f]+/).collect {|x| x.to_i(16)}.pack("C6")
197
- else
198
- raise ArgumentError, "Unkown format for mac address."
199
- end
200
- return ret
201
- end
202
-
203
- # Converts a binary string to a readable MAC (11:22:33:44:55:66).
204
- #
205
- # irb> PacketFu::EthHeader.str2mac("\x11\x22\x33\x44\x55\x66")
206
- #
207
- # #=> "11:22:33:44:55:66"
208
- def self.str2mac(mac='')
209
- if mac.to_s.size == 6 && mac.kind_of?(::String)
210
- ret = mac.unpack("C6").map {|x| sprintf("%02x",x)}.join(":")
211
- end
212
- end
213
-
214
- # Sets the source MAC address in a more readable way.
215
- def eth_saddr=(mac)
216
- mac = EthHeader.mac2str(mac)
217
- self[:eth_src].read mac
218
- self[:eth_src]
219
- end
220
-
221
- # Gets the source MAC address in a more readable way.
222
- def eth_saddr
223
- EthHeader.str2mac(self[:eth_src].to_s)
224
- end
225
-
226
- # Set the destination MAC address in a more readable way.
227
- def eth_daddr=(mac)
228
- mac = EthHeader.mac2str(mac)
229
- self[:eth_dst].read mac
230
- self[:eth_dst]
231
- end
232
-
233
- # Gets the destination MAC address in a more readable way.
234
- def eth_daddr
235
- EthHeader.str2mac(self[:eth_dst].to_s)
236
- end
237
-
238
- # Readability aliases
239
-
240
- alias :eth_dst_readable :eth_daddr
241
- alias :eth_src_readable :eth_saddr
242
-
243
- def eth_proto_readable
244
- "0x%04x" % eth_proto
245
- end
246
-
247
- end
1
+ require 'packetfu/protos/eth/header'
2
+ require 'packetfu/protos/eth/mixin'
248
3
 
4
+ module PacketFu
249
5
  # EthPacket is used to construct Ethernet packets. They contain an
250
6
  # Ethernet header, and that's about it.
251
7
  #
@@ -259,6 +15,8 @@ module PacketFu
259
15
  # eth_pkt.to_w('eth0') # Inject on the wire. (require root)
260
16
  #
261
17
  class EthPacket < Packet
18
+ include ::PacketFu::EthHeaderMixin
19
+
262
20
  attr_accessor :eth_header
263
21
 
264
22
  def self.can_parse?(str)