File: ec_profiles.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,468 kB
  • ctags: 6,333
  • sloc: ansic: 47,337; yacc: 310; lex: 204; makefile: 121; xml: 31; sh: 24
file content (784 lines) | stat: -rw-r--r-- 21,422 bytes parent folder | download | duplicates (4)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
    ettercap -- host profiling module

    Copyright (C) ALoR & NaGA

    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 <ec.h>
#include <ec_threads.h>
#include <ec_passive.h>
#include <ec_profiles.h>
#include <ec_packet.h>
#include <ec_hook.h>
#include <ec_scan.h>
#include <ec_log.h>

#define ONLY_REMOTE_PROFILES  3
#define ONLY_LOCAL_PROFILES   2

/* protos */

void __init profiles_init(void);

static void profile_purge(int flag);

static int profile_add_host(struct packet_object *po);
static int profile_add_user(struct packet_object *po);
static void update_info(struct host_profile *h, struct packet_object *po);
static void update_port_list(struct host_profile *h, struct packet_object *po);
static void update_port_list_with_advertised(struct host_profile *h, uint8_t L4_proto, uint16_t L4_src);
static void set_gateway(u_char *L2_addr);

/* global mutex on interface */

static pthread_mutex_t profile_mutex = PTHREAD_MUTEX_INITIALIZER;
#define PROFILE_LOCK     do { pthread_mutex_lock(&profile_mutex); } while(0)
#define PROFILE_UNLOCK   do { pthread_mutex_unlock(&profile_mutex); } while(0)

/************************************************/
  
/*
 * add the hook function
 */
void __init profiles_init(void)
{
   /* add the hook for the ARP packets */
   hook_add(HOOK_PACKET_ARP, &profile_parse);
   
   /* add the hook for ICMP packets */
   hook_add(HOOK_PACKET_ICMP, &profile_parse);

   /* add the hook for ICMPv6 packets */
   hook_add(HOOK_PACKET_ICMP6, &profile_parse);
   
   /* add the hook for DHCP packets */
   hook_add(HOOK_PROTO_DHCP_PROFILE, &profile_parse);
         
   /* receive all the top half packets */
   hook_add(HOOK_DISPATCHER, &profile_parse);
}


/*
 * decides if the packet has to be added
 * to the profiles
 */
void profile_parse(struct packet_object *po)
{

   /* we don't want profiles in memory. */
   if (!GBL_CONF->store_profiles) {
      return;
   }
   
   /* 
    * skip packet sent (spoofed) by us
    * else we will get duplicated hosts with our mac address
    * this is necessary because check_forwarded() is executed
    * in ec_ip.c, but here we are getting even arp packets...
    */
   EXECUTE(GBL_SNIFF->check_forwarded, po);
   if (po->flags & PO_FORWARDED)
      return;

   /*
    * recheck if the packet is compliant with the visualization filters.
    * we need to redo the test, because here we are hooked to ARP and ICMP
    * packets that are before the test in ec_decode.c
    */
   po->flags |= PO_IGNORE;
   EXECUTE(GBL_SNIFF->interesting, po);
   if ( po->flags & PO_IGNORE )
       return;
   
   /*
    * call the add function only if the packet
    * is interesting...
    *    - ARP packets
    *    - ICMP packets
    *    - DNS packets that contain GW information (they use fake icmp po)
    */
   if ( po->L3.proto == htons(LL_TYPE_ARP) ||                     /* arp packets */
        po->L4.proto == NL_TYPE_ICMP ||                           /* icmp packets */
        po->L4.proto == NL_TYPE_ICMP6
      )
      profile_add_host(po);
      
   /*  
    * we don't want to log conversations, only
    * open ports, OSes etc etc ;)
    */
   if ( is_open_port(po->L4.proto, po->L4.src, po->L4.flags) ||   /* src port is open */
        strcmp(po->PASSIVE.fingerprint, "") ||                    /* collected fingerprint */  
        po->DISSECTOR.banner ||                                     /* banner */
        po->DISSECTOR.os
      )
      profile_add_host(po);

   /* 
    * usernames and passwords are to be bound to 
    * destination host, not source.
    * do it here, search for the right host and add
    * the username to the right port.
    */
   if ( po->DISSECTOR.user ||                               /* user */
        po->DISSECTOR.pass ||                               /* pass */
        po->DISSECTOR.info                                 /* info */
      )
      profile_add_user(po);

   if ( po->DISSECTOR.advertised_port != 0 &&
        po->DISSECTOR.advertised_proto != 0
      )
      profile_add_host(po);

   return;
}


