File: ec_network.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 (481 lines) | stat: -rw-r--r-- 13,399 bytes parent folder | download | duplicates (2)
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
#include <ec.h>
#include <ec_capture.h>
#include <ec_decode.h>
#include <ec_queue.h>
#include <ec_network.h>
#include <ec_threads.h>

#include <pcap.h>
#include <libnet.h>
#if !defined(OS_WINDOWS)
#include <ifaddrs.h>
#endif

#if defined(OS_BSD_OPEN) || defined(OS_LINUX)
   /* LINUX does not care about timeout */
   /* OPENBSD needs 0 */
   #define PCAP_TIMEOUT 0
#elif defined(OS_SOLARIS)
   /* SOLARIS needs > 1 */
   #define PCAP_TIMEOUT 10
#else
   /* FREEBSD needs 1 */
   /* MACOSX  needs 1 */
   #define PCAP_TIMEOUT 1
#endif

struct source_entry {
   struct iface_env iface;
   LIST_ENTRY(source_entry) next;
};

/* globals */
static LIST_HEAD(,source_entry) sources_list;
static pthread_mutex_t sl_mutex = PTHREAD_MUTEX_INITIALIZER;
#define SOURCES_LIST_LOCK     do{ pthread_mutex_lock(&sl_mutex); }while(0)
#define SOURCES_LIST_UNLOCK   do{ pthread_mutex_unlock(&sl_mutex); }while(0)

/* protos */
static void close_network();
static void pcap_winit(pcap_t *pcap);
static void source_print(struct iface_env *source);
static int source_init(char *name, struct iface_env *source, bool primary, bool live);
static void source_close(struct iface_env *iface);
static int secondary_sources_init(char **sources);
static void close_secondary_sources(void);
static void l3_init(void);
static void l3_close(void);

/* the code */

void network_init()
{
   char *iface;
   char perrbuf[PCAP_ERRBUF_SIZE];

   DEBUG_MSG("init_network");

   GBL_PCAP->snaplen = UINT16_MAX;
   
   if(GBL_OPTIONS->read) {
      source_init(GBL_OPTIONS->pcapfile_in, GBL_IFACE, true, false);
      source_print(GBL_IFACE);
   } else {
      iface = GBL_OPTIONS->iface ? GBL_OPTIONS->iface : (GBL_OPTIONS->iface = pcap_lookupdev(perrbuf));
      ON_ERROR(iface, NULL, "No suitable interface found...");
      source_init(iface, GBL_IFACE, true, true);
      source_print(GBL_IFACE);
      if(GBL_SNIFF->type == SM_BRIDGED) {
         source_init(GBL_OPTIONS->iface_bridge, GBL_BRIDGE, true, true);
         source_print(GBL_BRIDGE);
         if(GBL_BRIDGE->dlt != GBL_IFACE->dlt)
            FATAL_ERROR("Can't bridge interfaces of different types");
      }
   }

   if(get_decoder(LINK_LAYER, GBL_IFACE->dlt) == NULL) {
      if(GBL_OPTIONS->read)
         FATAL_ERROR("Dump file not supported (%s)", pcap_datalink_val_to_description(GBL_PCAP->dlt));
      else
         FATAL_ERROR("Interface \"%s\" not supported (%s)", GBL_OPTIONS->iface, pcap_datalink_val_to_description(GBL_PCAP->dlt));
   }
   
   if(GBL_OPTIONS->write)
      pcap_winit(GBL_IFACE->pcap);
   
   GBL_PCAP->align = get_alignment(GBL_PCAP->dlt);
   SAFE_CALLOC(GBL_PCAP->buffer, UINT16_MAX + GBL_PCAP->align + 256, sizeof(char));

   if(GBL_OPTIONS->secondary) {
      secondary_sources_init(GBL_OPTIONS->secondary);
      atexit(close_secondary_sources);
   }

   /* Layer 3 handlers initialization */
   if(!GBL_OPTIONS->unoffensive)
      l3_init();
      
   atexit(close_network);
}

static void close_network()
{
   pcap_close(GBL_IFACE->pcap);
   if(GBL_SNIFF->type == SM_BRIDGED)
      pcap_close(GBL_BRIDGE->pcap);

   if(GBL_OPTIONS->write)
      pcap_dump_close(GBL_PCAP->dump);

   libnet_destroy(GBL_IFACE->lnet);
   libnet_destroy(GBL_BRIDGE->lnet);

   DEBUG_MSG("ATEXIT: close_network");
}

