2 * hdw-sniff, sniffer using pcap lib
4 * author: hackbard@hackdaworld.dyndns.org
10 /* IEEE 802.3 stuff -- i will concentrate on .11 stuff before! */
11 #include <netinet/if_ether.h> /* for ethhdr struct */
12 #include <netinet/ip.h> /* ip */
13 #include <netinet/in.h> /* in_addr */
15 /* IEEE 802.11 stuff -- will become one include later ... */
16 #include "ieee80211.h" /* from hunz's aeolus, short hostap_wlan.h */
17 #include "ieee802_11.h" /* from pcmcia-cs */
19 #include "hdw-sniff.h" /* my functions */
21 int main(int argc, char *argv[]) {
23 char pcap_error[PCAP_ERRBUF_SIZE];
30 /* parse the arguments */
32 printf("usage: %s <interface> <monitor mode> <logfile>\n",argv[0]);
36 printf("no logfile specified, writing to stdout ...\n");
39 if((logfile=fopen(argv[3],"w"))!=NULL) {
40 printf("writing to logfile %s ...\n",argv[3]);
43 printf("can't open logfile!\n");
47 /* setting up device and set monitor mode */
48 if(atoi(argv[2])==1 && (strncmp(argv[1],"wlan",4)==0)) {
49 printf("setting to monitor mode 3\n");
50 sprintf(sys_call,"iwpriv %s monitor 3",argv[1]);
53 printf("setting up interface\n");
54 sprintf(sys_call,"ifconfig %s up",argv[1]);
57 /* start pcap session */
58 pcap_handle=pcap_open_live(argv[1],BUFSIZ,1,-1,pcap_error);
59 if(pcap_handle==NULL) {
60 printf("%s: %s\n",argv[0],pcap_error);
64 /* grab a package until user breaks */
67 pcap_dispatch(pcap_handle,-1,(pcap_handler)pcap_process,
72 /* pcap_process callback function */
73 void pcap_process(u_char *count,const struct pcap_pkthdr *pcap_header,
74 const u_char *package) {
79 struct ieee802_11_hdr *w_hdr;
87 printf("---> package %d ---- %s",count,
88 ctime((const time_t*)pcap_header->ts.tv_sec));
89 printf("pcap header: ");
90 printf("capture length=%d ",pcap_header->caplen);
91 printf("length(off wire)=%d\n",pcap_header->len);
93 /* is ieee802.11 ? -- we assume yes :) */
99 e_hdr=(struct ethhdr *)package;
102 printf("%x ",ntohs(e_hdr->h_proto));
103 printf("dest_addr = ");
104 for(i=0;i<ETH_ALEN;i++)
105 printf("%x%s",*(e_hdr->h_dest+i),((i==ETH_ALEN-1)?" ":":"));
106 printf(" src_addr = ");
107 for(i=0;i<ETH_ALEN;i++)
108 printf("%x%s",*(e_hdr->h_source+i),((i==ETH_ALEN-1)?"\n":":"));
110 if(ntohs(e_hdr->h_proto)==ETH_P_IP) {
111 printf("ip protocol: ");
112 ip_hdr=(struct ip *)(package+sizeof(struct ethhdr));
113 printf("version = %x ",ntohs(ip_hdr->ip_v));
114 printf("header_length = %x \n",ntohs(ip_hdr->ip_hl));
115 printf("service = %x ",ntohs(ip_hdr->ip_tos));
116 printf("total_length(dec.) = %d \n",ntohs(ip_hdr->ip_len));
117 printf("ip_addresses: source = ");
118 printf("%s\tdestination = %s\n",inet_ntoa(ip_hdr->ip_src),
119 inet_ntoa(ip_hdr->ip_dst));
120 printf("ip_id = %x ",ntohs(ip_hdr->ip_id));
121 printf("ip_offset = %x \n",ntohs(ip_hdr->ip_off));
122 printf("time2live = %x ip_proto = %x\n",ntohs(ip_hdr->ip_ttl),
123 ntohs(ip_hdr->ip_p));
124 printf("chksum: %x\n",ntohs(ip_hdr->ip_sum));
128 printf("all_hex_dump:\n");
129 for(i=sizeof(struct ethhdr);i<pcap_header->caplen;i++)
130 printf("%x ",*(package+i));