myxray.c

来自「一个Linux下抓包演示程序」· C语言 代码 · 共 692 行 · 第 1/2 页

C
692
字号
               memcpy(igmpbufone,buf_recv,datalen);  }}break;}if (((dstip==NULL)||(strcmp(dstaddr,dstip)==0))&&((srcip==NULL)||(strcmp(srcaddr,srcip)==0))){if ((protol==6)&&(tcpenable)){//tcp   if (tcpflag!=NULL)if (strcmp(tcpflag,flagsmes)) goto AGAIN;  //do with ethhdr datatemptime=time(NULL);  printf("Recorded %sethhdr\nsrchw:%s--->dsthw:%s proto:%xH\n",ctime(&temptime),hwaddr(eth->h_source,buf1), hwaddr(eth->h_dest,buf2),ethprotol);  //iphdrprintf("iphdr\nver:%d ihl:%d tos:%d tot_len:%d id:%d df:%d mf:%d fragoff:%d TTL:%d proto:%d\nsrcaddr:%s dstaddr:%s\n",ip->version,ip->ihl, ip->tos,ntohs(ip->tot_len),ntohs(ip->id),df,mf,ipfragoff&IP_OFFMASK,ip->ttl,protol,srcaddr,dstaddr);  //tcphdr  printf("tcphdr\nsrcport:%d dstport:%d seq:%u ack:%u off:%d flag:%s win:%d\n",ntohs(tcp1->th_sport),ntohs(tcp1->th_dport), ntohl(tcp1->th_seq),ntohl(tcp1->th_ack),tcp1->th_off, flagsmes,ntohs(tcp1->th_win));printf("-----------------------------------------------------------\n");distcp++;  }//end tcpif ((protol==1)&&(icmpenable)){//icmptemptime=time(NULL);   printf("Recorded %sethhdr\nsrchw:%s--->dsthw:%s proto:%xH\n",ctime(&temptime),hwaddr(eth->h_source,buf1), hwaddr(eth->h_dest,buf2),ethprotol);printf("iphdr\nver:%d ihl:%d tos:%d tot_len:%d id:%d df:%d mf:%d fragoff:%d TTL:%d proto:%d\nsrcaddr:%s dstaddr:%s\n",ip->version,ip->ihl, ip->tos,ntohs(ip->tot_len),ntohs(ip->id),df,mf,ipfragoff&IP_OFFMASK,ip->ttl, protol,srcaddr,dstaddr);   printf("icmp\ntype:%d code:%d\n",icmphdr->icmp_type,icmphdr->icmp_code);printf("-----------------------------------------------------------\n");disicmp++;  }//end icmpif ((protol==17)&&(udpenable)){//udphdrtemptime=time(NULL);  printf("Recored %sethhdr\nsrchw:%s--->dsthw:%s proto:%xH\n",ctime(&temptime),hwaddr(eth->h_source,buf1), hwaddr(eth->h_dest,buf2),ethprotol);printf("iphdr\nver:%d ihl:%d tos:%d tot_len:%d id:%d df:%d mf:%d fragoff:%d TTL:%d proto:%d\nsrcaddr:%s dstaddr:%s\n",ip->version,ip->ihl, ip->tos,ntohs(ip->tot_len),ntohs(ip->id),df,mf,ipfragoff&IP_OFFMASK,ip->ttl,protol,srcaddr,dstaddr);  printf("udphdr\nsport:%d dsport:%d len:%d\n",ntohs(udp->uh_sport), ntohs(udp->uh_dport),ntohs(udp->uh_ulen));printf("-----------------------------------------------------------\n");disudp++;  }//end udpif ((protol==2)&&(igmpenable)){//igmptemptime=time(NULL);  printf("Recored %sethhdr\nsrchw:%s--->dsthw:%s proto:%xH\n",ctime(&temptime),hwaddr(eth->h_source,buf1), hwaddr(eth->h_dest,buf2),ethprotol);printf("iphdr\nver:%d ihl:%d tos:%d tot_len:%d id:%d df:%d mf:%d fragoff:%d TTL:%d proto:%d\nsrcaddr:%s dstaddr:%s\n",ip->version,ip->ihl, ip->tos,ntohs(ip->tot_len),ntohs(ip->id),df,mf,ipfragoff&IP_OFFMASK,ip->ttl, protol,srcaddr,dstaddr);  printf("igmphdr\ntype:%d code:%d group_addr:%s\n",igmp1->igmp_type,igmp1->igmp_code,inet_ntoa(igmp1->igmp_group));printf("-----------------------------------------------------------\n"); disigmp++;}//end igmp  fflush(stdout);} //endif strcmp}//endif ip goto AGAIN;}//end mainchar *hwaddr (unsigned char * s,char *d){sprintf (d, "%02X:%02X:%02X:%02X:%02X:%02X", s[0], s[1], s[2], s[3],s[4], s[5]);return d;}void leave(){if (ioctl(fd_recv, SIOCSIFFLAGS, &ifr_old) < 0) {perror("ioctl SIOCSIFFLAGS error");}if (fd_recv > 0) close(fd_recv);two=time(NULL);fclose(waringfd);printf("total received %u packets\n",total);printf("tcp packets %u, display %u, speed is %u /second\n",totaltcp,distcp,totaltcp/(two-one));printf("udp packets %u, display %u, speed is %u /second\n",totaludp,disudp,totaludp/(two-one));printf("arp packets %u, display %u, speed is %u /second\n",totalarp,disarp,totalarp/(two-one));printf("icmp packets %u, display %u, speed is %u /second\n",totalicmp,disicmp,totalicmp/(two-one));printf("igmp packets %u, display %u, speed is %u /second\n",totaligmp,disigmp,totaligmp/(two-one));printf("process terminamted.\n");exit(0);}void writearpbuf(FILE * fd,char *p,int counter){struct arp_packet *arphead;struct in_addr in2,in1;char *srcaddr,*dstaddr;time_t temptime;char strptr[255];char buf1[20],buf2[20],buf3[20],buf4[20];int ethprotol;temptime=time(NULL);        arphead=(struct arp_packet *)p;ethprotol=ntohs(arphead->frame_type);        memcpy(&in1,arphead->sndr_ip_addr, IP_ADDR_LEN);        memcpy(&in2,arphead->rcpt_ip_addr, IP_ADDR_LEN);        srcaddr=inet_ntoa(in1);        dstaddr=(char*)inet_ntop(AF_INET,&in2,strptr,sizeof(strptr));        snprintf(writebuf,PACKET_SIZE,"This arp packet catch %d times continuous\nrecorded %sethhdr\nsrchw:%s--->dsthw:%s proto:%xH arphdr\nhwtype:%d protol:%xH hw_size:%d pro_size:%d op:%d\ns_ha:% s s_ip:%s\nd_ha:%s d_ip:%s-----------------------------------------------------------\n",counter,ctime(&temptime), hwaddr(arphead->src_hw_addr,buf1),hwaddr(arphead->targ_hw_addr,buf2),ethprotol,            ntohs(arphead->hw_type),ntohs(arphead->prot_type),            arphead->hw_addr_size, arphead->prot_addr_size,            ntohs(arphead->op), hwaddr(arphead->sndr_hw_addr,buf3),            srcaddr, hwaddr(arphead->rcpt_hw_addr,buf4),dstaddr);        fwrite(writebuf,strlen(writebuf),1,fd);}void writetcpbuf(FILE * fd,char *p,int counter){struct ethhdr *eth ;struct iphdr * ip;struct tcphdr * tcp1;char *flagsmes;int flags;struct in_addr in1,in2;int ethprotol;int ihl,df,mf,ipfragoff;char * srcaddr,*dstaddr;int protol; char strptr[255];char buf1[20],buf2[20];eth=(struct ethhdr *)p;ethprotol=ntohs(eth->h_proto);ip = (struct iphdr *)(p+14);     ihl = (int)ip->ihl << 2;     in1.s_addr = ip->saddr;     in2.s_addr = ip->daddr;     srcaddr=inet_ntoa(in1);    dstaddr=(char *)inet_ntop(AF_INET,&in2,strptr,sizeof(strptr));        //iphdr        protol=ip->protocol;        ipfragoff=ntohs(ip->frag_off);        df=ipfragoff&IP_DF;        mf=ipfragoff&IP_MF;        if (df!=0) df=1;        if (mf!=0) mf=1;     protol=ip->protocol;tcp1 = (struct tcphdr *)(p+14 + ihl);        flags= tcp1->th_flags;        if (flags&TH_PUSH)   flagsmes="PUSH";        if (flags&TH_ACK)    flagsmes="ACK";        if (flags&TH_URG)    flagsmes="URG";        if (flags&TH_FIN)    flagsmes="FIN";        if (flags&TH_SYN)    flagsmes="SYN";        if (flags&TH_RST)    flagsmes="RST";temptime=time(NULL);        snprintf(writebuf,PACKET_SIZE,"This tcp packet catch %d times continuous\nrecorded %sethhdr\nsrchw:%s--->dsthw:%s proto:%xHiphdr\nver:%d ihl:%d tos:%d tot_len:%d id:%d df:%d mf:%d fragoff:%d TTL:%d proto:%d\nsrcaddr:%s dstaddr:%stcphdr\nsrcport:%d dstport:%d seq:%u ack:%u off:%d flag:%s win:%d-----------------------------------------------------------\n",counter+1,ctime(&temptime),         hwaddr(eth->h_source,buf1), hwaddr(eth->h_dest,buf2),         ethprotol,ip->version,ip->ihl,         ip->tos,ntohs(ip->tot_len), ntohs(ip->id),df,mf,         ipfragoff&IP_OFFMASK,ip->ttl,protol,srcaddr,dstaddr,         ntohs(tcp1->th_sport),ntohs(tcp1->th_dport),         ntohl(tcp1->th_seq),ntohl(tcp1->th_ack),tcp1->th_off,         flagsmes,ntohs(tcp1->th_win));         fwrite(writebuf,strlen(writebuf),1,fd);}void writeigmpbuf(FILE *fd,char *p,int counter){struct ethhdr *eth ;struct iphdr * ip;struct in_addr in1,in2;int ethprotol;int ihl,df,mf,ipfragoff;char * srcaddr,*dstaddr;int protol;char strptr[255];char buf1[20],buf2[20];struct igmp * igmp1;        eth=(struct ethhdr *)p;        ethprotol=ntohs(eth->h_proto);        ip = (struct iphdr *)(p+14);        ihl = (int)ip->ihl << 2;        in1.s_addr = ip->saddr;        in2.s_addr = ip->daddr;        srcaddr=inet_ntoa(in1);        dstaddr=(char *)inet_ntop(AF_INET,&in2,strptr,sizeof(strptr));        //iphdr        protol=ip->protocol;        ipfragoff=ntohs(ip->frag_off);        df=ipfragoff&IP_DF;mf=ipfragoff&IP_MF;        if (df!=0) df=1;        if (mf!=0) mf=1;        protol=ip->protocol;igmp1=(struct igmp *)(p+14+ihl);temptime=time(NULL);            snprintf(writebuf,PACKET_SIZE,"This packet catch %d times continuous\nrecorded %sethhdr\nsrchw:%s--->dsthw:%s proto:%xHiphdr\nver:%d ihl:%d tos:%d tot_len:%d id:%d df:%d mf:%d fragoff:%d TTL:%d proto:%d\nsrcaddr:%s dstaddr:%sigmphdr\ntype:%d code:%d group_addr:%s-----------------------------------------------------------\n",counter+1,ctime(&temptime),         hwaddr(eth->h_source,buf1), hwaddr(eth->h_dest,buf2),         ethprotol,ip->version,ip->ihl,         ip->tos,ntohs(ip->tot_len), ntohs(ip->id),df,mf,         ipfragoff&IP_OFFMASK,ip->ttl,protol,srcaddr,dstaddr,        igmp1->igmp_type,igmp1->igmp_code,inet_ntoa(igmp1->igmp_group));         fwrite(writebuf,strlen(writebuf),1,fd);}void writeudpbuf(FILE * fd,char *p,int counter){struct ethhdr *eth ;struct iphdr * ip;struct in_addr in1,in2;int ethprotol;int ihl,df,mf,ipfragoff;char * srcaddr,*dstaddr;int protol;char strptr[255];char buf1[20],buf2[20];struct udphdr *udp;eth=(struct ethhdr *)p;        ethprotol=ntohs(eth->h_proto);        ip = (struct iphdr *)(p+14);        ihl = (int)ip->ihl << 2;        in1.s_addr = ip->saddr;        in2.s_addr = ip->daddr;        srcaddr=inet_ntoa(in1);        dstaddr=(char *)inet_ntop(AF_INET,&in2,strptr,sizeof(strptr));        //iphdr        protol=ip->protocol;        ipfragoff=ntohs(ip->frag_off);        df=ipfragoff&IP_DF;        mf=ipfragoff&IP_MF;        if (df!=0) df=1;        if (mf!=0) mf=1;        protol=ip->protocol;        udp= (struct udphdr *)(p+14 + ihl);temptime=time(NULL);        snprintf(writebuf,PACKET_SIZE,"This udp packet catch %d times continuous\nrecorded %sethhdr\nsrchw:%s--->dsthw:%s proto:%xHiphdr\nver:%d ihl:%d tos:%d tot_len:%d id:%d df:%d mf:%d fragoff:%d TTL:%d proto:%d\nsrcaddr:%s dstaddr:%sudphdr\nsport:%d dsport:%d len:%d-----------------------------------------------------------\n",counter+1,ctime(&temptime),        hwaddr(eth->h_source,buf1), hwaddr(eth->h_dest,buf2),        ethprotol,ip->version,ip->ihl,        ip->tos,ntohs(ip->tot_len), ntohs(ip->id),df,mf,        ipfragoff&IP_OFFMASK,ip->ttl,protol,srcaddr,dstaddr,        ntohs(udp->uh_sport), ntohs(udp->uh_dport),ntohs(udp->uh_ulen));        fwrite(writebuf,strlen(writebuf),1,fd);}void writeicmpbuf(FILE * fd,char *p,int counter) {struct ethhdr *eth ;struct iphdr * ip;struct in_addr in1,in2;int ethprotol;int ihl,df,mf,ipfragoff;char * srcaddr,*dstaddr;int protol;char strptr[255];char buf1[20],buf2[20];struct icmp *icmphdr;     eth=(struct ethhdr *)p;        ethprotol=ntohs(eth->h_proto);        ip = (struct iphdr *)(p+14);        ihl = (int)ip->ihl << 2;        in1.s_addr = ip->saddr;        in2.s_addr = ip->daddr;        srcaddr=inet_ntoa(in1);        dstaddr=(char *)inet_ntop(AF_INET,&in2,strptr,sizeof(strptr));        //iphdr        protol=ip->protocol;        ipfragoff=ntohs(ip->frag_off);        df=ipfragoff&IP_DF;        mf=ipfragoff&IP_MF;        if (df!=0) df=1;        if (mf!=0) mf=1;        protol=ip->protocol;icmphdr=(struct icmp *)(p+14 + ihl);temptime=time(NULL);            snprintf(writebuf,PACKET_SIZE,"This icmp packet catch %d times continuous\nrecorded %sethhdr\nsrchw:%s--->dsthw:%s proto:%xHiphdr\nver:%d ihl:%d tos:%d tot_len:%d id:%d df:%d mf:%d fragoff:%d TTL:%d proto:%d\nsrcaddr:%s dstaddr:%sicmp\ntype:%d code:%d-----------------------------------------------------------\n",counter+1,ctime(&temptime),            hwaddr(eth->h_source,buf1), hwaddr(eth->h_dest,buf2),            ethprotol,ip->version,ip->ihl,            ip->tos,ntohs(ip->tot_len), ntohs(ip->id),df,mf,            ipfragoff&IP_OFFMASK,ip->ttl,protol,srcaddr,dstaddr,            icmphdr->icmp_type,icmphdr->icmp_code);            fwrite(writebuf,strlen(writebuf),1,fd);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?