// ******************************************** // Nom : FrameIP.cpp // Auteur : Sebastien.Fontaine@frameIP.com.pas.de.spam // Date de création : 21 Décembre 2002 // version : 5.1.3.10 // Licence : Cette executable est libre de toute utilisation. // La seule condition existante est de faire référence // au site http://www.frameip.com afin de respecter le travail d'autrui. // ******************************************** // ******************************************************** // Les includes // ******************************************************** // Il faut ajouter dans les proprités du projet => C++ => Command Line : // /I "C:\RepPerso\Personnel\Developpement\Projets\LibrairiePcap\Include" // /I "C:\RepPerso\Personnel\Developpement\Projets\LibrairieSocket" #include "LibrairieSocket.h" #include "pcap.h" // ******************************************************** // Les Librairies // ******************************************************** // Il faut ajouter dans les proprités du projet => Linker => Command Line : // /NODEFAULTLIB:libcd.lib // /LIBPATH:"C:\RepPerso\Personnel\Developpement\Projets\LibrairiePcap\Lib" #pragma comment(lib,"Packet.lib") #pragma comment(lib,"wpcap.lib") // ******************************************************** // Les procédures // ******************************************************** void initiation_des_variables(void); void gestion_des_arguments(int argc, char* argv[]); void initiation_des_variables_aleatoires(void); void initiation_des_variables_automatiques(void); void concatenation_des_entetes(void); void envoi_de_la_trame_winsock(void); void envoi_de_la_trame_pcap(void); void tirage_des_variables_aleatoires(void); void affichage_du_resultat(); // ******************************************************** // Définition des variables // ******************************************************** struct mac entete_mac; // Entête Ethernet struct ipv4 entete_ipv4; // Entête IPV4 struct icmp entete_icmp; // Entête ICMP struct igmp entete_igmp; // Entête IGMP struct tcp entete_tcp; // Entête TCP struct udp entete_udp; // Entête UDP char ip_source[15]; char ip_destination[15]; char ip_destination_igmp[15]; unsigned char trame_a_envoyer[65535]; // Chaine de caratère comprennant les entêtes et la data unsigned short taille_de_la_trame_a_envoyer=0; // Permet d'éviter un sizeof qui s'arrête au premier 0 trouvé char data[65535]; // Chaine de caratère représentant les data en couche 7 int taille_de_data; // Taille des données saisies unsigned int loops; // Compteur permettant de lancer x fois les trames bool bouclage_infinie; // Variable permettant de définir le mode de boucle infinie (quand loops=0) unsigned int pause_entre_chaque_frame; // En ms, permet de spécifier une pause à chaque tour de la boucle int nombre_de_caractere_emis; // Variable récupérant le nombre de caractères émis unsigned int choix_mode_d_envoi; // Variable définissant le mode d'envoi (0=Soket - 1=Libpcap) unsigned char numero_de_l_interface; // Numéro de l'interface qui sera utilisée struct gestion_des_interfaces liste_des_interfaces; // structure possédant les informations des intefaces struct adresse_mac adresse_mac_tampon; // Adresse MAC bool mac_destination_auto; // Variable permettant de savoir si l'adresse MAC de destination doit être résolu en ARP bool mac_source_auto; // Variable permettant de savoir si l'adresse MAC source doit être prise à partir de l'interface // ******************************************************** // Variables permettant de savoir si il faut tirer un nombre aléatoire // ******************************************************** bool ipv4_id_random; bool ipv4_type_random; bool ipv4_source_random; bool ipv4_destination_random; bool icmp_type_random; bool icmp_id_random; bool icmp_sequence_random; bool igmp_destination_random; bool tcp_port_source_random; bool tcp_port_destination_random; bool tcp_sequence_random; bool udp_port_source_random; bool udp_port_destination_random; // ******************************************************** // Variables permettant de savoir si il faut calculer le checksum // ******************************************************** bool ipv4_length_automatique; bool ipv4_checksum_automatique; bool icmp_checksum_automatique; bool igmp_checksum_automatique; bool tcp_checksum_automatique; bool udp_length_automatique; bool udp_checksum_automatique; int main (int argc, char* argv[]) { // ******************************************************** // Initiation // ******************************************************** initiation_des_variables(); gestion_des_arguments(argc,argv); initiation_des_variables_aleatoires(); initiation_des_variables_automatiques(); // ******************************************************** // Boucle d'envoi // ******************************************************** while ( (loops!=0)||(bouclage_infinie==TRUE) ) { // ******************************************************** // Gestion du compteur de la boucle // ******************************************************** loops--; // ******************************************************** // Concatenation des entêtes // ******************************************************** concatenation_des_entetes(); // ******************************************************** // Envoi de la trame // ******************************************************** if (choix_mode_d_envoi==0) envoi_de_la_trame_winsock(); else envoi_de_la_trame_pcap(); // ******************************************************** // Affichage du resultat // ******************************************************** affichage_du_resultat(); // ******************************************************** // Pause entre chaque trame sauf si c'est la dernière // ******************************************************** if (loops>0) Sleep(pause_entre_chaque_frame); } printf("\n"); return(1); } void initiation_des_variables() { structure_ip_local reception_des_ip_locales; unsigned int i; // ******************************************************** // Initiation diverse // ******************************************************** srand(GetTickCount()); // Initialise le Random // ******************************************************** // Affichage de la banniere // ******************************************************** printf("\nFrameIP - Create some IP frame - Version 5.1.3.10"); printf("\nCreate on December 21, 2002, Last compilation on August 18, 2006"); printf("\nCreated by Sebastien FONTAINE - http://www.frameip.com"); printf("\n"); // ******************************************************** // Options générales // ******************************************************** loops=1; bouclage_infinie=FALSE; pause_entre_chaque_frame=1000; choix_mode_d_envoi=1; numero_de_l_interface=0; liste_des_interfaces=recuperation_des_interfaces(); // ******************************************************** // Options de l'entête MAC // ******************************************************** adresse_mac_tampon=recuperation_de_l_adresse_mac(liste_des_interfaces.nom[numero_de_l_interface]); for (i=0;i<6;i++) { entete_mac.source[i]=adresse_mac_tampon.adresse[i]; entete_mac.destination[i]=255; } mac_source_auto=true; mac_destination_auto=false; entete_mac.type=htons(2048); // 08 00 indiquant un datagramme IP // ******************************************************** // Options de l'entête IP // ******************************************************** entete_ipv4.version=4; entete_ipv4.ihl=5; entete_ipv4.tos=0; entete_ipv4.length=htons(0); entete_ipv4.id=htons(0); entete_ipv4.flag_zero=0; entete_ipv4.flag_mf=0; entete_ipv4.flag_df=0; entete_ipv4.offset=0; entete_ipv4.offset2=0; // Offset2 n'est là que pour la séparation d'offset entete_ipv4.ttl=128; entete_ipv4.type=1; entete_ipv4.checksum=htons(0); reception_des_ip_locales=recuperation_ip_local(false); strcpy(ip_source,reception_des_ip_locales.adresse_ip_local[0]); strcpy(ip_destination,"217.12.3.11"); entete_ipv4.ip_source=resolution_de_nom(false,ip_source); entete_ipv4.ip_destination=resolution_de_nom(false,ip_destination); // ******************************************************** // Options de l'entête ICMP // ******************************************************** entete_icmp.type=8; entete_icmp.code=0; entete_icmp.checksum=htons(0); entete_icmp.id=htons(0); entete_icmp.sequence=htons(0); // ******************************************************** // Options de l'entête IGMP // ******************************************************** entete_igmp.version=1; entete_igmp.type=1; entete_igmp.reserve=0; entete_igmp.checksum=htons(0); strcpy(ip_destination_igmp,"224.0.0.1"); entete_igmp.ip_destination=resolution_de_nom(false,ip_destination_igmp); // ******************************************************** // Options de l'entête TCP // ******************************************************** entete_tcp.port_source=htons(0); entete_tcp.port_destination=htons(80); entete_tcp.sequence=htonl(0); entete_tcp.accuse=htonl(0); entete_tcp.offset=5; entete_tcp.reserved=0; entete_tcp.reserved2=0; entete_tcp.flag_urg=0; entete_tcp.flag_ack=0; entete_tcp.flag_psh=0; entete_tcp.flag_rst=0; entete_tcp.flag_syn=1; entete_tcp.flag_fin=0; entete_tcp.window=htons(0); entete_tcp.checksum=htons(0); entete_tcp.pointeur=htons(0); // ******************************************************** // Options de l'entête UDP // ******************************************************** entete_udp.port_source=htons(0); entete_udp.port_destination=htons(53); entete_udp.length=htons(0); entete_udp.checksum=htons(0); // ******************************************************** // Options de la couche DATA // ******************************************************** strcpy(data,"www.frameip.com"); taille_de_data=strlen(data); } void gestion_des_arguments(int argc,char* argv[]) { char *caractere_non_convertit; unsigned int i,j; bool size_modifiee=false; // Permet de gérer la saisie simultannée des argument -size et -data (dans cette ordre) unsigned short tempo3,tempo4; // Pour la séparation de l'argument -ip_offset unsigned char tempo1,tempo2; // Pour la séparation de l'argument -tcp_reserved // ******************************************************** // Affichage de l'aide // ******************************************************** if ( (argc>1) && (strcmp(argv[1],"-?")==0) || (argc==1) ) { printf("\nGENERAL OPTIONS"); printf("\n-? This help"); printf("\n-wait Wait after frame Defaut: %d ms",pause_entre_chaque_frame); printf("\n-loops Number of loops Defaut: %d (0 => no stop)",loops); printf("\n-send_mode 0=Soket 1=Libpcap Defaut: %d",choix_mode_d_envoi); printf("\n\nFREE INTERFACES"); for(i=0;i<(signed)liste_des_interfaces.nombre;i++) printf("\n%d - %s",i,liste_des_interfaces.description[i]); printf("\n-interface Interface choice Defaut: %d",numero_de_l_interface); printf("\n\nETHERNET HEADER OPTIONS (-send_mode 1)"); printf("\n-mac_source @ Ethernet Defaut: %.2X-%.2X-%.2X-%.2X-%.2X-%.2X",entete_mac.source[0],entete_mac.source[2],entete_mac.source[2],entete_mac.source[3],entete_mac.source[4],entete_mac.source[5]); printf("\n-mac_source_auto Between 0 or 1 Defaut: %d (1 => MAC from interface)",mac_source_auto); printf("\n-mac_destination @ Ethernet Defaut: %.2X-%.2X-%.2X-%.2X-%.2X-%.2X",entete_mac.destination[0],entete_mac.destination[2],entete_mac.destination[2],entete_mac.destination[3],entete_mac.destination[4],entete_mac.destination[5]); printf("\n-mac_destination_auto Between 0 or 1 Defaut: %d (1 => MAC from @IP)",mac_destination_auto); printf("\n-mac_type Between 0 & 65535 Defaut: %d",htons(entete_mac.type)); printf("\n\nIP HEADER OPTIONS (-mac_type 2048)"); printf("\n-ip_version Between 0 & 15 Defaut: %d",entete_ipv4.version); printf("\n-ip_ihl Between 0 & 15 Defaut: %d",entete_ipv4.ihl); printf("\n-ip_tos Between 0 & 255 Defaut: %d",entete_ipv4.tos); printf("\n-ip_length Between 0 & 65535 Defaut: %d (0 => automatic)",htons(entete_ipv4.length)); printf("\n-ip_id Between 0 & 65535 Defaut: %d (0 => random)",htons(entete_ipv4.id)); printf("\n-ip_flag_zero Between 0 or 1 Defaut: %d",entete_ipv4.flag_zero); printf("\n-ip_flag_mf Between 0 or 1 Defaut: %d",entete_ipv4.flag_mf); printf("\n-ip_flag_df Between 0 or 1 Defaut: %d",entete_ipv4.flag_df); printf("\n-ip_offset Between 0 & 8191 Defaut: %d",entete_ipv4.offset); printf("\n-ip_ttl Between 0 & 255 Defaut: %d",entete_ipv4.ttl); printf("\n-ip_type Between 0 & 255 Defaut: %d (0 => random)",entete_ipv4.type); printf("\n-ip_checksum Between 0 & 65535 Defaut: %d (0 => automatic)",htons(entete_ipv4.checksum)); printf("\n-ip_source @Ip or host name Defaut: %s (0 => random)",ip_source); printf("\n-ip_destination @Ip or host name Defaut: %s (0 => random)",ip_destination); printf("\n\nICMP HEADER OPTIONS (-ip_type 1)"); printf("\n-icmp_type Between 0 & 255 Defaut: %d (0 => random)",entete_icmp.type); printf("\n-icmp_code Between 0 & 255 Defaut: %d",entete_icmp.code); printf("\n-icmp_checksum Between 0 & 65535 Defaut: %d (0 => automatic)",htons(entete_icmp.checksum)); printf("\n-icmp_id Between 0 & 65535 Defaut: %d (0 => random)",htons(entete_icmp.id)); printf("\n-icmp_sequence Between 0 & 65535 Defaut: %d (0 => random)",htons(entete_icmp.sequence)); printf("\n\nIGMP HEADER OPTIONS (-ip_type 2)"); printf("\n-igmp_version Between 0 & 15 Defaut: %d",entete_igmp.version); printf("\n-igmp_type Between 0 & 15 Defaut: %d",entete_igmp.type); printf("\n-igmp_reserve Between 0 & 255 Defaut: %d",entete_igmp.reserve); printf("\n-igmp_checksum Between 0 & 65535 Defaut: %d (0 => automatic)",htons(entete_igmp.checksum)); printf("\n-igmp_destination @Ip or host name Defaut: %s (0 => random)",ip_destination_igmp); printf("\n\nTCP HEADER OPTIONS (-ip_type 6)"); printf("\n-tcp_port_source Between 0 & 65535 Defaut: %d (0 => random)",htons(entete_tcp.port_source)); printf("\n-tcp_port_destination Between 0 & 65535 Defaut: %d (0 => random)",htons(entete_tcp.port_destination)); printf("\n-tcp_sequence Between 0 & 2E16 Defaut: %d (0 => random)",htonl(entete_tcp.sequence)); printf("\n-tcp_acknowledge Between 0 & 2E16 Defaut: %d",htonl(entete_tcp.accuse)); printf("\n-tcp_offset Between 0 & 15 Defaut: %d",entete_tcp.offset); printf("\n-tcp_reserved Between 0 & 63 Defaut: %d",entete_tcp.reserved); printf("\n-tcp_flag_urg Between 0 or 1 Defaut: %d",entete_tcp.flag_urg); printf("\n-tcp_flag_ack Between 0 or 1 Defaut: %d",entete_tcp.flag_ack); printf("\n-tcp_flag_psh Between 0 or 1 Defaut: %d",entete_tcp.flag_psh); printf("\n-tcp_flag_rst Between 0 or 1 Defaut: %d",entete_tcp.flag_rst); printf("\n-tcp_flag_syn Between 0 or 1 Defaut: %d",entete_tcp.flag_syn); printf("\n-tcp_flag_fin Between 0 or 1 Defaut: %d",entete_tcp.flag_fin); printf("\n-tcp_window Between 0 & 65535 Defaut: %d",htons(entete_tcp.window)); printf("\n-tcp_checksum Between 0 & 65535 Defaut: %d (0 => automatic)",htons(entete_tcp.checksum)); printf("\n-tcp_pointeur Between 0 & 65535 Defaut: %d",htons(entete_tcp.pointeur)); printf("\n\nUDP HEADER OPTIONS (-ip_type 17)"); printf("\n-udp_port_source Between 0 & 65535 Defaut: %d (0 => random)",htons(entete_udp.port_source)); printf("\n-udp_port_destination Between 0 & 65535 Defaut: %d (0 => random)",htons(entete_udp.port_destination)); printf("\n-udp_length Between 0 & 65535 Defaut: %d (0 => automatic)",htons(entete_udp.length)); printf("\n-udp_checksum Between 0 & 65535 Defaut: %d (0 => automatic)",htons(entete_udp.checksum)); printf("\n\nOPTIONS OF THE DATA LAYER"); printf("\n-data_size data size Defaut: %d",taille_de_data); printf("\n-data_ascii specify a string Defaut: %s",data); printf("\n-data_hexa specify some hexa Defaut: %s",conversion_string_hexa(data,strlen(data))); printf("\n"); exit(0); // 0 permet d'indiquer une sortie normal sans erreurs } for (i=1;i>11; // ******************************************************** // Récupération des huit bits finaux du second octet // ******************************************************** entete_ipv4.offset2=(unsigned char)tempo3; } if (strcmp(strlwr(argv[i]),"-ip_ttl")==0) entete_ipv4.ttl=(unsigned char)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-ip_type")==0) entete_ipv4.type=(unsigned char)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-ip_checksum")==0) entete_ipv4.checksum=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-ip_source")==0) entete_ipv4.ip_source=(unsigned long)resolution_de_nom(false,argv[i+1]); if (strcmp(strlwr(argv[i]),"-ip_destination")==0) entete_ipv4.ip_destination=(unsigned long)resolution_de_nom(false,argv[i+1]); // ******************************************************** // Options de l'entête ICMP // ******************************************************** if (strcmp(strlwr(argv[i]),"-icmp_type")==0) entete_icmp.type=(unsigned char)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-icmp_code")==0) entete_icmp.code=(unsigned char)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-icmp_checksum")==0) entete_icmp.checksum=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-icmp_id")==0) entete_icmp.id=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-icmp_sequence")==0) entete_icmp.sequence=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); // ******************************************************** // Options de l'entête IGMP // ******************************************************** if (strcmp(strlwr(argv[i]),"-igmp_version")==0) entete_igmp.version=(unsigned char)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-igmp_type")==0) entete_igmp.type=(unsigned char)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-igmp_reserve")==0) entete_igmp.reserve=(unsigned char)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-igmp_checksum")==0) entete_igmp.checksum=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-igmp_destination")==0) entete_igmp.ip_destination=(unsigned long)strtod(argv[i+1],&caractere_non_convertit); // ******************************************************** // Options de l'entête TCP // ******************************************************** if (strcmp(strlwr(argv[i]),"-tcp_port_source")==0) entete_tcp.port_source=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-tcp_port_destination")==0) entete_tcp.port_destination=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-tcp_sequence")==0) entete_tcp.sequence=htonl((unsigned long)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-tcp_acknowledge")==0) entete_tcp.accuse=htonl((unsigned long)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-tcp_offset")==0) entete_tcp.offset=(unsigned short)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-tcp_reserved")==0) { tempo1=(unsigned char)strtod(argv[i+1],&caractere_non_convertit); // ******************************************************** // Récupération des quatre bits centraux // ******************************************************** tempo2=tempo1<<2; // Rajoute 2 0 à droite en décalant entete_tcp.reserved=tempo2>>4; // Rajoute 4 0 à gauche en décalant // ******************************************************** // Récupération des deux bits finaux // ******************************************************** tempo2=tempo1<<6; entete_tcp.reserved2=tempo2>>6; // Les deux bits suivant } if (strcmp(strlwr(argv[i]),"-tcp_flag_urg")==0) entete_tcp.flag_urg=(unsigned short)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-tcp_flag_ack")==0) entete_tcp.flag_ack=(unsigned short)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-tcp_flag_psh")==0) entete_tcp.flag_psh=(unsigned short)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-tcp_flag_rst")==0) entete_tcp.flag_rst=(unsigned short)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-tcp_flag_syn")==0) entete_tcp.flag_syn=(unsigned short)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-tcp_flag_fin")==0) entete_tcp.flag_fin=(unsigned short)strtod(argv[i+1],&caractere_non_convertit); if (strcmp(strlwr(argv[i]),"-tcp_window")==0) entete_tcp.window=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-tcp_checksum")==0) entete_tcp.checksum=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-tcp_pointeur")==0) entete_tcp.pointeur=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); // ******************************************************** // Options de l'entête UDP // ******************************************************** if (strcmp(strlwr(argv[i]),"-udp_port_source")==0) entete_udp.port_source=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-udp_port_destination")==0) entete_udp.port_destination=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-udp_length")==0) entete_udp.length=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); if (strcmp(strlwr(argv[i]),"-udp_checksum")==0) entete_udp.checksum=htons((unsigned short)strtod(argv[i+1],&caractere_non_convertit)); // ******************************************************** // Options de la couche DATA // ******************************************************** if (strcmp(strlwr(argv[i]),"-data_size")==0) { size_modifiee=true; // Indique que size à été modifié taille_de_data=(unsigned int)strtod(argv[i+1],&caractere_non_convertit); memcpy(data,dimensionnement_de_data_a_envoyer(false,data,taille_de_data,taille_de_data),taille_de_data); } if (strcmp(strlwr(argv[i]),"-data_ascii")==0) { strcpy(data,argv[i+1]); taille_de_data=strlen(argv[i+1]); if (size_modifiee==true) { strcpy(data,dimensionnement_de_data_a_envoyer(false,data,taille_de_data,taille_de_data)); taille_de_data=strlen(data); } } if (strcmp(strlwr(argv[i]),"-data_hexa")==0) { memcpy(data,conversion_hexa_string(argv[i+1],strlen(argv[i+1])),strlen(argv[i+1])); taille_de_data=strlen(argv[i+1])/2; if (size_modifiee==true) memcpy(data,dimensionnement_de_data_a_envoyer(false,data,taille_de_data,taille_de_data),taille_de_data); } } } void initiation_des_variables_aleatoires(void) { // ******************************************************** // Permet de savoir si l'utilisateur a demandé un calcul aléatoire. Utilie dans le cas des loops. // ******************************************************** if (entete_ipv4.id==0) ipv4_id_random=true; else ipv4_id_random=false; if (entete_ipv4.type==0) ipv4_type_random=true; else ipv4_type_random=false; if (entete_ipv4.ip_source==0) ipv4_source_random=true; else ipv4_source_random=false; if (entete_ipv4.ip_destination==0) ipv4_destination_random=true; else ipv4_destination_random=false; if (entete_icmp.type==0) icmp_type_random=true; else icmp_type_random=false; if (entete_icmp.id==0) icmp_id_random=true; else icmp_id_random=false; if (entete_icmp.sequence==0) icmp_sequence_random=true; else icmp_sequence_random=false; if (entete_igmp.ip_destination==0) igmp_destination_random=true; else igmp_destination_random=false; if (entete_tcp.port_source==0) tcp_port_source_random=true; else tcp_port_source_random=false; if (entete_tcp.port_destination==0) tcp_port_destination_random=true; else tcp_port_destination_random=false; if (entete_tcp.sequence==0) tcp_sequence_random=true; else tcp_sequence_random=false; if (entete_udp.port_source==0) udp_port_source_random=true; else udp_port_source_random=false; if (entete_udp.port_destination==0) udp_port_destination_random=true; else udp_port_destination_random=false; } void initiation_des_variables_automatiques(void) { unsigned int i; // Pour les boucles for if (entete_ipv4.length==0) ipv4_length_automatique=true; else ipv4_length_automatique=false; if (entete_ipv4.checksum==0) ipv4_checksum_automatique=true; else ipv4_checksum_automatique=false; if (entete_icmp.checksum==0) icmp_checksum_automatique=true; else icmp_checksum_automatique=false; if (entete_igmp.checksum==0) igmp_checksum_automatique=true; else igmp_checksum_automatique=false; if (entete_tcp.checksum==0) tcp_checksum_automatique=true; else tcp_checksum_automatique=false; if (entete_udp.length==0) udp_length_automatique=true; else udp_length_automatique=false; if (entete_udp.checksum==0) udp_checksum_automatique=true; else udp_checksum_automatique=false; // ******************************************************** // Préparation des adresses MAC // ******************************************************** if (mac_source_auto==true) { adresse_mac_tampon=recuperation_de_l_adresse_mac(liste_des_interfaces.nom[numero_de_l_interface]); for (i=0;i<6;i++) entete_mac.source[i]=adresse_mac_tampon.adresse[i]; } if (mac_destination_auto==true) { adresse_mac_tampon=resolution_arp(liste_des_interfaces.nom[numero_de_l_interface],0,ip_destination,3); for (i=0;i<6;i++) entete_mac.destination[i]=adresse_mac_tampon.adresse[i]; } } void concatenation_des_entetes(void) { // ******************************************************** // Affectation d'une valeur aléatoire pour les variables qui le nécssite // ******************************************************** tirage_des_variables_aleatoires(); // ******************************************************** // Concatenation des entetes avec la data // ******************************************************** switch (entete_ipv4.type) { case 1: // ICMP // ******************************************************** // Calcul du checksum ICMP // ******************************************************** if (icmp_checksum_automatique==true) entete_icmp.checksum=calcul_du_checksum_icmp(false,entete_icmp,data,taille_de_data); // ******************************************************** // Calcul de la longueur totale // ******************************************************** taille_de_la_trame_a_envoyer=(unsigned short)(sizeof(struct ipv4)+sizeof(struct icmp)+taille_de_data); if (ipv4_length_automatique==true) entete_ipv4.length=htons((unsigned short)taille_de_la_trame_a_envoyer); // ******************************************************** // Calcul du checksum IP // ******************************************************** if (ipv4_checksum_automatique==true) entete_ipv4.checksum=calcul_du_checksum_ip(false,entete_ipv4); // ******************************************************** // Concatenation des différentes couches // ******************************************************** memcpy(trame_a_envoyer,(unsigned short *)&entete_ipv4,sizeof(struct ipv4)); memcpy(trame_a_envoyer+sizeof(struct ipv4),(unsigned short *)&entete_icmp,sizeof(struct icmp)); memcpy(trame_a_envoyer+sizeof(struct ipv4)+sizeof(struct icmp),data,taille_de_data); break; case 2: // IGMP // ******************************************************** // Calcul du checksum IGMP // ******************************************************** if (igmp_checksum_automatique==true) entete_igmp.checksum=calcul_du_checksum_igmp(false,entete_igmp,data,taille_de_data); // ******************************************************** // Calcul de la longueur totale // ******************************************************** taille_de_la_trame_a_envoyer=(unsigned short)(sizeof(struct ipv4)+sizeof(struct igmp)+taille_de_data); if (ipv4_length_automatique==true) entete_ipv4.length=htons((unsigned short)taille_de_la_trame_a_envoyer); // ******************************************************** // Calcul du checksum IP // ******************************************************** if (ipv4_checksum_automatique==true) entete_ipv4.checksum=calcul_du_checksum_ip(false,entete_ipv4); // ******************************************************** // Concatenation des différentes couches // ******************************************************** memcpy(trame_a_envoyer,(unsigned short *)&entete_ipv4,sizeof(struct ipv4)); memcpy(trame_a_envoyer+sizeof(struct ipv4),(unsigned short *)&entete_igmp,sizeof(struct igmp)); memcpy(trame_a_envoyer+sizeof(struct ipv4)+sizeof(struct igmp),data,taille_de_data); break; case 6: // TCP // ******************************************************** // Calcul du checksum TCP // ******************************************************** if (tcp_checksum_automatique==true) entete_tcp.checksum=calcul_du_checksum_tcp(false,entete_ipv4.ip_source,entete_ipv4.ip_destination,entete_tcp,data,taille_de_data); // ******************************************************** // Calcul de la longueur totale // ******************************************************** taille_de_la_trame_a_envoyer=(unsigned short)(sizeof(struct ipv4)+sizeof(struct tcp)+taille_de_data); if (ipv4_length_automatique==true) entete_ipv4.length=htons((unsigned short)taille_de_la_trame_a_envoyer); // ******************************************************** // Calcul du checksum IP // ******************************************************** if (ipv4_checksum_automatique==true) entete_ipv4.checksum=calcul_du_checksum_ip(false,entete_ipv4); // ******************************************************** // Concatenation des différentes couches // ******************************************************** memcpy(trame_a_envoyer,(unsigned short *)&entete_ipv4,sizeof(struct ipv4)); memcpy(trame_a_envoyer+sizeof(struct ipv4),(unsigned short *)&entete_tcp,sizeof(struct tcp)); memcpy(trame_a_envoyer+sizeof(struct ipv4)+sizeof(struct tcp),data,taille_de_data); break; case 17: // UDP // ******************************************************** // Calcul de la longueur totale // ******************************************************** if (udp_length_automatique==true) entete_udp.length=htons(sizeof(struct udp)+(u_short)taille_de_data); // ******************************************************** // Calcul du checksum UDP // ******************************************************** if (udp_checksum_automatique==true) entete_udp.checksum=calcul_du_checksum_udp(false,entete_ipv4.ip_source,entete_ipv4.ip_destination,entete_udp,data,taille_de_data); // ******************************************************** // Calcul de la longueur totale // ******************************************************** taille_de_la_trame_a_envoyer=(unsigned short)(sizeof(struct ipv4)+sizeof(struct udp)+taille_de_data); if (ipv4_length_automatique==true) entete_ipv4.length=htons((unsigned short)taille_de_la_trame_a_envoyer); // ******************************************************** // Calcul du checksum IP // ******************************************************** if (ipv4_checksum_automatique==true) entete_ipv4.checksum=calcul_du_checksum_ip(false,entete_ipv4); // ******************************************************** // Concatenation des différentes couches // ******************************************************** memcpy(trame_a_envoyer,(unsigned short *)&entete_ipv4,sizeof(struct ipv4)); memcpy(trame_a_envoyer+sizeof(struct ipv4),(unsigned short *)&entete_udp,sizeof(struct udp)); memcpy(trame_a_envoyer+sizeof(struct ipv4)+sizeof(struct udp),data,taille_de_data); break; default: // ******************************************************** // Calcul de la longueur totale // ******************************************************** taille_de_la_trame_a_envoyer=(unsigned short)(sizeof(struct ipv4)+taille_de_data); if (ipv4_length_automatique==true) entete_ipv4.length=htons((unsigned short)taille_de_la_trame_a_envoyer); // ******************************************************** // Calcul du checksum IP // ******************************************************** if (ipv4_checksum_automatique==true) entete_ipv4.checksum=calcul_du_checksum_ip(false,entete_ipv4); // ******************************************************** // Concatenation des différentes couches // ******************************************************** memcpy(trame_a_envoyer,(unsigned short *)&entete_ipv4,sizeof(struct ipv4)); memcpy(trame_a_envoyer+sizeof(struct ipv4),data,taille_de_data); break; } if (choix_mode_d_envoi!=0) { // ******************************************************** // Concatenation des différentes couches avec l'entête MAC // ******************************************************** memcpy(trame_a_envoyer+14,trame_a_envoyer,taille_de_la_trame_a_envoyer); taille_de_la_trame_a_envoyer=taille_de_la_trame_a_envoyer+14; memcpy(trame_a_envoyer,(unsigned short *)&entete_mac,sizeof(struct mac)); } } void envoi_de_la_trame_winsock(void) { WSADATA initialisation_win32; SOCKET id_de_la_socket; SOCKADDR_IN information_sur_la_destination; int on=1; // Permet de paramétrer la fonction SetSockOpt // ******************************************************** // Initialisation de la Socket // ******************************************************** if (WSAStartup(MAKEWORD(2,2),&initialisation_win32)!=0) gestion_des_erreurs(1,"WSAStartup",1,1); // ******************************************************** // Ouverture d'une Socket // ******************************************************** id_de_la_socket=socket(AF_INET,SOCK_RAW,IPPROTO_RAW); if (id_de_la_socket==INVALID_SOCKET) gestion_des_erreurs(1,"socket",1,1); // ******************************************************** // Activation de l'option permettant d'inclure l'entete IP lors de l'emission // ******************************************************** if (setsockopt(id_de_la_socket,IPPROTO_IP,IP_HDRINCL,(char *)&on,sizeof(on))!=0) gestion_des_erreurs(1,"setsockopt",1,1); // ******************************************************** // Informations non utilisée mais nécessaire pour la fonction sendto // ******************************************************** information_sur_la_destination.sin_family=AF_INET; information_sur_la_destination.sin_addr.s_addr=entete_ipv4.ip_destination; information_sur_la_destination.sin_port=entete_tcp.port_destination; // ******************************************************** // Envoi de la trame en winsock // ******************************************************** nombre_de_caractere_emis=sendto(id_de_la_socket,(char *)trame_a_envoyer,taille_de_la_trame_a_envoyer,0,(struct sockaddr*)&information_sur_la_destination,sizeof(information_sur_la_destination)); if (nombre_de_caractere_emis==-1) gestion_des_erreurs(1,"sendto",1,1); } void envoi_de_la_trame_pcap(void) { pcap_t *pointeur_interface; char buffer_d_erreur[PCAP_ERRBUF_SIZE]; // ******************************************************** // Accède à l'interface // ******************************************************** if ((pointeur_interface=pcap_open_live(liste_des_interfaces.nom[numero_de_l_interface],65536,0,1000,buffer_d_erreur))==NULL) gestion_des_erreurs(1,"pcap_open_live",1,1); // ******************************************************** // Envoi de la trame // ******************************************************** if(pcap_sendpacket(pointeur_interface,trame_a_envoyer,taille_de_la_trame_a_envoyer)!=0) gestion_des_erreurs(1,"pcap_sendpacket",1,1); // ******************************************************** // Fermeture de l'accès à l'interface // ******************************************************** pcap_close(pointeur_interface); } void tirage_des_variables_aleatoires(void) { if (ipv4_id_random==true) entete_ipv4.id=(unsigned short)(rand()%65535+1); // Tire entre 1 et 65535 if (ipv4_type_random==true) entete_ipv4.type=(unsigned char)(rand()%255+1); // Tire entre 1 et 255 if (ipv4_source_random==true) entete_ipv4.ip_source=generation_d_une_adresse_ip_aleatoire(0); if (ipv4_destination_random==true) entete_ipv4.ip_destination=generation_d_une_adresse_ip_aleatoire(0); if (icmp_type_random==true) entete_icmp.type=(unsigned char)(rand()%255+1); // Tire entre 1 et 255 if (icmp_id_random==true) entete_icmp.id=(unsigned short)(rand()%65535+1); // Tire entre 1 et 65535 if (icmp_sequence_random==true) entete_icmp.sequence=(unsigned short)(rand()%65535+1); // Tire entre 1 et 65535 if (igmp_destination_random==true) entete_igmp.ip_destination=generation_d_une_adresse_ip_aleatoire(0); if (tcp_port_source_random==true) entete_tcp.port_source=(unsigned short)(rand()%64511+1+1024); // Tire entre 1025 et 65535 if (tcp_port_destination_random==true) entete_tcp.port_destination=(unsigned short)(rand()%65535+1); // Tire entre 1 et 65535 if (tcp_sequence_random==true) entete_tcp.sequence=(unsigned long)(rand()%4294967295+1); // Tire entre 1 et 4294967295 if (udp_port_source_random==true) entete_udp.port_source=(unsigned short)(rand()%64511+1+1024); // Tire entre 1025 et 65535 if (udp_port_destination_random==true) entete_udp.port_destination=(unsigned short)(rand()%65535+1); // Tire entre 1 et 65535 } void affichage_du_resultat(void) { struct in_addr convertion_inet_addr; // ******************************************************** // Affichage du resultat // ******************************************************** convertion_inet_addr.S_un.S_addr=entete_ipv4.ip_source; printf("\nThe frame was sent from %s",inet_ntoa(convertion_inet_addr)); convertion_inet_addr.S_un.S_addr=entete_ipv4.ip_destination; printf(" to %s with %d Bytes",inet_ntoa(convertion_inet_addr),taille_de_la_trame_a_envoyer); }