packetfu 1.1.11 → 1.1.12.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/.rspec +2 -0
  4. data/.travis.yml +2 -3
  5. data/README.md +127 -0
  6. data/examples/100kpackets.rb +11 -10
  7. data/examples/ackscan.rb +4 -1
  8. data/examples/arp.rb +4 -5
  9. data/examples/arphood.rb +5 -4
  10. data/examples/dissect_thinger.rb +10 -7
  11. data/examples/ethernet.rb +8 -3
  12. data/examples/ids.rb +22 -4
  13. data/examples/idsv2.rb +25 -6
  14. data/examples/ifconfig.rb +6 -3
  15. data/examples/new-simple-stats.rb +5 -6
  16. data/examples/packetfu-shell.rb +11 -48
  17. data/examples/pcap2pcapng.rb +32 -0
  18. data/examples/simple-sniffer.rb +9 -4
  19. data/examples/simple-stats.rb +7 -8
  20. data/examples/slammer.rb +2 -2
  21. data/examples/uniqpcap.rb +17 -7
  22. data/lib/packetfu.rb +10 -175
  23. data/lib/packetfu/capture.rb +2 -2
  24. data/lib/packetfu/common.rb +142 -0
  25. data/lib/packetfu/config.rb +8 -8
  26. data/lib/packetfu/inject.rb +3 -3
  27. data/lib/packetfu/packet.rb +22 -18
  28. data/lib/packetfu/pcap.rb +2 -1
  29. data/lib/packetfu/pcapng.rb +37 -0
  30. data/lib/packetfu/pcapng/block.rb +25 -0
  31. data/lib/packetfu/pcapng/epb.rb +112 -0
  32. data/lib/packetfu/pcapng/file.rb +316 -0
  33. data/lib/packetfu/pcapng/idb.rb +125 -0
  34. data/lib/packetfu/pcapng/shb.rb +146 -0
  35. data/lib/packetfu/pcapng/spb.rb +83 -0
  36. data/lib/packetfu/pcapng/unknown_block.rb +60 -0
  37. data/lib/packetfu/protos.rb +3 -0
  38. data/lib/packetfu/protos/arp.rb +10 -10
  39. data/lib/packetfu/protos/icmpv6.rb +131 -0
  40. data/lib/packetfu/protos/icmpv6/header.rb +69 -0
  41. data/lib/packetfu/protos/icmpv6/mixin.rb +14 -0
  42. data/lib/packetfu/protos/ip.rb +4 -5
  43. data/lib/packetfu/protos/ipv6/header.rb +2 -0
  44. data/lib/packetfu/protos/udp.rb +24 -12
  45. data/lib/packetfu/structfu.rb +27 -0
  46. data/lib/packetfu/utils.rb +55 -9
  47. data/lib/packetfu/version.rb +1 -1
  48. data/packetfu.gemspec +13 -7
  49. data/spec/arp_spec.rb +11 -5
  50. data/spec/eth_spec.rb +20 -11
  51. data/spec/fake_packets.rb +28 -0
  52. data/spec/hsrp_spec.rb +15 -0
  53. data/spec/icmp_spec.rb +12 -5
  54. data/spec/icmpv6_spec.rb +98 -0
  55. data/spec/invalid_spec.rb +28 -0
  56. data/spec/ip_spec.rb +10 -5
  57. data/spec/ipv4_icmp.pcap +0 -0
  58. data/spec/ipv4_udp.pcap +0 -0
  59. data/spec/ipv6_icmp.pcap +0 -0
  60. data/spec/ipv6_spec.rb +4 -0
  61. data/spec/ipv6_udp.pcap +0 -0
  62. data/spec/lldp_spec.rb +36 -0
  63. data/spec/octets_spec.rb +43 -0
  64. data/spec/packet_spec.rb +24 -0
  65. data/spec/packetfu_spec.rb +6 -1
  66. data/spec/pcap_spec.rb +286 -0
  67. data/spec/pcapng/epb_spec.rb +81 -0
  68. data/spec/pcapng/file_spec.rb +295 -0
  69. data/spec/pcapng/file_spec_helper.rb +45 -0
  70. data/spec/pcapng/idb_spec.rb +53 -0
  71. data/spec/pcapng/shb_spec.rb +42 -0
  72. data/spec/pcapng/spb_spec.rb +43 -0
  73. data/spec/pcapng/unknown_block_spec.rb +36 -0
  74. data/spec/spec_helper.rb +3 -31
  75. data/spec/tcp_spec.rb +4 -1
  76. data/spec/udp_spec.rb +149 -1
  77. data/spec/utils_spec.rb +98 -15
  78. data/test/pcapng-test/output_be/advanced/test100.pcapng +0 -0
  79. data/test/pcapng-test/output_be/advanced/test100.txt +11 -0
  80. data/test/pcapng-test/output_be/advanced/test101.pcapng +0 -0
  81. data/test/pcapng-test/output_be/advanced/test101.txt +11 -0
  82. data/test/pcapng-test/output_be/advanced/test102.pcapng +0 -0
  83. data/test/pcapng-test/output_be/advanced/test102.txt +14 -0
  84. data/test/pcapng-test/output_be/basic/test001.pcapng +0 -0
  85. data/test/pcapng-test/output_be/basic/test001.txt +9 -0
  86. data/test/pcapng-test/output_be/basic/test002.pcapng +0 -0
  87. data/test/pcapng-test/output_be/basic/test002.txt +7 -0
  88. data/test/pcapng-test/output_be/basic/test003.pcapng +0 -0
  89. data/test/pcapng-test/output_be/basic/test003.txt +8 -0
  90. data/test/pcapng-test/output_be/basic/test004.pcapng +0 -0
  91. data/test/pcapng-test/output_be/basic/test004.txt +9 -0
  92. data/test/pcapng-test/output_be/basic/test005.pcapng +0 -0
  93. data/test/pcapng-test/output_be/basic/test005.txt +9 -0
  94. data/test/pcapng-test/output_be/basic/test006.pcapng +0 -0
  95. data/test/pcapng-test/output_be/basic/test006.txt +9 -0
  96. data/test/pcapng-test/output_be/basic/test007.pcapng +0 -0
  97. data/test/pcapng-test/output_be/basic/test007.txt +9 -0
  98. data/test/pcapng-test/output_be/basic/test008.pcapng +0 -0
  99. data/test/pcapng-test/output_be/basic/test008.txt +9 -0
  100. data/test/pcapng-test/output_be/basic/test009.pcapng +0 -0
  101. data/test/pcapng-test/output_be/basic/test009.txt +9 -0
  102. data/test/pcapng-test/output_be/basic/test010.pcapng +0 -0
  103. data/test/pcapng-test/output_be/basic/test010.txt +9 -0
  104. data/test/pcapng-test/output_be/basic/test011.pcapng +0 -0
  105. data/test/pcapng-test/output_be/basic/test011.txt +10 -0
  106. data/test/pcapng-test/output_be/basic/test012.pcapng +0 -0
  107. data/test/pcapng-test/output_be/basic/test012.txt +10 -0
  108. data/test/pcapng-test/output_be/basic/test013.pcapng +0 -0
  109. data/test/pcapng-test/output_be/basic/test013.txt +9 -0
  110. data/test/pcapng-test/output_be/basic/test014.pcapng +0 -0
  111. data/test/pcapng-test/output_be/basic/test014.txt +9 -0
  112. data/test/pcapng-test/output_be/basic/test015.pcapng +0 -0
  113. data/test/pcapng-test/output_be/basic/test015.txt +9 -0
  114. data/test/pcapng-test/output_be/basic/test016.pcapng +0 -0
  115. data/test/pcapng-test/output_be/basic/test016.txt +11 -0
  116. data/test/pcapng-test/output_be/basic/test017.pcapng +0 -0
  117. data/test/pcapng-test/output_be/basic/test017.txt +9 -0
  118. data/test/pcapng-test/output_be/basic/test018.pcapng +0 -0
  119. data/test/pcapng-test/output_be/basic/test018.txt +12 -0
  120. data/test/pcapng-test/output_be/difficult/test200.pcapng +0 -0
  121. data/test/pcapng-test/output_be/difficult/test200.txt +8 -0
  122. data/test/pcapng-test/output_be/difficult/test201.pcapng +0 -0
  123. data/test/pcapng-test/output_be/difficult/test201.txt +11 -0
  124. data/test/pcapng-test/output_be/difficult/test202.pcapng +0 -0
  125. data/test/pcapng-test/output_be/difficult/test202.txt +14 -0
  126. data/test/pcapng-test/output_le/advanced/test100.pcapng +0 -0
  127. data/test/pcapng-test/output_le/advanced/test100.txt +11 -0
  128. data/test/pcapng-test/output_le/advanced/test101.pcapng +0 -0
  129. data/test/pcapng-test/output_le/advanced/test101.txt +11 -0
  130. data/test/pcapng-test/output_le/advanced/test102.pcapng +0 -0
  131. data/test/pcapng-test/output_le/advanced/test102.txt +14 -0
  132. data/test/pcapng-test/output_le/basic/test001.pcapng +0 -0
  133. data/test/pcapng-test/output_le/basic/test001.txt +9 -0
  134. data/test/pcapng-test/output_le/basic/test002.pcapng +0 -0
  135. data/test/pcapng-test/output_le/basic/test002.txt +7 -0
  136. data/test/pcapng-test/output_le/basic/test003.pcapng +0 -0
  137. data/test/pcapng-test/output_le/basic/test003.txt +8 -0
  138. data/test/pcapng-test/output_le/basic/test004.pcapng +0 -0
  139. data/test/pcapng-test/output_le/basic/test004.txt +9 -0
  140. data/test/pcapng-test/output_le/basic/test005.pcapng +0 -0
  141. data/test/pcapng-test/output_le/basic/test005.txt +9 -0
  142. data/test/pcapng-test/output_le/basic/test006.pcapng +0 -0
  143. data/test/pcapng-test/output_le/basic/test006.txt +9 -0
  144. data/test/pcapng-test/output_le/basic/test007.pcapng +0 -0
  145. data/test/pcapng-test/output_le/basic/test007.txt +9 -0
  146. data/test/pcapng-test/output_le/basic/test008.pcapng +0 -0
  147. data/test/pcapng-test/output_le/basic/test008.txt +9 -0
  148. data/test/pcapng-test/output_le/basic/test009.pcapng +0 -0
  149. data/test/pcapng-test/output_le/basic/test009.txt +9 -0
  150. data/test/pcapng-test/output_le/basic/test010.pcapng +0 -0
  151. data/test/pcapng-test/output_le/basic/test010.txt +9 -0
  152. data/test/pcapng-test/output_le/basic/test011.pcapng +0 -0
  153. data/test/pcapng-test/output_le/basic/test011.txt +10 -0
  154. data/test/pcapng-test/output_le/basic/test012.pcapng +0 -0
  155. data/test/pcapng-test/output_le/basic/test012.txt +10 -0
  156. data/test/pcapng-test/output_le/basic/test013.pcapng +0 -0
  157. data/test/pcapng-test/output_le/basic/test013.txt +9 -0
  158. data/test/pcapng-test/output_le/basic/test014.pcapng +0 -0
  159. data/test/pcapng-test/output_le/basic/test014.txt +9 -0
  160. data/test/pcapng-test/output_le/basic/test015.pcapng +0 -0
  161. data/test/pcapng-test/output_le/basic/test015.txt +9 -0
  162. data/test/pcapng-test/output_le/basic/test016.pcapng +0 -0
  163. data/test/pcapng-test/output_le/basic/test016.txt +11 -0
  164. data/test/pcapng-test/output_le/basic/test017.pcapng +0 -0
  165. data/test/pcapng-test/output_le/basic/test017.txt +9 -0
  166. data/test/pcapng-test/output_le/basic/test018.pcapng +0 -0
  167. data/test/pcapng-test/output_le/basic/test018.txt +12 -0
  168. data/test/pcapng-test/output_le/difficult/test200.pcapng +0 -0
  169. data/test/pcapng-test/output_le/difficult/test200.txt +8 -0
  170. data/test/pcapng-test/output_le/difficult/test201.pcapng +0 -0
  171. data/test/pcapng-test/output_le/difficult/test201.txt +11 -0
  172. data/test/pcapng-test/output_le/difficult/test202.pcapng +0 -0
  173. data/test/pcapng-test/output_le/difficult/test202.txt +14 -0
  174. data/test/sample-ipv6.pcapng +0 -0
  175. data/test/sample-spb.pcapng +0 -0
  176. data/test/sample.pcapng +0 -0
  177. data/test/sample2.pcapng +0 -0
  178. metadata +190 -68
  179. checksums.yaml.gz.sig +0 -0
  180. data.tar.gz.sig +0 -2
  181. data/INSTALL.rdoc +0 -40
  182. data/README.rdoc +0 -64
  183. data/examples/examples.rb +0 -4
  184. data/setup.rb +0 -1586
  185. data/test/func_lldp.rb +0 -25
  186. data/test/ptest.rb +0 -16
  187. data/test/test_eth.rb +0 -93
  188. data/test/test_hsrp.rb +0 -20
  189. data/test/test_invalid.rb +0 -28
  190. data/test/test_octets.rb +0 -36
  191. data/test/test_pcap.rb +0 -211
  192. data/test/test_udp.rb +0 -100
  193. metadata.gz.sig +0 -2
