
PacketGen
PacketGen provides simple ways to generate, send and capture network packets.
Installation
PacketGen depends on PcapRub, which needs pcap development files to install. On Debian, you have to do:
sudo apt install libpcap-dev
Installation using RubyGems is then easy:
gem install packetgen
Or add it to a Gemfile:
gem 'packetgen'
Usage
Easily create packets
PacketGen.gen('IP')
PacketGen.gen('TCP')
PacketGen.gen('IP').add('TCP')
PacketGen.gen('Eth')
PacketGen.gen('IP').add('IP')
PacketGen.gen('IP', src: '192.168.1.1', dst: '192.168.1.2')
PacketGen.gen('IP').to_s
Send packets on wire
PacketGen.gen('Eth', src: '00:00:00:00:00:01', dst: '00:00:00:00:00:02').to_w
PacketGen.gen('IP', src: '192.168.1.1', dst: '192.168.1.2').to_w
PacketGen.gen('Eth', src: '00:00:00:00:00:01', dst: '00:00:00:00:00:02').add('IP').to_w('eth1')
PacketGen.gen('RadioTap').
add('Dot11::Management', mac1: client, mac2: bssid, mac3: bssid).
add('Dot11::DeAuth', reason: 7).
to_w('wlan0')
Parse packets from binary data
packet = PacketGen.parse(binary_data)
Capture packets from wire
PacketGen.capture do |packet|
do_stuffs_with_packet
end
packets = PacketGen.capture(iface: 'eth0', max: 10)
packets = PacketGen.capture(iface: 'eth0', filter: 'ip src 1.1.1.2', max: 1)
Easily manipulate packets
pkt = PacketGen.gen('IP').add('TCP')
pkt.ip.src = '192.168.1.1'
pkt.ip(src: '192.168.1.1', ttl: 4)
pkt.tcp.dport = 80
pkt = PacketGen.gen('IP').add('IP')
pkt.ip.src = '192.168.1.1'
pkt.ip(2).src = '10.0.0.1'
pkt = PacketGen.gen('IP').add('TCP')
pkt.is? 'TCP'
pkt.is? 'IP'
pkt.is? 'UDP'
pkt2 = PacketGen.gen('IP')
pkt2.encapsulate pkt
pkt2.decapsulate(pkt2.ip)
Read/write PcapNG files
packets = PacketGen.read('file.pcapng')
packets.first.udp.sport = 65535
pkt.write('one_packet.pcapng')
PacketGen.write('more_packets.pcapng', packets)
PacketGen permits adding your own header classes.
First, define the new header class. For example:
module MyModule
class MyHeader < PacketGen::Header::Base
define_attr :field1, BinStruct::Int32
define_attr :field2, BinStruct::Int32
end
end
Then, class must be declared to PacketGen:
PacketGen::Header.add_class MyModule::MyHeader
Finally, bindings must be declared:
PacketGen::Header::IP.bind_header MyModule::MyHeader, protocol: 254
And use it:
pkt = Packet.gen('IP').add('MyHeader', field1: 0x12345678, field2: 0x87654321)
pkt.to_w
Interactive console
PacketGen provides an interactive console: pgconsole
.
In this console, context includes PacketGen module to give direct access to PacketGen
classes. A special config
object gives local network configuration:
$ pgconsole
pg(main)> config
=> #<PacketGen::Config:0x00559f27d2afe8
@hwaddr="75:74:73:72:71:70",
@iface="eth0",
@ipaddr="192.168.0.2">
pg(main)> packets = capture(max: 5)
pg(main)> exit
If pry
gem is installed, it is used as backend for pgconsole
, else IRB is used.
Plugins
PacketGen provides a plugin system (see wiki).
Available plugins (available as gem) are:
See also
Wiki: https://github.com/lemontree55/packetgen/wiki
API documentation: http://www.rubydoc.info/gems/packetgen
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/lemontree55/packetgen.
License
MIT License (see LICENSE)
Other sources
All original code maintains its copyright from its original authors and licensing.