some more data filtering, fixed data/ctrl bug, some more special info output.
[my-code/hdw-sniff.git] / main.c
1 /*
2  * main.c - main hdw-sniff
3  *
4  * Copyright (C) 2004/05 hackbard@hackdaworld.org
5  *
6  */
7
8 #include "main.h"
9
10 /* functions */
11
12 int display_console(t_info *info,char *string) {
13
14   int x,y,size,count,cnt,i,s;
15   t_display *display;
16   t_sta *sta;
17   char help[32];
18
19   display=(t_display *)&(info->display);
20
21   x=display->max_x-1;
22   y=display->max_y-1;
23
24   size=strlen(string);
25
26   if(info->view==VIEW_ALL) {
27     count=list_count(&(info->sniffed_sta));
28     cnt=((count>y-5)?y-5:count);
29
30     display_line(display,0,0,x,0,'-');
31     display_string(display,GUI_OFFSET_STA,1,"sta/ap",6);
32     display_string(display,GUI_OFFSET_SSID,1,"| ssid",6);
33     display_string(display,GUI_OFFSET_AP,1,"| ap",4);
34     display_string(display,GUI_OFFSET_WEP,1,"| wep",5);
35     display_string(display,GUI_OFFSET_MGMT,1,"| mgmt",6);
36     display_string(display,GUI_OFFSET_CTRL,1,"| ctrl",6);
37     display_string(display,GUI_OFFSET_DATA,1,"| data",6);
38     display_string(display,GUI_OFFSET_ACTIVE,1,"| act",5);
39     display_string(display,GUI_OFFSET_SIGNAL,1,"| sq",4);
40     display_line(display,0,2,x,2,'-');
41
42     list_reset(&(info->sniffed_sta));
43
44     for(i=0;i<cnt;i++) {
45       sta=(t_sta *)info->sniffed_sta.current->data;
46       snprintf(help,32,"%02x:%02x:%02x:%02x:%02x:%02x",
47                sta->addr[0],sta->addr[1],sta->addr[2],
48                sta->addr[3],sta->addr[4],sta->addr[5]);
49       display_string(display,GUI_OFFSET_STA,3+i,help,17);
50       s=strlen(sta->ssid);
51       s=((s>=16)?16:s);
52       help[0]='|'; help[1]=' ';
53       memcpy(&help[2],sta->ssid,s);
54       display_string(display,GUI_OFFSET_SSID,3+i,help,s+2);
55       snprintf(help,4,"| %c",sta->ap?'y':'n');
56       display_string(display,GUI_OFFSET_AP,3+i,help,3);
57       snprintf(help,5,"|  %c",sta->wep?'y':'n');
58       display_string(display,GUI_OFFSET_WEP,3+i,help,4);
59       snprintf(help,21,"| %04d | n.a. | %04d",
60                sta->count_mgmt,sta->count_data);
61       display_string(display,GUI_OFFSET_MGMT,3+i,help,20);
62       snprintf(help,5,"|  %c",sta->active);
63       display_string(display,GUI_OFFSET_ACTIVE,3+i,help,4);
64       snprintf(help,5,"| %02u",sta->sq);
65       display_string(display,GUI_OFFSET_SIGNAL,3+i,help,4);
66       list_next(&(info->sniffed_sta));
67     }
68   }
69
70   else if(info->view==VIEW_SINGLE) {
71     list_reset(&(info->sniffed_sta));
72     for(i=0;i<info->c_line;i++) list_next(&(info->sniffed_sta));
73     sta=(t_sta *)info->sniffed_sta.current->data;
74     display_line(display,0,0,x,0,'-');
75     snprintf(help,22,"sta %02x:%02x:%02x:%02x:%02x:%02x",
76              sta->addr[0],sta->addr[1],sta->addr[2],
77              sta->addr[3],sta->addr[4],sta->addr[5]);
78     display_string(display,1,1,help,21);
79     if(sta->ap) strcpy(help,"(access point)");
80     else strcpy(help,"(station)");
81     s=strlen(help);
82     display_string(display,23,1,help,s);
83     if(sta->wds) {
84       strcpy(help,"wds link");
85       display_string(display,60,1,help,8);
86     }
87     snprintf(help,2,"%c",sta->active);
88     display_string(display,x-1,1,help,1);
89     snprintf(help,7,"sq: %02d",sta->sq);
90     display_string(display,x-8,1,help,6);
91     display_line(display,0,2,x,2,'-');
92     display_string(display,1,3,"ssid: ",6);
93     s=strlen(sta->ssid);
94     memcpy(help,sta->ssid,s);
95     display_string(display,7,3,help,s);
96     if(sta->wep) strcpy(help,"crypted");
97     else strcpy(help,"not crypted");
98     s=strlen(help);
99     display_string(display,x-s-1,3,help,s);
100
101     snprintf(help,13,"mgmt: %06d",sta->count_mgmt);
102     display_string(display,1,5,help,12);
103     snprintf(help,13,"ctrl: %06d",sta->count_ctrl);
104     display_string(display,1,6,help,12);
105     snprintf(help,13,"data: %06d",sta->count_data);
106     display_string(display,1,7,help,12);
107
108     snprintf(help,33,"ethernet snap: %02x %02x %02x %02x %02x %02x",
109              sta->snap[0],sta->snap[1],sta->snap[2],
110              sta->snap[3],sta->snap[4],sta->snap[5]);
111     display_string(display,1,9,help,32);
112     snprintf(help,25,"bssid: %02x:%02x:%02x:%02x:%02x:%02x",
113              sta->bssid[0],sta->bssid[1],sta->bssid[2],
114              sta->bssid[3],sta->bssid[4],sta->bssid[5]);
115     display_string(display,1,10,help,24);
116   }
117
118   /* footer */
119   display_line(display,0,y-3,x,y-3,'-');
120
121   snprintf(help,14,"total: %06d",info->count);
122   display_string(display,1,y-2,help,13);
123   snprintf(help,13,"mgmt: %06d",info->count_m);
124   display_string(display,15,y-2,help,12);
125   snprintf(help,12,"ctrl: %06d",info->count_c);
126   display_string(display,28,y-2,help,11);
127   snprintf(help,13,"data: %06d",info->count_d);
128   display_string(display,40,y-2,help,12);
129
130   display_line(display,0,y-1,x,y-1,'-');
131
132   if(size!=0) {
133     display_string(display,0,y,info->clean_line,x);
134     display_string(display,1,y,string,size);
135   }
136
137   display_draw(display);
138
139   if(info->view==VIEW_ALL)
140     display_set_cursor(display,0,3+info->c_line);
141
142
143   return 23;
144 }
145
146 int noop(t_input *input,void *ptr) {
147
148   return 23;
149 }
150
151 int get_user_interaction(t_info *info) {
152
153   char *string;
154   int hlp=0;
155
156   string=(char *)malloc(info->display.max_x*sizeof(char));
157
158   input_get_event(&(info->input),noop,info);
159
160   /* clear display */
161   display_clear_screen(&(info->display));
162
163   if(info->input.content[0]=='x') {
164     info->mode^=MODE_HEXOUT;
165     display_console(info,"toggled hex output");
166   }
167
168   else if(info->input.content[0]=='a') {
169     info->mode^=MODE_ASCIIOUT;
170     display_console(info,"toggled ascii output");
171   }
172
173   else if(info->input.content[0]=='q') {
174     event_stop(&(info->event));
175     display_console(info,"quit!");
176   }
177   else if(info->input.content[0]=='n') {
178     hlp=list_count(&(info->sniffed_sta)); 
179     if(info->c_line<hlp-1) info->c_line++;
180   }
181   else if(info->input.content[0]=='p') {
182     if(info->c_line>0) info->c_line--;
183   }
184   else if(info->input.content[0]==0x0d) {
185     display_clear_screen(&(info->display));
186     if(info->view==VIEW_ALL) info->view=VIEW_SINGLE;
187     else info->view=VIEW_ALL;
188   }
189   else {
190     snprintf(string,info->display.max_x,"unknown event (%x)",
191              info->input.content[0]);
192     display_console(info,string);
193   }
194
195   return 23;
196 }
197
198 int react_on_event(t_event *event,void *ptr) {
199
200   t_info *info;
201
202   info=(t_info *)ptr;
203
204   if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
205   else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
206
207   return 23;
208 }
209
210 int usage(void) {
211   puts("usage: hdw-sniff <options>");
212   puts("\toptions:");
213   puts("\t\t-m <mode> \tieee80211 or prism");
214   puts("\t\t-d <device> \twlan0,eth0");
215   puts("\t\t-l <logfile>");
216   puts("\t\t-k <key> \t(string)");
217   puts("\t\t-D <file> \t(dump packages to file)");
218   puts("\t\t-h \tdisplay this help message");
219   puts("");
220
221   return 23;
222 }
223
224 int hop_channel(t_event *event,void *ptr) {
225  
226   struct iwreq iwreq;
227   t_info *info;
228
229   info=(t_info *)ptr;
230
231   if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
232     info->current_channel=1;
233   memset(&iwreq,0,sizeof(iwreq));
234   strcpy(iwreq.ifr_name,info->device);
235   iwreq.u.freq.e=0;
236   iwreq.u.freq.m=info->current_channel;
237   if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
238     puts("unable to hop channel");
239     perror("ioctl");
240     return -23;
241   }
242   ++(info->current_channel);
243
244   return 23;
245 }
246
247 int main(int argc, char **argv) {
248
249   t_info info;
250   int pcap_fd;
251   int i;
252   char sys_call[MAX_SYSCALL_CHARS];
253   char pcap_error[PCAP_ERRBUF_SIZE];
254
255   memset(&info,0,sizeof(t_info));
256
257  /* parse arguments */
258  for(i=1;i<argc;i++) {
259   if(argv[i][0]=='-') {
260    switch(argv[i][1]) {
261     case 'h':
262      usage();
263     case 'm':
264      if(!strncmp(argv[i+1],"ieee80211",9)) {
265       info.mode|=MODE_IEEE80211;
266       puts("expecting ieee802.11 header in package");
267      }
268      else if(!strncmp(argv[i+1],"prism",5)) {
269       info.mode|=MODE_PRISM;
270       puts("expecting prism header in package.");
271      }
272      else {
273       printf("unknown mode: %s\n",argv[1]);
274       return -23;
275      }
276      ++i;
277      break;
278     case 'l':
279      if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT|O_APPEND))!=0)
280       printf("logfile -> %s\n",argv[i+1]);
281      else
282       puts("warning: can't write to logfile.");
283      ++i;
284      break;
285     case 'D':
286      if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
287       printf("dump file -> %s\n",argv[i+1]);
288      else
289       puts("warning: can't dump to file.");
290      ++i;
291      break;
292     case 'd':
293      strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
294      ++i;
295      break;
296     default:
297      usage();
298      return -23;
299    }
300   } else {
301    usage();
302    return -23;
303   }
304  }
305
306  /* setting up device */
307  if(info.mode&MODE_IEEE80211)
308   sprintf(sys_call,"iwpriv %s monitor 2",info.device);
309  else if(info.mode&MODE_PRISM)
310   sprintf(sys_call,"iwpriv %s monitor 3",info.device);
311
312  puts("set monitoring mode ...");
313  system(sys_call);
314  
315  sprintf(sys_call,"ifconfig %s up",info.device);
316  puts("setting up device ...");
317  system(sys_call);
318
319  if(info.log_fd==0) {
320    if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT|O_APPEND))!=0)
321      puts("using logfile /tmp/hdw-sniff.log ...");
322    else {
323      puts("failed to open logfile ...");
324      return -23;
325    }
326  }
327
328  /* pcap */
329  if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
330  {
331   printf("%s: %s\n",argv[0],pcap_error);
332   return -23;
333  }
334  pcap_fd=pcap_fileno(info.pcap_handle);
335  /* -> non blocking? */
336
337  /* socket fd for channel hopping */
338  info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
339
340  display_init(&(info.display),info.log_fd);
341  if(info.display.max_x<HDW_SNIFF_MAX_WIDTH) {
342    display_shutdown(&(info.display));
343    puts("");
344    puts("");
345    puts("fatal! please increase your terminals width");
346    puts("");
347    return -23;
348  }
349  if(info.display.max_y<HDW_SNIFF_MAX_HEIGHT) {
350    display_shutdown(&(info.display));
351    puts("");
352    puts("");
353    puts("fatal! please increase your terminals width");
354    puts("");
355    return -23;
356  }
357  info.clean_line=(char *)malloc(info.display.max_x);
358  memset(info.clean_line,0x20,info.display.max_x);
359  info.view=VIEW_ALL;
360
361  display_console(&info,
362    "Welcome to hdw-sniff, Copyright (C) 2004/05 hackbard");
363  sleep(2);
364
365  input_init(&(info.input),info.log_fd);
366  input_ios_init(&info.input);
367
368  list_init(&(info.sniffed_sta),info.log_fd);
369
370  event_init(&(info.event),info.log_fd);
371  event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
372
373  event_math(0,&(info.event),READ,ADD);
374  event_math(pcap_fd,&(info.event),READ,ADD);
375
376  event_start(&(info.event),&info,react_on_event,hop_channel);
377
378  list_shutdown(&(info.sniffed_sta));
379  input_shutdown(&(info.input));
380  display_shutdown(&(info.display));
381  
382  puts("");
383  puts("");
384  puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
385  puts("");
386  puts("bugreports: hackbard@hackdaworld.org");
387
388  return 23;
389 }