Lets take as an example the TCP layer.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+I code a little program to create the *.cpp and .h files of a new layer
from a text file to automate the layer creation on libcrafter. What you should do
is:
1) Clone the development version of libcrafter and compile this utility (
protogen):
git clone https://code.google.com/p/libcrafter/
cd libcrafter/libcrafter/crafter/ProtoSource/make
2) On that directory you will see a lot of *.src files. Those files
are *txt* files used to generate the *.cpp and .h files with the
protogen program you just compile. For example, for the TCP layer the text file looks like:
name TCP
protoid 0x06
ShortField SrcPort 0 0 0
ShortField DstPort 0 2 80
WordField SeqNumber 1 0 0
WordField AckNumber 2 0 0
BitsField DataOffset 3 0 4 0
BitsField Reserved 3 4 3 0
TCPFlags Flags 3 7 0
ShortField WindowsSize 3 2 5840
XShortField CheckSum 4 0 0
ShortField UrgPointer 4 2 0
The name of this layer is TCP and the protoid is 0x06. The protoid is the number that should be on the Protocol field on
the IP header when this layer is on top of it. With that number, libcrafter knows that a TCP layer should be created after a IP layer with the Protocol field equal to 0x06.
The
first column is the type of field, the second is the name of the field, and the last
column is the default value of the field.
The columns on the middle (sometimes 2 columns, sometimes 3) are parameters that defines the position of
the field inside the header and the values depends on the type of the
field.
For example,
ShortField DstPort 0 2 80
means a short field (2 bytes field) on word "zero" (the first word) on byte "two" inside that word.
This one,
BitsField Reserved 3 4 3 0
is a bit field inside the word "three" starting at bit 4, 3 bits long (in the figure is 6 bits long, but according to RFC3540 is 3 bits long).
You can take a look at the *.src file of different protocols and you should be able to infer the meaning and parameters of each field type.
3) Generate the .cpp and .h for this layer:
$ ./protogen TCP.src
[@] Protocol name = TCP
[@] Protocol ID = 0x06
[@] Protocol size (bits) = 160
[@] Protocol size (bytes) = 20
This will generate 3 files:
TCPCraft.cpp, TCPConstructor.cpp and
TCP.h. The only file you should edit is
TCPCraft.cpp. Inside this file there are four virtual methods that could be implemented on the child class (you can inherit the base class behavior if you want). But you should implement them or delete them from the base class (
do not leave the methods in blank).
Craft methodIs the most
important method. This method is executed before a packet is sent and is
where you set all the fields tedious for the library user. For example
is where the checksum should be calculated (you can see examples on the
TCPCraft.cpp and UDPCraft.cpp files).
ParseLayerDataBasically, this method is the one that gives information to libcrafter of which is going to be the next layer to be created. If the layer you are creating doesn't contains that information, you just need to inherit the default behavior. The definition of the
ParseInfo structure is on
Layer.h.
MatchFilterReturns an expression for the SendRecv method to match an answer from the net using libpcap filters.
ReDefineActiveFields
This method is used only on the
ICMP layer. Is for handling situations where the field names or sizes depends on values inside the header.
4) Then copy the three files into the Protocol directory:
cp TCP.h TCPCraft.cpp TCPConstructor.cpp ../Protocols/
5) Then, you should register that protocol into the factory. Add the next lines into the "
InitCrafter" function:
gedit ../InitCrafter.cpp/* +++++ Lines to be added */
TCP tcp_dummy; /* Register the protocol, this is executed only once */
Protocol::AccessFactory()->Register(&tcp_dummy);
/* +++++ End of lines */And also, open the
Crafter.h header (
upper-case)
gedit ../Crafter.hand add this line at the end of the file
#include "Protocols/TCP.h"
6) Finally, open the
Makefile.am file and add the TCP*.cpp and TCP.h into the list.
7)
./configure && make && sudo make install
Now you should be able to create TCP layers on libcrafter programs:
Ethernet ether_layer;
TCP tcp_layer;Packet pck = ether_layer / tcp_layer;
You should try all this with a DummyProtocol and post here you results or any problems

you had.