@@ -0,0 +1,45 @@
1
+ # -*- coding: binary -*-
2
+
3
+ module PacketFu
4
+ module PcapNG
5
+
6
+ # Hash containing attended structure for each test file.
7
+ # Hash's values are arrays. Each element of these arrays are a section in
8
+ # pcapng file. A section is described as a hash which keys are block types
9
+ # and values number of each type in a section.
10
+ PCAPNG_TEST_FILES = {
11
+ "basic/test001.pcapng"=>[{:idb=>1, :epb=>4, :spb=>0, :unknown=>0}],
12
+ "basic/test002.pcapng"=>[{:idb=>0, :epb=>0, :spb=>0, :unknown=>0}],
13
+ "basic/test003.pcapng"=>[{:idb=>1, :epb=>0, :spb=>0, :unknown=>0}],
14
+ "basic/test004.pcapng"=>[{:idb=>2, :epb=>4, :spb=>0, :unknown=>0}],
15
+ "basic/test005.pcapng"=>[{:idb=>2, :epb=>4, :spb=>0, :unknown=>0}],
16
+ "basic/test006.pcapng"=>[{:idb=>2, :epb=>5, :spb=>0, :unknown=>0}],
17
+ "basic/test007.pcapng"=>[{:idb=>1, :epb=>1, :spb=>0, :unknown=>0}],
18
+ "basic/test008.pcapng"=>[{:idb=>2, :epb=>4, :spb=>0, :unknown=>0}],
19
+ "basic/test009.pcapng"=>[{:idb=>1, :epb=>2, :spb=>0, :unknown=>0}],
20
+ "basic/test010.pcapng"=>[{:idb=>1, :epb=>0, :spb=>4, :unknown=>0}],
21
+ "basic/test011.pcapng"=>[{:idb=>1, :epb=>2, :spb=>2, :unknown=>0}],
22
+ "basic/test012.pcapng"=>[{:idb=>1, :epb=>2, :spb=>2, :unknown=>0}],
23
+ "basic/test013.pcapng"=>[{:idb=>1, :epb=>0, :spb=>0, :unknown=>1}],
24
+ "basic/test014.pcapng"=>[{:idb=>3, :epb=>0, :spb=>0, :unknown=>3}],
25
+ "basic/test015.pcapng"=>[{:idb=>1, :epb=>0, :spb=>0, :unknown=>1}],
26
+ "basic/test016.pcapng"=>[{:idb=>1, :epb=>2, :spb=>2, :unknown=>3}],
27
+ "basic/test017.pcapng"=>[{:idb=>0, :epb=>0, :spb=>0, :unknown=>4}],
28
+ "basic/test018.pcapng"=>[{:idb=>1, :epb=>2, :spb=>2, :unknown=>4}],
29
+ "advanced/test100.pcapng"=>[{:idb=>3, :epb=>3, :spb=>2, :unknown=>5}],
30
+ "advanced/test101.pcapng"=>[{:idb=>3, :epb=>3, :spb=>1, :unknown=>6}],
31
+ "advanced/test102.pcapng"=>[{:idb=>3, :epb=>4, :spb=>1, :unknown=>12}],
32
+ "difficult/test200.pcapng"=>[{:idb=>1, :epb=>0, :spb=>0, :unknown=>0},
33
+ {:idb=>1, :epb=>0, :spb=>0, :unknown=>0},
34
+ {:idb=>1, :epb=>0, :spb=>0, :unknown=>0}],
35
+ "difficult/test201.pcapng"=>[{:idb=>2, :epb=>1, :spb=>0, :unknown=>1},
36
+ {:idb=>1, :epb=>1, :spb=>1, :unknown=>1},
37
+ {:idb=>2, :epb=>1, :spb=>0, :unknown=>2}],
38
+ "difficult/test202.pcapng"=>[{:idb=>2, :epb=>3, :spb=>0, :unknown=>4},
39
+ {:idb=>1, :epb=>2, :spb=>2, :unknown=>4},
40
+ {:idb=>2, :epb=>1, :spb=>0, :unknown=>4}]
41
+ }
42
+
43
+ end
44
+ end
45
+
@@ -0,0 +1,53 @@
1
+ # -*- coding: binary -*-
2
+ require 'spec_helper'
3
+ require 'packetfu'
4
+
5
+ module PacketFu
6
+ module PcapNG
7
+ describe IDB do
8
+ before(:each) { @idb = IDB.new }
9
+
10
+ it 'should have correct initialization values' do
11
+ expect(@idb).to be_a(IDB)
12
+ expect(@idb.endian).to eq(:little)
13
+ expect(@idb.type.to_i).to eq(PcapNG::IDB_TYPE.to_i)
14
+ expect(@idb.link_type.to_i).to eq(PcapNG::LINKTYPE_ETHERNET)
15
+ expect(@idb.snaplen.to_i).to eq(0)
16
+ expect(@idb.block_len.to_i).to eq(IDB::MIN_SIZE)
17
+ expect(@idb.block_len2).to eq(@idb.block_len)
18
+ end
19
+
20
+ it 'should decode tsresol on demand from its options' do
21
+ @idb.options.read [9, 1, 4].pack('vvC')
22
+ expect(@idb.ts_resol).to eq(1E-4)
23
+
24
+ @idb.options.read [9, 1, 0x83].pack('vvC')
25
+ expect(@idb.ts_resol(true)).to eq(2**-3)
26
+ end
27
+
28
+ context 'when reading' do
29
+ it 'should accept a String' do
30
+ str = ::File.read(::File.join(__dir__, '../..', 'test', 'sample.pcapng'))[52, 32]
31
+ expect { @idb.read(str) }.to_not raise_error
32
+ expect(@idb.type.to_i).to eq(PcapNG::IDB_TYPE.to_i)
33
+ expect(@idb.block_len.to_i).to eq(32)
34
+ expect(@idb.link_type.to_i).to eq(PcapNG::LINKTYPE_ETHERNET)
35
+ expect(@idb.snaplen.to_i).to eq(0xffff)
36
+ expect(@idb.has_options?).to be(true)
37
+ end
38
+
39
+ it 'should accept an IO' do
40
+ ::File.open(::File.join(__dir__, '../..', 'test', 'sample.pcapng')) do |f|
41
+ f.seek(52, :CUR)
42
+ @idb.read f
43
+ end
44
+ expect(@idb.type.to_i).to eq(PcapNG::IDB_TYPE.to_i)
45
+ expect(@idb.block_len.to_i).to eq(32)
46
+ expect(@idb.link_type.to_i).to eq(PcapNG::LINKTYPE_ETHERNET)
47
+ expect(@idb.snaplen.to_i).to eq(0xffff)
48
+ expect(@idb.has_options?).to be(true)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,42 @@
1
+ # -*- coding: binary -*-
2
+ require 'spec_helper'
3
+ require 'packetfu'
4
+
5
+ module PacketFu
6
+ module PcapNG
7
+ describe SHB do
8
+ before(:each) { @shb = SHB.new }
9
+
10
+ it 'should have correct initialization values' do
11
+ expect(@shb).to be_a(SHB)
12
+ expect(@shb.endian).to eq(:little)
13
+ expect(@shb.type.to_i).to eq(PcapNG::SHB_TYPE.to_i)
14
+ expect(@shb.block_len.to_i).to eq(SHB::MIN_SIZE)
15
+ expect(@shb.magic.to_s).to eq(SHB::MAGIC_LITTLE)
16
+ expect(@shb.ver_major.to_i).to eq(1)
17
+ expect(@shb.ver_minor.to_i).to eq(0)
18
+ expect(@shb.section_len.to_i).to eq(0xffffffff_ffffffff)
19
+ expect(@shb.block_len2).to eq(@shb.block_len)
20
+ expect(@shb.interfaces).to eq([])
21
+ expect(@shb.unknown_blocks).to eq([])
22
+ end
23
+
24
+ context 'when reading' do
25
+ it 'should accept a String' do
26
+ str = ::File.read(::File.join(__dir__, '../..', 'test', 'sample.pcapng'), 52)
27
+ expect { @shb.read(str) }.to_not raise_error
28
+ expect(@shb.block_len.to_i).to eq(52)
29
+ expect(@shb.has_options?).to be(true)
30
+ end
31
+
32
+ it 'should accept an IO' do
33
+ ::File.open(::File.join(__dir__, '../..', 'test', 'sample.pcapng')) do |f|
34
+ @shb.read(f)
35
+ end
36
+ expect(@shb.block_len.to_i).to eq(52)
37
+ expect(@shb.has_options?).to be(true)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ # -*- coding: binary -*-
2
+ require 'spec_helper'
3
+ require 'packetfu'
4
+
5
+ module PacketFu
6
+ module PcapNG
7
+ describe SPB do
8
+ before(:each) { @spb = SPB.new }
9
+
10
+ it 'should have correct initialization values' do
11
+ expect(@spb).to be_a(SPB)
12
+ expect(@spb.endian).to eq(:little)
13
+ expect(@spb.type.to_i).to eq(PcapNG::SPB_TYPE.to_i)
14
+ expect(@spb.orig_len.to_i).to eq(0)
15
+ expect(@spb.block_len.to_i).to eq(SPB::MIN_SIZE)
16
+ expect(@spb.block_len2).to eq(@spb.block_len)
17
+ end
18
+
19
+ context 'when reading' do
20
+ it 'should accept a String' do
21
+ str = ::File.read(::File.join(__dir__, '../..', 'test',
22
+ 'sample-spb.pcapng'))[128, 0x14c]
23
+ expect { @spb.read str }.to_not raise_error
24
+ expect(@spb.type.to_i).to eq(PcapNG::SPB_TYPE.to_i)
25
+ expect(@spb.block_len.to_i).to eq(0x14c)
26
+ expect(@spb.orig_len.to_i).to eq(0x13a)
27
+ expect(@spb.data.size).to eq(0x13a)
28
+ end
29
+
30
+ it 'should accept an IO' do
31
+ ::File.open(::File.join(__dir__, '../..', 'test', 'sample-spb.pcapng')) do |f|
32
+ f.seek(128, :CUR)
33
+ @spb.read f
34
+ end
35
+ expect(@spb.type.to_i).to eq(PcapNG::SPB_TYPE.to_i)
36
+ expect(@spb.block_len.to_i).to eq(0x14c)
37
+ expect(@spb.orig_len.to_i).to eq(0x13a)
38
+ expect(@spb.data.size).to eq(0x13a)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ # -*- coding: binary -*-
2
+ require 'spec_helper'
3
+ require 'packetfu'
4
+
5
+ module PacketFu
6
+ module PcapNG
7
+ describe UnknownBlock do
8
+ before(:each) { @ub = UnknownBlock.new }
9
+
10
+ it 'should have correct initialization values' do
11
+ expect(@ub).to be_a(UnknownBlock)
12
+ expect(@ub.endian).to eq(:little)
13
+ expect(@ub.type.to_i).to eq(0)
14
+ expect(@ub.block_len.to_i).to eq(UnknownBlock::MIN_SIZE)
15
+ expect(@ub.block_len2).to eq(@ub.block_len)
16
+ end
17
+
18
+ context 'when reading' do
19
+ it 'should accept a String' do
20
+ str = "\xff\xff\xff\xff\x0c\x00\x00\x00\x0c\x00\x00\x00"
21
+ expect { @ub.read(str) }.to_not raise_error
22
+ expect(@ub.type.to_i).to eq(0xffffffff)
23
+ expect(@ub.block_len.to_i).to eq(12)
24
+ end
25
+
26
+ it 'should accept an IO' do
27
+ ::File.open(::File.join(__dir__, '../..', 'test', 'sample.pcapng')) do |f|
28
+ @ub.read(f)
29
+ end
30
+ expect(@ub.type.to_i).to eq(0x0a0d0d0a)
31
+ expect(@ub.block_len.to_i).to eq(52)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,6 +1,5 @@
1
-
2
- $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
3
- require 'packetfu'
1
+ require 'coveralls'
2
+ Coveralls.wear!
4
3
 
