packetfu 1.1.10 → 1.1.11

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