packetfu 1.1.9 → 1.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/bench/octets.rb +9 -9
  2. data/examples/100kpackets.rb +12 -12
  3. data/examples/ackscan.rb +16 -16
  4. data/examples/arp.rb +35 -35
  5. data/examples/arphood.rb +36 -36
  6. data/examples/dissect_thinger.rb +6 -6
  7. data/examples/new-simple-stats.rb +23 -23
  8. data/examples/packetfu-shell.rb +25 -25
  9. data/examples/simple-sniffer.rb +9 -9
  10. data/examples/simple-stats.rb +23 -23
  11. data/examples/slammer.rb +3 -3
  12. data/lib/packetfu.rb +127 -127
  13. data/lib/packetfu/capture.rb +169 -169
  14. data/lib/packetfu/config.rb +52 -52
  15. data/lib/packetfu/inject.rb +56 -56
  16. data/lib/packetfu/packet.rb +528 -528
  17. data/lib/packetfu/pcap.rb +579 -579
  18. data/lib/packetfu/protos/arp.rb +90 -90
  19. data/lib/packetfu/protos/arp/header.rb +158 -158
  20. data/lib/packetfu/protos/arp/mixin.rb +36 -36
  21. data/lib/packetfu/protos/eth.rb +44 -44
  22. data/lib/packetfu/protos/eth/header.rb +243 -243
  23. data/lib/packetfu/protos/eth/mixin.rb +3 -3
  24. data/lib/packetfu/protos/hsrp.rb +69 -69
  25. data/lib/packetfu/protos/hsrp/header.rb +107 -107
  26. data/lib/packetfu/protos/hsrp/mixin.rb +29 -29
  27. data/lib/packetfu/protos/icmp.rb +71 -71
  28. data/lib/packetfu/protos/icmp/header.rb +82 -82
  29. data/lib/packetfu/protos/icmp/mixin.rb +14 -14
  30. data/lib/packetfu/protos/invalid.rb +49 -49
  31. data/lib/packetfu/protos/ip.rb +69 -69
  32. data/lib/packetfu/protos/ip/header.rb +291 -291
  33. data/lib/packetfu/protos/ip/mixin.rb +40 -40
  34. data/lib/packetfu/protos/ipv6.rb +50 -50
  35. data/lib/packetfu/protos/ipv6/header.rb +188 -188
  36. data/lib/packetfu/protos/ipv6/mixin.rb +29 -29
  37. data/lib/packetfu/protos/tcp.rb +176 -176
  38. data/lib/packetfu/protos/tcp/ecn.rb +35 -35
  39. data/lib/packetfu/protos/tcp/flags.rb +74 -74
  40. data/lib/packetfu/protos/tcp/header.rb +268 -268
  41. data/lib/packetfu/protos/tcp/hlen.rb +32 -32
  42. data/lib/packetfu/protos/tcp/mixin.rb +46 -46
  43. data/lib/packetfu/protos/tcp/option.rb +321 -321
  44. data/lib/packetfu/protos/tcp/options.rb +95 -95
  45. data/lib/packetfu/protos/tcp/reserved.rb +35 -35
  46. data/lib/packetfu/protos/udp.rb +116 -116
  47. data/lib/packetfu/protos/udp/header.rb +91 -91
  48. data/lib/packetfu/protos/udp/mixin.rb +3 -3
  49. data/lib/packetfu/structfu.rb +280 -280
  50. data/lib/packetfu/utils.rb +226 -217
  51. data/lib/packetfu/version.rb +41 -41
  52. data/packetfu.gemspec +2 -1
  53. data/spec/ethpacket_spec.rb +48 -48
  54. data/spec/packet_spec.rb +57 -57
  55. data/spec/packet_subclasses_spec.rb +8 -8
  56. data/spec/packetfu_spec.rb +59 -59
  57. data/spec/structfu_spec.rb +268 -268
  58. data/spec/tcp_spec.rb +75 -75
  59. data/test/all_tests.rb +13 -13
  60. data/test/func_lldp.rb +3 -3
  61. data/test/ptest.rb +2 -2
  62. data/test/test_arp.rb +116 -116
  63. data/test/test_capture.rb +45 -45
  64. data/test/test_eth.rb +68 -68
  65. data/test/test_hsrp.rb +9 -9
  66. data/test/test_icmp.rb +52 -52
  67. data/test/test_inject.rb +18 -18
  68. data/test/test_invalid.rb +16 -16
  69. data/test/test_ip.rb +36 -36
  70. data/test/test_ip6.rb +48 -48
  71. data/test/test_octets.rb +21 -21
  72. data/test/test_packet.rb +154 -154
  73. data/test/test_pcap.rb +170 -170
  74. data/test/test_structfu.rb +97 -97
  75. data/test/test_tcp.rb +320 -320
  76. data/test/test_udp.rb +76 -76
  77. metadata +4 -3
