This is probably old news to anybody who's used to Ruby on Macs, but I'm not.  I like using MacPorts when I can, so that's where my Ruby runs from.

I went looking for pcap modules and found a bunch, but the most promising seemed to be packetfu.  It came with a caveat: "PacketFu is reported to work on OS X, assuming you can get pcaprub installed correctly."

So the first step was to get pcaprub going.  You can get it at rubyforge, the trick is it has a README, a single C source file, and a small ruby script. It turns out the secret is to do 'ruby extconf.rb', which will create a Makefile for you, and you can then 'sudo make install'.

The author of packetfu recommends sticking with sources, available here. Check them out like this: 'svn checkout http://packetfu.googlecode.com/svn/trunk/ packetfu-read-only' - I like doing this in \~/src, but your tastes may differ.  Use the pcaprub_linux directory, and again, 'ruby extconf.rb' followed by a make / make install.  The test script bombs out - by default it wants to use the fw0 device.  Nevertheless, I pressed on and did as suggested: 'cd .. && sudo ruby setup.rb' which finished with no failures.

Note that following these steps will pollute your MacPorts ruby install with files about which it knows nothing - I generally like to avoid this, but it seems like scripting languages insist on having their own packaging systems that make it not quite impossible to work with other such systems.

Then I wrote a little test script that should just count packets:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/opt/local/bin/ruby

require 'packetfu'
filename = ARGV[0] || exit
count = 0
incap = PacketFu::Read.f2a(:file => filename)

incap.each do |pkt|
 p = PacketFu::Packet.parse(pkt)
 count += 1
end

puts "#{count} packets"

I would not recommend doing as I did and turning it loose on a 711MB pcap trace - top showed the process using 1.9GB of memory before I managed to get a responsive terminal to kill it.  It fared slightly better on a 12MB trace (a subset of the larger ones) and correctly counted 85411 packets, but still hit 265MB:

real    4m0.359s
user    3m16.012s
sys    0m1.891s

I'm a fairly-incompetent Ruby programmer, but the above seems fairly straightforward - I'm not sure I'd want to turn this parser loose on anything of any real size.  While 711MB is a pretty big trace to be parsing, 12 is pretty small.  To be fair, I had other processes running at the time, including a Windows 7 VM that was patching (unbeknownst to me) but it's a 4GB dual core machine.  I'd been hoping to more easily pull various bits of data out of the larger file, but it looks like I'm stuck with tcpdump and tshark for now.

ETA: I had been going to post again about xtractr, but it's not a 100% Mac/Ruby solution. It provides an interface so that I could use Mac Ruby installs to talk to an xtractr instance, but so far as I can tell, there's no 100% Mac solution. So I'm running it on an Ubuntu box, but I won't bother detailing how I got it going - it was pretty straightforward, although it required some work I'd already done to make gems sane on an 8.04 system.


Published

Category

Technology

Tags

Contact