Les Forums

Les Forums

Les forums sont fermés. Ils restent présent pour consultation et archivage.
Vous pouvez désormais poser vos questions directement dans les commentaires en bas de chaque page du site.
Alors n'hésitez pas à participer

Envoi , reception et "analyse" d'un packet TCP

Bonjour

Avec un pote nous avons commencer un scanner de port pour en apprendre un peu plus sur la prog reseau en C . Mais une fois que nous avons eu fini de faire la premier beta nous nous sommes apercu que la methode connect() c'etais pas top 🙁

Nous souhaitons donc utiliser la methode syn ack mais le probleme c'est que les raw socket c'est nouveau pour moi 😳

Et je ne sais pas si ce que je fais la est corecte (c'est une fonction tres alleger pour les test et pour ne pas vous faire perdre trop de temps)

Pleasee help

[code:1:2b3095df3b]#ifndef _LIBH
#define _LIBH

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>

#if defined (WIN32)
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <unistd.h>
#include <sys/socket.h> /* pour avoir AF_INET */
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/in.h> /* pour inet_ntoa() */
#include <netdb.h> /* pour gethostbyname() & struct hostent */
#include <sys/types.h>
#include <arpa/inet.h>
#endif

#endif

int main(int argc , char *argv[])
{
char packet[8192];
int sock ;
struct sockaddr_in sin;
struct iphdr *ip = (struct iphdr *)packet;
struct iphdr *iprecpt;
struct tcphdr *tcp = (struct tcphdr *)packet + sizeof(struct iphdr);
struct tcphdr *tcprecpt;
char *ipt = argv[1];
struct hostent *host = gethostbyname(ipt);

if(!host)
{
printf("Error resolution host\n");
return 1;
}
if ((sock = socket(AF_INET , SOCK_RAW , IPPROTO_TCP)) == -1)
{
printf("Socket error\n");
return 1;
}

sin.sin_family = AF_INET;
sin.sin_port = htons(80);
sin.sin_addr = *((struct in_addr *)host->h_addr);
memset(packet , 0 , 8192);

// Fill in IP headers.
ip->ihl = 5;
ip->version = 4;
ip->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
ip->id = htons(getuid());
ip->saddr = inet_addr("127.0.0.1");
ip->daddr = inet_ntoa(sin.sin_addr);
ip->ttl = 255;
ip->protocol = 6;
ip->check = 0;
ip->tos = 0;
ip->frag_off = 0;

// Fill in TCP headers.
tcp->source = htons(80);
tcp->dest = htons(80);
tcp->seq = htons(random());
tcp->ack = 0;
tcp->syn = 1;
tcp->window = htons(65535);
tcp->check = 0;
tcp->doff = 5;
tcp->rst = 0;
tcp->psh = 0;
tcp->fin = 0;
tcp->urg = 0;
tcp->ack_seq = htons(0);

sendto(sock, packet, ip->tot_len, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr));
recv(sock,packet,8192,0);
tcprecpt = (struct tcphdr *)packet;
iprecpt = (struct iphdr *)packet;
printf("%d %d\n\n",tcprecpt->syn,tcprecpt->ack);
if((tcprecpt->syn == 1) && (tcprecpt->ack == 1))
{printf("Connect reussi donc le port est ouvert\n");}

return 0;
}
[/code:1:2b3095df3b]