@@ -4,349 +4,349 @@ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
4
4
  require 'packetfu'
5
5
 
6
6
  class String
7
- def bin
8
- self.scan(/../).map {|x| x.to_i(16).chr}.join
9
- end
7
+ def bin
8
+ self.scan(/../).map {|x| x.to_i(16).chr}.join
9
+ end
10
10
  end
11
11
 
12
12
  class TcpEcnTest < Test::Unit::TestCase
13
- include PacketFu
14
-
15
- def test_ecn_set
16
- t = TcpEcn.new
17
- assert_kind_of TcpEcn, t
18
- assert_equal(0, t.to_i)
19
- t.n = 1
20
- assert_equal(4, t.to_i)
21
- t.c = 1
22
- assert_equal(6, t.to_i)
23
- t.e = 1
24
- assert_equal(7, t.to_i)
25
- end
26
-
27
- def test_ecn_read
28
- t = TcpEcn.new
29
- assert_kind_of TcpEcn, t
30
- t.read("\x30\xc0")
31
- assert_equal(0, t.n)
32
- assert_equal(1, t.c)
33
- assert_equal(1, t.e)
34
- t.read("\xa3\x38")
35
- assert_equal(1, t.n)
36
- assert_equal(0, t.c)
37
- assert_equal(0, t.e)
38
- end
39
-
40
- def test_hlen_set
41
- t = TcpHlen.new
42
- assert_kind_of TcpHlen, t
43
- assert_equal(0, t.to_i)
44
- t.hlen = 10
45
- assert_equal(10, t.to_i)
46
- end
47
-
48
- def test_hlen_read
49
- t = TcpHlen.new
50
- t.read("\xa0")
51
- assert_equal(10, t.to_i)
52
- end
53
-
54
- def test_reserved_set
55
- t = TcpReserved.new
56
- assert_kind_of TcpReserved, t
57
- assert_equal(0, t.to_i)
58
- t.r1 = 1
59
- assert_equal(4, t.to_i)
60
- t.r2 = 1
61
- assert_equal(6, t.to_i)
62
- t.r3 = 1
63
- assert_equal(7, t.to_i)
64
- end
65
-
66
- def test_reserved_read
67
- t = TcpReserved.new
68
- t.read("\xa0")
69
- assert_equal(0, t.to_i)
70
- end
13
+ include PacketFu
14
+
15
+ def test_ecn_set
16
+ t = TcpEcn.new
17
+ assert_kind_of TcpEcn, t
18
+ assert_equal(0, t.to_i)
19
+ t.n = 1
20
+ assert_equal(4, t.to_i)
21
+ t.c = 1
22
+ assert_equal(6, t.to_i)
23
+ t.e = 1
24
+ assert_equal(7, t.to_i)
25
+ end
26
+
27
+ def test_ecn_read
28
+ t = TcpEcn.new
29
+ assert_kind_of TcpEcn, t
30
+ t.read("\x30\xc0")
31
+ assert_equal(0, t.n)
32
+ assert_equal(1, t.c)
33
+ assert_equal(1, t.e)
34
+ t.read("\xa3\x38")
35
+ assert_equal(1, t.n)
36
+ assert_equal(0, t.c)
37
+ assert_equal(0, t.e)
38
+ end
39
+
40
+ def test_hlen_set
41
+ t = TcpHlen.new
42
+ assert_kind_of TcpHlen, t
43
+ assert_equal(0, t.to_i)
44
+ t.hlen = 10
45
+ assert_equal(10, t.to_i)
46
+ end
47
+
48
+ def test_hlen_read
49
+ t = TcpHlen.new
50
+ t.read("\xa0")
51
+ assert_equal(10, t.to_i)
52
+ end
53
+
54
+ def test_reserved_set
55
+ t = TcpReserved.new
56
+ assert_kind_of TcpReserved, t
57
+ assert_equal(0, t.to_i)
58
+ t.r1 = 1
59
+ assert_equal(4, t.to_i)
60
+ t.r2 = 1
61
+ assert_equal(6, t.to_i)
62
+ t.r3 = 1
63
+ assert_equal(7, t.to_i)
64
+ end
65
+
66
+ def test_reserved_read
67
+ t = TcpReserved.new
68
+ t.read("\xa0")
69
+ assert_equal(0, t.to_i)
70
+ end
71
71
 
