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