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>
16 /* IEEE 802.3 stuff -- i will concentrate on .11 stuff before! */
17 #include <netinet/if_ether.h> /* for ethhdr struct */
18 #include <netinet/ip.h> /* ip */
19 #include <netinet/in.h> /* in_addr , inet_ntoa */
21 /* IEEE 802.11 stuff -- will become one include later ... */
22 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
23 #include "ieee802_11.h" /* from pcmcia-cs */
25 #include "hdw-sniff.h" /* my functions */
27 int main(int argc, char *argv[]) {
29 char pcap_error[PCAP_ERRBUF_SIZE];
33 struct info_struct my_info_struct;
35 /* parse the arguments */
37 printf("usage: %s <interface> <monitor mode> <logfile>\n",
42 printf("no logfile specified, writing to stdout ...\n");
45 if((logfile=fopen(argv[3],"w"))!=NULL) {
46 printf("writing to logfile %s ...\n",argv[3]);
49 printf("can't open logfile!\n");
53 /* setting up device and set monitor mode */
54 if(atoi(argv[2])==1) {
55 printf("setting to monitor mode\n");
56 if(strncmp(argv[1],"wlan",4)==0)
57 sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
58 if(strncmp(argv[1],"eth",3)==0)
59 sprintf(sys_call,"ifconfig %s promisc",argv[1]);
62 printf("setting up interface\n");
63 sprintf(sys_call,"ifconfig %s up",argv[1]);
66 /* start pcap session */
67 pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
68 if(pcap_handle==NULL) {
69 printf("%s: %s\n",argv[0],pcap_error);
73 /* grab a package until user breaks */
74 my_info_struct.count=0;
75 my_info_struct.mmode=argv[2][0];
76 strcpy(my_info_struct.dev,argv[1]);
78 pcap_loop(pcap_handle,-1,pcap_process,(u_char *)&my_info_struct);
83 /* pcap_process callback function */
84 void pcap_process(u_char *info,const struct pcap_pkthdr *pcap_header,
85 const u_char *package) {
90 struct ieee802_11_hdr *w_hdr;
91 struct info_struct *my_info_struct;
94 my_info_struct=(struct info_struct *)info;
95 ++(my_info_struct->count);
98 printf("---> package %d ---- %s",my_info_struct->count,
99 ctime((const time_t*)&(pcap_header->ts.tv_sec)));
100 printf("pcap header:\n");
101 printf("capture_length: %d (dec.)",pcap_header->caplen);
102 printf("length(off wire): %d (dec.)\n",pcap_header->len);
104 /* is ieee802.11 ? -- we assume yes :) */
105 if((my_info_struct->mmode-0x30==1) &&
106 (strncmp(my_info_struct->dev,"wlan",4)==0)) {
107 printf("ieee802.11 header:\n");
108 w_hdr=(struct ieee802_11_hdr *)package;
112 printf("%x -> ",1<<i);
113 printf("%x\n",(w_hdr->frame_ctl & 1<<i));
116 printf("frame_control: %x duration_id: %x\n",ntohs(w_hdr->frame_ctl),
117 ntohs(w_hdr->duration_id));
118 /* skip ieee802.11 header */
119 package=package+sizeof(struct ieee802_11_hdr);
124 e_hdr=(struct ethhdr *)package;
127 printf("%x ",ntohs(e_hdr->h_proto));
128 printf("dest_addr = ");
129 for(i=0;i<ETH_ALEN;i++)
130 printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
131 printf(" src_addr = ");
132 for(i=0;i<ETH_ALEN;i++)
133 printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
135 if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
136 printf("ip protocol: ");
137 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
138 printf("version = %x ",ntohs(ip_hdr->ip_v));
139 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
140 printf("service = %x ",ntohs(ip_hdr->ip_tos));
141 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
142 printf("ip_addresses: source = ");
143 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
144 inet_ntoa(ip_hdr->ip_dst));
145 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
146 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
147 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
148 ntohs(ip_hdr->ip_p));
149 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
151 printf("all_hex_dump:\n");
152 for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
153 printf("%x ",*(package+i));