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) {
20 display=(t_display *)&(info->display);
27 display_line(display,0,0,2,0,'-');
28 display_string(display,4,0,"sta",3);
29 display_line(display,8,0,12,0,'-');
31 display_string(display,13,0,"bssid",5);
32 display_line(display,19,0,23,0,'-');
34 display_string(display,25,0,"M",1);
35 display_line(display,27,0,x,0,'-');
37 //display_line(display,0,0,0,y,'|');
38 //display_line(display,x,0,x,y,'|');
41 display_line(display,0,y-3,x,y-3,'-');
44 snprintf(help,32,"total: %06d",info->count);
45 display_string(display,3,y-2,help,13);
48 snprintf(help,32,"management: %06d",info->count_m);
49 display_string(display,18,y-2,help,18);
52 snprintf(help,32,"control: %06d",info->count_c);
53 display_string(display,38,y-2,help,15);
56 snprintf(help,32,"data: %06d",info->count_d);
57 display_string(display,55,y-2,help,12);
59 display_line(display,0,y-1,x,y-1,'-');
61 display_draw(display);
66 int noop(t_input *input,void *ptr) {
71 int get_user_interaction(t_info *info) {
75 string=(char *)malloc(info->display.max_x*sizeof(char));
77 input_get_event(&(info->input),noop,info);
80 if(info->input.content[0]=='h') {
81 display_console(info,"hdw-sniff help:");
82 display_console(info,"h - print this help");
83 display_console(info,"x - enable/disable hex output");
84 display_console(info,"a - enable/disable ascii output");
85 display_console(info,"q - quit");
86 display_console(info,"console navigation: arrow up/down");
87 display_console(info,"network list navigation: page up/down");
91 if(info->input.content[0]=='x') {
92 info->mode^=MODE_HEXOUT;
93 display_console(info,"toggled hex output");
96 else if(info->input.content[0]=='a') {
97 info->mode^=MODE_ASCIIOUT;
98 display_console(info,"toggled ascii output");
101 else if(info->input.content[0]=='q') {
102 event_stop(&(info->event));
103 display_console(info,"quit!");
107 snprintf(string,info->display.max_x,"unknown event (%x)",
108 info->input.content[0]);
109 display_console(info,string);
115 int react_on_event(t_event *event,void *ptr) {
121 if(event_check(event,0)==E_FD_YES) get_user_interaction(info);
122 else pcap_dispatch(info->pcap_handle,-1,parse_package,(u_char *)ptr);
128 puts("usage: hdw-sniff <options>");
130 puts("\t\t-m <mode> \tmonitor and/or wlanng");
131 puts("\t\t-d <device> \twlan0,eth0");
132 puts("\t\t-l <logfile>");
133 puts("\t\t-k <key> \t(string)");
134 puts("\t\t-D <file> \t(dump packages to file)");
135 puts("\t\t-h \tdisplay this help message");
141 int hop_channel(t_event *event,void *ptr) {
148 if((info->current_channel>CHANNEL_MAX)|(info->current_channel==0))
149 info->current_channel=1;
150 memset(&iwreq,0,sizeof(iwreq));
151 strcpy(iwreq.ifr_name,info->device);
153 iwreq.u.freq.m=info->current_channel;
154 if(ioctl(info->channel_hop_fd,SIOCSIWFREQ,&iwreq)<0) {
155 puts("unable to hop channel");
159 ++(info->current_channel);
164 int main(int argc, char **argv) {
169 char sys_call[MAX_SYSCALL_CHARS];
170 char pcap_error[PCAP_ERRBUF_SIZE];
172 memset(&info,0,sizeof(t_info));
174 /* parse arguments */
175 for(i=1;i<argc;i++) {
176 if(argv[i][0]=='-') {
181 if(!strncmp(argv[i+1],"monitor",7)) {
182 info.mode|=MODE_MONITOR;
183 puts("will go to monitor mode.");
185 else if(!strncmp(argv[i+1],"wlanng",6)) {
186 info.mode|=MODE_WLANNG;
187 puts("expecting wlanng header in package.");
190 printf("unknown mode: %s\n",argv[1]);
196 if((info.log_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
197 printf("logfile -> %s\n",argv[i+1]);
199 puts("warning: can't write to logfile.");
203 if((info.dump_fd=open(argv[i+1],O_RDWR|O_CREAT))!=0)
204 printf("dump file -> %s\n",argv[i+1]);
206 puts("warning: can't dump to file.");
210 strncpy(info.device,argv[i+1],MAX_DEV_CHARS);
223 /* setting up device */
224 if(info.mode&MODE_MONITOR) {
225 sprintf(sys_call,"iwconfig %s mode monitor",info.device);
226 puts("set monitoring mode ...");
229 sprintf(sys_call,"/sbin/ifconfig %s up",info.device);
230 puts("setting up device ...");
234 if((info.log_fd=open("/tmp/hdw-sniff.log",O_RDWR|O_CREAT))!=0)
235 puts("using logfile /tmp/hdw-sniff.log ...");
237 puts("failed to open logfile ...");
243 if((info.pcap_handle=pcap_open_live(info.device,BUFSIZ,1,-1,pcap_error))==NULL)
245 printf("%s: %s\n",argv[0],pcap_error);
248 pcap_fd=pcap_fileno(info.pcap_handle);
249 /* -> non blocking? */
251 /* socket fd for channel hopping */
252 info.channel_hop_fd=socket(AF_INET,SOCK_DGRAM,0);
254 display_init(&(info.display),info.log_fd);
256 input_init(&(info.input),info.log_fd);
257 //input.mode=CONTENT_BUFFER;
258 input_ios_init(&info.input);
260 event_init(&(info.event),info.log_fd);
261 event_set_timeout(&(info.event),HOP_SEC,HOP_USEC);
263 event_math(0,&(info.event),READ,ADD);
264 event_math(pcap_fd,&(info.event),READ,ADD);
266 list_init(&(info.sniffed_sta),info.log_fd);
268 display_console(&info,"foo");
270 event_start(&(info.event),&info,react_on_event,hop_channel);
272 input_shutdown(&(info.input));
273 display_shutdown(&(info.display));
277 puts("thanks for using hdw-sniff (C) 2004/05 hackbard");
279 puts("bugreports: hackbard@hackdaworld.org");