~ubuntu-branches/ubuntu/hardy/nast/hardy

« back to all changes in this revision

Viewing changes to common.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2004-02-17 22:14:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040217221421-f1h39tzviblbp2lh
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    NAST
 
3
 
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 
 
18
*/
 
19
 
 
20
/*
 
21
   Common functions
 
22
                                    */
 
23
 
 
24
#include "include/nast.h"
 
25
#include <stdarg.h>
 
26
 
 
27
/* run complex plugins here */
 
28
 
 
29
/* r: reset a connection
 
30
 * s: follow tcp stream
 
31
 * M: multi_scanner
 
32
 * S: single port_scanner
 
33
 */
 
34
 
 
35
int runcplx (char what, char *dev, int l)
 
36
{
 
37
   u_long ip_dst, ip_src;
 
38
   u_short port_dst, port_src;
 
39
   libnet_plist_t plist, *plist_p;
 
40
   char buff[50];
 
41
   u_short i;
 
42
   libnet_t *L;
 
43
   char errbuf[PCAP_ERRBUF_SIZE];
 
44
 
 
45
   i = 0;
 
46
   if ((L = libnet_init (LIBNET_LINK, NULL, errbuf))==NULL)
 
47
     {
 
48
        w_error(1,"Error loading libnet core!\n");
 
49
     }
 
50
 
 
51
   switch (what)
 
52
     {
 
53
      case 'r':
 
54
        /* don't touch here */
 
55
        error:
 
56
        puts ("Type connection extremes");
 
57
        puts ("------------------------");
 
58
        do
 
59
          {
 
60
             if (i) printf ("Cannot resolve input address, type again!\n");
 
61
             printf ("1 ip / hostname : ");
 
62
             fgets (buff, 50, stdin);
 
63
             if ((ip_src = libnet_name2addr4(L, dn (buff), LIBNET_RESOLVE))==-1)
 
64
               {
 
65
                  w_error(1,"Error: %s\n", libnet_geterror(L));
 
66
               }
 
67
             i ++;
 
68
          }
 
69
        while (ip_src == -1);
 
70
 
 
71
        i = 0;
 
72
        printf ("1 port (0 to autodetect) : ");
 
73
        fgets (buff, 50, stdin);
 
74
        port_src = atoi(buff);
 
75
        do
 
76
          {
 
77
             if (i) printf ("Cannot resolve input address, type again!\n");
 
78
             printf ("2 ip / hostname : ");
 
79
             fgets (buff, 50, stdin);
 
80
             if ((ip_dst = libnet_name2addr4(L, dn (buff), LIBNET_RESOLVE))==-1)
 
81
               {
 
82
                  w_error(1, "Error: %s\n", libnet_geterror(L));
 
83
               }
 
84
 
 
85
             i++;
 
86
          }
 
87
        while (ip_dst == -1);
 
88
 
 
89
        printf ("2 port (0 to autodetect) : ");
 
90
        fgets (buff, 50, stdin);
 
91
        port_dst = atoi(buff);
 
92
        if (!port_src && !port_dst)
 
93
          {
 
94
             printf ("\nOnly one port can be zero\n");
 
95
             i=0;
 
96
             goto error;
 
97
          }
 
98
 
 
99
        printf ("\n");
 
100
 
 
101
        /* demonize */
 
102
        if (demonize)
 
103
          printf ("Is very useless demonize me now! Omit\n\n");
 
104
 
 
105
        rst (dev, ip_src, ip_dst, port_src, port_dst);
 
106
        break;
 
107
 
 
108
      case 's':
 
109
        puts ("Type connection extremes");
 
110
        puts ("------------------------");
 
111
        do
 
112
          {
 
113
             if (i) printf ("Cannot resolve input address, type again!\n");
 
114
             printf ("1st ip : ");
 
115
             fgets (buff, 50, stdin);
 
116
             if ((ip_src = libnet_name2addr4(L, dn (buff), LIBNET_RESOLVE))==-1)
 
117
               {
 
118
                  w_error(1,"Error: %s\n", libnet_geterror(L));
 
119
               }
 
120
             i ++;
 
121
          }
 
122
        while (ip_src == -1);
 
123
        printf ("1st port : ");
 
124
        fgets (buff, 50, stdin);
 
125
        port_src = atoi(buff);
 
126
 
 
127
        i = 0;
 
128
        do
 
129
          {
 
130
             if (i) printf ("Cannot resolve input address, type again!\n");
 
131
             printf ("2nd : ");
 
132
             fgets (buff, 50, stdin);
 
133
             if ((ip_dst = libnet_name2addr4(L, dn (buff), LIBNET_RESOLVE))==-1)
 
134
               {
 
135
                  w_error(1, "Error: %s\n", libnet_geterror(L));
 
136
               }
 
137
 
 
138
             i++;
 
139
          }
 
140
        while (ip_dst == -1);
 
141
        printf ("2nd port : ");
 
142
        fgets (buff, 50, stdin);
 
143
        port_dst = atoi(buff);
 
144
 
 
145
        printf ("\n");
 
146
 
 
147
        /* demonize */
 
148
        if (demonize)
 
149
          bkg();
 
150
 
 
151
        stream (dev, ip_src, ip_dst, port_src, port_dst,l);
 
152
        break;
 
153
 
 
154
      case 'S':
 
155
        printf("Port Scanner extremes\n");
 
156
        printf("Insert IP to scan   : ");
 
157
        fgets(buff ,50 ,stdin);
 
158
        if ((ip_dst = libnet_name2addr4(L, dn(buff), LIBNET_RESOLVE))==-1)
 
159
          {
 
160
             w_error(1, "Error: %s\n", libnet_geterror(L));
 
161
          }
 
162
        do
 
163
          {
 
164
             bzero(buff,50);
 
165
             printf("Insert Port range   : ");
 
166
             fgets(buff ,50 ,stdin);
 
167
          }
 
168
        while (atoi(dn(buff)) < 1 || atoi(dn(buff)) > 65536);
 
169
        plist_p = &plist;
 
170
        if (libnet_plist_chain_new(L, &plist_p, dn(buff)) == -1)
 
171
          {
 
172
             w_error(1, "Bad token in port list: %s\n",libnet_geterror(L));
 
173
          }
 
174
        printf("\n");
 
175
 
 
176
        /* demonize */
 
177
        if (demonize)
 
178
          printf ("Is very useless demonize for single portscan! Omit\n\n");
 
179
 
 
180
        port(dev,ip_dst,plist_p,l);
 
181
        break;
 
182
 
 
183
      case 'M':
 
184
        printf("Port Scanner extremes\n");
 
185
        do
 
186
          {
 
187
             printf("Insert Port range   : ");
 
188
             fgets(buff ,50 ,stdin);
 
189
          }
 
190
        while (atoi(dn(buff)) < 1 || atoi(dn(buff)) > 65536);
 
191
        plist_p = &plist;
 
192
        if (libnet_plist_chain_new(L, &plist_p, dn(buff) )== -1)
 
193
          {
 
194
             w_error(1, "Bad token in port list: %s\n",libnet_geterror(L));
 
195
          }
 
196
        printf("\n");
 
197
 
 
198
        /* demonize */
 
199
        if (demonize)
 
200
          bkg();
 
201
 
 
202
        mhport (dev,plist_p,l);
 
203
        break;
 
204
     }
 
205
 
 
206
   if (L) libnet_destroy(L);
 
207
 
 
208
   return 0;
 
209
}
 