/* 
 * add the infos to the profiles tables
 * return the number of hosts added (1 if added, 0 if updated)
 */
static int profile_add_host(struct packet_object *po)
{
   struct host_profile *h;
   struct host_profile *c;
   struct host_profile *last = NULL;
   char tmp[MAX_ASCII_ADDR_LEN];
   
   /* 
    * do not store profiles for hosts with ip == 0.0.0.0
    * they are hosts requesting for a dhcp/bootp reply.
    * they will get an ip address soon and we are interested
    * only in the latter.
    */
   if (ip_addr_is_zero(&po->L3.src))
      return 0;
   
   /* We don't need a profile on ourselves, do we? */
   if(!memcmp(&po->L2.src, &GBL_IFACE->mac, MEDIA_ADDR_LEN) ||
      !memcmp(&po->L2.src, &GBL_BRIDGE->mac, MEDIA_ADDR_LEN))
      return 0;
   
   /* 
    * if the type is FP_HOST_NONLOCAL 
    * search for the GW and mark it
    */
   if (po->PASSIVE.flags & FP_HOST_NONLOCAL) {
      set_gateway(po->L2.src);
      /* the mac address of non local should not be saved */
      memset(&po->L2.src, 0, MEDIA_ADDR_LEN);
   }

   PROFILE_LOCK;

   /* search if it already exists */
   TAILQ_FOREACH(h, &GBL_PROFILES, next) {
      /* an host is identified by the mac and the ip address */
      /* if the mac address is null also update it since it could
       * be captured as a DHCP packet specifying the GW 
       */
      if ((!memcmp(h->L2_addr, po->L2.src, MEDIA_ADDR_LEN) ||
           !memcmp(po->L2.src, "\x00\x00\x00\x00\x00\x00", MEDIA_ADDR_LEN) ) &&
          !ip_addr_cmp(&h->L3_addr, &po->L3.src) ) {

         update_info(h, po);
         /* the host was already in the list
          * return 0 host added */
         PROFILE_UNLOCK;
         return 0;
      }
   }
  
   PROFILE_UNLOCK;
  
   DEBUG_MSG("profile_add_host %s", ip_addr_ntoa(&po->L3.src, tmp));
   
   /* 
    * the host was not found, create a new entry 
    * before the creation check if it has to be stored...
    */
   
   /* this is a local and we want only remote */
   if ((po->PASSIVE.flags & FP_HOST_LOCAL) && (GBL_CONF->store_profiles == ONLY_REMOTE_PROFILES))
      return 0;
   
   /* this is remote and we want only local */
   if ((po->PASSIVE.flags & FP_HOST_NONLOCAL) && (GBL_CONF->store_profiles == ONLY_LOCAL_PROFILES))
      return 0;
   
   /* create the new host */
   SAFE_CALLOC(h, 1, sizeof(struct host_profile));
   
   PROFILE_LOCK;
   
   /* fill the structure with the collected infos */
   update_info(h, po);
   
   /* search the right point to inser it (ordered ascending) */
   TAILQ_FOREACH(c, &GBL_PROFILES, next) {
      if ( ip_addr_cmp(&c->L3_addr, &h->L3_addr) > 0 )
         break;
      last = c;
   }
   
   if (TAILQ_FIRST(&GBL_PROFILES) == NULL) 
      TAILQ_INSERT_HEAD(&GBL_PROFILES, h, next);
   else if (c != NULL) 
      TAILQ_INSERT_BEFORE(c, h, next);
   else 
      TAILQ_INSERT_AFTER(&GBL_PROFILES, last, h, next);

   PROFILE_UNLOCK;
   
   DEBUG_MSG("profile_add_host: ADDED");
   
   return 1;   
}

