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

« back to all changes in this revision

Viewing changes to port.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 356, Boston, MA 02111-1567, USA.
 
17
 
 
18
*/
 
19
 
 
20
#include "include/nast.h"
 
21
 
 
22
/* single host syn port scanner*/
 
23
int port(char *dev,u_long dst_ip,libnet_plist_t *plist_p,int lg)
 
24
{
 
25
   int c, build_ip, fr, fd ;
 
26
   libnet_t *l;
 
27
   libnet_ptag_t tcp;
 
28
   libnet_ptag_t t;
 
29
   struct timeval tv;
 
30
   struct servent *service;
 
31
   fd_set rfsd;
 
32
   int sd,close;
 
33
   struct libnet_tcp_hdr *Tcp;
 
34
   struct libnet_ipv4_hdr *ip;
 
35
   struct libnet_icmpv4_hdr *icmp;
 
36
   u_long src_ip;
 
37
   u_char *pkt;
 
38
   u_short bport, eport, cport;
 
39
   char errbuf[LIBNET_ERRBUF_SIZE];
 
40
   int lineh;
 
41
   //char *filter="not src host 62.10.127.46";
 
42
   //char *filter="not src host 192.168.1.1";
 
43
   lineh = 1;
 
44
   pkt = NULL;
 
45
   close = bport = eport = cport = t = fr = fd = 0;
 
46
 
 
47
 
 
48
   tm = time(NULL);
 
49
   /* per avere sia ora che data si pu usare %c, ma il compilatore tira fuori dei warning decisamente noiosi:)*/
 
50
   strftime(timed,60,"%b %d %T",localtime(&tm));
 
51
 
 
52
   if (lg)
 
53
     {
 
54
        openfile();
 
55
        n_print (NULL,0,0,lg,"Logging to file... \n");
 
56
        fflush (stdout);
 
57
        n_print (NULL,0,0,lg,"NAST PORT SCAN REPORT\n");
 
58
        n_print (NULL,0,0,lg,"Made on %s\n\n", timed);
 
59
     }
 
60
 
 
61
#ifdef HAVE_LIBNCURSES
 
62
   if (graph)
 
63
     init_scr();
 
64
#endif
 
65
 
 
66
/* demonize */
 
67
   if (demonize)
 
68
     {
 
69
        w_error(0,"Is very useless demonize me in checking banner! Omit");
 
70
        demonize=0;
 
71
     }
 
72
 
 
73
   n_print("princ",lineh,2,lg,"Wait for scanning...\n\n");
 
74
   n_print("princ",++lineh,2,lg,"State          Port            Services                Notes\n\n");
 
75
   ++lineh;
 
76
   
 
77
   if(pcap_lookupnet(dev,&netp,&maskp,errbuf)==-1)
 
78
     {
 
79
        w_error(1,"pcap_lookupnet() error %s\n",errbuf);
 
80
     } 
 
81
 
 
82
   if ((descr = pcap_open_live (dev, BUFSIZ, 0, 1, errbuf))==NULL)
 
83
     {
 
84
        w_error(1, "pcap_open_live() error: %s\n",errbuf);
 
85
     }
 
86
 
 
87
   sd = pcap_fileno(descr);
 
88
 
 
89
   if ((offset=(device(dev,descr)))==-1) return -1;
 
90
 
 
91
   l = libnet_init(
 
92
                   LIBNET_RAW4,                            /* injection type */
 
93
                   dev,                                   /* network interface */
 
94
                   errbuf);                                /* errbuf */
 
95
 
 
96
   if (l == NULL)
 
97
     {
 
98
        w_error(1, "libnet_init() failed: %s", errbuf);
 
99
     }
 
100
     
 
101
   if ((src_ip = libnet_get_ipaddr4(l))==-1)
 
102
     {
 
103
        w_error(1, "Can't get local ip address : %s\n", libnet_geterror(l));
 
104
     }
 
105
     
 
106
   /*if(pcap_compile(descr,&fp,filter,0,netp) == -1)
 
107
          {
 
108
             if(w_error(0, "Error in pcap_compile, insert a different filter\n")==-1)
 
109
                return(0);
 
110
          }
 
111
        if(pcap_setfilter(descr,&fp) == -1)
 
112
          {
 
113
             w_error(1, "Error calling pcap_setfilter\n\n");
 
114
          }*/
 
115
 
 
116
   tcp = 0;
 
117
 
 
118
   build_ip = 1;
 
119
 
 
120
   while (libnet_plist_chain_next_pair(plist_p, &bport, &eport))
 
121
     {
 
122
        while (!(bport > eport) && bport != 0)
 
123
          {
 
124
             cport = bport++;
 
125
             tcp = libnet_build_tcp(
 
126
                                    1050,                                    /* source port */
 
127
                                    cport,                                    /* destination port */
 
128
                                    0x01010101,                                 /* sequence number */
 
129
                                    0,                                          /* acknowledgement num */
 
130
                                    TH_SYN,                                     /* control flags */
 
131
                                    32767,                                      /* window size */
 
132
                                    0,                                          /* checksum */
 
133
                                    0,                                          /* urgent pointer */
 
134
                                    LIBNET_TCP_H,                               /* TCP packet size */
 
135
                                    NULL,                                       /* payload */
 
136
                                    0,                                          /* payload size */
 
137
                                    l,                                          /* libnet handle */
 
138
                                    tcp);                                         /* libnet id */
 
139
             if (tcp == -1)
 
140
               {
 
141
                  w_error(1, "Can't build TCP header: %s\n", libnet_geterror(l));
 
142
               }
 
143
 
 
144
             if (build_ip)
 
145
               {
 
146
                  build_ip = 0;
 
147
                  t = libnet_build_ipv4(
 
148
                                        LIBNET_IPV4_H + LIBNET_TCP_H,               /* length */
 
149
                                        0,                                          /* TOS */
 
150
                                        242,                                        /* IP ID */
 
151
                                        0,                                          /* IP Frag */
 
152
                                        64,                                         /* TTL */
 
153
                                        IPPROTO_TCP,                                /* protocol */
 
154
                                        0,                                          /* checksum */
 
155
                                        src_ip,                                     /* source IP */
 
156
                                        dst_ip,                                     /* destination IP */
 
157
                                        NULL,                                       /* payload */
 
158
                                        0,                                          /* payload size */
 
159
                                        l,                                          /* libnet handle */
 
160
                                        0);
 
161
                  if (t == -1)
 
162
                    {
 
163
                       w_error(1, "Can't build IP header: %s\n", libnet_geterror(l));
 
164
                    }
 
165
 
 
166
               }
 
167
 
 
168
               /* usleep con be omissed when scanned another linux box,but if u scan a openBSD
 
169
               it must be uesed! otherwise it find drop rule that doesn't exist!*/
 
170
             //usleep(100);
 
171
             usleep(900);
 
172
             c = libnet_write(l);
 
173
             if (c == -1)
 
174
               {
 
175
                  w_error(1, "Libnet_write() Error: %s\n", libnet_geterror(l));
 
176
               }
 
177
               
 
178
 
 
179
            for(;;)
 
180
               {
 
181
                  fflush (logd);
 
182
                  tv.tv_sec = 2;
 
183
                  //tv.tv_usec = 75000;
 
184
                  FD_ZERO (&rfsd);
 
185
                  FD_SET (sd ,&rfsd);
 
186
 
 
187
                  if((pkt = (u_char *) pcap_next(descr,&hdr))==NULL)
 
188
                        {
 
189
                        break;
 
190
                        }
 
191
                        
 
192
                  ip = (struct libnet_ipv4_hdr *) (pkt + offset);
 
193
                  icmp = (struct libnet_icmpv4_hdr *) (pkt + offset + LIBNET_IPV4_H);
 
194
                  Tcp = (struct libnet_tcp_hdr *) (pkt + offset + LIBNET_IPV4_H);
 
195
 
 
196
                 
 
197
                  if (Tcp->th_flags == (TH_RST|TH_ACK))
 
198
                    {
 
199
                       close++;
 
200
                       break;
 
201
                    }
 
202
                       
 
203
                  service = getservbyport(htons(cport), "tcp");
 
204
 
 
205
                  if(ip->ip_p == IPPROTO_ICMP)
 
206
                    {
 
207
                       n_print("princ",lineh,2,lg,"Filtered     %d              %s",(cport), (service) ? service->s_name : "unknown");
 
208
                       if(!graph || (graph && lg)) fprintf(logd,"\t\t\t");
 
209
                       switch((icmp->icmp_type))
 
210
                         {
 
211
 
 
212
                          case 3:
 
213
                            switch (icmp->icmp_code)
 
214
                              {
 
215
                               case 0:
 
216
                                 n_print("princ",lineh,56,lg,"Network Unreachable(*)\n");
 
217
                                 break;
 
218
                               case 1:
 
219
                                 n_print("princ",lineh,56,lg,"Host Unreachable(*)\n");
 
220
                                 break;
 
221
                               case 2:
 
222
                                 n_print("princ",lineh,56,lg,"Protocol Unreachable(*)\n");
 
223
                                 break;
 
224
                               case 3:
 
225
                                 n_print("princ",lineh,56,lg,"Port Unreachable(*)\n");
 
226
                                 break;
 
227
                               case 9:
 
228
                                 n_print("princ",lineh,56,lg,"Destination network administratively prohibited(*)\n");
 
229
                                 break;
 
230
                               case 10:
 
231
                                 n_print("princ",lineh,56,lg,"Destination host administratively prohibited(*)\n");
 
232
                                 break;
 
233
                               case 13:
 
234
                                 n_print("princ",lineh,56,lg,"Comm. administratively prohibited(*)\n");
 
235
                              }
 
236
 
 
237
                            break;
 
238
                          default:
 
239
                            n_print("princ",lineh,56,lg,"%d(*)\n", icmp->icmp_type);
 
240
                            break;
 
241
                         }
 
242
                       fr++;
 
243
                       ++lineh;
 
244
                       break;
 
245
                    }
 
246
 
 
247
 
 
248
                  if (!select(sd+1, &rfsd, NULL, NULL, &tv))
 
249
                    {
 
250
                       n_print("princ",lineh,2,lg,"Filtered     %d              %s", (cport),(service) ? service->s_name : "unknown");
 
251
                       if(!graph || (graph && lg)) fprintf(logd,"\t\t\t");
 
252
                       n_print("princ",lineh,56,lg,"SYN packet timeout(**)\n");
 
253
                       fd++;
 
254
                       ++lineh;
 
255
                       break;
 
256
                    }
 
257
 
 
258
                  if (Tcp->th_seq != 0 && (Tcp->th_flags == (TH_SYN|TH_ACK)))
 
259
                    {
 
260
                       n_print("princ",lineh,2,lg,"Open         %d              %s", cport ,(service) ? service->s_name : "unknown");
 
261
                       if(!graph || (graph && lg)) fprintf(logd,"\t\t\t");
 
262
                       n_print("princ",lineh,56,lg,"None\n");
 
263
                       ++lineh;
 
264
                       break;
 
265
                    }
 
266
 
 
267
               }
 
268
 
 
269
          }
 
270
     }
 
271
 
 
272
   n_print("winfo",1,2,lg,"\nAll the other %d ports are in state closed\n",close);
 
273
   if (fr!=0) n_print("winfo",2,1,lg,"(*)Possible REJECT rule in the firewall\n");
 
274
   if (fd!=0) n_print("winfo",3,1,lg,"(**)Possible DROP rule in the firewall\n");
 
275
 
 
276
   libnet_destroy(l);
 
277
   pcap_close(descr);
 
278
   n_print("princ",lineh+2,1,lg,"Scanning terminated on %s\n",timed);
 
279
 
 
280
   if (lg)
 
281
     {
 
282
        n_print(NULL,0,0,lg,"Done! Results has been writed to '%s'\n", logname);
 
283
        fclose (logd);
 
284
     }
 
285
   printf ("\n");
 
286
   return 0;
 
287
 
 
288
   if (lg)
 
289
     {
 
290
        n_print(NULL,0,0,lg,"Error! Results has been writed to '%s'\n", logname);
 
291
        fclose (logd);
 
292
     }
 
293
   printf ("\n");
 
294
   return 1;
 
295
 
 
296
}
 
