2 * hdw-sniff, sniffer using pcap lib
4 * author: hackbard@hackdaworld.dyndns.org
10 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
15 #include <sys/types.h>
17 #include <sys/ioctl.h>
19 /* IEEE 802.3 stuff -- i will concentrate on .11 stuff before! */
20 #include <netinet/if_ether.h> /* for ethhdr struct */
21 #include <netinet/ip.h> /* ip */
22 #include <netinet/in.h> /* in_addr , inet_ntoa */
24 /* IEEE 802.11 stuff -- will become one include later ... */
25 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
26 #include "ieee802_11.h" /* from pcmcia-cs */
28 #include "hdw-sniff.h" /* my functions */
30 int main(int argc, char *argv[]) {
32 char pcap_error[PCAP_ERRBUF_SIZE];
36 struct timeval fd_set_tv;
39 struct info_struct my_info_struct;
41 /* parse the arguments */
43 printf("usage: %s <interface> <monitor mode> <logfile>\n",
48 printf("no logfile specified, writing to stdout ...\n");
51 if((logfile=fopen(argv[3],"w"))!=NULL) {
52 printf("writing to logfile %s ...\n",argv[3]);
55 printf("can't open logfile!\n");
59 /* setting up device and set monitor mode */
60 if(atoi(argv[2])==1) {
61 printf("setting to monitor mode\n");
62 if(strncmp(argv[1],"wlan",4)==0)
63 sprintf(sys_call,"iwpriv %s monitor 2",argv[1]);
64 if(strncmp(argv[1],"eth",3)==0)
65 sprintf(sys_call,"ifconfig %s promisc",argv[1]);
68 printf("setting up interface\n");
69 sprintf(sys_call,"ifconfig %s up",argv[1]);
72 /* start pcap session */
73 pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
74 if(pcap_handle==NULL) {
75 printf("%s: %s\n",argv[0],pcap_error);
79 /* set non blocking */
80 if((pcap_setnonblock(pcap_handle,1,pcap_error))==-1) {
81 printf("%s: %s\n",argv[0],pcap_error);
85 /* grab a package until user breaks */
86 my_info_struct.count=0;
87 my_info_struct.mmode=argv[2][0];
88 strcpy(my_info_struct.dev,argv[1]);
90 /* prepare for select */
91 pcap_fd=pcap_fileno(pcap_handle);
93 /* create file descriptor */
94 if((foo_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
95 printf("unable to create socket foo_fd\n");
103 FD_ZERO(&pcap_fd_set);
104 FD_SET(pcap_fd,&pcap_fd_set);
106 fd_set_tv.tv_usec=200000;
108 if((select(pcap_fd+1,&pcap_fd_set,NULL,NULL,&fd_set_tv)) && (FD_ISSET(pcap_fd,&pcap_fd_set))) pcap_dispatch(pcap_handle,-1,pcap_process,(u_char *)&my_info_struct);
109 else if((hop_channel(&my_info_struct,foo_fd))==-1) {
110 printf("channelhopping failed, aborting\n");
118 /* pcap_process callback function */
119 void pcap_process(u_char *info,const struct pcap_pkthdr *pcap_header,
120 const u_char *package) {
122 /* local variables */
123 struct linux_wlan_ng_prism_hdr *prism_hdr;
124 struct ieee802_11_hdr *w_hdr;
125 struct ethhdr *e_hdr;
126 struct iphdr *ip_hdr;
127 struct info_struct *my_info_struct;
128 int i,p_o,w_o,e_o,i_o;
130 my_info_struct=(struct info_struct *)info;
131 ++(my_info_struct->count);
134 p_o=((strncmp(my_info_struct->dev,"wlan",4)==0)?sizeof(struct linux_wlan_ng_prism_hdr):0);
135 w_o=((strncmp(my_info_struct->dev,"wlan",4)==0)?sizeof(struct ieee802_11_hdr):0);
136 e_o=sizeof(struct ethhdr);
137 i_o=sizeof(struct iphdr);
140 printf("---> package %d ---- %s",my_info_struct->count,
141 ctime((const time_t*)&(pcap_header->ts.tv_sec)));
142 printf("pcap header:\n");
143 printf("capture_length: %d (dec.)\t",pcap_header->caplen);
144 printf("length(off wire): %d (dec.)\n",pcap_header->len);
146 /* prism wlan ng headers */
147 if((my_info_struct->mmode-0x30==1) &&
148 (strncmp(my_info_struct->dev,"wlan",4)==0)) {
149 printf("prism header: (%d bytes)\n",p_o);
150 prism_hdr=(struct linux_wlan_ng_prism_hdr *)package;
151 printf("message code/length: %d/%d\n",ntohs(prism_hdr->msgcode),
152 ntohs(prism_hdr->msglen));
153 printf("device: %s\n",prism_hdr->devname);
154 /* skip wlan_ng_prism header */
155 // package=package+p_o;
157 /* ieee802.11 header */
158 printf("ieee802.11 header: (%d bytes)\n",w_o);
159 w_hdr=(struct ieee802_11_hdr *)(package+p_o);
163 printf("%x -> ",1<<i);
164 printf("%x\n",(ntohs(w_hdr->frame_ctl) & (1<<i)));
166 printf("frame_control: %x duration_id: %x\n",ntohs(w_hdr->frame_ctl),
167 ntohs(w_hdr->duration_id));
168 printf("version check ... %s\n",
169 ((ntohs(w_hdr->frame_ctl) & IEEE802_11_FCTL_VERS)==0x00)?
172 /* skip ieee802.11 header */
173 // package=package+w_o;
178 printf("ethernet: (%d bytes)\n",e_o);
179 e_hdr=(struct ethhdr *)(package+p_o+w_o);
182 printf("%x ",ntohs(e_hdr->h_proto));
183 printf("dest_addr = ");
184 for(i=0;i<ETH_ALEN;i++)
185 printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
186 printf(" src_addr = ");
187 for(i=0;i<ETH_ALEN;i++)
188 printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
191 if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
192 printf("ip protocol: (%d bytes)\n",i_o);
193 ip_hdr=(struct iphdr *)(package+p_o+w_o+e_o);
194 printf("version = %x ",ntohs(ip_hdr->version));
195 printf("header_length = %x \n",ntohs(ip_hdr->ihl));
196 printf("service = %x ",ntohs(ip_hdr->tos));
197 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->tot_len));
198 printf("source_ip: ");
201 ip_hdr->saddr&0xff<<i,
204 printf("destination_ip: ");
207 ip_hdr->daddr&0xff<<i,
210 // printf("ip_addresses: source = ");
211 // printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->saddr),
212 // inet_ntoa(ip_hdr->daddr));
213 printf("ip_id = %x ",ntohs(ip_hdr->id));
214 printf("ip_offset = %x \n",ntohs(ip_hdr->frag_off));
215 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ttl),
216 ntohs(ip_hdr->protocol));
217 // printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
219 printf("all ethernet dump: (hex)\n");
220 for(i=p_o+w_o+e_o;i<pcap_header->caplen;i++)
221 printf("%x ",*(package+i));
223 printf("all ethernet dump: (char)\n");
224 for(i=p_o+w_o+e_o;i<pcap_header->caplen;i++)
225 printf("%c ",*(package+i));
229 int hop_channel(struct info_struct *info,int foo_fd) {
230 if((info->mmode-0x30==1) && (strncmp(info->dev,"wlan",4)==0)) {
232 struct iwreq my_iwreq;
234 if (info->channel>=14) info->channel=1;
236 memset(&my_iwreq,0,sizeof(my_iwreq));
237 strcpy(my_iwreq.ifr_name,info->dev);
238 printf("debug: channel = %d\n",info->channel);
240 my_iwreq.u.freq.m=info->channel;
241 if((ioctl(foo_fd,SIOCSIWFREQ,&my_iwreq))==-1) {
242 printf("unable to hop channels\n");