72
72
  end
73
73
 
74
74
  class TcpFlagsTest < Test::Unit::TestCase
75
- include PacketFu
76
-
77
- def test_tcp_flags_set
78
- t = TcpFlags.new
79
- assert_kind_of TcpFlags, t
80
- t.fin = 1
81
- t.ack = 1
82
- assert_equal(0x11, t.to_i)
83
- t.fin = 0
84
- t.syn = 1
85
- assert_equal(0x12, t.to_i)
86
- end
87
-
88
- def test_tcp_flags_unset
89
- t = TcpFlags.new
90
- assert_kind_of TcpFlags, t
91
- t.syn = 1
92
- assert_equal(0x02, t.to_i)
93
- t.syn = 0
94
- assert_equal(0x00, t.to_i)
95
- t.syn = 1
96
- t.syn = false
97
- assert_equal(0x00, t.to_i)
98
- end
99
-
100
- def test_tcp_flags_read
101
- t = TcpFlags.new
102
- t.read("\x11")
103
- assert_equal(1, t.fin)
104
- assert_equal(1, t.ack)
105
- t.read("\xa6")
106
- assert_equal(1, t.urg)
107
- assert_equal(1, t.rst)
108
- assert_equal(1, t.syn)
109
- assert_equal(0, t.psh)
110
- assert_equal(0, t.ack)
111
- assert_equal(0, t.fin)
112
- end
75
+ include PacketFu
76
+
77
+ def test_tcp_flags_set
78
+ t = TcpFlags.new
79
+ assert_kind_of TcpFlags, t
80
+ t.fin = 1
81
+ t.ack = 1
82
+ assert_equal(0x11, t.to_i)
83
+ t.fin = 0
84
+ t.syn = 1
85
+ assert_equal(0x12, t.to_i)
86
+ end
87
+
88
+ def test_tcp_flags_unset
89
+ t = TcpFlags.new
90
+ assert_kind_of TcpFlags, t
91
+ t.syn = 1
92
+ assert_equal(0x02, t.to_i)
93
+ t.syn = 0
94
+ assert_equal(0x00, t.to_i)
95
+ t.syn = 1
96
+ t.syn = false
97
+ assert_equal(0x00, t.to_i)
98
+ end
99
+
100
+ def test_tcp_flags_read
101
+ t = TcpFlags.new
102
+ t.read("\x11")
103
+ assert_equal(1, t.fin)
104
+ assert_equal(1, t.ack)
105
+ t.read("\xa6")
106
+ assert_equal(1, t.urg)
107
+ assert_equal(1, t.rst)
108
+ assert_equal(1, t.syn)
109
+ assert_equal(0, t.psh)
110
+ assert_equal(0, t.ack)
111
+ assert_equal(0, t.fin)
112
+ end
113
113
 
114
114
  end
115
115
 
116
116
  class TcpOptionsTest < Test::Unit::TestCase
