2 * main.c - main hdw-sniff
4 * Copyright (C) 2004/05 hackbard@hackdaworld.org
12 int display_console(t_info *info,char *string) {
14 int x,y,size,count,cnt,i,s;
19 display=(t_display *)&(info->display);
26 if(info->view==VIEW_ALL) {
27 count=list_count(&(info->sniffed_sta));
28 cnt=((count>y-5)?y-5:count);
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,'-');
42 list_reset(&(info->sniffed_sta));
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);
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));
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)");
82 display_string(display,23,1,help,s);
84 strcpy(help,"wds link");
85 display_string(display,60,1,help,8);
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);
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");
99 display_string(display,x-s-1,3,help,s);
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);
108 snprintf(help,25,"bssid: %02x:%02x:%02x:%02x:%02x:%02x",
109 sta->bssid[0],sta->bssid[1],sta->bssid[2],
110 sta->bssid[3],sta->bssid[4],sta->bssid[5]);
111 display_string(display,1,9,help,24);
113 snprintf(help,67,"iv + keynr | crypted snap: %02x %02x %02x %02x | %02x %02x %02x %02x %02x %02x",
114 sta->snap[0],sta->snap[1],sta->snap[2],sta->snap[3],
115 sta->snap[4],sta->snap[5],sta->snap[6],sta->snap[7],
116 sta->snap[8],sta->snap[9]);
117 display_string(display,1,10,help,66);
122 display_line(display,0,y-3,x,y-3,'-');
124 snprintf(help,14,"total: %06d",info->count);
125 display_string(display,1,y-2,help,13);
126 snprintf(help,13,"mgmt: %06d",info->count_m);
127 display_string(display,15,y-2,help,12);
128 snprintf(help,12,"ctrl: %06d",info->count_c);
129 display_string(display,28,y-2,help,11);
130 snprintf(help,13,"data: %06d",info->count_d);
131 display_string(display,40,y-2,help,12);
133 display_line(display,0,y-1,x,y-1,'-');
136 display_string(display,0,y,info->clean_line,x);
137 display_string(display,1,y,string,size);
140 display_draw(display);
142 if(info->view==VIEW_ALL)
143 display_set_cursor(display,0,3+info->c_line);
149 int noop(t_input *input,void *ptr) {
154 int get_user_interaction(t_info *info) {
159 string=(char *)malloc(info->display.max_x*sizeof(char));
161 input_get_event(&(info->input),noop,info);
164 display_clear_screen(&(info->display));
166 if(info->input.content[0]=='x') {
167 info->mode^=MODE_HEXOUT;
168 display_console(info,"toggled hex output");
171 else if(info->input.content[0]=='a') {
172 info->mode^=MODE_ASCIIOUT;
173 display_console(info,"toggled ascii output");
176 else if(info->input.content[0]=='q') {
177 event_stop(&(info->event));
178 display_console(info,"quit!");
180 else if(info->input.content[0]=='n') {
181 hlp=list_count(&(info->sniffed_sta));
182 if(info->c_line<hlp-1) info->c_line++;
184 else if(info->input.content[0]=='p') {
185 if(info->c_line>0) info->c_line--;
187 else if(info->input.content[0]==0x0d) {
188 display_clear_screen(&(info->display));
189 if(info->view==VIEW_ALL) info->view=VIEW_SINGLE;
190 else info->view=VIEW_ALL;
193 snprintf(string,info->display.max_x,"unknown event (%x)",
194 info->input.content[0]);
195 display_console(info,string);
201 int react_on_event(t_event *event,void *ptr) {
207 if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
208 else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
214 puts("usage: hdw-sniff <options>");
216 puts("\t\t-m <mode> \tieee80211 or prism");
217 puts("\t\t-d <device> \twlan0,eth0");
218 puts("\t\t-l <logfile>");
219 //puts("\t\t-k <key> \t(string)");
220 puts("\t\t-D <file> \t(dump packages to file (pcap format))");
221 puts("\t\t-h \tdisplay this help message");
227 int hop_channel(t_event *event,void *ptr) {
234 if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
235 info->current_channel=1;
236 memset(&iwreq,0,sizeof(iwreq));
237 strcpy(iwreq.ifr_name,info->device);
239 iwreq.u.freq.m=info->current_channel;
240 if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
241 puts("unable to hop channel");
245 ++(info->current_channel);
250 int main(int argc, char **argv) {
255 char sys_call[MAX_SYSCALL_CHARS];
256 char pcap_error[PCAP_ERRBUF_SIZE];
258 memset(&info,0,sizeof(t_info));
260 /* parse arguments */
261 for(i=1;i<argc;i++) {
262 if(argv[i][0]=='-') {
267 if(!strncmp(argv[i+1],"ieee80211",9)) {
268 info.mode|=MODE_IEEE80211;
269 puts("expecting ieee802.11 header in package");
271 else if(!strncmp(argv[i+1],"prism",5)) {
272 info.mode|=MODE_PRISM;
273 puts("expecting prism header in package.");
276 printf("unknown mode: %s\n",argv[1]);
282 if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT|O_APPEND))!=0)
283 printf("logfile -> %s\n",argv[i+1]);
285 puts("warning: can't write to logfile.");
289 printf("dump file -> %s\n",argv[i+1]);
290 strncpy(info.dump_file,argv[i+1],128);
294 strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
307 /* setting up device */
308 if(info.mode&MODE_IEEE80211)
309 sprintf(sys_call,"iwpriv %s monitor 2",info.device);
310 else if(info.mode&MODE_PRISM)
311 sprintf(sys_call,"iwpriv %s monitor 3",info.device);
313 puts("set monitoring mode ...");
316 sprintf(sys_call,"ifconfig %s up",info.device);
317 puts("setting up device ...");
321 if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT|O_APPEND))!=0)
322 puts("using logfile /tmp/hdw-sniff.log ...");
324 puts("failed to open logfile ...");
330 if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
332 printf("%s: %s\n",argv[0],pcap_error);
335 pcap_fd=pcap_fileno(info.pcap_handle);
336 /* -> non blocking? */
339 if(strcmp(info.dump_file,"")) {
340 if((info.dump_handle=pcap_dump_open(info.pcap_handle,info.dump_file))==NULL) {
341 puts("pcap open dump file failed");
346 /* socket fd for channel hopping */
347 info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
349 display_init(&(info.display),info.log_fd);
350 if(info.display.max_x<HDW_SNIFF_MAX_WIDTH) {
351 display_shutdown(&(info.display));
354 puts("fatal! please increase your terminals width");
358 if(info.display.max_y<HDW_SNIFF_MAX_HEIGHT) {
359 display_shutdown(&(info.display));
362 puts("fatal! please increase your terminals width");
366 info.clean_line=(char *)malloc(info.display.max_x);
367 memset(info.clean_line,0x20,info.display.max_x);
370 display_console(&info,
371 "Welcome to hdw-sniff, Copyright (C) 2004/05 hackbard");
374 input_init(&(info.input),info.log_fd);
375 input_ios_init(&info.input);
377 list_init(&(info.sniffed_sta),info.log_fd);
379 event_init(&(info.event),info.log_fd);
380 event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
382 event_math(0,&(info.event),READ,ADD);
383 event_math(pcap_fd,&(info.event),READ,ADD);
385 event_start(&(info.event),&info,react_on_event,hop_channel);
387 list_shutdown(&(info.sniffed_sta));
388 input_shutdown(&(info.input));
389 display_shutdown(&(info.display));
391 if(info.dump_handle!=NULL) pcap_dump_close(info.dump_handle);
395 puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
397 puts("bugreports: hackbard@hackdaworld.org");