2 * hdw-sniff, sniffer using pcap lib
4 * author: hackbard@hackdaworld.dyndns.org
10 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
16 /* IEEE 802.3 stuff -- i will concentrate on .11 stuff before! */
17 #include <netinet/if_ether.h> /* for ethhdr struct */
18 #include <netinet/ip.h> /* ip */
19 #include <netinet/in.h> /* in_addr , inet_ntoa */
21 /* IEEE 802.11 stuff -- will become one include later ... */
22 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
23 #include "ieee802_11.h" /* from pcmcia-cs */
25 #include "hdw-sniff.h" /* my functions */
27 int main(int argc, char *argv[]) {
29 char pcap_error[PCAP_ERRBUF_SIZE];
33 struct info_struct my_info_struct;
35 /* parse the arguments */
37 printf("usage: %s <interface> <monitor mode> <logfile>\n",
42 printf("no logfile specified, writing to stdout ...\n");
45 if((logfile=fopen(argv[3],"w"))!=NULL) {
46 printf("writing to logfile %s ...\n",argv[3]);
49 printf("can't open logfile!\n");
53 /* setting up device and set monitor mode */
54 if(atoi(argv[2])==1) {
55 printf("setting to monitor mode\n");
56 if(strncmp(argv[1],"wlan",4)==0)
57 sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
58 if(strncmp(argv[1],"eth",3)==0)
59 sprintf(sys_call,"ifconfig %s promisc",argv[1]);
62 printf("setting up interface\n");
63 sprintf(sys_call,"ifconfig %s up",argv[1]);
66 /* start pcap session */
67 pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
68 if(pcap_handle==NULL) {
69 printf("%s: %s\n",argv[0],pcap_error);
73 /* grab a package until user breaks */
74 my_info_struct.count=0;
75 my_info_struct.mmode=argv[2][0];
76 strcpy(my_info_struct.dev,argv[1]);
78 pcap_loop(pcap_handle,-1,pcap_process,(u_char *)&my_info_struct);
83 /* pcap_process callback function */
84 void pcap_process(u_char *info,const struct pcap_pkthdr *pcap_header,
85 const u_char *package) {
88 struct linux_wlan_ng_prism_hdr *prism_hdr;
89 struct ieee802_11_hdr *w_hdr;
92 struct info_struct *my_info_struct;
95 my_info_struct=(struct info_struct *)info;
96 ++(my_info_struct->count);
99 printf("---> package %d ---- %s",my_info_struct->count,
100 ctime((const time_t*)&(pcap_header->ts.tv_sec)));
101 printf("pcap header:\n");
102 printf("capture_length: %d (dec.)\t",pcap_header->caplen);
103 printf("length(off wire): %d (dec.)\n",pcap_header->len);
105 /* prism wlan ng headers */
106 if((my_info_struct->mmode-0x30==1) &&
107 (strncmp(my_info_struct->dev,"wlan",4)==0)) {
108 printf("prism header:\n");
109 prism_hdr=(struct linux_wlan_ng_prism_hdr *)package;
110 printf("message code/length: %d/%d\n",ntohs(prism_hdr->msgcode),
111 ntohs(prism_hdr->msglen));
112 printf("device: %s\n",prism_hdr->devname);
114 /* ieee802.11 header */
115 printf("ieee802.11 header:\n");
116 package+=sizeof(struct linux_wlan_ng_prism_hdr);
117 w_hdr=(struct ieee802_11_hdr *)package;
121 printf("%x -> ",1<<i);
122 printf("%x\n",(ntohs(w_hdr->frame_ctl) & (1<<i)));
124 printf("frame_control: %x duration_id: %x\n",ntohs(w_hdr->frame_ctl),
125 ntohs(w_hdr->duration_id));
126 printf("version check ... %s\n",
127 ((ntohs(w_hdr->frame_ctl) & IEEE802_11_FCTL_VERS)==0x00)?
130 /* skip ieee802.11 header */
131 package=package+sizeof(struct ieee802_11_hdr);
136 e_hdr=(struct ethhdr *)package;
139 printf("%x ",ntohs(e_hdr->h_proto));
140 printf("dest_addr = ");
141 for(i=0;i<ETH_ALEN;i++)
142 printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
143 printf(" src_addr = ");
144 for(i=0;i<ETH_ALEN;i++)
145 printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
147 if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
148 printf("ip protocol: ");
149 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
150 printf("version = %x ",ntohs(ip_hdr->ip_v));
151 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
152 printf("service = %x ",ntohs(ip_hdr->ip_tos));
153 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
154 printf("ip_addresses: source = ");
155 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
156 inet_ntoa(ip_hdr->ip_dst));
157 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
158 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
159 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
160 ntohs(ip_hdr->ip_p));
161 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
163 printf("all_hex_dump:\n");
164 for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
165 printf("%x ",*(package+i));