117
- include PacketFu
118
-
119
- def test_tcp_option
120
- t = TcpOption.new
121
- assert_equal("\x00", t.to_s)
122
- t = TcpOption.new(:kind => 2, :optlen => 4, :value => 1024)
123
- assert_equal("\x02\x04\x04\x00", t.to_s)
124
- t = TcpOption.new(:kind => 0xf0, :optlen => 6, :value => 1024)
125
- assert_equal("\xf0\x06\x00\x00\x04\x00", t.to_s)
126
- t = TcpOption.new(:kind => 0xf0, :optlen => 6, :value => "1024")
127
- assert_equal("\xf0\x061024", t.to_s)
128
- t = TcpOption.new(:kind => 0xf0, :optlen => 6, :value => nil)
129
- assert_equal("\xf0\x06", t.to_s)
130
- t = TcpOption.new(:kind => 0xf1, :optlen => 10, :value => "a1b2c3d4e5")
131
- assert_equal("\xf1\x0aa1b2c3d4e5", t.to_s)
132
- end
133
-
134
- def test_eol
135
- t = TcpOption::EOL.new
136
- assert_equal("\x00", t.to_s)
137
- assert_equal(0, t.kind.to_i)
138
- assert_equal(0, t.kind.value)
139
- assert_equal(nil, t.optlen.value)
140
- assert_equal("", t.value)
141
- assert_equal("EOL",t.decode)
142
- end
143
-
144
- def test_nop
145
- t = TcpOption::NOP.new
146
- assert_equal("\x01", t.to_s)
147
- assert_equal("NOP",t.decode)
148
- end
149
-
150
- def test_mss
151
- t = TcpOption::MSS.new
152
- t.read("\x02\x04\x05\xb4")
153
- assert_equal("MSS:1460",t.decode)
154
- t = TcpOption::MSS.new(:value => 1460)
155
- assert_equal("\x02\x04\x05\xb4", t.to_s)
156
- assert_equal("MSS:1460",t.decode)
157
- end
158
-
159
- def test_sack
160
- t = TcpOption::SACKOK.new
161
- assert_equal("\x04\x02", t.to_s)
162
- assert_equal("SACKOK",t.decode)
163
- end
164
-
165
- def test_sackok
166
- t = TcpOption::SACK.new
167
- assert_equal("\x05\x02", t.to_s)
168
- assert_equal("SACK:",t.decode)
169
- t = TcpOption::SACK.new(:value => "ABCD")
170
- assert_equal("\x05\x06\x41\x42\x43\x44", t.to_s)
171
- assert_equal("SACK:ABCD",t.decode)
172
- t = TcpOptions.new
173
- t.encode("SACK:ABCD,NOP,NOP") # Testing the variable optlen
174
- assert_equal("SACK:ABCD,NOP,NOP",t.decode)
175
- end
176
-
177
- def test_echo
178
- t = TcpOption::ECHO.new(:value => "ABCD")
179
- assert_equal("\x06\x06\x41\x42\x43\x44", t.to_s)
180
- assert_equal("ECHO:ABCD",t.decode)
181
- t = TcpOption::ECHO.new
182
- t.read("\x06\x06\x41\x42\x43\x44")
183
- assert_equal("ECHO:ABCD",t.decode)
184
- end
185
-
186
- def test_echoreply
187
- t = TcpOption::ECHOREPLY.new(:value => "ABCD")
188
- assert_equal("\x07\x06\x41\x42\x43\x44", t.to_s)
189
- assert_equal("ECHOREPLY:ABCD",t.decode)
190
- t = TcpOption::ECHOREPLY.new
191
- t.read("\x07\x06\x41\x42\x43\x44")
192
- assert_equal("ECHOREPLY:ABCD",t.decode)
193
- end
194
-
195
- def test_tsopt
196
- t = TcpOption::TS.new
197
- assert_equal("\x08\x0a\x00\x00\x00\x00\x00\x00\x00\x00", t.to_s)
198
- assert_equal("TS:0;0",t.decode)
199
- end
200
-
201
- def test_tcpoptions
202
- opt_string = "0101080a002af12c12ef0d57".bin
203
- t = TcpOptions.new
204
- t.read opt_string
205
- assert_equal("NOP,NOP,TS:2814252;317656407", t.decode)
206
- assert_equal(opt_string, t.to_s)
207
- opt_string = "020405b40402080a002af1120000000001030306".bin
208
- t = TcpOptions.new
209
- t.read opt_string
210
- assert_equal("MSS:1460,SACKOK,TS:2814226;0,NOP,WS:6", t.decode)
211
- end
212
-
213
- def test_tcpoptions_encode
214
- opt_string = "mss:1460,sackok,ts:2814226;0,nop,ws:6"
215
- t = TcpOptions.new
216
- t.encode opt_string
217
- assert_equal(opt_string.upcase, t.decode)
218
- assert_kind_of(StructFu::Int8,t[0].kind)
219
- assert_kind_of(StructFu::Int8,t[0].optlen)
220
- assert_kind_of(StructFu::Int16,t[0].value)
221
- assert_equal("\x02\x04\x05\xb4", t[0].to_s)
222
- assert_equal("\x08\x0a\x00\x2a\xf1\x12\x00\x00\x00\x00", t[2].to_s)
223
- end
117
+ include PacketFu
118
+
119
+ def test_tcp_option
120
+ t = TcpOption.new
121
+ assert_equal("\x00", t.to_s)
122
+ t = TcpOption.new(:kind => 2, :optlen => 4, :value => 1024)
123
+ assert_equal("\x02\x04\x04\x00", t.to_s)
124
+ t = TcpOption.new(:kind => 0xf0, :optlen => 6, :value => 1024)
125
+ assert_equal("\xf0\x06\x00\x00\x04\x00", t.to_s)
126
+ t = TcpOption.new(:kind => 0xf0, :optlen => 6, :value => "1024")
127
+ assert_equal("\xf0\x061024", t.to_s)
128
+ t = TcpOption.new(:kind => 0xf0, :optlen => 6, :value => nil)
129
+ assert_equal("\xf0\x06", t.to_s)
130
+ t = TcpOption.new(:kind => 0xf1, :optlen => 10, :value => "a1b2c3d4e5")
131
+ assert_equal("\xf1\x0aa1b2c3d4e5", t.to_s)
132
+ end
133
+
134
+ def test_eol
135
+ t = TcpOption::EOL.new
136
+ assert_equal("\x00", t.to_s)
137
+ assert_equal(0, t.kind.to_i)
138
+ assert_equal(0, t.kind.value)
139
+ assert_equal(nil, t.optlen.value)
140
+ assert_equal("", t.value)
141
+ assert_equal("EOL",t.decode)
142
+ end
143
+
144
+ def test_nop
145
+ t = TcpOption::NOP.new
146
+ assert_equal("\x01", t.to_s)
147
+ assert_equal("NOP",t.decode)
148
+ end
149
+
150
+ def test_mss
151
+ t = TcpOption::MSS.new
152
+ t.read("\x02\x04\x05\xb4")
153
+ assert_equal("MSS:1460",t.decode)
154
+ t = TcpOption::MSS.new(:value => 1460)
155
+ assert_equal("\x02\x04\x05\xb4", t.to_s)
156
+ assert_equal("MSS:1460",t.decode)
157
+ end
158
+
159
+ def test_sack
160
+ t = TcpOption::SACKOK.new
161
+ assert_equal("\x04\x02", t.to_s)
162
+ assert_equal("SACKOK",t.decode)
163
+ end
164
+
165
+ def test_sackok
166
+ t = TcpOption::SACK.new
167
+ assert_equal("\x05\x02", t.to_s)
168
+ assert_equal("SACK:",t.decode)
169
+ t = TcpOption::SACK.new(:value => "ABCD")
170
+ assert_equal("\x05\x06\x41\x42\x43\x44", t.to_s)
171
+ assert_equal("SACK:ABCD",t.decode)
172
+ t = TcpOptions.new
173
+ t.encode("SACK:ABCD,NOP,NOP") # Testing the variable optlen
174
+ assert_equal("SACK:ABCD,NOP,NOP",t.decode)
175
+ end
176
+
177
+ def test_echo
178
+ t = TcpOption::ECHO.new(:value => "ABCD")
179
+ assert_equal("\x06\x06\x41\x42\x43\x44", t.to_s)
180
+ assert_equal("ECHO:ABCD",t.decode)
181
+ t = TcpOption::ECHO.new
182
+ t.read("\x06\x06\x41\x42\x43\x44")
183
+ assert_equal("ECHO:ABCD",t.decode)
184
+ end
185
+
186
+ def test_echoreply
187
+ t = TcpOption::ECHOREPLY.new(:value => "ABCD")
188
+ assert_equal("\x07\x06\x41\x42\x43\x44", t.to_s)
189
+ assert_equal("ECHOREPLY:ABCD",t.decode)
190
+ t = TcpOption::ECHOREPLY.new
191
+ t.read("\x07\x06\x41\x42\x43\x44")
192
+ assert_equal("ECHOREPLY:ABCD",t.decode)
193
+ end
194
+
195
+ def test_tsopt
196
+ t = TcpOption::TS.new
197
+ assert_equal("\x08\x0a\x00\x00\x00\x00\x00\x00\x00\x00", t.to_s)
198
+ assert_equal("TS:0;0",t.decode)
199
+ end
200
+
201
+ def test_tcpoptions
202
+ opt_string = "0101080a002af12c12ef0d57".bin
203
+ t = TcpOptions.new
204
+ t.read opt_string
205
+ assert_equal("NOP,NOP,TS:2814252;317656407", t.decode)
206
+ assert_equal(opt_string, t.to_s)
207
+ opt_string = "020405b40402080a002af1120000000001030306".bin
208
+ t = TcpOptions.new
209
+ t.read opt_string
210
+ assert_equal("MSS:1460,SACKOK,TS:2814226;0,NOP,WS:6", t.decode)
211
+ end
212
+
213
+ def test_tcpoptions_encode
214
+ opt_string = "mss:1460,sackok,ts:2814226;0,nop,ws:6"
215
+ t = TcpOptions.new
216
+ t.encode opt_string
217
+ assert_equal(opt_string.upcase, t.decode)
218
+ assert_kind_of(StructFu::Int8,t[0].kind)
219
+ assert_kind_of(StructFu::Int8,t[0].optlen)
220
+ assert_kind_of(StructFu::Int16,t[0].value)
221
+ assert_equal("\x02\x04\x05\xb4", t[0].to_s)
222
+ assert_equal("\x08\x0a\x00\x2a\xf1\x12\x00\x00\x00\x00", t[2].to_s)
223
+ end
224
224
 
