?!? wtf :)
[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;
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         FD_ZERO(&pcap_fd_set);
93         FD_SET(pcap_fd,&pcap_fd_set);
94         fd_set_tv.tv_sec=0;
95         fd_set_tv.tv_usec=500000;
96
97         /* do loopp */
98         while(1) {
99                 if((select(pcap_fd+1,&pcap_fd_set,NULL,NULL,&fd_set_tv)) && (FD_ISSET(pcap_fd,&pcap_fd_set))) pcap_loop(pcap_handle,-1,pcap_process,(u_char *)&my_info_struct);
100                 else if((hop_channel(&my_info_struct,pcap_fd))==-1) { 
101                         printf("channelhopping failed, aborting\n");
102                         return -1;
103                 }
104         }
105         return 0;
106 }
107
108
109 /* pcap_process callback function */
110 void pcap_process(u_char *info,const struct pcap_pkthdr *pcap_header,
111                         const u_char *package) {
112         
113         /* local variables */
114         struct linux_wlan_ng_prism_hdr *prism_hdr;
115         struct ieee802_11_hdr *w_hdr;
116         struct ip *ip_hdr;
117         struct ethhdr *e_hdr;
118         struct info_struct *my_info_struct;
119         int i;
120
121         my_info_struct=(struct info_struct *)info;
122         ++(my_info_struct->count);
123
124         printf("\n");
125         printf("---> package %d ---- %s",my_info_struct->count,
126                         ctime((const time_t*)&(pcap_header->ts.tv_sec)));
127         printf("pcap header:\n");
128         printf("capture_length: %d (dec.)\t",pcap_header->caplen);
129         printf("length(off wire): %d (dec.)\n",pcap_header->len);
130         
131         /* prism wlan ng headers */
132         if((my_info_struct->mmode-0x30==1) && 
133                 (strncmp(my_info_struct->dev,"wlan",4)==0)) {
134         printf("prism header:\n");
135         prism_hdr=(struct linux_wlan_ng_prism_hdr *)package;
136         printf("message code/length: %d/%d\n",ntohs(prism_hdr->msgcode),
137                                                 ntohs(prism_hdr->msglen));
138         printf("device: %s\n",prism_hdr->devname);
139
140         /* ieee802.11 header */
141         printf("ieee802.11 header:\n");
142         package+=sizeof(struct linux_wlan_ng_prism_hdr);
143         w_hdr=(struct ieee802_11_hdr *)package;
144         
145         printf("debug:\n");
146         for(i=0;i<16;i++) {
147                 printf("%x -> ",1<<i);
148                 printf("%x\n",(ntohs(w_hdr->frame_ctl) & (1<<i)));
149         }
150         printf("frame_control: %x duration_id: %x\n",ntohs(w_hdr->frame_ctl),
151                                                 ntohs(w_hdr->duration_id));
152         printf("version check ... %s\n",
153         ((ntohs(w_hdr->frame_ctl) & IEEE802_11_FCTL_VERS)==0x00)?
154                                                         "ok":"unknown");
155         // printf("type: 
156         /* skip ieee802.11 header */
157         package=package+sizeof(struct ieee802_11_hdr);
158         }
159
160         /* ieee802.3 */
161         /* ethernet */
162         e_hdr=(struct ethhdr *)package;
163         /* what types ? */
164         printf("type = ");
165         printf("%x  ",ntohs(e_hdr->h_proto));
166         printf("dest_addr = ");
167         for(i=0;i<ETH_ALEN;i++)
168         printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
169         printf(" src_addr = ");
170         for(i=0;i<ETH_ALEN;i++)
171         printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
172                 /* IP ? */
173         if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
174                 printf("ip protocol: ");
175                 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
176                 printf("version = %x ",ntohs(ip_hdr->ip_v));
177                 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
178                 printf("service = %x ",ntohs(ip_hdr->ip_tos));
179                 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
180                 printf("ip_addresses: source = ");
181                 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
182                                                 inet_ntoa(ip_hdr->ip_dst));
183                 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
184                 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
185                 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
186                                                 ntohs(ip_hdr->ip_p));
187                 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
188         }
189         printf("all_hex_dump:\n");
190         for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
191                 printf("%x ",*(package+i));
192         printf("\n");
193 }
194
195 int hop_channel(struct info_struct *info,int pcap_fd) {
196         struct iwreq my_iwreq;
197         int foo_fd;
198         /* oder vielleicht doch pcap_fd ? */
199         if((foo_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
200                 printf("unable to create socket\n");
201                 return -1;
202         }
203
204         if (info->channel==15) info->channel=1;
205
206         memset(&my_iwreq,0,sizeof(my_iwreq));
207         strcpy(my_iwreq.ifr_name,info->dev);
208         printf("debug: device = %s\n",my_iwreq.ifr_name);
209         my_iwreq.u.freq.e=0;
210         my_iwreq.u.freq.m=info->channel;
211         // if((ioctl(foo_fd,SIOCSIWFREQ,&my_iwreq))==-1) {
212         if((ioctl(pcap_fd,SIOCSIWFREQ,&my_iwreq))==-1) {
213                 printf("unable to hop channels\n");
214                 perror("ioctl");
215                 return -1;
216         }
217         printf("debug: hopped to channel %d\n",info->channel);
218         ++(info->channel);
219         return 0;
220 }
221