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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
u_short calcul_du_checksum(u_char *data, u_short len)
{
unsigned long checksum = 0;
u_short *DataPtr;
u_short valData;
u_short valTmp, valTmp2;
DataPtr = (u_short*)&data[0];
while(len > 0)
{
valData = *DataPtr++;
if(len == 1){
valData &= 0x00FF;
len--;
} else {
len -= 2;
}
checksum += valData;
}
checksum = (checksum & 0xFFFF) + ((checksum >> 16) & 0xFFFF);
valTmp = ((checksum >> 16) & 0xFFFF);
valTmp2 = (checksum & 0xFFFF);
valTmp2 += valTmp;
checksum &= 0xFFFF0000;
checksum |= valTmp2;
valTmp2 = (checksum & 0xFFFF);
return ~valTmp2;
}
// */
// *************************************************************
int main()
{
#define PACKET_SIZE (1500)
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
//char packet_filter[] = "ip and udp";
//struct bpf_program fcode;
u_char buf[PACKET_SIZE];
unsigned short crc;
int cpt = 0;
/* Retrieve the device list */
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* Print the list */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
/* Check if the user specified a valid adapter */
if(inum < 1 || inum > i)
{
printf("\nAdapter number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* Open the adapter */
if ((adhandle= pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* Check the link layer. We support only Ethernet for simplicity. */
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
//if(d->addresses != NULL){
// /* Retrieve the mask of the first address of the interface */
// netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
//} else {
// /* If the interface is without addresses we suppose to be in a C class network */
// netmask=0xffffff;
//}
// ***********************
// MAC destination : 01:00:5e:01:01:01
buf[0]=0x01;
buf[1]=0x00;
buf[2]=0x5e;
buf[3]=0x01;
buf[4]=0x01;
buf[5]=0x01;
// MAC source : 00:24:E8:00:3B:A0
buf[6]=0x00;
buf[7]=0x24;
buf[8]=0x00;
buf[9]=0xE8;
buf[10]=0x3B;
buf[11]=0xA0;
// trame type IP
buf[12]=0x08;
buf[13]=0x00;
// ***********************
buf[14]=0x45; // version IP V4 / len = 20 bytes
buf[15]=0x88; // ???
// total len => 1436
buf[16]=0x05;
buf[17]=0x9C;
// identification => incrémenté de 2 a chaque paquet envoyé
//buf[18]=0x00;
//buf[19]=0x00;
buf[18]=(cpt >> 8) & 0x1F;
buf[19]=cpt & 0xFF;
cpt+=2;
// flag = 0x02 / Fragment offset = 0;
buf[20]=0x40;
buf[21]=0x00;
buf[22]=0x0a; // TTL = 10
buf[23]=0x11; // PROTOCL = UDP
// IP Checksum
buf[24]=0x00;
buf[25]=0x00;
//buf[24]=0xb7;
//buf[25]=0x83;
// IP Source : 10.0.200.3
buf[26]=10;
buf[27]=0;
buf[28]=200;
buf[29]=3;
// IP Destination : 224.1.1.1
buf[30]=224;
buf[31]=1;
buf[32]=1;
buf[33]=1;
// affectation du checksum du Header IP
crc = calcul_du_checksum(&buf[14], 20);
buf[24]= crc & 0xFF;
buf[25]= (crc >> 8) & 0xFF;
// ***********************
// source port = 6840
buf[34]=0x1a;
buf[35]=0xb8;
// destination port = 6780
buf[36]=0x1a;
buf[37]=0x7c;
// len = 1416
buf[38]=0x05;
buf[39]=0x88;
// UDP Checksum
//buf[40]=0x5f;
//buf[41]=0xc1;
buf[40]=0x00;
buf[41]=0x00;
// affectation du checksum du Header UDP
crc = calcul_du_checksum(&buf[34], 8);
buf[40]= crc & 0xFF;
buf[41]= (crc >> 8) & 0xFF;
// ***********************
// remplissage des data UDP
for(i=42;i<PACKET_SIZE;i++){
buf[i]= (u_char)i;
}
// ************************
// => mettre des bytes de bourrage si le paquet est trop petit (la norme impose une taille min)
while(1){
//for(i=0; i<1000; i++){
// Send down the packet
if (pcap_sendpacket(adhandle, // Adapter
buf, // buffer with the packet
PACKET_SIZE // size
) != 0)
{
fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(adhandle));
return 3;
}
}
pcap_close(adhandle);
return 0;
} |
Partager