225
225
  end
226
226
 
227
227
  class TcpHeaderTest < Test::Unit::TestCase
228
- include PacketFu
229
-
230
- def test_header_new
231
- t = TCPHeader.new
232
- assert_kind_of TCPHeader, t
233
- assert_equal 20, t.sz
234
- assert_equal 13, t.size
235
- end
236
-
237
- def test_header_read
238
- t = TCPHeader.new
239
- str = "da920050c9fd6d2b2f54cc2f8018005c74de00000101080a002af11e12ef0d4a".bin
240
- str << "474554202f20485454502f312e310d0a557365722d4167656e743a206375726c2f372e31382e322028693438362d70632d6c696e75782d676e7529206c69626375726c2f372e31382e32204f70656e53534c2f302e392e3867207a6c69622f312e322e332e33206c696269646e2f312e31300d0a486f73743a207777772e706c616e622d73656375726974792e6e65740d0a4163636570743a202a2f2a0d0a0d0a".bin
241
- t.read str
242
- assert_equal 55954, t.tcp_sport
243
- assert_equal 80, t.tcp_dport
244
- assert_equal 3388828971, t.tcp_seq
245
- assert_equal 794086447, t.tcp_ack
246
- assert_equal 8, t.tcp_hlen
247
- assert_equal 0, t.tcp_reserved
248
- assert_equal 0, t.tcp_ecn
249
- assert_equal 1, t.tcp_flags.psh
250
- assert_equal 1, t.tcp_flags.ack
251
- assert_equal 0, t.tcp_flags.syn
252
- assert_equal 92, t.tcp_win
253
- assert_equal 0x74de, t.tcp_sum
254
- assert_equal "NOP,NOP,TS:2814238;317656394", t.tcp_options
255
- assert_equal "GET /", t.body[0,5]
256
- assert_equal "*\x0d\x0a\x0d\x0a", t.body[-5,5]
257
- end
228
+ include PacketFu
229
+
230
+ def test_header_new
231
+ t = TCPHeader.new
232
+ assert_kind_of TCPHeader, t
233
+ assert_equal 20, t.sz
234
+ assert_equal 13, t.size
235
+ end
236
+
237
+ def test_header_read
238
+ t = TCPHeader.new
239
+ str = "da920050c9fd6d2b2f54cc2f8018005c74de00000101080a002af11e12ef0d4a".bin
240
+ str << "474554202f20485454502f312e310d0a557365722d4167656e743a206375726c2f372e31382e322028693438362d70632d6c696e75782d676e7529206c69626375726c2f372e31382e32204f70656e53534c2f302e392e3867207a6c69622f312e322e332e33206c696269646e2f312e31300d0a486f73743a207777772e706c616e622d73656375726974792e6e65740d0a4163636570743a202a2f2a0d0a0d0a".bin
241
+ t.read str
242
+ assert_equal 55954, t.tcp_sport
243
+ assert_equal 80, t.tcp_dport
244
+ assert_equal 3388828971, t.tcp_seq
245
+ assert_equal 794086447, t.tcp_ack
246
+ assert_equal 8, t.tcp_hlen
247
+ assert_equal 0, t.tcp_reserved
248
+ assert_equal 0, t.tcp_ecn
249
+ assert_equal 1, t.tcp_flags.psh
250
+ assert_equal 1, t.tcp_flags.ack
251
+ assert_equal 0, t.tcp_flags.syn
252
+ assert_equal 92, t.tcp_win
253
+ assert_equal 0x74de, t.tcp_sum
254
+ assert_equal "NOP,NOP,TS:2814238;317656394", t.tcp_options
255
+ assert_equal "GET /", t.body[0,5]
256
+ assert_equal "*\x0d\x0a\x0d\x0a", t.body[-5,5]
257
+ end
258
258
 