/* set the info in a host profile */

static void update_info(struct host_profile *h, struct packet_object *po)
{
   
   /* if it is marked as the gateway or unkown, don't update */
   if ( !(h->type & FP_GATEWAY) && !(h->type & FP_UNKNOWN) )
      h->type = po->PASSIVE.flags;
   
   /* update the mac address only if local or unknown */
   if (h->type & FP_HOST_LOCAL || h->type == FP_UNKNOWN)
      memcpy(h->L2_addr, po->L2.src, MEDIA_ADDR_LEN);
   
   /* the ip address */
   memcpy(&h->L3_addr, &po->L3.src, sizeof(struct ip_addr));

   /* the distance in HOP */
   if (po->L3.ttl > 1)
      h->distance = TTL_PREDICTOR(po->L3.ttl) - po->L3.ttl + 1;
   else
      h->distance = po->L3.ttl;
      
   /* get the hostname */
   host_iptoa(&po->L3.src, h->hostname);

   if (po->DISSECTOR.os && h->os == NULL)
      h->os = strdup(po->DISSECTOR.os);

   /* 
    * update the fingerprint only if there isn't a previous one
    * or if the previous fingerprint was an ACK
    * fingerprint. SYN fingers are more reliable
    */
   if (po->PASSIVE.fingerprint[FINGER_TCPFLAG] != '\0' &&
        (h->fingerprint[FINGER_TCPFLAG] == '\0' || 
         h->fingerprint[FINGER_TCPFLAG] == 'A') )
      memcpy(h->fingerprint, po->PASSIVE.fingerprint, FINGER_LEN);

   /* add the open port */
   update_port_list(h, po);

   if (po->DISSECTOR.advertised_proto != 0 && po->DISSECTOR.advertised_port != 0)
      update_port_list_with_advertised(h, po->DISSECTOR.advertised_proto, po->DISSECTOR.advertised_port);
}


/* 
 * search the host with this L2_addr
 * and mark it as the GW
 */

static void set_gateway(u_char *L2_addr)
{
   struct host_profile *h;

   /* skip null mac addresses */
   if (!memcmp(L2_addr, "\x00\x00\x00\x00\x00\x00", MEDIA_ADDR_LEN))
      return;
   
   PROFILE_LOCK;

   TAILQ_FOREACH(h, &GBL_PROFILES, next) {
      if (!memcmp(h->L2_addr, L2_addr, MEDIA_ADDR_LEN) ) {
         h->type |= FP_GATEWAY; 
         PROFILE_UNLOCK;
         return;
      }
   }
   
   PROFILE_UNLOCK;
}

/* 
 * update the list of open ports
 */
   
static void update_port_list(struct host_profile *h, struct packet_object *po)
{
   struct open_port *o;
   struct open_port *p;
   struct open_port *last = NULL;

   /* search for an existing port */
   LIST_FOREACH(o, &(h->open_ports_head), next) {
      if (o->L4_proto == po->L4.proto && o->L4_addr == po->L4.src) {
         /* set the banner for the port */
         if (o->banner == NULL && po->DISSECTOR.banner)
            o->banner = strdup(po->DISSECTOR.banner);

         /* already logged */
         return;
      }
   }
  
   /* skip this port, the packet was logged for
    * another reason, not the open port */
   if ( !is_open_port(po->L4.proto, po->L4.src, po->L4.flags) )
      return;

   DEBUG_MSG("update_port_list");
   
   /* create a new entry */
   SAFE_CALLOC(o, 1, sizeof(struct open_port));
   
   o->L4_proto = po->L4.proto;
   o->L4_addr = po->L4.src;
   
   /* search the right point to inser it (ordered ascending) */
   LIST_FOREACH(p, &(h->open_ports_head), next) {
      if ( ntohs(p->L4_addr) > ntohs(o->L4_addr) )
         break;
      last = p;
   }

   /* insert in the right position */
   if (LIST_FIRST(&(h->open_ports_head)) == NULL) 
      LIST_INSERT_HEAD(&(h->open_ports_head), o, next);
   else if (p != NULL) 
      LIST_INSERT_BEFORE(p, o, next);
   else 
      LIST_INSERT_AFTER(last, o, next);
   
}

