File: ec_sniff_unified.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: 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 (189 lines) | stat: -rw-r--r-- 5,033 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
/*
    ettercap -- unified sniffing method 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_capture.h>
#include <ec_send.h>
#include <ec_threads.h>
#include <ec_inject.h>
#include <ec_conntrack.h>
#include <ec_sslwrap.h>

/* proto */
void start_unified_sniff(void);
void stop_unified_sniff(void);
void forward_unified_sniff(struct packet_object *po);
void unified_check_forwarded(struct packet_object *po);
void unified_set_forwardable(struct packet_object *po);

/*******************************************/

/*
 * creates the threads for capturing 
 */
void start_unified_sniff(void)
{
   DEBUG_MSG("start_unified_sniff");
   if (GBL_SNIFF->active == 1) {
      USER_MSG("Unified sniffing already started...\n");
      return;
   }
   
   USER_MSG("Starting Unified sniffing...\n\n");
   
   /* create the timeouter thread */
   if (!GBL_OPTIONS->read) { 
      pthread_t pid;
      
      pid = ec_thread_getpid("timer");
      if (pthread_equal(pid, EC_PTHREAD_NULL))
         ec_thread_new("timer", "conntrack timeouter", &conntrack_timeouter, NULL);
   }

   /* create the thread for packet capture */
   capture_start(GBL_IFACE);

   if(GBL_OPTIONS->secondary)
      secondary_sources_foreach(capture_start);

   /* start ssl_wrapper thread */
   if (!GBL_OPTIONS->read && !GBL_OPTIONS->unoffensive && !GBL_OPTIONS->only_mitm && GBL_OPTIONS->ssl_mitm)
      ec_thread_new("sslwrap", "wrapper for ssl connections", &sslw_start, NULL);

   GBL_SNIFF->active = 1;
}


/*
 * kill the capturing threads, but leave untouched the others
 */
void stop_unified_sniff(void)
{
   pthread_t pid;
   
   DEBUG_MSG("stop_unified_sniff");
   
   if (GBL_SNIFF->active == 0) {
      USER_MSG("Unified sniffing is not running...\n");
      return;
   }
  
   /* kill it */
   capture_stop(GBL_IFACE);

   if(GBL_OPTIONS->secondary)
      secondary_sources_foreach(capture_stop);
   
   pid = ec_thread_getpid("sslwrap");
   if (!pthread_equal(pid, EC_PTHREAD_NULL))
      ec_thread_destroy(pid);

   USER_MSG("Unified sniffing was stopped.\n");

   GBL_SNIFF->active = 0;
}


void forward_unified_sniff(struct packet_object *po)
{
   /* if it was not initialized, no packet are forwardable */
   switch(ntohs(po->L3.proto)) {
      case LL_TYPE_IP:
         if(!GBL_LNET->lnet_IP4)
            return;
         if(!(GBL_IFACE->has_ipv4))
            return;
         break;
      case LL_TYPE_IP6:
         if(!GBL_LNET->lnet_IP6)
            return;
         if(!(GBL_IFACE->has_ipv6))
            return;
         break;
   }
   
   /* if unoffensive is set, don't forward any packet */
   if (GBL_OPTIONS->unoffensive || GBL_OPTIONS->read)
      return;

   /* 
    * forward the packet to Layer 3, the kernel
    * will route them to the correct destination (host or gw)
    */

   /* don't forward dropped packets */
   if ((po->flags & PO_DROPPED) == 0)
      send_to_L3(po);

    /* 
     * if the packet was modified and it exceeded the mtu,
     * we have to inject the exceeded data
     */
    if (po->DATA.inject) 
       inject_buffer(po); 
}

/*
 * check if the packet has been forwarded by us
 * the source mac address is our, but the ip address is different
 */
void unified_check_forwarded(struct packet_object *po) 
{
   /* the interface was not configured, the packets are not forwardable */
   if (!GBL_IFACE->is_ready)
      return;
   
   /* 
    * dont sniff forwarded packets (equal mac, different ip) 
    * but only if we are on live connections
    */
   if (GBL_CONF->skip_forwarded && !GBL_OPTIONS->read &&
       !memcmp(GBL_IFACE->mac, po->L2.src, MEDIA_ADDR_LEN) &&
       ip_addr_is_ours(&po->L3.src) != E_FOUND) {
      po->flags |= PO_FORWARDED;
   }
}

/* 
 * if the dest mac address of the packet is
 * the same of GBL_IFACE->mac but the dest ip is
 * not the same as GBL_IFACE->ip, the packet is not
 * for us and we can do mitm on it before forwarding.
 */
void unified_set_forwardable(struct packet_object *po)
{
   /* 
    * if the mac is our, but the ip is not...
    * it has to be forwarded
    */
   if (!memcmp(GBL_IFACE->mac, po->L2.dst, MEDIA_ADDR_LEN) &&
       memcmp(GBL_IFACE->mac, po->L2.src, MEDIA_ADDR_LEN) &&
       ip_addr_is_ours(&po->L3.dst) != E_FOUND) {
      po->flags |= PO_FORWARDABLE;
   }
   
}


/* EOF */

// vim:ts=3:expandtab