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