297
 
 
298
/* multy hosts - catch banner*/
 
299
int mport (u_char *dev, u_short ports[],int lg)
 
300
{
 
301
   struct host *uphost;
 
302
   struct servent *service;
 
303
   struct in_addr daddr;
 
304
   u_short i, j, n;
 
305
   char banner[1024];
 
306
   char *msg = "HEAD / HTTP/1.0\n\n";
 
307
   struct sockaddr_in sin;
 
308
   char ip[20];
 
309
   int sd, r, size, bsent,z,k,x,y;
 
310
   struct timeval tv;
 
311
   fd_set rfds;
 
312
   int lineh;
 
313
   int linep;
 
314
   int len;
 
315
   char *buf_p, *banner_p, *p;
 
316
   u_char tmpbuf[1024], *ph=NULL, obuf[4];
 
317
 
 
318
   lineh = 3;
 
319
   linep = 6;
 
320
 
 
321
   if (lg)
 
322
     {
 
323
        openfile();
 
324
        n_print (NULL,0,0,lg,"Logging to file... \n");
 
325
        fflush (stdout);
 
326
        n_print (NULL,0,0,lg,"NAST BANNER SCAN REPORT\n");
 
327
        n_print (NULL,0,0,lg,"Made on %s\n\n", timed);
 
328
     }
 
329
 
 
330
#ifdef HAVE_LIBNCURSES
 
331
   if (graph)
 
332
     init_scr();
 
333
#endif
 
334
 
 
335
/* demonize */
 
336
   if (demonize)
 
337
     {
 
338
        w_error(0,"Is very useless demonize me in checking banner! Omit");
 
339
        demonize=0;
 
340
     }
 
341
 
 
342
   tm = time(NULL);
 
343
   strftime(timed,60,"%b %d %T",localtime(&tm));
 
344
 
 
345
   n_print ("princ",1,1,lg, "Builing hosts list... ");
 
346
 
 
347
  if ((uphost = map_lan(dev, 0, &n))==NULL)
 
348
     {
 
349
        if(w_error(0, "\nCan't build truly host list! mmhhh!\nReport bug to author please\n\n")==-1)
 
350
          return(0);
 
351
     }
 
352
   if (n==0)
 
353
     {
 
354
        n_print("winfo",1,1,lg,"                                                        ");
 
355
        n_print("winfo",1,1,lg,"\nWhat are you doing? You are alone in this network!\n\n");
 
356
     }
 
357
 
 
358
   n_print("princ",1,25,lg,"done\n\n");
 
359
 
 
360
   memset (&sin, 0, sizeof (struct sockaddr_in));
 
361
   sin.sin_family = AF_INET;
 
362
 
 
363
   for (i=0; i<n; i++)
 
364
   {
 
365
   sprintf(ip, "%d.%d.%d.%d", uphost[i].ip[0], uphost[i].ip[1], uphost[i].ip[2], uphost[i].ip[3]);
 
366
   daddr.s_addr = inet_addr (ip);
 
367
   sin.sin_addr = daddr;
 
368
   j = 0;
 
369
   n_print("princ",lineh,2,lg,"IP : %s (%s)\n", ip, libnet_addr2name4(inet_addr(ip), LIBNET_RESOLVE));
 
370
   n_print("princ",++lineh,2,lg,"OPEN PORTS\t\tBANNER\n");
 
371
 
 
372
   for (;;)
 
373
     {
 
374
        sd = socket(AF_INET, SOCK_STREAM, 0);
 
375
        sin.sin_port = htons(ports[j]);
 
376
 
 
377
        if ((connect(sd, (struct sockaddr *)&sin, sizeof(sin))) != -1)
 
378
          {
 
379
             service = getservbyport(htons(ports[j]), "tcp");
 
380
             n_print("princ",linep,2,lg,"%d (%s)", ports[j], service->s_name);
 
381
             if(!graph) printf("\t\t");
 
382
 
 
383
             FD_ZERO (&rfds);
 
384
             FD_SET (sd, &rfds);
 
385
             tv.tv_sec=2;
 
386
             tv.tv_usec=0;
 
387
             fcntl(sd, F_SETFL, O_NONBLOCK);
 
388
             
 
389
             if(ports[j]==80)
 
390
               { 
 
391
                  select(sd+1, &rfds, NULL, NULL, &tv);
 
392
                  size = strlen(msg);
 
393
                  bzero(banner,1024);
 
394
                  bsent = send(sd, msg, size, 0);
 
395
                  r = read (sd, banner, 1024);
 
396
                  len = strlen("Server: ");
 
397
 
 
398
                  banner_p = (char *)malloc(strlen(banner)+1);
 
399
                  bzero(banner_p, strlen(banner)+1);
 
400
 
 
401
                  for(buf_p = strtok(banner, "\n"); buf_p != NULL;)
 
402
                    {
 
403
                       p = strstr(buf_p, "Server: ");
 
404
                       if(p)
 
405
                         {
 
406
                            memmove(banner_p, (p+len), strlen(buf_p)-len);
 
407
                         }
 
408
                       buf_p = strtok(NULL, "\n");
 
409
                    }
 
410
                  if(banner_p)
 
411
                    {
 
412
                       strncpy(banner, banner_p, 1024);
 
413
                    }
 
414
                  else
 
415
                    {
 
416
                       strncpy(banner, "no banner available", 1024);
 
417
                    }
 
418
                  free(banner_p);
 
419
                  if (banner[r-1]=='\n') banner[r-1]='\0';
 
420
                  n_print("princ",linep,24,lg,"%s",banner);
 
421
                  if(!graph) printf("\n");
 
422
                  ++linep;
 
423
                  close(sd);
 
424
                  j++;
 
425
                  continue;
 
426
               }
 
427
 
 
428
            if(ports[j] == 23)
 
429
               {
 
430
               y=0;
 
431
               select(sd+1, &rfds, NULL, NULL, &tv);
 
432
                  do
 
433
                    {  
 
434
                       usleep(100000);
 
435
                       bzero(tmpbuf, 1024);
 
436
                       r = read (sd, tmpbuf, 1024);
 
437
                       if(r==-1)
 
438
                         break;
 
439
 
 
440
                       for(z = 0; z < r; z++)
 
441
                         {
 
442
                            if((z % 3) == 0 && z > 0)
 
443
                              {
 
444
                                 if(tmpbuf[z-3] != 255)
 
445
                                   {
 
446
                                      bzero(banner, 1024);
 
447
                                      z=0;
 
448
                                      for(k = 0; k < r; k++)
 
449
                                        {
 
450
                                           if(tmpbuf[k] == 255)
 
451
                                             {
 
452
                                                k++; k++;
 
453
                                             }
 
454
                                           else if(tmpbuf[k] != 0 && tmpbuf[k] != 13)
 
455
                                             {
 
456
                                                banner[z] = tmpbuf[k];
 
457
                                                z++;
 
458
                                             }
 
459
                                        }
 
460
                                      banner[z] = '\0';
 
461
                                      break;
 
462
                                   }
 
463
                              }
 
464
                         }
 
465
                       ph = tmpbuf;
 
466
                       x = strlen(tmpbuf);
 
467
                       while(x > 0)
 
468
                         {
 
469
                            obuf[0] = 255;
 
470
                            ph++; x--;
 
471
                            if( (*ph == 251) || (*ph == 252))
 
472
                              y = 254;
 
473
                            if( (*ph == 253) || (*ph == 254))
 
474
                              y = 252;
 
475
                            if(y)
 
476
                              {
 
477
                                 obuf[1] = y;
 
478
                                 ph++; x--;
 
479
                                 obuf[2] = *ph;
 
480
                                 send(sd, obuf, 3, 0);
 
481
                                 y = 0;
 
482
                              }
 
483
                            ph++; x--;
 
484
                         }
 
485
                    }
 
486
                  while(ph != NULL);
 
487
 
 
488
                  if (banner[r-1]=='\n') banner[r-1]='\0';
 
489
                  for(i=0;i<=(strlen(banner));i++)
 
490
                        {
 
491
                        if(banner[i]=='\n'){
 
492
                                banner[i]=' ';
 
493
                                }
 
494
                        }
 
495
                  n_print("princ",linep,24,lg,"%s\n",banner);
 
496
                  linep++;
 
497
                  close(sd);
 
498
                  j++;
 
499
                  continue;
 
500
               }
 
501
 
 
502
             
 
503
 
 
504
                  /* read the banner */
 
505
             if (select (sd+1, &rfds, NULL, NULL, &tv))
 
506
               {
 
507
                  memset (&banner, 0, 1024);
 
508
                  r = read (sd, banner, 1024);
 
509
                  if (banner[r-1]=='\n') banner[r-1]='\0';
 
510
                  n_print("princ",linep,24,lg,"%s\n", banner);
 
511
                  ++linep;
 
512
               }
 
513
              /* 1st time out expired */
 
514
             else
 
515
               {
 
516
                       /* send two \n to socket */
 
517
                  write (sd, "\n\n", 2);
 
518
                       /* reset timer */
 
519
                  FD_ZERO (&rfds);
 
520
                  FD_SET (sd, &rfds);
 
521
                  tv.tv_sec=8;
 
522
                  tv.tv_usec=0;
 
523
 
 
524
                       /* try a 2nd time */
 
525
                  if (select (sd+1, &rfds, NULL, NULL, &tv))
 
526
                    {
 
527
                       memset (&banner, 0, 1024);
 
528
                       r = recv (sd, banner, 1024, 0);
 
529
                       if (banner[r-1]=='\n') banner[r-1]='\0';
 
530
                       n_print("princ",linep,24,lg,"%s\n", banner);
 
531
                       ++linep;
 
532
                    }
 
533
               }
 
534
 
 
535
          }
 
536
 
 
537
        fflush (logd);
 
538
        close (sd);
 
539
        j++;
 
540
 
 
541
        if (ports[j] == '\0') break;
 
542
     }
 
543
   lineh = linep+2;
 
544
   linep = linep+5;
 
545
 
 
546
   if(!graph) printf("\n");
 
547
 
 
548
   }
 
549
 
 
550
   free (uphost);
 
551
   n_print("winfo",1,1,lg,"                                                        ");
 
552
   n_print("winfo",1,2,lg,"\nScanning terminated on %s\n",timed);
 
553
   if (lg)
 
554
     {
 
555
        fclose (logd);
 
556
     }
 
557
 
 
558
   printf ("\n");
 
559
   return 0;
 
560
 
 
561
}
 
