fc67686898c7166950aa6dd017d2fd048711e3c5
[my-code/hdw-sniff.git] / hdw-sniff.c
1 /*
2  * hdw-sniff, sniffer using pcap lib
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <sys/socket.h>
11 #include <time.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include <pcap.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <sys/ioctl.h>
18
19 /* IEEE 802.3 stuff -- i will concentrate on .11 stuff before! */
20 #include <netinet/if_ether.h>  /* for ethhdr struct */
21 #include <netinet/ip.h> /* ip */
22 #include <netinet/in.h> /* in_addr , inet_ntoa */
23
24 /* IEEE 802.11 stuff -- will become one include later ... */
25 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
26 #include "ieee802_11.h" /* from pcmcia-cs */
27
28 #include "hdw-sniff.h" /* my functions */
29
30 int main(int argc, char *argv[]) {
31
32         char pcap_error[PCAP_ERRBUF_SIZE];
33         pcap_t *pcap_handle;
34         int pcap_fd,foo_fd;
35         fd_set pcap_fd_set;
36         struct timeval fd_set_tv;
37         char sys_call[30];
38         FILE *logfile;
39         struct info_struct my_info_struct;
40         
41         /* parse the arguments */
42         if(argc<3) {
43                 printf("usage: %s <interface> <monitor mode> <logfile>\n",
44                         argv[0]);
45                 return 0;
46         }
47         if(argc!=4) {
48                 printf("no logfile specified, writing to stdout ...\n");
49         } 
50         else {
51                 if((logfile=fopen(argv[3],"w"))!=NULL) {
52                         printf("writing to logfile %s ...\n",argv[3]);
53                 }
54                 else {
55                         printf("can't open logfile!\n");
56                 }
57         }
58                         
59         /* setting up device and set monitor mode */
60         if(atoi(argv[2])==1) {
61                 printf("setting to monitor mode\n");
62                 if(strncmp(argv[1],"wlan",4)==0)
63                         sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
64                 if(strncmp(argv[1],"eth",3)==0)
65                         sprintf(sys_call,"ifconfig %s promisc",argv[1]);
66         system(sys_call);
67         }
68         printf("setting up interface\n");
69         sprintf(sys_call,"ifconfig %s up",argv[1]);
70         system(sys_call);
71
72         /* start pcap session */
73         pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
74         if(pcap_handle==NULL) {
75                 printf("%s: %s\n",argv[0],pcap_error);
76                 return 1;
77         }
78
79         /* set non blocking */
80         if((pcap_setnonblock(pcap_handle,1,pcap_error))==-1) {
81                 printf("%s: %s\n",argv[0],pcap_error);
82                 return 1;
83         }
84         
85         /* grab a package until user breaks */
86         my_info_struct.count=0;
87         my_info_struct.mmode=argv[2][0];
88         strcpy(my_info_struct.dev,argv[1]);
89
90         /* prepare for select */
91         pcap_fd=pcap_fileno(pcap_handle);
92
93         /* create file descriptor */
94         if((foo_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
95                 printf("unable to create socket foo_fd\n");
96                 return -1;
97         }
98
99         /* do loopp */
100         while (1) {
101
102                 /* set pcap_fd */
103                 FD_ZERO(&pcap_fd_set);
104                 FD_SET(pcap_fd,&pcap_fd_set);
105                 fd_set_tv.tv_sec=0;
106                 fd_set_tv.tv_usec=200000;
107
108                 if((select(pcap_fd+1,&pcap_fd_set,NULL,NULL,&fd_set_tv)) && (FD_ISSET(pcap_fd,&pcap_fd_set))) pcap_dispatch(pcap_handle,-1,pcap_process,(u_char *)&my_info_struct);
109                 else if((hop_channel(&my_info_struct,foo_fd))==-1) { 
110                         printf("channelhopping failed, aborting\n");
111                         return -1;
112                 }
113         }
114         return 0;
115 }
116
117
118 /* pcap_process callback function */
119 void pcap_process(u_char *info,const struct pcap_pkthdr *pcap_header,
120                         const u_char *package) {
121         
122         /* local variables */
123         char tmp_buf[20];
124         struct linux_wlan_ng_prism_hdr *prism_hdr;
125         struct ieee802_11_hdr *w_hdr;
126         struct ethhdr *e_hdr;
127         struct iphdr *ip_hdr;
128         struct info_struct *my_info_struct;
129         int i,p_o,w_o,e_o,i_o;
130         
131         my_info_struct=(struct info_struct *)info;
132         ++(my_info_struct->count);
133         
134         /* cache offsets */
135         p_o=((strncmp(my_info_struct->dev,"wlan",4)==0)?sizeof(struct linux_wlan_ng_prism_hdr):0);
136         w_o=((strncmp(my_info_struct->dev,"wlan",4)==0)?sizeof(struct ieee802_11_hdr):0);
137         e_o=sizeof(struct ethhdr);
138         i_o=sizeof(struct iphdr);
139
140         /* new package */
141         printf("\n");
142         printf("---> package %d ---- %s",my_info_struct->count,
143                         ctime((const time_t*)&(pcap_header->ts.tv_sec)));
144         
145         /* pcap header */
146         printf("pcap header: ");
147         printf("capture_length(dec): %d\t",pcap_header->caplen);
148         printf("off_wire_length(dec): %d\n",pcap_header->len);
149
150         /* wireless stuff */
151         /* prism wlan ng headers */
152         if((my_info_struct->mmode-0x30==1) && 
153                 (strncmp(my_info_struct->dev,"wlan",4)==0)) {
154         printf("prism header: (%d bytes)\n",p_o);
155         prism_hdr=(struct linux_wlan_ng_prism_hdr *)package;
156         printf("| message code/length: %d/%d |\t",prism_hdr->msgcode,
157                                                 prism_hdr->msglen);
158         printf("device: %s |\n",prism_hdr->devname);
159         /* ieee802.11 header */
160         printf("ieee802.11 header: (%d bytes)\n",w_o);
161         w_hdr=(struct ieee802_11_hdr *)(package+p_o);
162         
163         printf("fc: ");
164         for(i=0;i<16;i++)
165                 printf("%s%d%s",(i==0?"|":""),
166                                 (((w_hdr->frame_ctl) & (1<<i))>0?1:0),
167                                 (i==15?"|\n":"|"));
168         printf("    | v | t |  s-t  |t|f|m|r|p|m|w|o|\n");
169         /* frame type */
170         /* management */
171         if(!(w_hdr->frame_ctl & 0x0c)) {
172         if((w_hdr->frame_ctl & IEEE802_11_STYPE_ASSOC_REQ)>0)
173                 strcpy(tmp_buf,"association request");
174         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_ASSOC_RESP)>0)
175                 strcpy(tmp_buf,"association response");
176         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_REASSOC_REQ)>0)
177                 strcpy(tmp_buf,"reassociation request");
178         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_REASSOC_RESP)>0)
179                 strcpy(tmp_buf,"reassociation response");
180         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_PROBE_REQ)>0)
181                 strcpy(tmp_buf,"probe request");
182         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_PROBE_RESP)>0)
183                 strcpy(tmp_buf,"probe response");
184         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_BEACON)>0)
185                 strcpy(tmp_buf,"beacon");
186         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_ATIM)>0)
187                 strcpy(tmp_buf,"announcement traffic indication message");
188         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_DISASSOC)>0)
189                 strcpy(tmp_buf,"disassociation");
190         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_AUTH)>0)
191                 strcpy(tmp_buf,"authentification");
192         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_DEAUTH)>0)
193                 strcpy(tmp_buf,"deauthentification");
194         else strcpy(tmp_buf,"impossible situation \%) - go mail the author.");
195         }
196         else strcpy(tmp_buf,"control & monitor frame types not supported yet");
197         /* print out frame type */
198         printf("=> %s\n",tmp_buf);
199         
200         printf("duration/id: 0x%x\n",w_hdr->duration_id);
201         printf("version check ... %s\n",
202         ((w_hdr->frame_ctl & IEEE802_11_FCTL_VERS)==0x00)?
203                                                         "ok":"unknown");
204         }
205
206         /* ieee802.3 */
207         /* ethernet */
208         if((strncmp(my_info_struct->dev,"eth",3)==0) | 
209                 ((w_hdr->frame_ctl & IEEE802_11_FTYPE_DATA)>0)) {
210         printf("ethernet: (%d bytes)\n",e_o);
211         e_hdr=(struct ethhdr *)(package+p_o+w_o);
212         /* what types ? */
213         printf("type = ");
214         printf("%x  ",ntohs(e_hdr->h_proto));
215         printf("dest_addr = ");
216         for(i=0;i<ETH_ALEN;i++)
217         printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
218         printf(" src_addr = ");
219         for(i=0;i<ETH_ALEN;i++)
220         printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
221
222                 /* IP ? */
223         if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
224                 printf("ip protocol: (%d bytes)\n",i_o);
225                 ip_hdr=(struct iphdr *)(package+p_o+w_o+e_o);
226                 printf("version = %x ",ntohs(ip_hdr->version));
227                 printf("header_length = %x \n",ntohs(ip_hdr->ihl));
228                 printf("service = %x ",ntohs(ip_hdr->tos));
229                 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->tot_len));
230                 printf("source_ip: ");
231                 for(i=0;i<=3;++i) {
232                         printf("%d%s",
233                         (ip_hdr->saddr&(0xff<<(8*i)))>>(8*i),
234                         (i==3?"\n":"."));
235                 }
236                 printf("destination_ip: ");
237                 for(i=0;i<=3;++i) {
238                         printf("%d%s",
239                         (ip_hdr->daddr&(0xff<<(8*i)))>>(8*i),
240                         (i==3?"\n":"."));
241                 }
242                 printf("ip_id = %x ",ntohs(ip_hdr->id));
243                 printf("ip_offset = %x \n",ntohs(ip_hdr->frag_off));
244                 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ttl),
245                                                 ntohs(ip_hdr->protocol));
246                 // printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
247         }
248         }
249
250         /* check what we have ... */
251         printf("all dump: (hex)\n");
252         for(i=p_o+w_o;i<pcap_header->caplen;i++)
253                 printf("%x ",*(package+i));
254         printf("\n");
255         printf("all dump: (char)\n");
256         for(i=p_o+w_o;i<pcap_header->caplen;i++)
257                 printf("%c ",*(package+i));
258         printf("\n");
259 }
260
261 int hop_channel(struct info_struct *info,int foo_fd) {
262         if((info->mmode-0x30==1) && (strncmp(info->dev,"wlan",4)==0)) {
263
264         struct iwreq my_iwreq;
265
266         if (info->channel>=14) info->channel=1;
267
268         memset(&my_iwreq,0,sizeof(my_iwreq));
269         strcpy(my_iwreq.ifr_name,info->dev);
270         printf("debug: channel = %d\n",info->channel);
271         my_iwreq.u.freq.e=0;
272         my_iwreq.u.freq.m=info->channel;
273         if((ioctl(foo_fd,SIOCSIWFREQ,&my_iwreq))==-1) {
274                 printf("unable to hop channels\n");
275                 perror("ioctl");
276                 return -1;
277         }
278         ++(info->channel);
279         }
280         return 0;
281 }