File: ec_sniff_bridge.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 (211 lines) | stat: -rw-r--r-- 5,880 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
/*
    ettercap -- bridged 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_conntrack.h>

struct origin_mac_table {
   u_int8 mac[MEDIA_ADDR_LEN];
   LIST_ENTRY(origin_mac_table) next;
};

static LIST_HEAD(, origin_mac_table) iface_origin_table;
static LIST_HEAD(, origin_mac_table) bridge_origin_table;

/* proto */
void start_bridge_sniff(void);
void stop_bridge_sniff(void);
void forward_bridge_sniff(struct packet_object *po);
void bridge_check_forwarded(struct packet_object *po);
void bridge_set_forwardable(struct packet_object *po);

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

void start_bridge_sniff(void)
{
   DEBUG_MSG("start_bridge_sniff");
   
   if (GBL_SNIFF->active == 1) {
      USER_MSG("Bridged sniffing already started...\n");
      return;
   }
   
   USER_MSG("Starting Bridged 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);
   
   /* create the thread for packet capture on the bridged interface */
   capture_start(GBL_BRIDGE);

   GBL_SNIFF->active = 1;
}

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

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

   GBL_SNIFF->active = 0;
}


void forward_bridge_sniff(struct packet_object *po)
{
   /* don't forward dropped packets */
   if (po->flags & PO_DROPPED)
      return;

   /*
    * If the filters modified the packet len
    * recalculate it (only if some L3 decoder parsed it).
    */
   if (po->fwd_packet)
      po->len = po->L2.len + po->fwd_len;
         
   /* 
    * send the packet to the other interface.
    * the socket was opened during the initialization
    * phase (parameters parsing) by bridge_init()
    */
   if (po->flags & PO_FROMIFACE)
      send_to_bridge(po);
   else if (po->flags & PO_FROMBRIDGE)
      send_to_L2(po);
   
}

/*
 * keep a list of source mac addresses for each interface.
 * each list will contain mac address coming form an host connected
 * on the iface.
 * we can determine if a packet is forwarded or not searching it in
 * the lists.
 */
void bridge_check_forwarded(struct packet_object *po)
{
   struct origin_mac_table *omt;
   u_char tmp[MAX_ASCII_ADDR_LEN];

   /* avoid gcc complaining for unused var */
   (void)tmp;
   
   if (po->flags & PO_FROMIFACE) {
      /* search the mac in the iface table */
      LIST_FOREACH(omt, &iface_origin_table, next)
         if (!memcmp(omt->mac, po->L2.src, MEDIA_ADDR_LEN))
            return;

      /* 
       * now search it in the opposite table
       * if it was registered there, the packet is forwarded
       */
      LIST_FOREACH(omt, &bridge_origin_table, next)
         if (!memcmp(omt->mac, po->L2.src, MEDIA_ADDR_LEN)) {
            po->flags |= PO_FORWARDED;
            return;
         }
   }
         
   if (po->flags & PO_FROMBRIDGE) {
      /* search the mac in the bridge table */
      LIST_FOREACH(omt, &bridge_origin_table, next)
         if (!memcmp(omt->mac, po->L2.src, MEDIA_ADDR_LEN))
            return;
      
      /* 
       * now search it in the opposite table
       * if it was registered there, the packet is forwarded
       */
      LIST_FOREACH(omt, &iface_origin_table, next)
         if (!memcmp(omt->mac, po->L2.src, MEDIA_ADDR_LEN)) {
            po->flags |= PO_FORWARDED;
            return;
         }
   }


   /* allocate a new entry for the newly discovered mac address */
   SAFE_CALLOC(omt, 1, sizeof(struct origin_mac_table));

   memcpy(omt->mac, po->L2.src, MEDIA_ADDR_LEN);

   /* insert the new mac address in the proper list */
   if (po->flags & PO_FROMIFACE) {
      DEBUG_MSG("Added the mac [%s] to IFACE table", mac_addr_ntoa(po->L2.src, tmp));
      LIST_INSERT_HEAD(&iface_origin_table, omt, next);
   }
   
   if (po->flags & PO_FROMBRIDGE) {
      DEBUG_MSG("Added the mac [%s] to BRIDGE table", mac_addr_ntoa(po->L2.src, tmp));
      LIST_INSERT_HEAD(&bridge_origin_table, omt, next);
   }
}

/* 
 * in bridged sniffing all the packet must be forwarded
 * on the other iface
 */
void bridge_set_forwardable(struct packet_object *po)
{
   /* If for us on the iface */
   if ( !memcmp(GBL_IFACE->mac, po->L2.src, MEDIA_ADDR_LEN) || !memcmp(GBL_IFACE->mac, po->L2.dst, MEDIA_ADDR_LEN) )
      return;

   /* If for us on the bridge */
   if ( !memcmp(GBL_BRIDGE->mac, po->L2.src, MEDIA_ADDR_LEN) || !memcmp(GBL_BRIDGE->mac, po->L2.dst, MEDIA_ADDR_LEN) )
      return;

   /* in bridged sniffing all the packet have to be forwarded */      
   po->flags |= PO_FORWARDABLE;
}



/* EOF */

// vim:ts=3:expandtab