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