static void update_port_list_with_advertised(struct host_profile *h, uint8_t L4_proto, uint16_t L4_src)
{
   struct open_port *o;
   struct open_port *p;
   struct open_port *last = NULL;

   /* search for an existing port */
   LIST_FOREACH(o, &(h->open_ports_head), next) {
      if (o->L4_proto == L4_proto && o->L4_addr == L4_src) {
          // already logged
         return;
      }
   }

   DEBUG_MSG("update_port_list_with_advertised");

   /* create a new entry */
   SAFE_CALLOC(o, 1, sizeof(struct open_port));

   o->L4_proto = L4_proto;
   o->L4_addr = L4_src;

   /* search the right point to inser it (ordered ascending) */
   LIST_FOREACH(p, &(h->open_ports_head), next) {
      if ( ntohs(p->L4_addr) > ntohs(o->L4_addr) )
         break;
      last = p;
   }

   /* insert in the right position */
   if (LIST_FIRST(&(h->open_ports_head)) == NULL)
      LIST_INSERT_HEAD(&(h->open_ports_head), o, next);
   else if (p != NULL)
      LIST_INSERT_BEFORE(p, o, next);
   else
      LIST_INSERT_AFTER(last, o, next);
}
/* 
 * update the users list
 */

static int profile_add_user(struct packet_object *po)
{
   struct host_profile *h;
   struct open_port *o = NULL;
   struct active_user *u;
   struct active_user *a;
   struct active_user *last = NULL;
   int found = 0;

   /* no info to update */
   if (po->DISSECTOR.user == NULL || po->DISSECTOR.pass == NULL)
      return 0;
  
   DEBUG_MSG("profile_add_user");
  
   PROFILE_LOCK; 
   
   /* search the right port on the right host */
   TAILQ_FOREACH(h, &GBL_PROFILES, next) {
      
      /* right host */
      if ( !ip_addr_cmp(&h->L3_addr, &po->L3.dst) ) {
      
         LIST_FOREACH(o, &(h->open_ports_head), next) {
            /* right port and proto */
            if (o->L4_proto == po->L4.proto && o->L4_addr == po->L4.dst) {
               found = 1;
               break;
            }
         }
      }
      /* if already found, exit the loop */
      if (found) 
         break;
   }
   
   /* 
    * the port was not found... possible ?
    * yes, but extremely rarely.
    * don't worry, we have lost this for now, 
    * but the next time it will be captured.
    */
   if (!found || o == NULL) {
      PROFILE_UNLOCK;
      return 0;
   }
   
   /* search if the user was already logged */ 
   LIST_FOREACH(u, &(o->users_list_head), next) {
      if (!strcmp(u->user, po->DISSECTOR.user) && 
          !strcmp(u->pass, po->DISSECTOR.pass) &&
          !ip_addr_cmp(&u->client, &po->L3.src)) {
         PROFILE_UNLOCK;
         return 0;
      }
   }
   
   SAFE_CALLOC(u, 1, sizeof(struct active_user));
   
   /* if there are infos copy it, else skip */
   if (po->DISSECTOR.user && po->DISSECTOR.pass) {
      u->user = strdup(po->DISSECTOR.user);
      u->pass = strdup(po->DISSECTOR.pass);
      u->failed = po->DISSECTOR.failed;
      /* save the source of the connection */
      memcpy(&u->client, &po->L3.src, sizeof(struct ip_addr));
   } else {
      SAFE_FREE(u);
      PROFILE_UNLOCK;
      return 0;
   }
  
   if (po->DISSECTOR.info)
      u->info = strdup(po->DISSECTOR.info);
  
   /* search the right point to inser it (ordered alphabetically) */
   LIST_FOREACH(a, &(o->users_list_head), next) {
      if ( strcmp(a->user, u->user) > 0 )
         break;
      last = a;
   }
   
   /* insert in the right position */
   if (LIST_FIRST(&(o->users_list_head)) == NULL) 
      LIST_INSERT_HEAD(&(o->users_list_head), u, next);
   else if (a != NULL) 
      LIST_INSERT_BEFORE(a, u, next);
   else 
      LIST_INSERT_AFTER(last, u, next);
   
   PROFILE_UNLOCK;
   
   return 1;
}