210
 
 
211
/* delete \n */
 
212
char * dn (char * s)
 
213
{
 
214
   if (s[strlen(s)-1]=='\n')
 
215
     s[strlen(s)-1]='\0';
 
216
   return s;
 
217
}
 
218
 
 
219
/* open a file to log to */
 
220
void openfile(void)
 
221
{
 
222
   if ((logd = (fopen(logname,"w"))) == NULL)
 
223
     {
 
224
        w_error(1, "Unable to open logfile descriptor: %s\n\n", strerror(errno));
 
225
     }
 
226
}
 
227
 
 
228
/* signal handler */
 
229
void sigexit()
 
230
{
 
231
#ifdef HAVE_LIBNCURSES
 
232
# include <ncurses.h>
 
233
   if(graph)
 
234
     {
 
235
        endwin();
 
236
        printf("Thank you for using NAST\n\n");
 
237
        exit(0);
 
238
     }
 
239
 
 
240
#endif
 
241
 
 
242
   if (!tr && sniff_glob)
 
243
     {
 
244
        if (pcap_stats(descr,&statistic) < 0)
 
245
          w_error(1, "Error: pcap_stats: %s\n", pcap_geterr(descr));
 
246
        else
 
247
          {
 
248
             printf("\n\nPackets Received:\t\t%d\n", statistic.ps_recv);
 
249
             printf("Packets Dropped by kernel:\t%d\n", statistic.ps_drop);
 
250
          }
 
251
     }
 
252
 
 
253
   if (tl)
 
254
     pcap_dump_close(dumper);
 
255
 
 
256
   if (logd) 
 
257
     fclose(logd);
 
258
 
 
259
   exit(0);
 
260
}
 
261
 
 
262
/* demonize process */
 
263
void bkg(void)
 
264
{
 
265
   if (fork()) exit(0);
 
266
   printf ("\nRunning in background with PID %d\n", getpid());
 
267
   puts("\n");
 
268
   fclose (stdout);
 
269
}
 
270
 
 
271
/* convert u_char to "##:##:##...##" format */
 
272
char * nast_hex_ntoa (u_char *s)
 
273
{
 
274
   char *r = calloc (18, sizeof (char));
 
275
 
 
276
   sprintf (r, "%02X:%02X:%02X:%02X:%02X:%02X",
 
277
            s[0], s[1], s[2], s[3], s[4], s[5]);
 
278
 
 
279
   return r;
 
280
}
 
281
 
 
282
/* convert u_char[4] to "###.###.###.###" format */
 
283
char * nast_atoda (u_char *s) /* array to dot array */
 
284
{
 
285
   char *r = calloc (16, sizeof (char));
 
286
 
 
287
   sprintf (r, "%d.%d.%d.%d", s[0], s[1], s[2], s[3]);
 
288
 
 
289
   return r;
 
290
}
 
291
 
 
292
void n_print(char *wins, int y, int x, int lg, char *string, ...)
 
293
{
 
294
   char msg[2048];
 
295
   int n;
 
296
   va_list ap;
 
297
 
 
298
   va_start(ap, string);
 
299
   n = vsnprintf(msg, 2048, string, ap);
 
300
   va_end(ap);
 
301
 
 
302
   if(!graph && !lg)
 
303
     {
 
304
        printf("%s",msg);
 
305
        fflush(NULL);
 
306
     }
 
307
 
 
308
   if(!graph && lg)
 
309
     {
 
310
        fprintf(logd,"%s\n",msg);
 
311
        printf("%s",msg);
 
312
        fflush(NULL);
 
313
     }
 
314
 
 
315
#ifdef HAVE_LIBNCURSES
 
316
   if(graph && lg)
 
317
     {
 
318
        fprintf(logd,"%s",msg);
 
319
        ng_print(wins,y,x,msg);
 
320
     }
 
321
 
 
322
   if(graph && !lg)
 
323
     ng_print(wins,y,x,msg);
 
324
#endif
 
325
}
 
326