2 * hdw-sniff, sniffer using pcap lib
4 * author: hackbard@hackdaworld.dyndns.org
10 /* IEEE 802.3 stuff -- i will concentrate on .11 stuff before! */
11 #include <netinet/if_ether.h> /* for ethhdr struct */
12 #include <netinet/ip.h> /* ip */
13 #include <netinet/in.h> /* in_addr , inet_ntoa */
15 /* IEEE 802.11 stuff -- will become one include later ... */
16 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
17 #include "ieee802_11.h" /* from pcmcia-cs */
19 #include "hdw-sniff.h" /* my functions */
21 int main(int argc, char *argv[]) {
23 char pcap_error[PCAP_ERRBUF_SIZE];
30 /* parse the arguments */
32 printf("usage: %s <interface> <monitor mode> <logfile>\n",
37 printf("no logfile specified, writing to stdout ...\n");
40 if((logfile=fopen(argv[3],"w"))!=NULL) {
41 printf("writing to logfile %s ...\n",argv[3]);
44 printf("can't open logfile!\n");
48 /* setting up device and set monitor mode */
49 if(atoi(argv[2])==1 && (strncmp(argv[1],"wlan",4)==0)) {
50 printf("setting to monitor mode 3\n");
51 sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
54 printf("setting up interface\n");
55 sprintf(sys_call,"ifconfig %s up",argv[1]);
58 /* start pcap session */
59 pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
60 if(pcap_handle==NULL) {
61 printf("%s: %s\n",argv[0],pcap_error);
65 /* grab a package until user breaks */
68 pcap_loop(pcap_handle,-1,pcap_process,(u_char *)p_count);
72 /* pcap_process callback function */
73 void pcap_process(u_char *count,const struct pcap_pkthdr *pcap_header,
74 const u_char *package) {
79 struct ieee802_11_hdr *w_hdr;
87 printf("---> package %d ---- %s",*p_count,
88 ctime((const time_t*)&(pcap_header->ts.tv_sec)));
89 printf("pcap header: ");
90 printf("capture length=%d ",pcap_header->caplen);
91 printf("length(off wire)=%d\n",pcap_header->len);
93 /* is ieee802.11 ? -- we assume yes :) */
94 if(atoi(argv[2])==1 && (strncmp(argv[1],"wlan",4)==0)) {
95 printf("debug: ieee802.11\n");
101 e_hdr=(struct ethhdr *)package;
104 printf("%x ",ntohs(e_hdr->h_proto));
105 printf("dest_addr = ");
106 for(i=0;i<ETH_ALEN;i++)
107 printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
108 printf(" src_addr = ");
109 for(i=0;i<ETH_ALEN;i++)
110 printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
112 if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
113 printf("ip protocol: ");
114 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
115 printf("version = %x ",ntohs(ip_hdr->ip_v));
116 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
117 printf("service = %x ",ntohs(ip_hdr->ip_tos));
118 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
119 printf("ip_addresses: source = ");
120 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
121 inet_ntoa(ip_hdr->ip_dst));
122 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
123 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
124 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
125 ntohs(ip_hdr->ip_p));
126 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
128 printf("all_hex_dump:\n");
129 for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
130 printf("%x ",*(package+i));