static void pcap_winit(pcap_t *pcap)
{
   pcap_dumper_t *pdump;
   pdump = pcap_dump_open(pcap, GBL_OPTIONS->pcapfile_out);
   ON_ERROR(pdump, NULL, "pcap_dump_open: %s", pcap_geterr(pcap));
   GBL_PCAP->dump = pdump;
}

static void source_print(struct iface_env *source)
{
   char strbuf[256];
   struct net_list *ip6;

   if(source->is_live) {
      USER_MSG("Listening on:\n");
      USER_MSG("%6s -> %s\n", source->name, mac_addr_ntoa(source->mac, strbuf));
      if(source->has_ipv4) {
         USER_MSG("\t  %s/", ip_addr_ntoa(&source->ip, strbuf));
         USER_MSG("%s\n", ip_addr_ntoa(&source->netmask, strbuf));
      }
      if(source->has_ipv6) {
         LIST_FOREACH(ip6, &source->ip6_list, next) {
            USER_MSG("\t  %s/%d\n", ip_addr_ntoa(&ip6->ip, strbuf), ip6->prefix);
         }
         USER_MSG("\n");
      } else {
         USER_MSG("\n\n");
      }
   } else {
      USER_MSG("Reading from %s\n", source->name);
   }

}

static int source_init(char *name, struct iface_env *source, bool primary, bool live)
{
   int ret;
   pcap_t *pcap = NULL;
   libnet_t *lnet = NULL;
   struct bpf_program bpf;
   char pcap_errbuf[PCAP_ERRBUF_SIZE];
   char lnet_errbuf[LIBNET_ERRBUF_SIZE];
   int snaplen;

   struct libnet_ether_addr *mac;
   struct sockaddr_in *sa4;
   struct sockaddr_in6 *sa6;
   struct net_list *ip6;

#if !defined(OS_WINDOWS)
  struct ifaddrs *ifaddrs, *ifaddr;
#endif

   DEBUG_MSG("source_init %s", name);

   BUG_IF(source == NULL);

   /* ===pcap initialization=== */
   if(live) {
      pcap = pcap_open_live(name, GBL_PCAP->snaplen, GBL_PCAP->promisc, PCAP_TIMEOUT, pcap_errbuf);
      if(pcap == NULL) {
         if(primary)
            ON_ERROR(pcap, NULL, "pcap_open_live: %s", pcap_errbuf);
         else
            return -E_INITFAIL;
      }
   } else {
      /* secondary sources must not be offline */
      if(!primary)
         return -E_NOTHANDLED;

      struct stat st;
      int stat_result;
      FILE* pcap_file_h;

      pcap = pcap_open_offline(name, pcap_errbuf);
      ON_ERROR(pcap, NULL, "pcap_open_offline: %s", pcap_errbuf);

      pcap_file_h = pcap_file(pcap);
      ON_ERROR(pcap_file_h, 0, "pcap_fileno returned an invalid file handle");

      stat_result = fstat(fileno(pcap_file_h), &st);
      ON_ERROR(stat_result, -1, "fstat failed.");

      GBL_PCAP->dump_size = st.st_size;
   }
   source->dlt = pcap_datalink(pcap);
   if(primary)
      GBL_PCAP->dlt = source->dlt;
   if(source->dlt == DLT_IEEE802_11) {
      DEBUG_MSG("Wireless monitor mode used, switching to unoffensive mode");
      source->unoffensive = 1;
      if(primary)
         GBL_OPTIONS->unoffensive = 1;
   }
   if(!strcmp(name, "lo")) {
      DEBUG_MSG("Loopback interface used, switching to unoffensive mode");
      source->unoffensive = 1;
      if(primary)
         GBL_OPTIONS->unoffensive = 1;
   }

   if(GBL_PCAP->filter && strcmp(GBL_PCAP->filter, "") && live) {
      u_int net, mask;
      if(pcap_lookupnet(name, &net, &mask, pcap_errbuf) == -1)
         ERROR_MSG("%s - %s", name, pcap_errbuf);
      if(pcap_compile(pcap, &bpf, GBL_PCAP->filter, 1, mask) < 0)
         ERROR_MSG("Wrong pcap filter: %s - %s", name, pcap_geterr(pcap));
      if(pcap_setfilter(pcap, &bpf) == 1)
         ERROR_MSG("Cannot set pcap filter: %s - %s", name, pcap_geterr(pcap));
   }

   snaplen = pcap_snapshot(pcap);
   DEBUG_MSG("requested snaplen for %s: %d, assigned snaplen: %d", name, GBL_PCAP->snaplen, snaplen);
   if(primary)
      GBL_PCAP->snaplen = snaplen;
   source->pcap = pcap;

   SAFE_STRDUP(source->name, name);

   if(live) {
      source->is_live = 1;
   } else {
      source->is_ready = 1;
      return E_SUCCESS;
   }

   if(!GBL_OPTIONS->unoffensive && !source->unoffensive) {
      lnet = libnet_init(LIBNET_LINK_ADV, name, lnet_errbuf);
      ON_ERROR(lnet, NULL, "libnet_init: %s", lnet_errbuf);

      mac = libnet_get_hwaddr(lnet);
      memcpy(&source->mac, mac, MEDIA_ADDR_LEN);
   }

   source->lnet = lnet;

   source->mtu = get_iface_mtu(name);
#if defined(OS_WINDOWS)
  pcap_if_t *dev;

  for (dev = (pcap_if_t *)GBL_PCAP->ifs; dev; dev = dev->next) {
      if(strcmp(dev->name, name))
        continue;

      if(dev->addresses->addr->sa_family == AF_INET) {
        sa4 = (struct sockaddr_in*) dev->addresses->addr;

        ip_addr_init(&source->ip, AF_INET, (u_char*)&sa4->sin_addr);
        if(GBL_OPTIONS->netmask) {
            struct in_addr net;

            if(inet_aton(GBL_OPTIONS->netmask, &net) == 0)
              FATAL_ERROR("Invalid netmask %s", GBL_OPTIONS->netmask);
            ip_addr_init(&source->netmask, AF_INET, (u_char*)&net.s_addr);
        } else {
            sa4 = (struct sockaddr_in*) dev->addresses->netmask;
            ip_addr_init(&source->netmask, AF_INET, (u_char*)&sa4->sin_addr);
        }
        ip_addr_get_network(&source->ip, &source->netmask, &source->network);
        source->has_ipv4 = 1;
      }
      else if(dev->addresses->addr->sa_family == AF_INET6) {
        SAFE_CALLOC(ip6, 1, sizeof(*ip6));
        sa6 = (struct sockaddr_in6*) dev->addresses->addr;
        ip_addr_init(&ip6->ip, AF_INET6, (u_char*)&sa6->sin6_addr);

        sa6 = (struct sockaddr_in6*) dev->addresses->netmask;
        ip_addr_init(&ip6->netmask, AF_INET6, (u_char*)&sa6->sin6_addr);
        ip_addr_get_network(&ip6->ip, &ip6->netmask, &ip6->network);
        ip6->prefix = ip_addr_get_prefix(&ip6->netmask);
        LIST_INSERT_HEAD(&source->ip6_list, ip6, next);
        source->has_ipv6 = 1;
      }
  }

#else
   ret = getifaddrs(&ifaddrs);
   ON_ERROR(ret, -1, "getifaddrs: %s", strerror(errno));

   for(ifaddr = ifaddrs; ifaddr; ifaddr = ifaddr->ifa_next) {
      if (ifaddr->ifa_addr == NULL)
         continue;
      if(strcmp(ifaddr->ifa_name, name))
         continue;

      if(ifaddr->ifa_addr->sa_family == AF_INET) {
         sa4 = (struct sockaddr_in*)ifaddr->ifa_addr;
         ip_addr_init(&source->ip, AF_INET, (u_char*)&sa4->sin_addr);
         if(GBL_OPTIONS->netmask) {
            struct in_addr net;
            if(inet_aton(GBL_OPTIONS->netmask, &net) == 0)
               FATAL_ERROR("Invalid netmask %s", GBL_OPTIONS->netmask);
            ip_addr_init(&source->netmask, AF_INET, (u_char*)&net);
         } else {
            sa4 = (struct sockaddr_in*)ifaddr->ifa_netmask;
            ip_addr_init(&source->netmask, AF_INET, (u_char*)&sa4->sin_addr);
         }
         ip_addr_get_network(&source->ip, &source->netmask, &source->network);
         source->has_ipv4 = 1;
      } else if(ifaddr->ifa_addr->sa_family == AF_INET6) {
         SAFE_CALLOC(ip6, 1, sizeof(*ip6));
         sa6 = (struct sockaddr_in6*)ifaddr->ifa_addr;
         ip_addr_init(&ip6->ip, AF_INET6, (u_char*)&sa6->sin6_addr);
         sa6 = (struct sockaddr_in6*)ifaddr->ifa_netmask;
         ip_addr_init(&ip6->netmask, AF_INET6, (u_char*)&sa6->sin6_addr);
         ip_addr_get_network(&ip6->ip, &ip6->netmask, &ip6->network);
         ip6->prefix = ip_addr_get_prefix(&ip6->netmask);
         LIST_INSERT_HEAD(&source->ip6_list, ip6, next);
         source->has_ipv6 = 1;
      }
   }

   freeifaddrs(ifaddrs);
#endif  /* OS_WINDOWS */

   source->is_ready = 1;

   return E_SUCCESS;
}