5
4
  puts "rspec #{RSpec::Core::Version::STRING}"
6
5
  if RSpec::Core::Version::STRING[0] == '3'
@@ -13,32 +12,5 @@ if RSpec::Core::Version::STRING[0] == '3'
13
12
  end
14
13
  end
15
14
 
15
+ require 'packetfu/common'
16
16
 
17
- module FakePacket
18
- def layer
19
- 7
20
- end
21
- end
22
-
23
- class PacketFu::FooPacket < PacketFu::Packet
24
- extend FakePacket
25
- end
26
-
27
- class PacketFu::BarPacket < PacketFu::Packet
28
- extend FakePacket
29
- end
30
-
31
- class PacketBaz
32
- end
33
-
34
- def add_fake_packets
35
- PacketFu.add_packet_class(PacketFu::FooPacket)
36
- PacketFu.add_packet_class(PacketFu::BarPacket)
37
- end
38
-
39
- def remove_fake_packets
40
- PacketFu.remove_packet_class(PacketFu::FooPacket)
41
- PacketFu.remove_packet_class(PacketFu::BarPacket)
42
- end
43
-
44
- remove_fake_packets
@@ -1,4 +1,8 @@
1
1
  require 'spec_helper'
2
+ require 'packetfu/protos/eth'
3
+ require 'packetfu/protos/ip'
4
+ require 'packetfu/protos/tcp'
5
+ require 'packetfu/pcap'
2
6
 
