packetfu 1.1.11 → 1.1.12.pre

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 (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,32 @@
1
+ # Usage:
2
+ # rvmsudo ruby examples/pcap2pcapng.rb test.pcap test.pcapng
3
+
4
+ # Path setting slight of hand:
5
+ $: << File.expand_path("../../lib", __FILE__)
6
+
7
+ require 'packetfu'
8
+
9
+ pcap_filename = ARGV[0].chomp
10
+ pcapng_filename = ARGV[1].chomp
11
+
12
+ unless File.exists?(pcap_filename)
13
+ puts "PCAP input file #{pcap_filename} could not be found"
14
+ end
15
+
16
+ if File.exists?(pcapng_filename)
17
+ puts "PCAP-NG output file #{pcap_filename} already exists"
18
+ puts "Do you wish to overwrite the file? (Y/N, Default = N)"
19
+ STDOUT.flush
20
+ response = $stdin.gets.chomp
21
+ unless response == "Y"
22
+ puts "Aborting..."
23
+ exit 0
24
+ end
25
+ end
26
+
27
+ puts "Reading PCAP to packet array from #{File.expand_path(pcap_filename)}"
28
+ packet_array = PacketFu::PcapFile.file_to_array(pcap_filename)
29
+
30
+ puts "Writing packet array to PCAP-NG at #{File.expand_path(pcapng_filename)}"
31
+ pcapng_file = PacketFu::PcapNG::File.new()
32
+ pcapng_file.array_to_file(:array => packet_array, :file => pcapng_filename)
@@ -1,11 +1,16 @@
1
1
  #!/usr/bin/env ruby
2
2
  # -*- coding: binary -*-
3
- require './examples'
3
+
4
+ # Usage:
5
+ # rvmsudo ruby examples/simple-sniffer.rb
6
+
7
+ # Path setting slight of hand:
8
+ $: << File.expand_path("../../lib", __FILE__)
4
9
  require 'packetfu'
5
10
 
6
11
  puts "Simple sniffer for PacketFu #{PacketFu.version}"
7
12
  include PacketFu
8
- iface = ARGV[0] || "eth0"
13
+ iface = ARGV[0] || PacketFu::Utils.default_int
9
14
 
10
15
  def sniff(iface)
11
16
  cap = Capture.new(:iface => iface, :start => true)
@@ -21,7 +26,7 @@ end
21
26
 
22
27
  sniff(iface)
23
28
 
24
- =begin
29
+ =begin
25
30
  Results look like this:
26
31
  145.58.33.95 -> 192.168.11.70 1514 TCP
27
32
  212.233.158.76 -> 192.168.11.70 110 UDP
@@ -38,4 +43,4 @@ Results look like this:
38
43
  8.8.8.8 -> 192.168.11.70 128 UDP
39
44
  8.8.8.8 -> 192.168.11.70 187 UDP
40
45
  24.45.247.232 -> 192.168.11.70 70 TCP
41
- =end
46
+ =end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # -*- coding: binary -*-
3
3
 
4
- # Simple-stats.rb takes a pcap file, and gives some simple
4
+ # Simple-stats.rb takes a pcap file, and gives some simple
5
5
  # stastics on the protocols found. It's mainly used to
6
6
  # demonstrate a method to parse pcap files.
7
7
  #
@@ -10,7 +10,11 @@
10
10
  # See new-simple-stats.rb for an example of the streaming
11
11
  # parsing method.
12
12
 
13
- require './examples' # For path setting slight-of-hand
13
+ # Usage:
14
+ # ruby examples/simple-stats.rb test/sample.pcap
15
+
16
+ # Path setting slight of hand:
17
+ $: << File.expand_path("../../lib", __FILE__)
14
18
  require 'packetfu'
15
19
 
16
20
  # Takes a file name, parses the packets, and records the packet
@@ -23,7 +27,7 @@ def count_packet_types(file)
23
27
  pcapfile.read(file)
24
28
  pcapfile.each do |p|
25
29
  # Now it's a PacketFu packet struct.
26
- pkt = PacketFu::Packet.parse(p.data)
30
+ pkt = PacketFu::Packet.parse(p.data)
27
31
  kind = pkt.class.to_s.split("::").last
28
32
  if stats[kind]
29
33
  stats[kind] += 1
@@ -44,8 +48,3 @@ if File.readable?(infile = (ARGV[0] || 'in.pcap'))
44
48
  else
45
49
  raise RuntimeError, "Need an infile, like so: #{$0} in.pcap"
46
50
  end
47
-
48
-
49
-
50
-
51
-
@@ -8,6 +8,8 @@ raise RuntimeError, "Need a target" unless target
8
8
  action = ARGV[1]
9
9
  raise RuntimeError, "Need an action. Try file or your interface." unless action
10
10
 
11
+ # Path setting slight of hand:
12
+ $: << File.expand_path("../../lib", __FILE__)
11
13
  require 'packetfu'
12
14
  include PacketFu
13
15
 
@@ -30,5 +32,3 @@ if action == 'file'.downcase
30
32
  else
31
33
  puts kill_packet.to_w(action.downcase)
32
34
  end
33
-
34
-
@@ -1,15 +1,25 @@
1
- # Uniqpcap.rb takes a pcap file, strips out duplicate packets, and
1
+ # Uniqpcap.rb takes a pcap file, strips out duplicate packets, and
2
2
  # writes them to a file.
3
3
  #
4
- # The duplicate pcap problem is common when I'm capturing
4
+ # The duplicate pcap problem is common when I'm capturing
5
5
  # traffic to/from a VMWare image, for some reason.
6
6
  #
7
- # Currently, the timestamp information is lost due to PcapRub's
8
- # file read. For me, this isn't a big deal. Future versions
7
+ # Currently, the timestamp information is lost due to PcapRub's
8
+ # file read. For me, this isn't a big deal. Future versions
9
9
  # will deal with timestamps correctly.
10
- require './examples' # For path setting slight-of-hand
10
+
11
+ # Usage:
12
+ # ruby examples/uniqcap.rb test/sample.pcap
13
+
14
+ # Path setting slight of hand:
15
+ $: << File.expand_path("../../lib", __FILE__)
11
16
  require 'packetfu'
12
17
 
13
- in_array = PacketFu::Read.f2a(:file => ARGV[0])
14
- puts PacketFu::Write.a2f(:file => "uniq-" + ARGV[0], :arr => in_array.uniq).inspect
18
+ pcap_file = ARGV[0].chomp
19
+
20
+ in_array = PacketFu::Read.f2a(:file => pcap_file)
21
+
22
+ puts "Original Packets: #{in_array.size}"
23
+ puts "Uniq'd Packets: #{in_array.uniq.size}"
15
24
 
25
+ puts PacketFu::Write.a2f(:file => pcap_file + ".uniq", :arr => in_array.uniq).inspect
@@ -1,176 +1,11 @@
1
1
  # -*- coding: binary -*-
2
-
3
- # :title: PacketFu Documentation
4
- # :main: README
5
-
6
- cwd = File.expand_path(File.dirname(__FILE__))
7
-
8
- $: << cwd
9
-
10
- require File.join(cwd,"packetfu","structfu")
11
- require "ipaddr"
12
- require 'rubygems' if RUBY_VERSION =~ /^1\.[0-8]/
13
-
14
- module PacketFu
15
-
16
- # Picks up all the protocols defined in the protos subdirectory
17
- def self.require_protos(cwd)
18
- protos_dir = File.join(cwd, "packetfu", "protos")
19
- Dir.new(protos_dir).each do |fname|
20
- next unless fname[/\.rb$/]
21
- begin
22
- require File.join(protos_dir,fname)
23
- rescue
24
- warn "Warning: Could not load `#{fname}'. Skipping."
25
- end
26
- end
27
- end
28
-
29
- # Deal with Ruby's encoding by ignoring it.
30
- def self.force_binary(str)
31
- str.force_encoding Encoding::BINARY if str.respond_to? :force_encoding
32
- end
33
-
34
- # Sets the expected byte order for a pcap file. See PacketFu::Read.set_byte_order
35
- @byte_order = :little
36
-
37
- # Checks if pcaprub is loaded correctly.
38
- @pcaprub_loaded = false
39
-
40
- # PacketFu works best with Pcaprub version 0.8-dev (at least)
41
- # The current (Aug 01, 2010) pcaprub gem is 0.9, so should be fine.
42
- def self.pcaprub_platform_require
43
- begin
44
- require 'pcaprub'
45
- rescue LoadError
46
- return false
47
- end
48
- @pcaprub_loaded = true
49
- end
50
-
51
- pcaprub_platform_require
52
-
53
- if @pcaprub_loaded
54
- pcaprub_regex = /[0-9]\.([8-9]|[1-7][0-9])(-dev)?/ # Regex for 0.8 and beyond.
55
- if Pcap.version !~ pcaprub_regex
56
- @pcaprub_loaded = false # Don't bother with broken versions
57
- raise LoadError, "PcapRub not at a minimum version of 0.8-dev"
58
- end
59
- require "packetfu/capture"
60
- require "packetfu/inject"
61
- end
62
-
63
- # Returns the status of pcaprub
64
- def self.pcaprub_loaded?
65
- @pcaprub_loaded
66
- end
67
-
68
- # Returns an array of classes defined in PacketFu
69
- def self.classes
70
- constants.map { |const| const_get(const) if const_get(const).kind_of? Class}.compact
71
- end
72
-
73
- # Adds the class to PacketFu's list of packet classes -- used in packet parsing.
74
- def self.add_packet_class(klass)
75
- raise "Need a class" unless klass.kind_of? Class
76
- if klass.name !~ /[A-Za-z0-9]Packet/
77
- raise "Packet classes should be named 'ProtoPacket'"
78
- end
79
- @packet_classes ||= []
80
- @packet_classes << klass
81
- self.clear_packet_groups
82
- @packet_classes.sort_by! { |x| x.name }
83
- end
84
-
85
- # Presumably, there may be a time where you'd like to remove a packet class.
86
- def self.remove_packet_class(klass)
87
- raise "Need a class" unless klass.kind_of? Class
88
- @packet_classes ||= []
89
- @packet_classes.delete klass
90
- self.clear_packet_groups
91
- @packet_classes
92
- end
93
-
94
- # Returns an array of packet classes
95
- def self.packet_classes
96
- @packet_classes || []
97
- end
98
-
99
- # Returns an array of packet types by packet prefix.
100
- def self.packet_prefixes
101
- return [] if @packet_classes.nil?
102
- self.reset_packet_groups unless @packet_class_prefixes
103
- @packet_class_prefixes
104
- end
105
-
106
- def self.packet_classes_by_layer
107
- return [] if @packet_classes.nil?
108
- self.reset_packet_groups unless @packet_classes_by_layer
109
- @packet_classes_by_layer
110
- end
111
-
112
- def self.packet_classes_by_layer_without_application
113
- return [] if @packet_classes.nil?
114
- self.reset_packet_groups unless @packet_classes_by_layer_without_application
115
- @packet_classes_by_layer_without_application
116
- end
117
-
118
- def self.clear_packet_groups
119
- @packet_class_prefixes = nil
120
- @packet_classes_by_layer = nil
121
- @packet_classes_by_layer_without_application = nil
122
- end
123
-
124
- def self.reset_packet_groups
125
- @packet_class_prefixes = @packet_classes.map {|p| p.to_s.split("::").last.to_s.downcase.gsub(/packet$/,"")}
126
- @packet_classes_by_layer = @packet_classes.sort_by { |pclass| pclass.layer }.reverse
127
- @packet_classes_by_layer_without_application = @packet_classes_by_layer.reject { |pclass| pclass.layer_symbol == :application }
128
- end
129
-
130
- # The current inspect style. One of :hex, :dissect, or :default
131
- # Note that :default means Ruby's default, which is usually
132
- # far too long to be useful.
133
- def self.inspect_style
134
- @inspect_style ||= :dissect
135
- end
136
-
137
- # Setter for PacketFu's @inspect_style
138
- def self.inspect_style=(arg)
139
- @inspect_style = case arg
140
- when :hex, :pretty
141
- :hex
142
- when :dissect, :verbose
143
- :dissect
144
- when :default, :ugly
145
- :default
146
- else
147
- :dissect
148
- end
149
- end
150
-
151
- # Switches inspect styles in a round-robin fashion between
152
- # :dissect, :default, and :hex
153
- def toggle_inspect
154
- case @inspect_style
155
- when :hex, :pretty
156
- @inspect_style = :dissect
157
- when :dissect, :verbose
158
- @inspect_style = :default
159
- when :default, :ugly
160
- @inspect_style = :hex
161
- else
162
- @inspect_style = :dissect
163
- end
164
- end
165
-
166
-
167
- end
168
-
169
- require File.join(cwd,"packetfu","version")
170
- require File.join(cwd,"packetfu","pcap")
171
- require File.join(cwd,"packetfu","packet")
172
- PacketFu.require_protos(cwd)
173
- require File.join(cwd,"packetfu","utils")
174
- require File.join(cwd,"packetfu","config")
175
-
176
- # vim: nowrap sw=2 sts=0 ts=2 ff=unix ft=ruby
2
+ require 'ipaddr'
3
+ require 'packetfu/common'
4
+ require 'packetfu/structfu'
5
+ require 'packetfu/version'
6
+ require 'packetfu/pcap'
7
+ require 'packetfu/packet'
8
+ require 'packetfu/protos'
9
+ require 'packetfu/utils'
10
+ require 'packetfu/config'
11
+ require 'packetfu/pcapng'
@@ -4,7 +4,7 @@ module PacketFu
4
4
  # The Capture class is used to construct PcapRub objects in order to collect
5
5
  # packets from an interface.
6
6
  #
7
- # This class requires PcapRub. In addition, you will need root (or root-like) privileges
7
+ # This class requires PcapRub. In addition, you will need root (or root-like) privileges
8
8
  # in order to capture from the interface.
9
9
  #
10
10
  # Note, on some wireless cards, setting :promisc => true will disable capturing.
@@ -83,7 +83,7 @@ module PacketFu
83
83
  # clear() clears the @stream and @array variables, essentially starting the
84
84
  # capture session over. Valid arguments are:
85
85
  #
86
- # :array
86
+ # :array
87
87
  # If true, the @array is cleared.
88
88
  # :stream
89
89
  # If true, the @stream is cleared.
@@ -0,0 +1,142 @@
1
+ require 'packetfu/structfu'
2
+ require 'packetfu/packet'
3
+
4
+ module PacketFu
5
+
6
+ # Deal with Ruby's encoding by ignoring it.
7
+ def self.force_binary(str)
8
+ str.force_encoding Encoding::BINARY if str.respond_to? :force_encoding
9
+ end
10
+
11
+ # Sets the expected byte order for a pcap file. See PacketFu::Read.set_byte_order
12
+ @byte_order = :little
13
+
14
+ # Checks if pcaprub is loaded correctly.
15
+ @pcaprub_loaded = false
16
+
17
+ # PacketFu works best with Pcaprub version 0.8-dev (at least)
18
+ # The current (Aug 01, 2010) pcaprub gem is 0.9, so should be fine.
19
+ def self.pcaprub_platform_require
20
+ begin
21
+ require 'pcaprub'
22
+ rescue LoadError
23
+ return false
24
+ end
25
+ @pcaprub_loaded = true
26
+ end
27
+
28
+ pcaprub_platform_require
29
+
30
+ if @pcaprub_loaded
31
+ pcaprub_regex = /[0-9]\.([8-9]|[1-7][0-9])(-dev)?/ # Regex for 0.8 and beyond.
32
+ if Pcap.version !~ pcaprub_regex
33
+ @pcaprub_loaded = false # Don't bother with broken versions
34
+ raise LoadError, "PcapRub not at a minimum version of 0.8-dev"
35
+ end
36
+ require "packetfu/capture"
37
+ require "packetfu/inject"
38
+ end
39
+
40
+ # Returns the status of pcaprub
41
+ def self.pcaprub_loaded?
42
+ @pcaprub_loaded
43
+ end
44
+
45
+ # Returns an array of classes defined in PacketFu
46
+ def self.classes
47
+ constants.map { |const| const_get(const) if const_get(const).kind_of? Class}.compact
48
+ end
49
+
50
+ # Adds the class to PacketFu's list of packet classes -- used in packet parsing.
51
+ def self.add_packet_class(klass)
52
+ raise "Need a class" unless klass.kind_of? Class
53
+ if klass.name !~ /[A-Za-z0-9]Packet/
54
+ raise "Packet classes should be named 'ProtoPacket'"
55
+ end
56
+ @packet_classes ||= []
57
+ @packet_classes << klass
58
+ self.clear_packet_groups
59
+ @packet_classes.sort_by! { |x| x.name }
60
+ end
61
+
62
+ # Presumably, there may be a time where you'd like to remove a packet class.
63
+ def self.remove_packet_class(klass)
64
+ raise "Need a class" unless klass.kind_of? Class
65
+ @packet_classes ||= []
66
+ @packet_classes.delete klass
67
+ self.clear_packet_groups
68
+ @packet_classes
69
+ end
70
+
71
+ # Returns an array of packet classes
72
+ def self.packet_classes
73
+ @packet_classes || []
74
+ end
75
+
76
+ # Returns an array of packet types by packet prefix.
77
+ def self.packet_prefixes
78
+ return [] if @packet_classes.nil?
79
+ self.reset_packet_groups unless @packet_class_prefixes
80
+ @packet_class_prefixes
81
+ end
82
+
83
+ def self.packet_classes_by_layer
84
+ return [] if @packet_classes.nil?
85
+ self.reset_packet_groups unless @packet_classes_by_layer
86
+ @packet_classes_by_layer
87
+ end
88
+
89
+ def self.packet_classes_by_layer_without_application
90
+ return [] if @packet_classes.nil?
91
+ self.reset_packet_groups unless @packet_classes_by_layer_without_application
92
+ @packet_classes_by_layer_without_application
93
+ end
94
+
95
+ def self.clear_packet_groups
96
+ @packet_class_prefixes = nil
97
+ @packet_classes_by_layer = nil
98
+ @packet_classes_by_layer_without_application = nil
99
+ end
100
+
101
+ def self.reset_packet_groups
102
+ @packet_class_prefixes = @packet_classes.map {|p| p.to_s.split("::").last.to_s.downcase.gsub(/packet$/,"")}
103
+ @packet_classes_by_layer = @packet_classes.sort_by { |pclass| pclass.layer }.reverse
104
+ @packet_classes_by_layer_without_application = @packet_classes_by_layer.reject { |pclass| pclass.layer_symbol == :application }
105
+ end
106
+
107
+ # The current inspect style. One of :hex, :dissect, or :default
108
+ # Note that :default means Ruby's default, which is usually
109
+ # far too long to be useful.
110
+ def self.inspect_style
111
+ @inspect_style ||= :dissect
112
+ end
113
+
114
+ # Setter for PacketFu's @inspect_style
115
+ def self.inspect_style=(arg)
116
+ @inspect_style = case arg
117
+ when :hex, :pretty
118
+ :hex
119
+ when :dissect, :verbose
120
+ :dissect
121
+ when :default, :ugly
122
+ :default
123
+ else
124
+ :dissect
125
+ end
126
+ end
127
+
128
+ # Switches inspect styles in a round-robin fashion between
129
+ # :dissect, :default, and :hex
130
+ def toggle_inspect
131
+ case @inspect_style
132
+ when :hex, :pretty
133
+ @inspect_style = :dissect
134
+ when :dissect, :verbose
135
+ @inspect_style = :default
136
+ when :default, :ugly
137
+ @inspect_style = :hex
138
+ else
139
+ @inspect_style = :dissect
140
+ end
141
+ end
142
+ end