File: common.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 (326 lines) | stat: -rw-r--r-- 6,845 bytes parent folder | download
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
    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.

*/

/*
   Common functions
                                    */

#include "include/nast.h"
#include <stdarg.h>

/* run complex plugins here */

/* r: reset a connection
 * s: follow tcp stream
 * M: multi_scanner
 * S: single port_scanner
 */

int runcplx (char what, char *dev, int l)
{
   u_long ip_dst, ip_src;
   u_short port_dst, port_src;
   libnet_plist_t plist, *plist_p;
   char buff[50];
   u_short i;
   libnet_t *L;
   char errbuf[PCAP_ERRBUF_SIZE];

   i = 0;
   if ((L = libnet_init (LIBNET_LINK, NULL, errbuf))==NULL)
     {
	w_error(1,"Error loading libnet core!\n");
     }

   switch (what)
     {
      case 'r':
      	/* don't touch here */
	error:
	puts ("Type connection extremes");
	puts ("------------------------");
	do
	  {
	     if (i) printf ("Cannot resolve input address, type again!\n");
	     printf ("1 ip / hostname : ");
	     fgets (buff, 50, stdin);
	     if ((ip_src = libnet_name2addr4(L, dn (buff), LIBNET_RESOLVE))==-1)
	       {
		  w_error(1,"Error: %s\n", libnet_geterror(L));
	       }
	     i ++;
	  }
	while (ip_src == -1);

	i = 0;
	printf ("1 port (0 to autodetect) : ");
	fgets (buff, 50, stdin);
	port_src = atoi(buff);
	do
	  {
	     if (i) printf ("Cannot resolve input address, type again!\n");
	     printf ("2 ip / hostname : ");
	     fgets (buff, 50, stdin);
	     if ((ip_dst = libnet_name2addr4(L, dn (buff), LIBNET_RESOLVE))==-1)
	       {
		  w_error(1, "Error: %s\n", libnet_geterror(L));
	       }

	     i++;
	  }
	while (ip_dst == -1);

	printf ("2 port (0 to autodetect) : ");
	fgets (buff, 50, stdin);
	port_dst = atoi(buff);
	if (!port_src && !port_dst)
	  {
	     printf ("\nOnly one port can be zero\n");
	     i=0;
	     goto error;
	  }

	printf ("\n");

	/* demonize */
	if (demonize)
	  printf ("Is very useless demonize me now! Omit\n\n");

	rst (dev, ip_src, ip_dst, port_src, port_dst);
	break;

      case 's':
	puts ("Type connection extremes");
	puts ("------------------------");
	do
	  {
	     if (i) printf ("Cannot resolve input address, type again!\n");
	     printf ("1st ip : ");
	     fgets (buff, 50, stdin);
	     if ((ip_src = libnet_name2addr4(L, dn (buff), LIBNET_RESOLVE))==-1)
	       {
		  w_error(1,"Error: %s\n", libnet_geterror(L));
	       }
	     i ++;
	  }
	while (ip_src == -1);
	printf ("1st port : ");
	fgets (buff, 50, stdin);
	port_src = atoi(buff);

	i = 0;
	do
	  {
	     if (i) printf ("Cannot resolve input address, type again!\n");
	     printf ("2nd : ");
	     fgets (buff, 50, stdin);
	     if ((ip_dst = libnet_name2addr4(L, dn (buff), LIBNET_RESOLVE))==-1)
	       {
		  w_error(1, "Error: %s\n", libnet_geterror(L));
	       }

	     i++;
	  }
	while (ip_dst == -1);
	printf ("2nd port : ");
	fgets (buff, 50, stdin);
	port_dst = atoi(buff);

	printf ("\n");

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

	stream (dev, ip_src, ip_dst, port_src, port_dst,l);
	break;

      case 'S':
	printf("Port Scanner extremes\n");
	printf("Insert IP to scan   : ");
	fgets(buff ,50 ,stdin);
	if ((ip_dst = libnet_name2addr4(L, dn(buff), LIBNET_RESOLVE))==-1)
	  {
	     w_error(1, "Error: %s\n", libnet_geterror(L));
	  }
	do
	  {
	     bzero(buff,50);
	     printf("Insert Port range   : ");
	     fgets(buff ,50 ,stdin);
	  }
	while (atoi(dn(buff)) < 1 || atoi(dn(buff)) > 65536);
	plist_p = &plist;
	if (libnet_plist_chain_new(L, &plist_p, dn(buff)) == -1)
	  {
	     w_error(1, "Bad token in port list: %s\n",libnet_geterror(L));
	  }
	printf("\n");

	/* demonize */
	if (demonize)
	  printf ("Is very useless demonize for single portscan! Omit\n\n");

	port(dev,ip_dst,plist_p,l);
	break;

      case 'M':
	printf("Port Scanner extremes\n");
	do
	  {
	     printf("Insert Port range   : ");
	     fgets(buff ,50 ,stdin);
	  }
	while (atoi(dn(buff)) < 1 || atoi(dn(buff)) > 65536);
	plist_p = &plist;
	if (libnet_plist_chain_new(L, &plist_p, dn(buff) )== -1)
	  {
	     w_error(1, "Bad token in port list: %s\n",libnet_geterror(L));
	  }
	printf("\n");

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

	mhport (dev,plist_p,l);
	break;
     }

   if (L) libnet_destroy(L);

   return 0;
}

