Results 1 to 3 of 3
Hi everyone,
First of all, thanks for taking the time to read this post.
Im having a little issue here with linux socket.
Basically, i want to rewrite SCAPPY (packet ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-30-2009 #1Just Joined!
- Join Date
- Jan 2009
- Posts
- 10
[SOLVED] Send raw packet in linux
Hi everyone,
First of all, thanks for taking the time to read this post.
Im having a little issue here with linux socket.
Basically, i want to rewrite SCAPPY (packet forger in PYTHON) in c++.
But, when I send my ethernet packet, I do not receive anything with TCPDUMP, as if they were not sent.
P.S Please not comments about the ugliness of the code, i am well aware of it, but u know, its more easy and quick when im testing, i shall clean the code once everything works.
So I have my class ClEthernet here, wich basically, let you forge an ethernet packet:
and my class ClSocket (wich is not finished...).Code:#pragma once #define __FAVOR_BSD #include <netinet/ip.h> #include <arpa/inet.h> #include <netinet/in.h> #include <net/if_arp.h> #include <netinet/if_ether.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <iostream> struct Ethhdr { u_char targ_hw_addr[6]; u_char src_hw_addr[6]; u_short frame_type; }; class ClEthernet { private: char* MACStringToArray(char* MAC); public: char * sourceMAC; /* Sender hardware address. */ char * destMAC; /* Target hardware address. */ unsigned short protocol; //ETHERTYPE_IP 0x0800 , ETHERTYPE_ARP 0x0806 ClEthernet(); void GeneratePacket(); char* packetBuffer; int GetPacketSize(); }; char* ClEthernet::MACStringToArray(char* str) { int i; char c,val; char* buf = new char[6]; for(i=0;i<6;i++) { if( !(c = tolower(*str++))) printf("Invalid hardware address"); if(isdigit(c)) val = c-'0'; else if(c >= 'a' && c <= 'f') val = c-'a'+10; else printf("Invalid hardware address"); *buf = val << 4; if( !(c = tolower(*str++))) printf("Invalid hardware address"); if(isdigit(c)) val = c-'0'; else if(c >= 'a' && c <= 'f') val = c-'a'+10; else printf("Invalid hardware address"); *buf++ |= val; if(*str == ':')str++; } return buf; } ClEthernet::ClEthernet() { sourceMAC = "00:00:00:00:00:00"; destMAC = "00:00:00:00:00:00"; packetBuffer = NULL; protocol = ETHERTYPE_IP; } int ClEthernet::GetPacketSize() { return (sizeof(struct Ethhdr)); } void ClEthernet::GeneratePacket() { int packet_size = (sizeof(struct Ethhdr)); printf("PacketSize = %d\n",packet_size); char* packet=(char*)malloc(packet_size); memset(packet,0,packet_size); printf("Packet after creation is %s\n",packet); if(!packet) { printf("Could not allocate enough memory!\n"); return; } struct Ethhdr *eth = (struct Ethhdr*)packet; printf("lenght of packet before zeroing : %d\n",strlen(packet)); printf("lenght of packet after zeroing : %d\n",strlen(packet)); printf("PAcket after being zeroed : %s",packet); memcpy(eth->targ_hw_addr ,MACStringToArray(destMAC),sizeof(u_char)*6); memcpy(eth->src_hw_addr ,MACStringToArray(sourceMAC),sizeof(u_char)*6); eth->frame_type = htons(protocol); packetBuffer = packet; }
Code:#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <netdb.h> #include <sys/socket.h> #include <arpa/inet.h> #include <linux/if_ether.h> //for testing purpose only //#include "ClIP.h" #include "ClEthernet.h" //end of testing const char* DEFAULT_DEVICE = "eth0"; class ClSocket { private: int sockId; public: ClSocket(); bool Send(char* packet,unsigned int packetSize); char* deviceName; char* srcIP; char* destIP; }; ClSocket::ClSocket() { sockId=socket(AF_INET,SOCK_PACKET,htons(ETH_P_ALL)); if(sockId<0) { printf("Error creating socket\n"); } deviceName = (char*)DEFAULT_DEVICE; srcIP = NULL; destIP = NULL; } bool ClSocket::Send(char* packet,unsigned int packetSize) { if(packetSize<0) { printf("PacketSize is <0!\n"); return false; } struct in_addr src_in_addr,targ_in_addr; struct sockaddr sa; src_in_addr.s_addr = inet_addr(srcIP); targ_in_addr.s_addr = inet_addr(destIP); strcpy(sa.sa_data,deviceName); if(sendto(sockId,packet,packetSize,0,&sa,sizeof(sa)) < 0) { printf("Error sending packet\n"); return false; } return true; } int main() { ClSocket sock; ClEthernet eth; sock.srcIP = "192.168.3.31"; sock.destIP = "192.168.3.1"; ClEthernet eth; eth.sourceMAC = "00:00:00:00:00:00"; eth.destMAC = "11:22:33:44:55:55"; eth.protocol = ETHERTYPE_IP; eth.GeneratePacket(); char* packet = new char[eth.GetPacketSize()]; eth.GeneratePacket(); sock.Send(eth.packetBuffer,eth.GetPacketSize()); //sock.Send("Im traveling on ethernet!",26);
Again, my apologies for the uglyness of my code.
But, if you notice, the last line of ClSocket( the commencted one), works, and by that I mean I can see in in tcpdump.
But the normal sending (the line before the commencted one) do not produce any output.
Im currently searching on setsockopt() , maybe its an issue with that, i dont know.
If you have any idea why the packet is not sent, please let me know.
P.S The socket do not produce any error.
Any suggestion is greatly appreciated.
My thanks for your precious time.
- 03-03-2010 #2Just Joined!
- Join Date
- Jan 2009
- Posts
- 13
Did you debug it?
just look at the value of packet buffer.
- 09-27-2010 #3Just Joined!
- Join Date
- Jan 2009
- Posts
- 10
Well, a year later, part of the code has been accepted as an open-source c++ packet injector : if you feel like it, give it a look:
www . cubsystem . com / sho
Thanks a for your help guys.



