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
|
#include <stdio.h>
#include <stdarg.h>
#include <winsock2.h>
#include <string.h>
#include <errno.h>
#pragma comment(lib, "ws2_32.lib")
extern int errno;
struct pseudohdr
{
u_long saddr;
u_long daddr;
u_char zero;
u_long protocol;
u_long length;
};
struct tcphdr
{
unsigned short sport; /* port source */
unsigned short dport; /* port de destination */
unsigned int seqnum; /* numéro de séquence */
unsigned int acknum; /* accusé de réception */
unsigned char dataoffset:4; /* décalage des données (data
offset) */
/* flags */
unsigned char reserved:6;
unsigned char urg:1;
unsigned char ack:1;
unsigned char psh:1;
unsigned char rst:1;
unsigned char syn:1;
unsigned char fin:1;
unsigned short windows; /* fenêtre */
unsigned short checksum; /* checksum */
unsigned short urgpointer; /* pointeur de données urgentes */
};
struct iphdr
{
unsigned char version:4; /* version du protocol IP*/
unsigned char ihl:4; /* longeur de l'en tète */
unsigned char tos; /* type de service */
unsigned short tot_len; /* longueur totale du datagramme */
unsigned short id; /* identification */
unsigned char M:1; /* flag M*/
unsigned char df:1; /* flag df*/
unsigned char mf:1; /* flag mf*/
unsigned short frag_off:13; /* décalage de fragmentation */
unsigned char ttl; /* durée de vie du paquet */
unsigned char protocol; /* protocole */
unsigned short checksum; /* somme de contrôle */
unsigned long saddr; /* adresse IP source */
unsigned long daddr; /* adresse IP destinataire */
};
struct paquettcp
{
struct iphdr ippart;
struct tcphdr tcppart;
struct pseudohdr pseudopart;
char data[4096];
};
struct HostInfo //structure pour chaque hôte (source & dest)
{
char * Nom;
char * IP;
short Len;
short Type;
};
unsigned short checksum(u_short *addr, int len);
int main(int argc,char ** argv)
{
/* Toutes les variables déclarées (plus clair de cette façon je trouve) */
int taillepaquet = 0;
int lasocket = 0;
int i = 0;
int nbPacket = 0;
unsigned int ipid = 0;
unsigned int frago = 0;
unsigned int ttl = 0;
unsigned int mf = 0;
unsigned int df = 0;
unsigned int portsrc = 0;
unsigned int portdest = 0;
unsigned int flagack = 0;
unsigned int flagurg = 0;
unsigned int flagfin = 0;
unsigned int flagsyn = 0;
unsigned int flagrst = 0;
unsigned int flagpsh = 0;
unsigned int seqnum = 0;
unsigned int acknum = 0;
unsigned int decdata = 0;
unsigned int win = 0;
unsigned int urgptr = 0;
unsigned int Ip_Source_Bin = 0;
unsigned int Ip_Dest_Bin = 0;
char * data = (char *)calloc(4096,sizeof(char));
char * ipsource = (char *)calloc(32,sizeof(char));
char * ipdestination = (char *)calloc(32,sizeof(char));
char ** Temp_Addr = NULL;
struct sockaddr_in sock;
struct paquettcp * lepaquet = (struct paquettcp *)calloc(1,sizeof(struct paquettcp));
struct hostent * tSource_Info = (struct hostent *)calloc(1,sizeof(struct hostent));
struct hostent * tDest_Info = (struct hostent *)calloc(1,sizeof(struct hostent));
struct hostent * Temp_Info = NULL;
struct HostInfo * Source_Info = (struct HostInfo *)calloc(1,sizeof(struct HostInfo));
struct HostInfo * Dest_Info = (struct HostInfo *)calloc(1,sizeof(struct HostInfo));
Source_Info->IP = (char *)calloc(32,sizeof(char));
Dest_Info->IP = (char *)calloc(32,sizeof(char));
WSADATA WSAData;
if(!WSAStartup(MAKEWORD(2,0), &WSAData))
{
printf("[*] Winsock a ete initialise avec succes.\n");
}
else
{
printf("Erreur lors de l'initialisation de Winsock, code d'erreur : %d\n", GetLastError());
system("PAUSE");
return(-1);
}
printf("TCP MAKER\n");
printf("1. Configuration IP\n");
printf("IP source : ");
scanf("%32s", ipsource);
fflush(stdin);
printf("IP destination : ");
scanf("%32s", ipdestination);
fflush(stdin);
printf("Verification des donnees IP entrees\n");
Ip_Source_Bin = inet_addr(ipsource);
Ip_Dest_Bin = inet_addr(ipdestination);
if(Ip_Source_Bin == -1 || Ip_Dest_Bin == -1)
{
printf("Erreur -- IP invalides\n");
system("PAUSE");
return(-1);
}
/*
Bon en gros le trip c'est d'utiliser une variable temporaire (Temp_Info)
et ensuite d'utiliser la structure tSource_Info pour finalement tout copier dans
Source_Info
*/
Temp_Info = gethostbyaddr((char *)&Ip_Source_Bin,sizeof(Ip_Source_Bin),AF_INET);
if(Temp_Info == NULL)
{
printf("Adresse source invalide\n");
system("PAUSE");
return(-1);
}
memcpy(tSource_Info,Temp_Info,sizeof(struct hostent));
Temp_Addr = tSource_Info->h_addr_list;
Source_Info->Nom = (char *)calloc(strlen(tSource_Info->h_name)+1,sizeof(char));
strncpy(Source_Info->Nom,tSource_Info->h_name,strlen(tSource_Info->h_name));
strncpy(Source_Info->IP,(char *)inet_ntoa(*(struct in_addr *) *Temp_Addr),32);
Source_Info->Len = tSource_Info->h_length;
Source_Info->Type = tSource_Info->h_addrtype;
Temp_Info = NULL;
Temp_Addr = NULL;
Temp_Info = gethostbyaddr((char *)&Ip_Dest_Bin,sizeof(Ip_Dest_Bin),AF_INET);
if(Temp_Info == NULL)
{
printf("Adresse dest invalide\n");
system("PAUSE");
return(-1);
}
memcpy(tDest_Info,Temp_Info,sizeof(struct hostent));
Temp_Addr = tDest_Info->h_addr_list;
Dest_Info->Nom = (char *)calloc(strlen(tDest_Info->h_name)+1,sizeof(char));
strncpy(Dest_Info->Nom,tDest_Info->h_name,strlen(tDest_Info->h_name));
strncpy(Dest_Info->IP,(char *)inet_ntoa(*(struct in_addr *) *Temp_Addr),32);
Dest_Info->Len = tDest_Info->h_length;
Dest_Info->Type = tDest_Info->h_addrtype;
Temp_Info = NULL;
Temp_Addr = NULL;
printf("Nom source : %s\n", Source_Info->Nom);
printf("Nom dest : %s\n", Dest_Info->Nom);
printf("Adresse source : %s\n", Source_Info->IP);
printf("Adresse dest : %s\n", Dest_Info->IP);
system("PAUSE");
/* Ici tu mets tout ton code pour la gestion de tes packets TCP */
/* Libération massive de mémoire :) (fin du programme)*/
free(data); data = NULL;
free(ipsource); ipsource = NULL;
free(ipdestination); ipdestination = NULL;
free(lepaquet); lepaquet = NULL;
free(tSource_Info); tSource_Info = NULL;
free(tDest_Info); tDest_Info = NULL;
free(Source_Info); Source_Info = NULL;
free(Dest_Info); Dest_Info = NULL;
return 0;
} |
Partager