1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h> //
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#include <pcap.h>
#pragma comment(lib, "wpcap.lib")
typedef struct EthernetHeader EthernetHeader;
typedef struct IPHEADER IPHEADER;
typedef struct enteteICMP enteteICMP;
#pragma pack(1)
struct EthernetHeader
{
unsigned char Mac_destin1[2];
unsigned char Mac_destin2[2];
unsigned char Mac_destin3[2];
unsigned char Mac_source1[2];
unsigned char Mac_source2[2];
unsigned char Mac_source3[2];
unsigned short int type_Ether;
};
#pragma pack()
#pragma pack(1)
struct IPHEADER
{
unsigned char version;
unsigned char typServ;
unsigned short int tot_len;
unsigned short int id;
unsigned short int offset ;
unsigned char ttl;
unsigned char protocol;
unsigned short int somme; //modded
unsigned long int ip_source;
unsigned long int ip_dest;
};
#pragma pack()
#pragma pack(1)
struct enteteICMP
{
unsigned char TypeMsg;
unsigned char CodeMsg;
unsigned short int Sommecontrole;
unsigned short int idMsg;
unsigned short int Numseq;
};
#pragma pack()
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t * alldevs;
pcap_t *adhandle;
EthernetHeader ethernet;
IPHEADER ip;
enteteICMP icmp;
char * buffer= NULL;
char Mac_destin01[2] = { '\x52', '\x54' } ;
char Mac_destin02[2] = { '\x00', '\x12' } ; // 52:54:00:12:35:02
char Mac_destin03[2] = { '\x35', '\x02' } ;
char Mac_source01[2] = { '\x08', '\x00' } ;
char Mac_source02[2] = { '\x27', '\xa6' } ; // 08:00:27:A6:12:9D
char Mac_source03[2] = { '\x12', '\x9D' } ;
char DATA[32] = "abcdefghijklmnopqrstuvwabcdefghi";
unsigned short int global_size = 0;
void RemplissageChar( char * var1 , char * var2 );
USHORT checksum(void * lpData, size_t size);
int main(void)
{
global_size = sizeof(EthernetHeader) + sizeof(IPHEADER) + sizeof(enteteICMP )+ sizeof(DATA);
buffer = malloc(global_size);
if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING , NULL , &alldevs , errbuf) == -1 )
{
printf(" \n error de recherche de la liste des DEVICES ..." );
return -1;
}
if( ( adhandle = pcap_open( alldevs->name, 65536, PCAP_OPENFLAG_PROMISCUOUS, -1, NULL, errbuf )) == NULL )
{
printf(" \n error pcap_open() ..." );
return -1;
}
RemplissageChar( Mac_destin01 , ethernet.Mac_destin1);
RemplissageChar( Mac_destin02 , ethernet.Mac_destin2);
RemplissageChar( Mac_destin03 , ethernet.Mac_destin3);
RemplissageChar( Mac_source01 , ethernet.Mac_source1);
RemplissageChar( Mac_source02 , ethernet.Mac_source2);
RemplissageChar( Mac_source03 , ethernet.Mac_source3);
ethernet.type_Ether = htons(0x0800) ;
ip.version = 69;
ip.typServ = 0;
ip.tot_len = htons(sizeof(IPHEADER) + sizeof(enteteICMP )+ sizeof(DATA));
ip.id = htons(52);
ip.offset = 0;
ip.ttl = 128;
ip.protocol = 1;
ip.somme = 0x0000; //modded
ip.ip_source = inet_addr("10.0.2.15");
ip.ip_dest = inet_addr("192.168.0.1");
ip.somme = checksum( &ip, sizeof(ip) );
icmp.TypeMsg = 8;
icmp.CodeMsg = 0;
icmp.Sommecontrole = 0;
//icmp.Sommecontrole = checksum( &icmp, sizeof(icmp) );//wireshark me conseil ===> htons(0x335c);
icmp.idMsg = htons(14);
icmp.Numseq = htons(12);
icmp.Sommecontrole = checksum( &icmp, sizeof(icmp) ); // htons(0x4d42);
memcpy(buffer, ðernet, sizeof(ethernet));
memcpy(buffer + sizeof(ethernet), &ip , sizeof(ip) );
memcpy(buffer + sizeof(ethernet) + sizeof(ip) , &icmp , sizeof(icmp) );
memcpy(buffer + sizeof(ethernet) + sizeof(ip) + sizeof(icmp) , DATA , strlen(DATA) );
if ( pcap_sendpacket(adhandle , buffer, global_size ) == -1 )
{
printf(" \n error pcap_sendpacket ..." );
return -1;
}
free(buffer);
pcap_freealldevs(alldevs);
return 0;
}
void RemplissageChar( char * var1 , char * var2 )
{
int i = 0;
int Longueur = 0;
Longueur = sizeof(var1);
for ( i = 0 ; i < Longueur ; i++ )
{
var2[i] = var1[i] ;
}
}
USHORT checksum(void * lpData, size_t size)
{
USHORT * t = lpData;
DWORD somme = 0;
size_t i, n = size / sizeof(USHORT);
for(i = 0; i < n; i++)
somme += t[i];
if (size % 2)
{
UCHAR * u = lpData;
somme += u[size - 1];
}
while (HIWORD(somme))
somme = HIWORD(somme) + LOWORD(somme);
return LOWORD(~somme);
} |
Partager