562
 
 
563
int mhport(u_char *dev,libnet_plist_t *plist_p,int lg)
 
564
{
 
565
   int c, build_ip, fr=0, fd=0 ;
 
566
   libnet_t *l;
 
567
   libnet_ptag_t tcp=0;
 
568
   libnet_ptag_t t=0;
 
569
   struct timeval tv;
 
570
   struct servent *service;
 
571
   fd_set rfsd;
 
572
   int sd,close = 0;
 
573
   struct pcap_pkthdr pcap_h;
 
574
   struct libnet_tcp_hdr *Tcp;
 
575
   struct libnet_ipv4_hdr *ip;
 
576
   struct libnet_icmpv4_hdr *icmp;
 
577
   u_long src_ip = 0;
 
578
   u_char *pkt;
 
579
   u_short bport = 0, eport = 0, cport = 0, i = 0 ,n = 0;
 
580
   char errbuf[LIBNET_ERRBUF_SIZE];
 
581
   struct host * uphost;
 
582
   u_char testip[20];
 
583
   int lineh;
 
584
   int linep;
 
585
 
 
586
   lineh = 3;
 
587
   linep = 6;
 
588
 
 
589
   tm = time(NULL);
 
590
   strftime(timed,60,"%b %d %T",localtime(&tm));
 
591
 
 
592
   if (lg)
 
593
     {
 
594
        openfile();
 
595
        n_print (NULL,0,0,lg,"Logging to file... \n");
 
596
        fflush (stdout);
 
597
        n_print (NULL,0,0,lg,"NAST MULTI PORT SCAN REPORT\n");
 
598
        n_print (NULL,0,0,lg,"Made on %s\n\n", timed);
 
599
     }
 
600
 
 
601
#ifdef HAVE_LIBNCURSES
 
602
   if (graph)
 
603
     init_scr();
 
604
#endif
 
605
 
 
606
/* demonize */
 
607
   if (demonize)
 
608
     {
 
609
        w_error(0,"Is very useless demonize me in checking banner! Omit");
 
610
        demonize=0;
 
611
     }
 
612
 
 
613
   n_print ("princ",1,1,lg,"Builing hosts list...");
 
614
 
 
615
   if ((uphost = map_lan(dev, 0, &n))==NULL)
 
616
     {
 
617
        if(w_error(0, "\nCan't build truly host list! mmhhh!\nReport bug to author please\n\n")==-1)
 
618
          return(0);
 
619
     }
 
620
   if (n==0)
 
621
     {
 
622
        if(w_error(0, "\nWhat are you doing? You are alone in this network!\n\n")==-1)
 
623
          return(0);
 
624
     }
 
625
 
 
626
   n_print ("princ",1,22,lg,"done\n");
 
627
 
 
628
   for(i=0;i<n;i++)
 
629
     {
 
630
        usleep(6000);
 
631
        tcp = 0; c = 0;
 
632
 
 
633
        l = libnet_init(
 
634
                        LIBNET_RAW4,                            /* injection type */
 
635
                        dev,                                   /* network interface */
 
636
                        errbuf);                                /* errbuf */
 
637
 
 
638
        if (l == NULL)
 
639
          {
 
640
             w_error(1, "libnet_init() failed: %s", errbuf);
 
641
          }
 
642
        if ((src_ip = libnet_get_ipaddr4(l))==-1)
 
643
          {
 
644
             w_error(1, "Can't get local ip address : %s\n", libnet_geterror(l));
 
645
          }
 
646
 
 
647
        sprintf(testip, "%d.%d.%d.%d", uphost[i].ip[0], uphost[i].ip[1], uphost[i].ip[2], uphost[i].ip[3]);
 
648
 
 
649
 
 
650
        n_print("princ",++lineh,1,lg,"Wait for scanning...");
 
651
        n_print("princ",lineh,22,lg,"%d.%d.%d.%d\n\n", uphost[i].ip[0], uphost[i].ip[1], uphost[i].ip[2], uphost[i].ip[3]);
 
652
        n_print("princ",++lineh,2,lg,"State             Port            Services                Notes\n\n");
 
653
        
 
654
        pcap_lookupnet(dev,&netp,&maskp,errbuf);
 
655
        
 
656
        ++lineh;
 
657
        
 
658
        if ((descr = pcap_open_live (dev, BUFSIZ, NOT_PROMISC, 10,errbuf)) == NULL)
 
659
          {
 
660
             w_error(1,"pcap_open_live() error: %s\n",errbuf);
 
661
          }
 
662
 
 
663
        sd = pcap_fileno(descr);
 
664
 
 
665
        if ((offset=(device(dev,descr)))==-1) return -1;
 
666
 
 
667
        build_ip = 1;
 
668
        while (libnet_plist_chain_next_pair(plist_p, &bport, &eport))
 
669
          {
 
670
             while (!(bport > eport) && bport != 0)
 
671
               {
 
672
                  cport = bport++;
 
673
                  tcp = libnet_build_tcp(
 
674
                                         1050,                                    /* source port */
 
675
                                         cport,                                    /* destination port */
 
676
                                         1234567,                                 /* sequence number */
 
677
                                         0,                                          /* acknowledgement num */
 
678
                                         TH_SYN,                                     /* control flags */
 
679
                                         32767,                                      /* window size */
 
680
                                         0,                                          /* checksum */
 
681
                                         0,                                          /* urgent pointer */
 
682
                                         LIBNET_TCP_H,                               /* TCP packet size */
 
683
                                         NULL,                                       /* payload */
 
684
                                         0,                                          /* payload size */
 
685
                                         l,                                          /* libnet handle */
 
686
                                         tcp);                                         /* libnet id */
 
687
                  if (tcp == -1)
 
688
                    {
 
689
                       libnet_destroy(l);
 
690
                       pcap_close(descr);
 
691
                       w_error(1, "Can't build TCP header: %s\n", libnet_geterror(l));
 
692
 
 
693
                    }
 
694
 
 
695
                  if (build_ip)
 
696
                    {
 
697
                       build_ip = 0;
 
698
                       t = libnet_build_ipv4(
 
699
                                             LIBNET_IPV4_H + LIBNET_TCP_H,               /* length */
 
700
                                             0,                                          /* TOS */
 
701
                                             242,                                        /* IP ID */
 
702
                                             0,                                          /* IP Frag */
 
703
                                             64,                                         /* TTL */
 
704
                                             IPPROTO_TCP,                                /* protocol */
 
705
                                             0,                                          /* checksum */
 
706
                                             src_ip,                                     /* source IP */
 
707
                                             inet_addr(testip),                                     /* destination IP */
 
708
                                             NULL,                                       /* payload */
 
709
                                             0,                                          /* payload size */
 
710
                                             l,                                          /* libnet handle */
 
711
                                             0);
 
712
                       if (t == -1)
 
713
                         {  
 
714
                            libnet_destroy(l);
 
715
                            pcap_close(descr);
 
716
                            w_error(1, "Can't build IP header: %s\n", libnet_geterror(l));
 
717
                         }
 
718
 
 
719
                    }
 
720
                  //usleep(5);
 
721
                  c = libnet_write(l);
 
722
                  if (c == -1)
 
723
                    {
 
724
                       w_error(1, "Error: %s\n", libnet_geterror(l));
 
725
                    }
 
726
 
 
727
                  for(;;)
 
728
                    {
 
729
                       fflush (logd);
 
730
 
 
731
                       tv.tv_sec = 2;
 
732
                       FD_ZERO (&rfsd);
 
733
                       FD_SET (sd ,&rfsd);
 
734
 
 
735
                       pkt = (u_char *) pcap_next(descr,&pcap_h);
 
736
                       ip = (struct libnet_ipv4_hdr *) (pkt + offset);
 
737
                       icmp = (struct libnet_icmpv4_hdr *) (pkt + offset + LIBNET_IPV4_H);
 
738
                       Tcp = (struct libnet_tcp_hdr *) (pkt + offset + sizeof(struct libnet_ipv4_hdr));
 
739
 
 
740
                       if (Tcp->th_flags == (TH_RST|TH_ACK))
 
741
                         {
 
742
                            close++;
 
743
                            break;
 
744
                         }
 
745
 
 
746
                       service = getservbyport(htons(cport), "tcp");
 
747
 
 
748
                /*ho lasciato gli icmp pi logici x un filtraggio...dubito che vada bene un echo_request:)*/
 
749
                       if(ip->ip_p == IPPROTO_ICMP)
 
750
                         {
 
751
                            n_print("princ",lineh,2,lg,"Filtered        %d              %s", cport, (service) ? service->s_name : "unknown");
 
752
                            if(!graph || (graph && lg)) fprintf(logd,"\t\t\t");
 
753
                            switch((icmp->icmp_type))
 
754
                              {
 
755
 
 
756
                               case 3:
 
757
                                 switch (icmp->icmp_code)
 
758
                                   {
 
759
                                    case 0:
 
760
                                      n_print("princ",lineh,56,lg,"Network Unreachable(*)\n");
 
761
                                      break;
 
762
                                    case 1:
 
763
                                      n_print("princ",lineh,56,lg,"Host Unreachable(*)\n");
 
764
                                      break;
 
765
                                    case 2:
 
766
                                      fprintf(logd,"Protocol Unreachable(*)\n");
 
767
                                      break;
 
768
                                    case 3:
 
769
                                      n_print("princ",lineh,56,lg,"Port Unreachable(*)\n");
 
770
                                      break;
 
771
                                    case 9:
 
772
                                      n_print("princ",lineh,56,lg,"Destination network administratively prohibited(*)\n");
 
773
                                      break;
 
774
                                    case 10:
 
775
                                      n_print("princ",lineh,56,lg,"Destination host administratively prohibited(*)\n");
 
776
                                      break;
 
777
                                    case 13:
 
778
                                      n_print("princ",lineh,56,lg,"Comm. administratively prohibited(*)\n");
 
779
                                      break;
 
780
                                   }
 
781
 
 
782
                                 break;
 
783
                               default:
 
784
                                 n_print("princ",lineh,56,lg,"%i(*)\n", icmp->icmp_type);
 
785
                                 break;
 
786
                              }
 
787
                            fr++;
 
788
                            ++lineh;
 
789
                            break;
 
790
                         }
 
791
 
 
792
                       if (!select(sd+1, &rfsd, NULL, NULL, &tv))
 
793
                         {
 
794
                            n_print("princ",lineh,2,lg,"Filtered        %d              %s", cport,(service) ? service->s_name : "unknown");
 
795
                            if(!graph || (graph && lg)) fprintf(logd,"\t\t\t");
 
796
                            n_print("princ",lineh,56,lg,"SYN packet timeout(**)\n");
 
797
                            fd++;
 
798
                            ++lineh;
 
799
                            break;
 
800
                         }
 
801
                         
 
802
                       if (Tcp->th_seq != 0 && (Tcp->th_flags == (TH_SYN|TH_ACK)))
 
803
                         {
 
804
                            n_print("princ",lineh,2,lg,"Open            %d              %s", cport,(service) ? service->s_name : "unknown");
 
805
                            if(!graph || (graph && lg)) fprintf(logd,"\t\t\t");
 
806
                            n_print("princ",lineh,56,lg,"None\n");
 
807
                            ++lineh;
 
808
                            break;
 
809
                         }
 
810
 
 
811
                    }
 
812
 
 
813
               }
 
814
          }
 
815
        n_print("princ",++lineh,2,lg,"\nAll the other %d ports are in state closed\n",close);
 
816
        if (fr!=0) n_print("princ",++lineh,1,lg,"(*)Possible REJECT rule in the firewall\n");
 
817
        if (fd!=0) n_print("princ",++lineh,1,lg,"(**)Possible DROP rule in the firewall\n");
 
818
 
 
819
        libnet_destroy(l);
 
820
        pcap_close(descr);
 
821
        close=0;
 
822
        fd = 0; fr =0;
 
823
        lineh = lineh+2;
 
824
     }
 
825
 
 
826
   free (uphost);
 
827
   n_print("winfo",2,2,lg,"Scanning terminated on %s\n",timed);
 
828
   if (lg)
 
829
     {
 
830
        printf ("Done! Results has been writed to '%s'\n", logname);
 
831
        fclose (logd);
 
832
     }
 
833
 
 
834
   printf ("\n");
 
835
   return 0;
 
836
 
 
837
}
 
838