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;
22 display=(t_display *)&(info->display);
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 display_line(display,0,y-3,x,y-3,'-');
76 snprintf(help,14,"total: %06d",info->count);
77 display_string(display,3,y-2,help,13);
78 snprintf(help,19,"management: %06d",info->count_m);
79 display_string(display,18,y-2,help,18);
80 snprintf(help,16,"control: %06d",info->count_c);
81 display_string(display,38,y-2,help,15);
82 snprintf(help,13,"data: %06d",info->count_d);
83 display_string(display,55,y-2,help,12);
85 display_line(display,0,y-1,x,y-1,'-');
88 blank=(char *)malloc(display->max_x);
89 memset(blank,0x20,display->max_x);
90 display_string(display,0,y,blank,x);
91 display_string(display,1,y,string,size);
95 display_draw(display);
100 int noop(t_input *input,void *ptr) {
105 int get_user_interaction(t_info *info) {
109 string=(char *)malloc(info->display.max_x*sizeof(char));
111 input_get_event(&(info->input),noop,info);
114 if(info->input.content[0]=='h') {
115 display_console(info,"hdw-sniff help:");
116 display_console(info,"h - print this help");
117 display_console(info,"x - enable/disable hex output");
118 display_console(info,"a - enable/disable ascii output");
119 display_console(info,"q - quit");
120 display_console(info,"console navigation: arrow up/down");
121 display_console(info,"network list navigation: page up/down");
125 if(info->input.content[0]=='x') {
126 info->mode^=MODE_HEXOUT;
127 display_console(info,"toggled hex output");
130 else if(info->input.content[0]=='a') {
131 info->mode^=MODE_ASCIIOUT;
132 display_console(info,"toggled ascii output");
135 else if(info->input.content[0]=='q') {
136 event_stop(&(info->event));
137 display_console(info,"quit!");
141 snprintf(string,info->display.max_x,"unknown event (%x)",
142 info->input.content[0]);
143 display_console(info,string);
149 int react_on_event(t_event *event,void *ptr) {
155 if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
156 else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
162 puts("usage: hdw-sniff <options>");
164 puts("\t\t-m <mode> \tmonitor and/or wlanng");
165 puts("\t\t-d <device> \twlan0,eth0");
166 puts("\t\t-l <logfile>");
167 puts("\t\t-k <key> \t(string)");
168 puts("\t\t-D <file> \t(dump packages to file)");
169 puts("\t\t-h \tdisplay this help message");
175 int hop_channel(t_event *event,void *ptr) {
182 if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
183 info->current_channel=1;
184 memset(&iwreq,0,sizeof(iwreq));
185 strcpy(iwreq.ifr_name,info->device);
187 iwreq.u.freq.m=info->current_channel;
188 if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
189 puts("unable to hop channel");
193 ++(info->current_channel);
198 int main(int argc, char **argv) {
203 char sys_call[MAX_SYSCALL_CHARS];
204 char pcap_error[PCAP_ERRBUF_SIZE];
206 memset(&info,0,sizeof(t_info));
208 /* parse arguments */
209 for(i=1;i<argc;i++) {
210 if(argv[i][0]=='-') {
215 if(!strncmp(argv[i+1],"monitor",7)) {
216 info.mode|=MODE_MONITOR;
217 puts("will go to monitor mode.");
219 else if(!strncmp(argv[i+1],"wlanng",6)) {
220 info.mode|=MODE_WLANNG;
221 puts("expecting wlanng header in package.");
224 printf("unknown mode: %s\n",argv[1]);
230 if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT|O_APPEND))!=0)
231 printf("logfile -> %s\n",argv[i+1]);
233 puts("warning: can't write to logfile.");
237 if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
238 printf("dump file -> %s\n",argv[i+1]);
240 puts("warning: can't dump to file.");
244 strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
257 /* setting up device */
258 if(info.mode&MODE_MONITOR) {
259 sprintf(sys_call,"iwconfig %s mode monitor",info.device);
260 puts("set monitoring mode ...");
263 sprintf(sys_call,"/sbin/ifconfig %s up",info.device);
264 puts("setting up device ...");
268 if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT|O_APPEND))!=0)
269 puts("using logfile /tmp/hdw-sniff.log ...");
271 puts("failed to open logfile ...");
277 if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
279 printf("%s: %s\n",argv[0],pcap_error);
282 pcap_fd=pcap_fileno(info.pcap_handle);
283 /* -> non blocking? */
285 /* socket fd for channel hopping */
286 info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
288 display_init(&(info.display),info.log_fd);
289 if(info.display.max_x<HDW_SNIFF_MAX_WIDTH) {
290 display_shutdown(&(info.display));
293 puts("fatal! please increase your terminals width");
297 if(info.display.max_y<HDW_SNIFF_MAX_HEIGHT) {
298 display_shutdown(&(info.display));
301 puts("fatal! please increase your terminals width");
306 input_init(&(info.input),info.log_fd);
307 //input.mode=CONTENT_BUFFER;
308 input_ios_init(&info.input);
310 event_init(&(info.event),info.log_fd);
311 event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
313 event_math(0,&(info.event),READ,ADD);
314 event_math(pcap_fd,&(info.event),READ,ADD);
316 list_init(&(info.sniffed_sta),info.log_fd);
318 display_console(&info,
319 " Welcome to hdw-sniff ... :-D");
323 display_console(&info,
324 " Copyright (C) 2004/05 hackbard@hackdaworld.org (press any button)");
328 event_start(&(info.event),&info,react_on_event,hop_channel);
330 list_shutdown(&(info.sniffed_sta));
331 input_shutdown(&(info.input));
332 display_shutdown(&(info.display));
336 puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
338 puts("bugreports: hackbard@hackdaworld.org");