259
259
  end
260
260
 
261
261
  class TCPPacketTest < Test::Unit::TestCase
262
- include PacketFu
263
-
264
- def test_tcp_peek
265
- t = TCPPacket.new
266
- t.ip_saddr = "10.20.30.40"
267
- t.ip_daddr = "50.60.70.80"
268
- t.tcp_src = 55954
269
- t.tcp_dport = 80
270
- t.tcp_flags.syn = 1
271
- t.tcp_flags.ack = true
272
- t.payload = "GET / HTTP/1.1\x0d\x0aHost: 50.60.70.80\x0d\x0a\x0d\x0a"
273
- t.recalc
274
- puts "\n"
275
- puts "TCP Peek format: "
276
- puts t.peek
277
- assert (t.peek.size <= 80)
278
- end
279
-
280
- def test_tcp_pcap
281
- t = TCPPacket.new
282
- assert_kind_of TCPPacket, t
283
- t.recalc
284
- t.to_f('tcp_test.pcap','a')
285
- t.recalc
286
- #t.to_f('tcp_test.pcap','a')
287
- t.ip_saddr = "10.20.30.40"
288
- t.ip_daddr = "50.60.70.80"
289
- t.payload = "+some fakey-fake tcp packet"
290
- t.tcp_sport = 1206
291
- t.tcp_dst = 13013
292
- t.tcp_flags.syn = 1
293
- t.tcp_flags.ack = true
294
- t.tcp_flags.psh = false
295
- t.recalc
296
- #t.to_f('tcp_test.pcap','a')
297
- end
298
-
299
- def test_tcp_read
300
- sample_packet = PcapFile.new.file_to_array(:f => 'sample.pcap')[7]
301
- pkt = Packet.parse(sample_packet)
302
- assert_kind_of TCPPacket, pkt
303
- assert_equal(0x5a73, pkt.tcp_sum)
304
- pkt.to_f('tcp_test.pcap','a')
305
- end
306
-
307
- def test_tcp_alter
308
- sample_packet = PcapFile.new.file_to_array(:f => 'sample2.pcap')[3]
309
- pkt = Packet.parse(sample_packet)
310
- assert_kind_of TCPPacket, pkt
311
- pkt.tcp_sport = 13013
312
- pkt.payload = pkt.payload.gsub(/planb/,"brandx")
313
- pkt.recalc
314
- pkt.to_f('tcp_test.pcap','a')
315
- end
316
-
317
- def test_tcp_read_strip
318
- str = "e0f8472161a600254ba0760608004500004403554000400651d0c0a83207c0a832370224c1d22d94847f0b07c4ba8018ffff30ba00000101080a8731821433564b8c01027165000000000000200000000000".bin
319
- str << "0102".bin # Tacking on a couple extra bites tht we'll strip off.
320
- not_stripped = TCPPacket.new
321
- not_stripped.read(str)
322
- assert_equal 18, not_stripped.tcp_header.body.length
323
- stripped = TCPPacket.new
324
- stripped.read(str, :strip => true)
325
- assert_equal 16, stripped.tcp_header.body.length
326
- end
327
-
328
- def test_tcp_reread
329
- sample_packet = PacketFu::TCPPacket.new
330
- pkt = Packet.parse(sample_packet.to_s)
331
- assert sample_packet.is_tcp?
332
- assert pkt.is_tcp?
333
- end
262
+ include PacketFu
263
+
264
+ def test_tcp_peek
265
+ t = TCPPacket.new
266
+ t.ip_saddr = "10.20.30.40"
267
+ t.ip_daddr = "50.60.70.80"
268
+ t.tcp_src = 55954
269
+ t.tcp_dport = 80
270
+ t.tcp_flags.syn = 1
271
+ t.tcp_flags.ack = true
272
+ t.payload = "GET / HTTP/1.1\x0d\x0aHost: 50.60.70.80\x0d\x0a\x0d\x0a"
273
+ t.recalc
274
+ puts "\n"
275
+ puts "TCP Peek format: "
276
+ puts t.peek
277
+ assert (t.peek.size <= 80)
278
+ end
279
+
280
+ def test_tcp_pcap
281
+ t = TCPPacket.new
282
+ assert_kind_of TCPPacket, t
283
+ t.recalc
284
+ t.to_f('tcp_test.pcap','a')
285
+ t.recalc
286
+ #t.to_f('tcp_test.pcap','a')
287
+ t.ip_saddr = "10.20.30.40"
288
+ t.ip_daddr = "50.60.70.80"
289
+ t.payload = "+some fakey-fake tcp packet"
290
+ t.tcp_sport = 1206
291
+ t.tcp_dst = 13013
292
+ t.tcp_flags.syn = 1
293
+ t.tcp_flags.ack = true
294
+ t.tcp_flags.psh = false
295
+ t.recalc
296
+ #t.to_f('tcp_test.pcap','a')
297
+ end
298
+
299
+ def test_tcp_read
300
+ sample_packet = PcapFile.new.file_to_array(:f => 'sample.pcap')[7]
301
+ pkt = Packet.parse(sample_packet)
302
+ assert_kind_of TCPPacket, pkt
303
+ assert_equal(0x5a73, pkt.tcp_sum)
304
+ pkt.to_f('tcp_test.pcap','a')
305
+ end
306
+
307
+ def test_tcp_alter
308
+ sample_packet = PcapFile.new.file_to_array(:f => 'sample2.pcap')[3]
309
+ pkt = Packet.parse(sample_packet)
310
+ assert_kind_of TCPPacket, pkt
311
+ pkt.tcp_sport = 13013
312
+ pkt.payload = pkt.payload.gsub(/planb/,"brandx")
313
+ pkt.recalc
314
+ pkt.to_f('tcp_test.pcap','a')
315
+ end
316
+
317
+ def test_tcp_read_strip
318
+ str = "e0f8472161a600254ba0760608004500004403554000400651d0c0a83207c0a832370224c1d22d94847f0b07c4ba8018ffff30ba00000101080a8731821433564b8c01027165000000000000200000000000".bin
319
+ str << "0102".bin # Tacking on a couple extra bites tht we'll strip off.
320
+ not_stripped = TCPPacket.new
321
+ not_stripped.read(str)
322
+ assert_equal 18, not_stripped.tcp_header.body.length
323
+ stripped = TCPPacket.new
324
+ stripped.read(str, :strip => true)
325
+ assert_equal 16, stripped.tcp_header.body.length
326
+ end
327
+
328
+ def test_tcp_reread
329
+ sample_packet = PacketFu::TCPPacket.new
330
+ pkt = Packet.parse(sample_packet.to_s)
331
+ assert sample_packet.is_tcp?
332
+ assert pkt.is_tcp?
333
+ end
334
334
 
335
335
  end
336
336
 
337
337
  class TCPPacketTest < Test::Unit::TestCase
338
- include PacketFu
339
-
340
- def test_tcp_edit_opts
341
- t = TCPPacket.new
342
- assert_equal(0, t.tcp_options.size)
343
- assert_equal(0, t.tcp_opts_len)
344
- assert_equal(5, t.tcp_hlen)
345
- t.tcp_options = "NOP,NOP,NOP,NOP"
346
- assert_equal(4, t.tcp_opts_len)
347
- t.recalc
348
- assert_equal(6, t.tcp_hlen)
349
- end
338
+ include PacketFu
339
+
340
+ def test_tcp_edit_opts
341
+ t = TCPPacket.new
342
+ assert_equal(0, t.tcp_options.size)
343
+ assert_equal(0, t.tcp_opts_len)
344
+ assert_equal(5, t.tcp_hlen)
345
+ t.tcp_options = "NOP,NOP,NOP,NOP"
346
+ assert_equal(4, t.tcp_opts_len)
347
+ t.recalc
348
+ assert_equal(6, t.tcp_hlen)
349
+ end
350
350
 
351
351
  end
352
352