3
7
  include PacketFu
4
8
 
@@ -97,4 +101,3 @@ describe TCPPacket do
97
101
  end
98
102
 
99
103
  end
100
-
@@ -1,18 +1,130 @@
1
1
  require 'spec_helper'
2
+ require 'packetfu/protos/eth'
3
+ require 'packetfu/protos/ip'
4
+ require 'packetfu/protos/ipv6'
5
+ require 'packetfu/protos/udp'
6
+ require 'packetfu/pcap'
2
7
 
3
8
  include PacketFu
4
9
 
10
+ class String
11
+ def bin
12
+ self.scan(/../).map {|x| x.to_i(16).chr}.join
13
+ end
14
+ end
5
15
 
6
16
  describe UDPPacket do
7
17
 
8
- context "new" do
18
+ context 'when read from a pcap file' do
19
+ context '(UDP over IPv4)' do
20
+ before(:all) do
21
+ @udp4_packet = PcapFile.read_packets(File.join(File.dirname(__FILE__),
22
+ "ipv4_udp.pcap")).first
23
+ end
24
+
25
+ it 'should be recognized as a UDP packet' do
26
+ expect(@udp4_packet.is_udp?).to be(true)
27
+ expect(@udp4_packet.ipv6?).to be(false)
28
+ end
29
+
30
+ it 'should have the right port numbers' do
31
+ expect(@udp4_packet.udp_src).to eq(41000)
32
+ expect(@udp4_packet.udp_dst).to eq(42000)
33
+ end
34
+
35
+ it 'should have the right length' do
36
+ expect(@udp4_packet.udp_len).to eq(24)
37
+ end
38
+
39
+ it 'should have the right checksum' do
40
+ expect(@udp4_packet.udp_sum).to eq(0xbd81)
41
+ end
42
+ end
43
+
44
+ context '(UDP over IPv6)' do
45
+ before(:all) do
46
+ @udp6_packet = PcapFile.read_packets(File.join(File.dirname(__FILE__),
47
+ "ipv6_udp.pcap")).first
48
+ end
49
+
50
+ it 'should be recognized as a UDP packet' do
51
+ expect(@udp6_packet.is_udp?).to be(true)
52
+ expect(@udp6_packet.ipv6?).to be(true)
53
+ end
54
+
55
+ it 'should have the right port numbers' do
56
+ expect(@udp6_packet.udp_src).to eq(6809)
57
+ expect(@udp6_packet.udp_dst).to eq(6810)
58
+ end
9
59
 
