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
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <pcap.h>
#include <pcap-bpf.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <linux/tcp.h>
#include <netinet/ip.h>
char **str_split(char *s, const char *ct)
{
char **tab=NULL;
int i;
char *cs=NULL;
size_t size=1;
void *temp=NULL;
//printf("test\n");
if(s!=NULL && ct != NULL)
{
for(i=0; (cs=strtok(s,ct)); i++)
{
if(size <=i+1){
size <<=1;
temp=realloc(tab,sizeof(*tab) * size);
if(temp!=NULL){
tab=temp;
}
else{
fprintf(stderr,"mémoire insffisante\n");
free(tab);
tab=NULL;
exit(EXIT_FAILURE);
}
}
tab[i]=cs;
s=NULL;
}
tab[i]=NULL;
}
return tab;
}
char *rebuild_mac(char **mac_digit){
int index_temp=0;
char mac_address[20];
// printf("Test rebuild_mac\n");
strcpy(mac_address,"\0");
while(index_temp <6)
{
if(index_temp!=0 && index_temp<6){
strcat(mac_address,":");
}
if(strlen(mac_digit[index_temp])<2)
{
strcat(mac_address,"0");
strcat(mac_address,mac_digit[index_temp]);
}else{
strcat(mac_address,mac_digit[index_temp]);
}
//printf("Temp : %s -> taille : %d index_temp : %d\n", mac_address,strlen(mac_address),index_temp);
index_temp++;
}
//printf("Fin dela boucle while\n");
return mac_address;
}
int main(int argc, char **argv)
{
pcap_t *desc;
pcap_t *desc_save;
struct pcap_pkthdr hdr;
struct bpf_program bp ;
struct ethhdr *EtherHdr;
struct ip *IpHeader;
struct tpchdr *TcpHeader;
bpf_u_int32 netmask=32;
FILE *fp;
const u_char *packet;
char **temp;
char errbuf[PCAP_ERRBUF_SIZE];
char * buff =NULL;
char *save_temp;
char *fichier_pcap = argv[1];
char mac_address_src_1[20];
char mac_address_dst_1[20];
char mac_address_src_2[20];
char mac_address_dst_2[20];
char previous_mac_address[20];
char mac_AP[20];
char ip_address_src_1[20];
char ip_address_dst_1[20];
char ip_address_src_2[20];
char ip_address_dst_2[20];
char directory[100];
int index_temp= 0;
int num_protocole= 0;
int mac_found= 0;
int host_found = 0;
int i = 0;
int size_tab=100;
int multiple_ip = 0;
//End of variables declaration
if(argc<=0){
fprintf(stderr,"Missing arguments");
exit(EXIT_FAILURE);
}
// We take the directory in wich we will write te ip/mac file
desc = pcap_open_offline(fichier_pcap, errbuf);
if (desc == NULL){
printf("File damaged or missing\n", errbuf), exit(2);
}
IpHeader=NULL;
// On rehcerhce l'adresse Mac et l'adresse IP du PA .
while(packet=pcap_next(desc,&hdr))
{
EtherHdr= (struct etherhdr *)packet;
if(EtherHdr->h_proto!=ETH_P_IP){ //if we are not using an ARP packet
//printf("EtherHdr proto : %d\n",ETH_HLEN);
IpHeader = (struct ip *)(packet + ETH_HLEN);
//printf("protocole IP : %d\n", IpHeader->ip_p);
if(IpHeader->ip_p ==IPPROTO_TCP){
printf("taille de la trame IP : %d\n", ETH_HLEN);
TcpHeader = (struct tcphdr *)(packet + 34); //IpHeader->ip_hl*4 in order to have the lenght in bytes
printf("Numéro du port : %x\n",ntohs(TcpHeader->source));
}
}
}
pcap_close(desc); //on ferme le fichier
return 0;
} |
Partager