Getting Started

Install PacketGen

Use rubygem:

$ gem install packetgen

or use bundler. Put this line in you Gemfile:

gem packetgen

Starting PacketGen

The easiest way to start PacketGen is using interactive console. To send packets, root privileges are needed. In a terminal, do:

$ sudo pgconsole

pgconsole uses IRB

pg>

Interactive use

First step

Build a packet and play with it:

Here, gen (a shortcut to PacketGen.gen) generate a PacketGen::Packet object with a IP header.

Then, IP header is accessed and/or modified through #ip method, which returns a PacketGen::Header::IP object (mapping of a IP header).

Put layers together

To add layers to a packet, PacketGen::Packet#add method should be used. Adding a header on a packet may update fields from underlying packet. Here, adding a TCP header to our IP packet will update IP protocol field to 6 (TCP protocol number):

#add may be chained:

You may also add whatever you want as packet body:

Generate binary data and read packets

From a packet, you may generate binary data which will be sent on network. You may also parse binary data to create packets:

Read and write files

You may read packets from PCAP or PCAP-NG files:

You also may write packets to a file (only PCAP-NG is supported):

Or write a single packet to a file:

Send packets

Sending a packet is as easy as:

The packet will be send on your first network interface. You also may choose interface on which sends packet:

In general, packets are erroneous because some fields are not properly set. To easily fix that, use PacketGen::Packet#calc, which will calculate all calculatable fields (for now: length and checksum ones):

Of course, this is to you to put correct values for addresses or ports, by example.

Capture packets

You may capture packets to post-process them:

This command will capture at most 50 packets from eth0, during at most 10 seconds.

You also may process them on the fly:

Captured packets may be filtered using a tcpdump filter:

Go further

Read others pages from this wiki.

API documentation also gives all methods for PacketGen::Packet and for all header classes.

Last updated