/*
 * purge local hosts from the list
 */
void profile_purge_local(void)
{
   DEBUG_MSG("profile_purge_local");
   profile_purge(FP_HOST_LOCAL);
   return;
}

/*
 * purge local hosts from the list
 */
void profile_purge_remote(void)
{
   DEBUG_MSG("profile_purge_remote");
   profile_purge(FP_HOST_NONLOCAL);
   return;
}

/*
 * purge all the host list 
 */
void profile_purge_all(void)
{
   DEBUG_MSG("profile_purge_all");
   profile_purge( FP_HOST_LOCAL | FP_HOST_NONLOCAL );
   return;
}

/*
 * do the actual elimination 
 */
static void profile_purge(int flags)
{
   struct host_profile *h, *tmp_h = NULL;
   struct open_port *o, *tmp_o = NULL;
   struct active_user *u, *tmp_u = NULL;

   PROFILE_LOCK;

   TAILQ_FOREACH_SAFE(h, &GBL_PROFILES, next, tmp_h) {

      /* the host matches the flags */
      if (h->type & flags) {
         /* free all the alloc'd ports */
         LIST_FOREACH_SAFE(o, &(h->open_ports_head), next, tmp_o) {
            
            SAFE_FREE(o->banner);
            
            LIST_FOREACH_SAFE(u, &(o->users_list_head), next, tmp_u) {
               /* free the current infos */
               SAFE_FREE(u->user);
               SAFE_FREE(u->pass);
               SAFE_FREE(u->info);
               /* remove from the list */
               LIST_REMOVE(u, next);
               SAFE_FREE(u);
            }
            LIST_REMOVE(o, next);
            SAFE_FREE(o);
         }
         SAFE_FREE(h->os);
         TAILQ_REMOVE(&GBL_PROFILES, h, next);
         SAFE_FREE(h);
      }
   }
   
   PROFILE_UNLOCK;
}

/*
 * convert the LOCAL profiles into the hosts list 
 * (created by the initial scan)
 * this is useful to start in silent mode and collect
 * the list in passive mode.
 */

int profile_convert_to_hostlist(void)
{
   struct host_profile *h;
   int count = 0;

   /* first, delete the hosts list */
   del_hosts_list();

   /* now parse the profile list and create the hosts list */
   PROFILE_LOCK;

   TAILQ_FOREACH(h, &GBL_PROFILES, next) {
      /* add only local hosts */
      if (h->type & FP_HOST_LOCAL) {
         /* the actual add */
         add_host(&h->L3_addr, h->L2_addr, h->hostname);
         count++;
      }
   }

   PROFILE_UNLOCK;
   
   return count;
}


/*
 * fill the desc and return the next/prev element
 */
void * profile_print(int mode, void *list, char **desc, size_t len)
{
   struct host_profile *h = (struct host_profile *)list;
   struct host_profile *hl;
   char tmp[MAX_ASCII_ADDR_LEN];

   /* NULL is used to retrieve the first element */
   if (list == NULL)
      return TAILQ_FIRST(&GBL_PROFILES);

   /* the caller wants the description */
   if (desc != NULL) {
      struct open_port *o;
      struct active_user *u;
      int found = 0;
         
      /* search at least one account */
      LIST_FOREACH(o, &(h->open_ports_head), next) {
         LIST_FOREACH(u, &(o->users_list_head), next) {
            found = 1;
         }
      }
      
      ip_addr_ntoa(&h->L3_addr, tmp);
      snprintf(*desc, len, "%c %15s   %s", (found) ? '*' : ' ', 
                                           tmp, 
                                           (h->hostname) ? h->hostname : "" );
   }
  
   /* return the next/prev/current to the caller */
   switch (mode) {
      case -1:
         return TAILQ_PREV(h, gbl_ptail, next);
         break;
      case +1:
         return TAILQ_NEXT(h, next);
         break;
      case 0:
         /* if exists in the list, return it */
         TAILQ_FOREACH(hl, &GBL_PROFILES, next) {
            if (hl == h)
               return h;
         }
         /* else, return NULL */
         return NULL;
         break;
      default:
         return list;
         break;
   }
         
   return NULL;
}

