dunno - ;)
[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                         dprintf(file_fd,"|iv - --|id|crypted_snap - --|\n");
58                 }
59                 else {
60                         printf("can't open logfile. not logged to file!\n");
61                 }
62         }
63                         
64         /* setting up device and set monitor mode */
65         if(atoi(argv[2])==1) {
66                 printf("setting to monitor mode\n");
67                 if(strncmp(argv[1],"wlan",4)==0)
68                         sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
69                 if(strncmp(argv[1],"eth",3)==0)
70                         sprintf(sys_call,"ifconfig %s promisc",argv[1]);
71         system(sys_call);
72         }
73         printf("setting up interface\n");
74         sprintf(sys_call,"ifconfig %s up",argv[1]);
75         system(sys_call);
76
77         /* start pcap session */
78         pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
79         if(pcap_handle==NULL) {
80                 printf("%s: %s\n",argv[0],pcap_error);
81                 return 1;
82         }
83
84         /* set non blocking */
85         if((pcap_setnonblock(pcap_handle,1,pcap_error))==-1) {
86                 printf("%s: %s\n",argv[0],pcap_error);
87                 return 1;
88         }
89         
90         /* set info struct */
91         my_info_struct.count=0;
92         my_info_struct.mmode=argv[2][0];
93         my_info_struct.ssid_list=(struct list *)malloc(sizeof(struct list));
94         my_info_struct.ssid_list->next=NULL;
95         // strcpy(my_info_struct.ssid_list->ssid,"test");
96         strcpy(my_info_struct.dev,argv[1]);
97
98         /* prepare for select */
99         pcap_fd=pcap_fileno(pcap_handle);
100
101         /* create file descriptor */
102         if((foo_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
103                 printf("unable to create socket foo_fd\n");
104                 return -1;
105         }
106
107         /* do loopp */
108         while (1) {
109
110                 /* set pcap_fd */
111                 FD_ZERO(&pcap_fd_set);
112                 FD_SET(pcap_fd,&pcap_fd_set);
113                 fd_set_tv.tv_sec=0;
114                 fd_set_tv.tv_usec=200000;
115
116                 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);
117                 else if((hop_channel(&my_info_struct,foo_fd))==-1) { 
118                         printf("channelhopping failed, aborting\n");
119                         return -1;
120                 }
121         }
122         return 0;
123 }
124
125
126 /* pcap_process callback function */
127 void pcap_process(u_char *info,const struct pcap_pkthdr *pcap_header,
128                         const u_char *package) {
129         
130         /* local variables */
131         char tmp_buf[20],tmp_buf1[10],tmp_buf2[32];
132         unsigned char crypted_snap[12];
133         struct linux_wlan_ng_prism_hdr *prism_hdr;
134         struct ieee802_11_hdr *w_hdr;
135         struct snaphdr *snap_hdr;
136         struct beacon_struct *beacon_hdr;
137         struct ethhdr *e_hdr;
138         struct info_struct *my_info_struct;
139         int i,p_o,w_o,e_o,i_o;
140         
141         my_info_struct=(struct info_struct *)info;
142         ++(my_info_struct->count);
143         
144         /* cache offsets */
145         p_o=((strncmp(my_info_struct->dev,"wlan",4)==0)?sizeof(struct linux_wlan_ng_prism_hdr):0);
146         w_o=((strncmp(my_info_struct->dev,"wlan",4)==0)?sizeof(struct ieee802_11_hdr):0);
147         e_o=sizeof(struct ethhdr);
148         i_o=sizeof(struct iphdr);
149
150         /* new package */
151         printf("\n");
152         printf("---> package %d ---- %s",my_info_struct->count,
153                         ctime((const time_t*)&(pcap_header->ts.tv_sec)));
154         
155         /* pcap header */
156         printf("pcap header: ");
157         printf("capture_length(dec): %d\t",pcap_header->caplen);
158         printf("off_wire_length(dec): %d\n",pcap_header->len);
159
160         /* wireless stuff */
161         /* prism wlan ng headers */
162         if((my_info_struct->mmode-0x30==1) &
163                 (strncmp(my_info_struct->dev,"wlan",4)==0)) {
164         printf("prism header: (%d bytes)\n",p_o);
165         prism_hdr=(struct linux_wlan_ng_prism_hdr *)package;
166         printf("| message code/length: %d/%d |\t",prism_hdr->msgcode,
167                                                 prism_hdr->msglen);
168         printf("device: %s |\n",prism_hdr->devname);
169         /* ieee802.11 header */
170
171         /* we need smaller w_hdr for non distributed frames */
172         if((w_hdr->frame_ctl & (1<<8)) & (w_hdr->frame_ctl & (1<<9))) {
173                 printf("=> distributed packet !!!!11\n");
174         } else w_o-=(sizeof(struct snaphdr)-sizeof(unsigned short));
175
176         printf("ieee802.11 header: (%d bytes)\n",w_o);
177         w_hdr=(struct ieee802_11_hdr *)(package+p_o);
178         
179         printf("fc: ");
180         for(i=0;i<16;i++)
181                 printf("%s%d%s",(i==0?"|":""),
182                                 (((w_hdr->frame_ctl) & (1<<i))>0?1:0),
183                                 (i==15?"|\n":"|"));
184         printf("    | v | t |  s-t  |t|f|m|r|p|m|w|o|\n");
185         /* frame type */
186         /* management */
187         if(!(w_hdr->frame_ctl & 0x0c)) {
188         tmp_buf1[0]='\0'; tmp_buf2[0]='\0';
189         if((w_hdr->frame_ctl & IEEE802_11_STYPE_ASSOC_REQ)>0)
190                 strcpy(tmp_buf,"association request");
191         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_ASSOC_RESP)>0)
192                 strcpy(tmp_buf,"association response");
193         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_REASSOC_REQ)>0)
194                 strcpy(tmp_buf,"reassociation request");
195         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_REASSOC_RESP)>0)
196                 strcpy(tmp_buf,"reassociation response");
197         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_PROBE_REQ)>0)
198                 strcpy(tmp_buf,"probe request");
199         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_PROBE_RESP)>0)
200                 strcpy(tmp_buf,"probe response");
201         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_BEACON)==IEEE802_11_STYPE_BEACON) {
202                 beacon_hdr=(struct beacon_struct *)(package+p_o+w_o);
203                 beacon_hdr->ssid_s.elementid==0?strcpy(tmp_buf1,"essid = ")
204                                                 :strcpy(tmp_buf1,"ibssid = ");
205                 strncpy(tmp_buf2,beacon_hdr->ssid_s.ssid,
206                                 beacon_hdr->ssid_s.length);
207                 tmp_buf2[beacon_hdr->ssid_s.length]='\0';
208                 strcpy(tmp_buf,"beacon");
209                 add_ssid_2_list(tmp_buf2,my_info_struct->ssid_list);
210         }
211         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_ATIM)>0)
212                 strcpy(tmp_buf,"announcement traffic indication message");
213         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_DISASSOC)>0)
214                 strcpy(tmp_buf,"disassociation");
215         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_AUTH)>0)
216                 strcpy(tmp_buf,"authentification");
217         else if((w_hdr->frame_ctl & IEEE802_11_STYPE_DEAUTH)>0)
218                 strcpy(tmp_buf,"deauthentification");
219         else strcpy(tmp_buf,"impossible situation \%) - go mail the author.");
220         }
221         else strcpy(tmp_buf,"control or data frame type");
222
223         printf("=> %s\n",tmp_buf);
224         if(strlen(tmp_buf1)>0 & strlen(tmp_buf2)>0) printf("   %s%s\n",
225                                                         tmp_buf1,
226                                                         tmp_buf2);
227         printf("duration/id: 0x%x\n",w_hdr->duration_id);
228         printf("version check ... %s\n",
229         ((w_hdr->frame_ctl & IEEE802_11_FCTL_VERS)==0x00)?
230                                                         "ok":"unknown");
231         }
232
233         /* ethernet */
234         if((strncmp(my_info_struct->dev,"eth",3)==0) | 
235         ((w_hdr->frame_ctl & IEEE802_11_FTYPE_DATA)==IEEE802_11_FTYPE_DATA)) {
236
237         if((strncmp(my_info_struct->dev,"eth",3)==0)) {
238                 printf("ethernet: (%d bytes)\n",e_o);
239                 e_hdr=(struct ethhdr *)(package+p_o+w_o);
240                 /* what types ? */
241                 printf("type = ");
242                 printf("%x  ",ntohs(e_hdr->h_proto));
243                 printf("dest_addr = ");
244                 for(i=0;i<ETH_ALEN;i++)
245                 printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
246                 printf(" src_addr = ");
247                 for(i=0;i<ETH_ALEN;i++) printf("%x%s",*(e_hdr->h_source+i),
248                                                 ((i==ETH_ALEN-1)?"\n":":"));
249                 if((ntohs(e_hdr->h_proto)==ETH_P_IP))
250                                         parse_ip(package+p_o+w_o+e_o);
251         } 
252         else {
253                 snap_hdr=(struct snaphdr *)(package+p_o+w_o);
254                 if((snap_hdr->snap[0]==0xaa) &
255                    (snap_hdr->snap[1]==0xaa) &
256                    (snap_hdr->snap[2]==0x03) &
257                    (snap_hdr->snap[3]==0x00) &
258                    (snap_hdr->snap[4]==0x00) &
259                    (snap_hdr->snap[5]==0x00)) {
260                         printf("- no encryption!\n");
261                         if(snap_hdr->proto==ntohs(ETH_P_IP)) {
262                                 e_o=sizeof(struct snaphdr);
263                                 parse_ip(snap_hdr+e_o);
264                         }
265
266                 }
267                 else {
268                         printf("- crypted packet!\n");
269                         /* print crypted snap - write into file */
270                         printf("snap: (iv(3) + index(1) + crypted snap(6)) ");
271                         for(i=0;i<10;i++) {
272                                 printf("%x ",*(snap_hdr->snap+i));
273                                 crypted_snap[i]=*(snap_hdr->snap+i);
274                         }
275                         /* xor with plain
276                         crypted_snap[4]^=0xaa;
277                         crypted_snap[5]^=0xaa;
278                         crypted_snap[6]^=0x03;
279                         crypted_snap[7]^=0x00;
280                         crypted_snap[8]^=0x00;
281                         crypted_snap[9]^=0x00;
282                         */
283
284                         printf("\n");
285                         crypted_snap[10]='\0';
286                         crypted_snap[11]='\n';
287                         
288                         if(file_fd>0) {
289                                 printf("debug: saved to file\n");
290                                 dprintf(file_fd,IVLINE,IVL_ARGS);
291                         }
292                 }
293         }
294         }
295
296         /* dump it */
297 #ifdef SHOW_HEX
298         printf("all dump: (hex)\n");
299         for(i=p_o+w_o;i<pcap_header->caplen;i++)
300                 printf("%x ",*(package+i));
301         printf("\n");
302 #endif
303 #ifdef DEBUG_CHAR
304         printf("all dump: (char)\n");
305         for(i=p_o+w_o;i<pcap_header->caplen;i++)
306                 printf("%c ",*(package+i));
307         printf("\n");
308 #endif
309 }
310
311 int hop_channel(struct info_struct *info,int foo_fd) {
312         if((info->mmode-0x30==1) && (strncmp(info->dev,"wlan",4)==0)) {
313
314         struct iwreq my_iwreq;
315
316         if (info->channel>=C_MAX) info->channel=1;
317
318         memset(&my_iwreq,0,sizeof(my_iwreq));
319         strcpy(my_iwreq.ifr_name,info->dev);
320         my_iwreq.u.freq.e=0;
321         my_iwreq.u.freq.m=info->channel;
322         if((ioctl(foo_fd,SIOCSIWFREQ,&my_iwreq))==-1) {
323                 printf("unable to hop channels\n");
324                 perror("ioctl");
325                 return -1;
326         }
327         ++(info->channel);
328         }
329         return 0;
330 }
331
332 int parse_ip(char *ip_o) {
333         struct iphdr *ip_hdr;
334         int i;
335
336         printf("ip protocol:\n");
337         ip_hdr=(struct iphdr *)ip_o;
338         printf("version = %x ",ntohs(ip_hdr->version));
339         printf("header_length = %x \n",ntohs(ip_hdr->ihl));
340         printf("service = %x ",ntohs(ip_hdr->tos));
341         printf("total_length(dec.) = %d \n",ntohs(ip_hdr->tot_len));
342         printf("source_ip: ");
343                 for(i=0;i<=3;++i) {
344                         printf("%d%s",
345                         (ip_hdr->saddr&(0xff<<(8*i)))>>(8*i),
346                         (i==3?"\n":"."));
347                 }
348         printf("destination_ip: ");
349         for(i=0;i<=3;++i) {
350                 printf("%d%s",
351                 (ip_hdr->daddr&(0xff<<(8*i)))>>(8*i),
352                 (i==3?"\n":"."));
353         }
354         printf("ip_id = %x ",ntohs(ip_hdr->id));
355         printf("ip_offset = %x \n",ntohs(ip_hdr->frag_off));
356         printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ttl),
357                                                 ntohs(ip_hdr->protocol));
358         // printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
359 }