/* delete \n */
char * dn (char * s)
{
   if (s[strlen(s)-1]=='\n')
     s[strlen(s)-1]='\0';
   return s;
}

/* open a file to log to */
void openfile(void)
{
   if ((logd = (fopen(logname,"w"))) == NULL)
     {
	w_error(1, "Unable to open logfile descriptor: %s\n\n", strerror(errno));
     }
}

/* signal handler */
void sigexit(int sig)
{
#ifdef HAVE_LIBNCURSES
# include <ncurses.h>
   if(graph)
     {
	endwin();
	printf("Thank you for using NAST\n\n");
	exit(0);
     }

#endif

   if (!tr && sniff_glob)
     {
	if (pcap_stats(descr,&statistic) < 0)
	  w_error(1, "Error: pcap_stats: %s\n", pcap_geterr(descr));
	else
	  {
	     printf("\n\nPackets Received:\t\t%d\n", statistic.ps_recv);
	     printf("Packets Dropped by kernel:\t%d\n", statistic.ps_drop);
	  }
     }

   if (tl)
     pcap_dump_close(dumper);

   if (logd) 
     fclose(logd);

   exit(0);
}

/* demonize process */
void bkg(void)
{
   if (fork()) exit(0);
   printf ("\nRunning in background with PID %d\n", getpid());
   puts("\n");
   fclose (stdout);
}

/* convert u_char to "##:##:##...##" format */
char * nast_hex_ntoa (u_char *s)
{
   char *r = calloc (18, sizeof (char));

   sprintf (r, "%02X:%02X:%02X:%02X:%02X:%02X",
	    s[0], s[1], s[2], s[3], s[4], s[5]);

   return r;
}

/* convert u_char[4] to "###.###.###.###" format */
char * nast_atoda (u_char *s) /* array to dot array */
{
   char *r = calloc (16, sizeof (char));

   sprintf (r, "%d.%d.%d.%d", s[0], s[1], s[2], s[3]);

   return r;
}

void n_print(char *wins, int y, int x, int lg, char *string, ...)
{
   char msg[2048];
   int n;
   va_list ap;

   va_start(ap, string);
   n = vsnprintf(msg, 2048, string, ap);
   va_end(ap);

   if(!graph && !lg)
     {
	printf("%s",msg);
	fflush(NULL);
     }

   if(!graph && lg)
     {
	fprintf(logd,"%s\n",msg);
	printf("%s",msg);
	fflush(NULL);
     }

#ifdef HAVE_LIBNCURSES
   if(graph && lg)
     {
	fprintf(logd,"%s",msg);
	ng_print(wins,y,x,msg);
     }

   if(graph && !lg)
     ng_print(wins,y,x,msg);
#endif
}