/*
 * dump the whole profile list into an eci file
 */
int profile_dump_to_file(char *filename)
{
   struct log_fd fd;
   char eci[strlen(filename)+5];
   struct host_profile *h;
   struct open_port *o;
   struct active_user *u;
   struct packet_object po;
  
   DEBUG_MSG("profile_dump_to_file: %s", filename);

   /* append the extension */
   snprintf(eci, strlen(filename)+5, "%s.eci", filename);
   
   if (GBL_OPTIONS->compress)
      fd.type = LOG_COMPRESSED;
   else
      fd.type = LOG_UNCOMPRESSED;
        
   /* open the file for dumping */
   if (log_open(&fd, eci) != E_SUCCESS)
      return -E_FATAL;

   /* this is an INFO file */
   log_write_header(&fd, LOG_INFO);

   /* now parse the profile list and dump to the file */
   PROFILE_LOCK;

   TAILQ_FOREACH(h, &GBL_PROFILES, next) {
      
      memset(&po, 0, sizeof(struct packet_object));
      
      /* create the po for logging */
      memcpy(&po.L2.src, h->L2_addr, MEDIA_ADDR_LEN);
      memcpy(&po.L3.src, &h->L3_addr, sizeof(struct ip_addr));

      /* fake the distance by subtracting it from a power of 2 */
      po.L3.ttl = 128 - h->distance + 1;
      po.PASSIVE.flags = h->type;
      memcpy(&po.PASSIVE.fingerprint, h->fingerprint, FINGER_LEN);
      
      /* log for each host */
      log_write_info_arp_icmp(&fd, &po);
      
      /* log the info. needed to record the fingerprint.
       * the above function will not log it 
       */
      log_write_info(&fd, &po);
      
      LIST_FOREACH(o, &(h->open_ports_head), next) {
         
         memcpy(&po.L2.src, h->L2_addr, MEDIA_ADDR_LEN);
         memcpy(&po.L3.src, &h->L3_addr, sizeof(struct ip_addr));
         memset(&po.PASSIVE.fingerprint, 0, FINGER_LEN);
         
         po.L4.src = o->L4_addr;
         /* put the fake syn+ack to impersonate an open port */
         po.L4.flags = TH_SYN | TH_ACK;
         po.L4.proto = o->L4_proto;
         
         /* log the packet for the open port */
         log_write_info(&fd, &po);
        
         po.DISSECTOR.banner = o->banner;
         
         /* log for the banner */
         if (o->banner)
            log_write_info(&fd, &po);
      
         LIST_FOREACH(u, &(o->users_list_head), next) {

            memcpy(&po.L3.dst, &h->L3_addr, sizeof(struct ip_addr));
            /* the source addr is the client address */
            memcpy(&po.L3.src, &u->client, sizeof(struct ip_addr));
        
            /* to exclude the open port check */
            po.L4.flags = TH_PSH;
            po.L4.dst = o->L4_addr;
            po.L4.src = 0;
            
            po.DISSECTOR.user = u->user;
            po.DISSECTOR.pass = u->pass;
            po.DISSECTOR.info = u->info;
            po.DISSECTOR.failed = u->failed;

            /* log for each account:
             * the host must be on the dest
             */
            log_write_info(&fd, &po);
         }
      }
   }

   PROFILE_UNLOCK;
   
   /* close the file */
   log_close(&fd);

   return E_SUCCESS;
}

/* EOF */

// vim:ts=3:expandtab