File: sniff.c

package info (click to toggle)
nast 0.2.0-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,824 kB
  • sloc: ansic: 7,516; sh: 2,768; makefile: 63
file content (224 lines) | stat: -rw-r--r-- 5,756 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
    nast

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include "include/nast.h"

/* prototypes */
void sniff (int d, int x, FILE *output, FILE *ldd);
int run_sniffer (u_short promisc, u_short data, u_short hex, u_short f, u_short l, u_short tcpdlog, u_short tcpdread, char *filter, char *dev, char *ldname);

/* plugin to run sniffer */
int run_sniffer (u_short promisc, u_short data, u_short hex, u_short f, u_short l, u_short tcpdlog, u_short tcpdread, char *filter, char *dev, char *ldname)
{
   char *mask;
   char errbuf[PCAP_ERRBUF_SIZE];
   libnet_t *L;
   int ld;
   struct libnet_ether_addr *e;
   struct in_addr addr;
   /* log data FILE descriptor */
   FILE *ldd;

   ldd = NULL;
   ld = 0;
   npkt = 0;
   if (ldname != NULL) /* != NULL */
     (ld=1);

   /* ask pcap for the network address and mask of the device */
   if ((pcap_lookupnet(dev,&netp,&maskp,errbuf))==-1)
     {
	w_error(1, "pcap_lookupnet error: %s\n\n", errbuf);
     }

   if (tcpdlog) /* write in tcdl file in tcpdump log format */
     {
	if ((descr = pcap_open_live (dev, BUFSIZ, promisc, 10, errbuf))==NULL)
	  {
	     w_error(1, "pcap_open_live() error: %s\n\n",errbuf);
	  }

	if ((dumper = pcap_dump_open(descr,tcpdl))==NULL)
	  {
	     w_error(1, "pcap_dump_open() error: %s\n\n",errbuf);
	  }
     }

   else if(tcpdread) /* read from tcpdl file */
     {
	if ((descr = pcap_open_offline(tcpdl,errbuf))==NULL)
	  {
	     w_error(1, "pcap_open_offline() error: %s\n\n",errbuf);
	  }
     }
   /* normal case */
   else if ((descr = pcap_open_live (dev, BUFSIZ, promisc, 10, errbuf))==NULL)
     {
	w_error(1, "pcap_open_live() error: %s\n\n",errbuf);
     }

   if ((offset=(device(dev,descr)))==-1) return -1;
   
   L = libnet_init (LIBNET_LINK, dev, errbuf);

   e = libnet_get_hwaddr(L);
   if (!e)
     {
	w_error(1, "Can't get hardware address: %s\n\n", libnet_geterror(L));
     }

   addr.s_addr = maskp;
   if ((mask = inet_ntoa(addr))==NULL)
     {
	w_error(1, "Impossible get the mask\n\n");
     }

   if(!graph)
     {
	printf("%sSniffing on:\n\n%s", CYAN, NORMAL);
	printf("%s- Device:\t%s%s\n",BOLD, NORMAL, dev);
	printf("%s- MAC address:\t%s%s\n", BOLD, NORMAL, nast_hex_ntoa (e->ether_addr_octet));
	printf("%s- IP address:\t%s%s\n", BOLD, NORMAL, libnet_addr2name4(libnet_get_ipaddr4(L), 0));
	printf("%s- Netmask:\t%s%s\n",BOLD, NORMAL, mask);

	printf("%s- Promisc mode:\t%s", BOLD, NORMAL);
	if (promisc) printf("Set\n");
	else printf("Not set\n");

	printf ("%s- Filter:\t%s", BOLD, NORMAL);
	if (filter) printf("%s\n", filter);
	else printf("None\n");

	printf("%s- Logging:\t%s", BOLD, NORMAL);
	if (!ld && !l) printf ("None\n");
	else if (ld && !l) printf ("Sniffed data\n");
	else if (!ld && l) printf ("Traffic\n");
	else if (ld && l) printf ("Traffic and Sniffed data\n");
     }
   /* log all packets */
   if (l)
     {
	openfile();

	fprintf(logd, "NAST SNIFFER LOGGING REPORT\n");
	fprintf(logd, "Made on %s, device %s (%s)\n\n", timed, dev, libnet_addr2name4(libnet_get_ipaddr4(L), 0));
     }

   /* log only data */
   if (ld)
     {
	if ((ldd = (fopen(ldname,"w"))) == NULL)
	  {
	     w_error(1, "\nError: unable to open logfile descriptor: %s\n\n", strerror(errno));
	  }

	fprintf(ldd, "NAST SNIFFED DATA REPORT\n");
	fprintf(ldd, "Made on %s, device %s (%s)\n\n", timed, dev, libnet_addr2name4(libnet_get_ipaddr4(L), 0));
     }

   libnet_destroy(L);

   if (f)
     {
	if(pcap_compile(descr,&fp,filter,0,netp) == -1)
	  {
	     if(w_error(0, "Error in pcap_compile, insert a different filter\n")==-1)
	     	return(0);
	  }
	if(pcap_setfilter(descr,&fp) == -1)
	  {
	     w_error(1, "Error calling pcap_setfilter\n\n");
	  }

     }

   /* demonize only now */
   if (demonize)
     bkg();

   /* sniff here */
   while(1)
     {
	  (packet = (u_char *) pcap_next (descr, &hdr));
	  if(packet==NULL) 
		continue;

	     ++npkt;
	     
	     if (!ldname)
	        sniff (data, hex, stdout, NULL);
		
	     /* print to stdout and write to data file*/
	     /* this works also for tcpdump read file!! */
	     else
	       {
		  sniff (data, hex, stdout, ldd);        
		  fflush (ldd);
	       }

	     if (l)
	       {
		  sniff (data, hex, logd, NULL);          /* log packets to file */
		  fflush (logd);
	       }

	     if (tcpdlog)
	       {
	          fflush((FILE *)dumper);
	          pcap_dump((u_char *)dumper,&hdr,packet);  
	       }

     }

   if (l) 
     fclose (logd);
   if (ldname) 
     fclose (ldd);
   return 0;
}

/* sniffer function heandler */
void sniff (int d, int x, FILE *output, FILE *ldd)
{
   struct libnet_ipv4_hdr *ip;
   u_int16_t type;

   type = handle_ethernet (packet);

   if ((type==ETHERTYPE_ARP) || (type==ETHERTYPE_REVARP))
     handle_ARP(output);

   ip = (struct libnet_ipv4_hdr *) (packet+offset);
   switch(ip->ip_p)
     {
      case IPPROTO_TCP:
	handle_TCP (d, x, output, ldd);
	break;
      case IPPROTO_UDP:
	handle_UDP (d, x, output, ldd);
	break;
      case IPPROTO_ICMP:
	handle_ICMP (d, x, output, ldd);
	break;
      case IPPROTO_IGMP:
	handle_IGMP (output);
	break;
     }

}