static void source_close(struct iface_env *iface)
{
#ifdef WITH_IPV6
   struct net_list *n;
#endif

   iface->is_ready = 0;

   if(iface->pcap != NULL)
      pcap_close(iface->pcap);

   if(iface->lnet != NULL)
      libnet_destroy(iface->lnet);
  
#ifdef WITH_IPV6 
   LIST_FOREACH(n, &iface->ip6_list, next) {
      LIST_REMOVE(n, next);
      SAFE_FREE(n);
   }
#endif

   SAFE_FREE(iface->name);
   memset(iface, 0, sizeof(*iface));
}

static int secondary_sources_init(char **sources)
{
   struct source_entry *se;
   int n = 0;

   SOURCES_LIST_LOCK;

   for(n = 0; sources[n] != NULL; n++) {
      SAFE_CALLOC(se, 1, sizeof(*se));

      /* secondary interfaces are always live */
      source_init(sources[n], &se->iface, true, false);
      if(se->iface.is_ready)
         LIST_INSERT_HEAD(&sources_list, se, next);
      else 
         SAFE_FREE(se);
   }

   SOURCES_LIST_UNLOCK;

   return n;
}

void secondary_sources_foreach(void (*callback)(struct iface_env*))
{
    struct source_entry *se;

    SOURCES_LIST_LOCK;

    LIST_FOREACH(se, &sources_list, next) {
        callback(&se->iface);
    }

    SOURCES_LIST_UNLOCK;
}

