2 * main.c - main hdw-sniff
4 * Copyright (C) 2004/05 hackbard@hackdaworld.org
10 // void parse_package(unsigned char *ptr,const struct pcap_pkthdr *pcap_header,const unsigned char *package);
14 int display_console(t_info *info,char *string) {
16 int x,y,size,count,cnt,i,s;
21 display=(t_display *)&(info->display);
28 if(info->view==VIEW_ALL) {
29 count=list_count(&(info->sniffed_sta));
30 cnt=((count>y-5)?y-5:count);
32 display_line(display,0,0,x,0,'-');
33 display_string(display,GUI_OFFSET_STA,1,"sta/ap",6);
34 display_string(display,GUI_OFFSET_SSID,1,"| ssid",6);
35 display_string(display,GUI_OFFSET_AP,1,"| ap",4);
36 display_string(display,GUI_OFFSET_WEP,1,"| wep",5);
37 display_string(display,GUI_OFFSET_MGMT,1,"| mgmt",6);
38 display_string(display,GUI_OFFSET_CTRL,1,"| ctrl",6);
39 display_string(display,GUI_OFFSET_DATA,1,"| data",6);
40 display_string(display,GUI_OFFSET_ACTIVE,1,"| act",5);
41 display_line(display,0,2,x,2,'-');
43 list_reset(&(info->sniffed_sta));
46 sta=(t_sta *)info->sniffed_sta.current->data;
47 snprintf(help,32,"%02x:%02x:%02x:%02x:%02x:%02x",
48 sta->addr[0],sta->addr[1],sta->addr[2],
49 sta->addr[3],sta->addr[4],sta->addr[5]);
50 display_string(display,GUI_OFFSET_STA,3+i,help,17);
53 help[0]='|'; help[1]=' ';
54 memcpy(&help[2],sta->ssid,s);
55 display_string(display,GUI_OFFSET_SSID,3+i,help,s+2);
56 snprintf(help,4,"| %c",sta->ap?'y':'n');
57 display_string(display,GUI_OFFSET_AP,3+i,help,3);
58 snprintf(help,5,"| %c",sta->wep?'y':'n');
59 display_string(display,GUI_OFFSET_WEP,3+i,help,4);
61 snprintf(help,21,"| %04d | %04d | %04d",
62 sta->count_mgmt,sta->count_ctrl,
65 snprintf(help,21,"| %04d | n.a. | n.a.",
67 display_string(display,GUI_OFFSET_MGMT,3+i,help,20);
68 snprintf(help,5,"| %c",sta->active);
69 display_string(display,GUI_OFFSET_ACTIVE,3+i,help,4);
70 list_next(&(info->sniffed_sta));
74 else if(info->view==VIEW_SINGLE) {
75 list_reset(&(info->sniffed_sta));
76 for(i=0;i<info->c_line;i++) list_next(&(info->sniffed_sta));
77 sta=(t_sta *)info->sniffed_sta.current->data;
78 display_line(display,0,0,x,0,'-');
79 snprintf(help,22,"sta %02x:%02x:%02x:%02x:%02x:%02x",
80 sta->addr[0],sta->addr[1],sta->addr[2],
81 sta->addr[3],sta->addr[4],sta->addr[5]);
82 display_string(display,1,1,help,21);
83 display_line(display,0,2,x,2,'-');
84 display_string(display,1,3,info->clean_line,x);
85 display_string(display,1,3,"ssid: ",6);
87 memcpy(help,sta->ssid,s);
88 display_string(display,7,3,help,s);
89 display_string(display,1,5,"more specific info soon ...",27);
93 display_line(display,0,y-3,x,y-3,'-');
95 snprintf(help,14,"total: %06d",info->count);
96 display_string(display,1,y-2,help,13);
97 snprintf(help,13,"mgmt: %06d",info->count_m);
98 display_string(display,15,y-2,help,12);
99 snprintf(help,12,"ctrl: %06d",info->count_c);
100 display_string(display,28,y-2,help,11);
101 snprintf(help,13,"data: %06d",info->count_d);
102 display_string(display,40,y-2,help,12);
104 display_line(display,0,y-1,x,y-1,'-');
107 display_string(display,0,y,info->clean_line,x);
108 display_string(display,1,y,string,size);
111 display_draw(display);
113 if(info->view==VIEW_ALL)
114 display_set_cursor(display,0,3+info->c_line);
120 int noop(t_input *input,void *ptr) {
125 int get_user_interaction(t_info *info) {
130 string=(char *)malloc(info->display.max_x*sizeof(char));
132 input_get_event(&(info->input),noop,info);
135 if(info->input.content[0]=='h') {
136 display_console(info,"hdw-sniff help:");
137 display_console(info,"h - print this help");
138 display_console(info,"x - enable/disable hex output");
139 display_console(info,"a - enable/disable ascii output");
140 display_console(info,"q - quit");
141 display_console(info,"console navigation: arrow up/down");
142 display_console(info,"network list navigation: page up/down");
146 if(info->input.content[0]=='x') {
147 info->mode^=MODE_HEXOUT;
148 display_console(info,"toggled hex output");
151 else if(info->input.content[0]=='a') {
152 info->mode^=MODE_ASCIIOUT;
153 display_console(info,"toggled ascii output");
156 else if(info->input.content[0]=='q') {
157 event_stop(&(info->event));
158 display_console(info,"quit!");
160 else if(info->input.content[0]=='n') {
161 hlp=list_count(&(info->sniffed_sta));
162 if(info->c_line<hlp-1) info->c_line++;
164 else if(info->input.content[0]=='p') {
165 if(info->c_line>0) info->c_line--;
167 else if(info->input.content[0]==0x0d) {
168 display_clear_screen(&(info->display));
169 if(info->view==VIEW_ALL) info->view=VIEW_SINGLE;
170 else info->view=VIEW_ALL;
173 snprintf(string,info->display.max_x,"unknown event (%x)",
174 info->input.content[0]);
175 display_console(info,string);
181 int react_on_event(t_event *event,void *ptr) {
187 if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
188 else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
194 puts("usage: hdw-sniff <options>");
196 puts("\t\t-m <mode> \tmonitor and/or wlanng");
197 puts("\t\t-d <device> \twlan0,eth0");
198 puts("\t\t-l <logfile>");
199 puts("\t\t-k <key> \t(string)");
200 puts("\t\t-D <file> \t(dump packages to file)");
201 puts("\t\t-h \tdisplay this help message");
207 int hop_channel(t_event *event,void *ptr) {
214 if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
215 info->current_channel=1;
216 memset(&iwreq,0,sizeof(iwreq));
217 strcpy(iwreq.ifr_name,info->device);
219 iwreq.u.freq.m=info->current_channel;
220 if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
221 puts("unable to hop channel");
225 ++(info->current_channel);
230 int main(int argc, char **argv) {
235 char sys_call[MAX_SYSCALL_CHARS];
236 char pcap_error[PCAP_ERRBUF_SIZE];
238 memset(&info,0,sizeof(t_info));
240 /* parse arguments */
241 for(i=1;i<argc;i++) {
242 if(argv[i][0]=='-') {
247 if(!strncmp(argv[i+1],"monitor",7)) {
248 info.mode|=MODE_MONITOR;
249 puts("will go to monitor mode.");
251 else if(!strncmp(argv[i+1],"wlanng",6)) {
252 info.mode|=MODE_WLANNG;
253 puts("expecting wlanng header in package.");
256 printf("unknown mode: %s\n",argv[1]);
262 if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT|O_APPEND))!=0)
263 printf("logfile -> %s\n",argv[i+1]);
265 puts("warning: can't write to logfile.");
269 if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
270 printf("dump file -> %s\n",argv[i+1]);
272 puts("warning: can't dump to file.");
276 strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
289 /* setting up device */
290 if(info.mode&MODE_MONITOR) {
291 sprintf(sys_call,"iwconfig %s mode monitor",info.device);
292 puts("set monitoring mode ...");
295 sprintf(sys_call,"ifconfig %s up",info.device);
296 puts("setting up device ...");
300 if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT|O_APPEND))!=0)
301 puts("using logfile /tmp/hdw-sniff.log ...");
303 puts("failed to open logfile ...");
309 if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
311 printf("%s: %s\n",argv[0],pcap_error);
314 pcap_fd=pcap_fileno(info.pcap_handle);
315 /* -> non blocking? */
317 /* socket fd for channel hopping */
318 info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
320 display_init(&(info.display),info.log_fd);
321 if(info.display.max_x<HDW_SNIFF_MAX_WIDTH) {
322 display_shutdown(&(info.display));
325 puts("fatal! please increase your terminals width");
329 if(info.display.max_y<HDW_SNIFF_MAX_HEIGHT) {
330 display_shutdown(&(info.display));
333 puts("fatal! please increase your terminals width");
337 info.clean_line=(char *)malloc(info.display.max_x);
338 memset(info.clean_line,0x20,info.display.max_x);
341 display_console(&info,
342 "Welcome to hdw-sniff, Copyright (C) 2004/05 hackbard");
345 input_init(&(info.input),info.log_fd);
346 input_ios_init(&info.input);
348 list_init(&(info.sniffed_sta),info.log_fd);
350 event_init(&(info.event),info.log_fd);
351 event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
353 event_math(0,&(info.event),READ,ADD);
354 event_math(pcap_fd,&(info.event),READ,ADD);
356 event_start(&(info.event),&info,react_on_event,hop_channel);
358 list_shutdown(&(info.sniffed_sta));
359 input_shutdown(&(info.input));
360 display_shutdown(&(info.display));
364 puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
366 puts("bugreports: hackbard@hackdaworld.org");