📄 sniffer.c
字号:
#include <pcap.h> #include <stdio.h>#include<sys/time.h>void processPacket(u_char *arg,const struct pcap_pkthdr* pkthdr,const u_char * packet){int i=0, *counter=(int *)arg;printf("\nPacket count: %d",++(*counter));printf("\nRecived Packet size: %d",pkthdr->len);printf("\nPayLoad:\n");//hex_print(*packet,pkthdr->len);///*for(i=0;i<pkthdr->len;i++) { printf("%02x ",packet[i]); //if(isprint(packet[i])) // printf("%c",packet[i]); //else // printf("%d ",packet[i]); if((i%16==0&&i!=0)||i==pkthdr->len-1) printf("\n"); } //*/return ;} int main(int argc, char *argv[]) { pcap_t *handle; /* Session handle */ char *dev; /* The device to sniff on */ char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */ struct bpf_program fp; /* The compiled filter */ char filter_exp[] = "port 23"; /* The filter expression */ bpf_u_int32 mask; /* Our netmask */ bpf_u_int32 net; /* Our IP */ struct pcap_pkthdr header; /* The header that pcap gives us */ const u_char *packet; /* The actual packet */ int i=0,count=0; /* Define the device */ dev = pcap_lookupdev(errbuf); if (dev == NULL) { fprintf(stderr, "Couldn't find default device: %s\n", errbuf); return(2); } /* Find the properties for the device */ if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) { fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf); net = 0; mask = 0; } /* Open the session in promiscuous mode */ handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf); if (handle == NULL) { //fprintf(stderr, "Couldn't open device %s: %s\n", somedev, errbuf); return(2); }// /* Compile and apply the filter */ // if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) { // fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle)); // return(2); // } // if (pcap_setfilter(handle, &fp) == -1) { // fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle)); // return(2); // } /* Grab a packet */ //packet = pcap_next(handle, &header); /* Print its length */ //printf("\nJacked a packet "); //printf("\nTime stamp:%s",ctime(&header.ts)); //printf("\nLength of portion present:%d",header.caplen); //printf("\nLength this packet (off wire):%d\n",header.len); /* And close the session */ pcap_loop(handle,-1,processPacket,(u_char *)&count); pcap_close(handle); return(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -