c++ program, which generates TCP packets.
-
The work on this page generates a full TCP packet, based on
TCP layer.
-
Similar to the ARP example, I added the common layer:
/* --------- Common data to all headers --------- */
The raw print now contains the MAC layer as well:
ff ff ff 22 22 22 00 0c 29 b4 41 b0 08 00 45 00
00 38 00 00 40 00 40 06 09 d8 c0 a8 00 b4 d4 b3
9a d8 00 0b 00 50 8c 24 15 fa 00 00 00 00 50 02
16 d0 a3 42 00 00 41 72 62 69 74 72 61 72 79 50
61 79 6c 6f 61 64
-
The code of the TCP generator, is shown below:
#include <iostream>
#include <crafter.h>
/* Collapse namespaces */
using namespace std;
using namespace Crafter;
int main() {
/* Set the interface */
string iface = "eth0";
/* Get the IP address associated to the interface */
string MyIP = GetMyIP(iface);
/* Get the MAC Address associated to the interface */
string MyMAC = GetMyMAC(iface);
/* --------- Common data to all headers --------- */
Ethernet ether_header;
ether_header.SetSourceMAC(MyMAC); // <-- Set our MAC as a source
ether_header.SetDestinationMAC("ff:ff:ff:22:22:22");
/* Create an IP header */
IP ip_header;
/* Set the Source and Destination IP address */
ip_header.SetSourceIP(MyIP);
ip_header.SetDestinationIP("www.google.com");
Packet* packet = new Packet;
packet->PushLayer(ether_header);
/* Create an TCP - SYN header */
TCP tcp_header;
tcp_header.SetSrcPort(11);
tcp_header.SetDstPort(80);
tcp_header.SetSeqNumber(RNG32());
tcp_header.SetFlags(TCP::SYN);
/* A raw layer, this could be any array of bytes or chars */
RawLayer payload("ArbitraryPayload");
/* Create a packets */
Packet tcp_packet = ip_header / tcp_header / payload;
/* Write the packet on the wire */
tcp_packet.FillPkt();
packet->PushLayer(ip_header);
packet->PushLayer(tcp_header);
packet->PushLayer(payload);
cout << endl << "[@] Print after sending: " << endl;
tcp_packet.Print();
tcp_packet.RawString();
cout << "packet " << endl;
(*packet).RawString();
return 0;
}
|