60
+ it 'should have the right length' do
61
+ expect(@udp6_packet.udp_len).to eq(12)
62
+ end
63
+
64
+ it 'should have the right checksum' do
65
+ expect(@udp6_packet.udp_sum).to eq(0xb9be)
66
+ end
67
+ end
68
+
69
+ context 'UDP over IPv4 v2' do
70
+ it "should have all the UDP attributes" do
71
+ udp_packet = PcapFile.new.file_to_array(:f => 'test/sample.pcap')[0]
72
+ udp_pkt = Packet.parse(udp_packet)
73
+ expect(udp_pkt).to be_kind_of(UDPPacket)
74
+ expect(udp_pkt.udp_sum.to_i).to eql(0x8bf8)
75
+ end
76
+ end
77
+
78
+ context 'UDP over IPv4 alter' do
79
+ it "should read and allow us to alert the payload" do
80
+ udp_packet = PcapFile.new.file_to_array(:f => 'test/sample.pcap')[0]
81
+ udp_pkt = Packet.parse(udp_packet)
82
+ expect(udp_pkt).to be_kind_of(UDPPacket)
83
+
84
+ udp_pkt.payload = udp_pkt.payload.gsub(/metasploit/,"MeatPistol")
85
+ udp_pkt.recalc
86
+ expect(udp_pkt.udp_sum).to eql(0x8341)
87
+ end
88
+ end
89
+ end
90
+
91
+ context "when initializing UDPHeader from scratch" do
92
+ before(:each) { @udp_header = UDPHeader.new }
93
+ it 'should have the right instance variables' do
94
+ expect(@udp_header).to be_kind_of(UDPHeader)
95
+ expect(@udp_header.to_s.size).to eql(8)
96
+ expect(@udp_header.to_s).to eql("\x00\x00\x00\x00\x00\b\x00\x00")
97
+ expect(@udp_header.udp_src).to eq(0)
98
+ expect(@udp_header.udp_dst).to eq(0)
99
+ expect(@udp_header.udp_len).to eq(8)
100
+ expect(@udp_header.udp_sum).to eq(0)
101
+ end
102
+
103
+ it 'should allow setting of port numbers' do
104
+ @udp_header.udp_src = 1024
105
+ @udp_header.udp_dst = 1025
106
+ expect(@udp_header.udp_src).to eq(1024)
107
+ expect(@udp_header.udp_dst).to eq(1025)
108
+ end
109
+ end
110
+
111
+ context "when initializing UDPPacket from scratch" do
10
112
  it "should create UDP on IPv4 packets by default" do