static void close_secondary_sources(void)
{
   struct source_entry *se;

   SOURCES_LIST_LOCK;

   LIST_FOREACH(se, &sources_list, next) {
      LIST_REMOVE(se, next);
      source_close(&se->iface);
      SAFE_FREE(se);
   }

   SOURCES_LIST_UNLOCK;
}

static void l3_init(void)
{
   libnet_t *l4;
#ifdef WITH_IPV6
   libnet_t *l6;
#endif
#ifdef OS_WINDOWS
   const char *name = GBL_OPTIONS->iface;
#else
   const char *name = NULL;
#endif


   char lnet_errbuf[LIBNET_ERRBUF_SIZE];

   DEBUG_MSG("l3_init");

   /* open the socket at layer 3 */
   l4 = libnet_init(LIBNET_RAW4_ADV, name, lnet_errbuf);               
   if (l4 == NULL) {
      DEBUG_MSG("send_init: libnet_init(LIBNET_RAW4_ADV) failed: %s", lnet_errbuf);
      USER_MSG("Libnet failed IPv4 initialization. Don't send IPv4 packets.\n");
   }

   GBL_LNET->lnet_IP4 = l4;               

#ifdef WITH_IPV6
   /* open the socket at layer 3 for IPv6 */
   l6 = libnet_init(LIBNET_RAW6_ADV, name, lnet_errbuf);
   if(l6 == NULL) {
      DEBUG_MSG("%s: libnet_init(LIBNET_RAW6_ADV) failed: %s", __func__, lnet_errbuf);
      USER_MSG("Libnet failed IPv6 initialization. Don't send IPv6 packets.\n");
   }
   
   GBL_LNET->lnet_IP6 = l6;
#endif

   atexit(l3_close);
}

static void l3_close(void)
{
   if(GBL_LNET->lnet_IP4)
      libnet_destroy(GBL_LNET->lnet_IP4);
#ifdef WITH_IPV6
   if(GBL_LNET->lnet_IP6)
      libnet_destroy(GBL_LNET->lnet_IP6);
#endif
   
   DEBUG_MSG("ATEXIT: send_closed");
}

struct iface_env* iface_by_mac(u_int8 mac[MEDIA_ADDR_LEN])
{
   struct source_entry *se;

   SOURCES_LIST_LOCK;

   LIST_FOREACH(se, &sources_list, next) {
      if(!memcmp(se->iface.mac, mac, MEDIA_ADDR_LEN)) {
         SOURCES_LIST_UNLOCK;
         return &se->iface;
      }
   }

   SOURCES_LIST_UNLOCK;
   return NULL;
}