Dpkt tutorial #1: ICMP echo
In this dpkt tutorial, I will demonstrate how to construct and send a simple ICMP ECHO packet.
Dpkt is a sweet framework for creating and parsing packets. while dpkt doesn' t have much documentation, once you get the hang of using one module, the rest fall into place fairly easily. i'll be doing a number of dpkt tutorials with simple tasks in hopes of providing some "documentation by example ". if you have any tasks you 'd like to see done in dpkt, drop me a line.
In this tutorial, We'll be creating an icmp echo packet, aka a ping. it's good to start off with a simple example to see how to construct a packet SS multiple layers using the various modules of dpkt. let's get started!
Taking a look at dpkt/ICMP. py, we see the following classes for ICMP and it's echo payload:
class ICMP(dpkt.Packet): __hdr__ = ( ('type', 'B', 8), ('code', 'B', 0), ('sum', 'H', 0) ) class Echo(dpkt.Packet): __hdr__ = (('id', 'H', 0), ('seq', 'H', 0))
Since we'll be sending the ICMP echo out via the standard socket interface, we don't have to concerns ourselves with lower layers such as IP or the link-layer (we'll save that for another tutorial ).
When constructing packets with dpkt, I usually take an top-to-bottom approach, starting with the highest level structure and working our way down the layers. for this ICMP request, we wocould start with contructing the echo payload and then the ICMP base payload (and then the IP payload, the link-layer payload, and so on if we were constructing lower-level payloads ). to create the echo payload, we simple create an instance of the ECHO class:
echo = dpkt.icmp.ICMP.Echo()
The attributes for the echo payload (listed in _ HDR _) are ID and SEQ, both of which 16-bit integers ('H') and default to 0. we can easily change these attributes of the ECHO payload and Will randomize them for kicks:
echo.id = random.randint(0, 0xffff)echo.seq = random.randint(0, 0xffff)
ICMP echo requests often include a data payload that is Echo 'ed back by the specified er. we will include a payload by assigning a string to the echo data attribute. the data attribute is special in dpkt-land and we will see its use later when linking together the echo payload with the ICMP payload. for now, we assign a dummy payload:
echo.data = 'hello world'
We can now pretty print, hexdump, or print the raw bytes of the ECHO payload:
>>> print `echo`Echo(id=24528, seq=11482, data='hello world')
>>> print binascii.hexlify(str(echo))5fd02cda68656c6c6f20776f726c64
>>> print str(echo)_?,?hello world
Next, we create the ICMP payload and assign its attributes using the constants found in dpkt/ICMP. py:
icmp = dpkt.icmp.ICMP()icmp.type = dpkt.icmp.ICMP_ECHO
To link our echo payload to our ICMP payload, we again use the data attribute. since the echo payload is a "sub-payload" of the ICMP packet, we assign it to the ICMP's data attribute:
icmp.data = echo
Again, we can now pretty print, hexdump, or print the raw bytes of the entire ICMP/ECHO packet and see how the echo payload is nested within the ICMP payload:
>>> print `icmp`ICMP(data=Echo(id=24528, seq=11482, data='hello world'))
>>> print binascii.hexlify(str(icmp))0800d9865fd02cda68656c6c6f20776f726c64
>>> print str(icmp)??_?,?hello world
Now that we have the full binary payload of our ICMP packet (STR (ICMP), we can send it out via a standard ICMP socket:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)s.connect(('74.125.67.100', 1))s.send(str(icmp))
Success! This completes our tutorial showing the construction of an ICMP echo request with dpkt.
The full Python script for this tutorial follows:
#!/usr/bin/env pythonimport dpktimport socket, randomecho = dpkt.icmp.ICMP.Echo()echo.id = random.randint(0, 0xffff)echo.seq = random.randint(0, 0xffff)echo.data = 'hello world'icmp = dpkt.icmp.ICMP()icmp.type = dpkt.icmp.ICMP_ECHOicmp.data = echos = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)s.connect(('74.125.67.100', 1))sent = s.send(str(icmp))print 'sent %d bytes' % sent