11
113
  udp = UDPPacket.new
12
114
  expect(udp.ip_header).to be_a(IPHeader)
13
115
  expect(udp.ipv6_header).to be_nil
14
116
  end
15
117
 
118
+ it "should allow re-reading" do
119
+ udp_packet = PacketFu::UDPPacket.new
120
+ udp_packet2 = Packet.parse(udp_packet.to_s)
121
+
122
+ expect(udp_packet).to be_kind_of(UDPPacket)
123
+ expect(udp_packet2).to be_kind_of(UDPPacket)
124
+ expect(udp_packet.is_udp?).to be(true)
125
+ expect(udp_packet2.is_udp?).to be(true)
126
+ end
127
+
16
128
  it "should create UDP on IPv6 packets" do
17
129
  udp = UDPPacket.new(:on_ipv6 => true)
18
130
  expect(udp.ip_header).to be_nil
@@ -28,5 +140,41 @@ describe UDPPacket do
28
140
  expect(udp.udp_len).to eq(24)
29
141
  end
30
142
 
143
+ it 'should support peek functionnality (IPv4 case)' do
144
+ udp = UDPPacket.new
145
+ udp.ip_saddr = '192.168.1.1'
146
+ udp.ip_daddr = '192.168.1.254'
147
+ udp.udp_src = 32756
148
+ udp.udp_dst = 80
149
+ udp.payload = 'abcdefghijklmnopqrstuvwxyz'
150
+ udp.recalc
151
+ expect(udp.peek).to match(/U 68\s+192.168.1.1:32756\s+->\s+192.168.1.254:80/)
152
+ end
153
+
154
+ it 'should support peek functionnality (IPv6 case)' do
155
+ udp = UDPPacket.new(:on_ipv6 => true)
156
+ udp.ipv6_saddr = '2000::1'
157
+ udp.ipv6_daddr = '2001::1'
158
+ udp.udp_src = 32756
159
+ udp.udp_dst = 80
160
+ udp.payload = 'abcdefghijklmnopqrstuvwxyz'
161
+ udp.recalc
162
+ expect(udp.peek).to match(/6U 88\s+2000::1:32756\s+->\s+2001::1:80/)
163
+ end
31
164
  end
165
+
166
+ context "when reading UDPPacket from string" do
167
+ it "should create UDPPacket and strip extra bytes" do
168
+ str = "01005e7ffffa100ba9eb63400800450000a12d7c0000011159b446a5fb7ceffffffacdf3076c008d516e4d2d534541524348202a20485454502f312e310d0a486f73743a3233392e3235352e3235352e3235303a313930300d0a53543a75726e3a736368656d61732d75706e702d6f72673a6465766963653a496e7465726e6574476174657761794465766963653a310d0a4d616e3a22737364703a646973636f766572220d0a4d583a330d0a0d0a".bin
169
+ str << "0102".bin # Tacking on a couple extra bytes tht we'll strip off.
170
+ not_stripped = UDPPacket.new
171
+ not_stripped.read(str)
172
+ expect(not_stripped.udp_header.body.length).to eql(135)
173
+
174
+ stripped = UDPPacket.new
175
+ stripped.read(str, :strip => true)
176
+ expect(stripped.udp_header.body.length).to eql(133)
177
+ end
178
+ end
179
+
32
180
  end