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: ");
101 printf("capture length=%d ",pcap_header->caplen);
102 printf("length(off wire)=%d\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("debug: ieee802.11\n");
113 e_hdr=(struct ethhdr *)package;
116 printf("%x ",ntohs(e_hdr->h_proto));
117 printf("dest_addr = ");
118 for(i=0;i<ETH_ALEN;i++)
119 printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
120 printf(" src_addr = ");
121 for(i=0;i<ETH_ALEN;i++)
122 printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
124 if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
125 printf("ip protocol: ");
126 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
127 printf("version = %x ",ntohs(ip_hdr->ip_v));
128 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
129 printf("service = %x ",ntohs(ip_hdr->ip_tos));
130 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
131 printf("ip_addresses: source = ");
132 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
133 inet_ntoa(ip_hdr->ip_dst));
134 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
135 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
136 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
137 ntohs(ip_hdr->ip_p));
138 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
140 printf("all_hex_dump:\n");
141 for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
142 printf("%x ",*(package+i));