Navigation
Artikel
Stuff
RSS Feeds
|
Sourcecodes - Pakete mit Libnet erzeugenSprachenübersicht/C / C++/ C#/Netzwerk Keywords: Pakete erzeugen, Libnet, sourcecode Dieser Quellcode demonstriert das erstellen eines TCP Package von dem Ethernet Frame bis zum TCP Paket mithilfe von Libnet.
Code: // Libnet Sourcecode // // This program shows how to use libnet // // Written by Simon Hecht // www.online-tutorials.net // // Reference: // http://www.datenterrorist.de/doc/libnet-reference.txt #include "main.h" //Returns the Mac from a string like 00:12:3f:f5:58:ee u_int8_t* GetMAC(const std::string Mac) { u_int8_t* Buffer = new u_int8_t[6]; char* nc; for(int i = 0; i < 18; i+=3) Buffer[i/3] = strtol(Mac.substr(i, 2).c_str(), &nc, 16); return Buffer; } //Returns the Mac from a string like 00:12:3f:f5:58:ee u_int8_t* GetMAC(char* Mac) { return GetMAC(std::string(Mac)); } int main(int args, char* argv[]) { if (args < 7) { std::cout << "Usage: " << argv[0] << " [interface] [source mac] [sourceip] [src port] [dst mac] [dst ip] [dst port]\n"; return 0; } /***************************************************** // Initialisierungsvariablen *****************************************************/ //The error buffer char Errbuf[LIBNET_ERRBUF_SIZE]; //The libnet struct libnet_t *Libnet; //The device (eth0 for example) char *Device = argv[1]; //The Paket tag libnet_ptag_t PacketTag = 0; /***************************************************** // Initialise libnet *****************************************************/ //Initialise libnet Libnet = libnet_init(LIBNET_LINK, Device, Errbuf); /***************************************************** // Packetvariables TCP *****************************************************/ u_int16_t SourcePort = atoi(argv[4]); //Source Port u_int16_t DestinationPort = atoi(argv[7]); //Destination Port u_int32_t SequenceNumber = 100; //Sequence Number u_int32_t ACKNumber = 1; //ACK NUmber u_int8_t ControlFlags = TH_RST; //Flags | TH_URG, TH_ACK, TH_PSH, TH_RST, TH_SYN, TH_FIn u_int16_t WindowSize = 0; //Window Size u_int16_t CheckSum = 0; //Checksum u_int16_t UrgentPointer = 0; //UrgentPointer u_int16_t Length = LIBNET_TCP_H; //Length u_int8_t* TCPPayLoad = 0; //PayLoad u_int32_t TCPPayLoadSize = 0; //Payload_size /***************************************************** // Packetvariables IP *****************************************************/ u_int16_t PacketLength = LIBNET_IPV4_H + LIBNET_TCP_H + TCPPayLoadSize;//Total Length u_int8_t TOS = 0; //TOS (Type of Service) u_int16_t ID = 0x200; //ID u_int16_t FragmOffset = 0; //Fragment Offset u_int8_t TTL = 64; //TTL (Time To Live) u_int8_t Protocol = IPPROTO_TCP; //Protocol (IPPROTO_ICMP, IPPROTO_TCP, IPPROTO_UDP) u_int16_t Checksum = 0; //Checksum u_int32_t SourceIP = 0; //Source IP Address u_int32_t DestIP = 0; //Destination IP Address u_int8_t* IPPayLoad = 0; //Payload u_int32_t IPPayLoadSize = 0; //Payload size //SourceIP = libnet_get_ipaddr4(Libnet); //DestIP = libnet_name2addr4(Libnet, argv[1], LIBNET_RESOLVE); //Resolve the IP DestIP = libnet_name2addr4(Libnet, argv[6], LIBNET_RESOLVE); SourceIP = libnet_name2addr4(Libnet, argv[3], LIBNET_RESOLVE); /***************************************************** // Paccketvariables Ethernet *****************************************************/ u_int8_t* SourceEthernet = GetMAC(argv[2]); //argv[2] u_int8_t* DestEthernet = GetMAC(argv[5]); //argv[5] u_int16_t Type = ETHERTYPE_IP; u_int8_t* EthernetPayload = 0; u_int32_t EthernetPayLoadSize = 0; /***************************************************** // Builds die Packet *****************************************************/ //Builds the TCP Packet PacketTag = libnet_build_tcp(SourcePort, DestinationPort, SequenceNumber, ACKNumber, ControlFlags, WindowSize, CheckSum, UrgentPointer, Length, TCPPayLoad, TCPPayLoadSize, Libnet, 0); if(PacketTag == -1) { std::cout << "libnet_build_tcp() failed: " << libnet_geterror(Libnet) << std::endl; libnet_destroy(Libnet); return 0; } //Builds the IP Packet PacketTag = libnet_build_ipv4(PacketLength, TOS, ID, FragmOffset, TTL, Protocol, Checksum, SourceIP, DestIP, IPPayLoad, IPPayLoadSize, Libnet, 0); if(PacketTag == -1) { std::cout << "libnet_build_ipv4() failed: " << libnet_geterror(Libnet) << std::endl; libnet_destroy(Libnet); return 0; } //Builds the Ethernet Paket PacketTag = libnet_build_ethernet(DestEthernet, SourceEthernet, Type, EthernetPayload, EthernetPayLoadSize, Libnet, 0); if(PacketTag == -1) { std::cout << "libnet_build_ethernet() failed: " << libnet_geterror(Libnet) << std::endl; libnet_destroy(Libnet); return 0; } //Sends it if(libnet_write(Libnet) == -1) { std::cout << "libnet_write() failed: " << libnet_geterror(Libnet) << std::endl; } else std::cout << "Package send!" << std::endl; /***************************************************** // Beenden *****************************************************/ libnet_destroy(Libnet); return 0; }
Code: CC_OPTIONS=-Wall -O2 CC_INCLUDE=-I/usr/include/libnet LD_OPTIONS=-O2 -Wall LD_LINK=-lnet TARGET=test TARGET_DIR=./ TMP_DIR=tmp/ SRC=\ main.cpp OBJ=$(SRC:%.cpp=$(TMP_DIR)%.o) all: $(OBJ) g++ $(LD_OPTIONS) $(OBJ) -o $(TARGET_DIR)$(TARGET) $(LD_LINK) $(TMP_DIR)%.o : %.cpp g++ $(CC_OPTIONS) $(CC_INCLUDE) -c -o $@ $< clean: rm -f $(TMP_DIR)*.o depend: gcc -MM $(CC_OPTIONS) $(CC_INCLUDE) $(SRC) > Dependencies Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren? Sprachenübersicht/C / C++/ C#/Netzwerk/Pakete mit Libnet erzeugen |