File: ec_mitm.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 (217 lines) | stat: -rw-r--r-- 4,841 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
/*
    ettercap -- mitm management 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_mitm.h>
#include <ec_poll.h>
#include <ec_scan.h>
#include <ec_sleep.h>

/* globals */


static SLIST_HEAD (, mitm_entry) mitm_table;

struct mitm_entry {
   int selected;
   int started;
   struct mitm_method *mm;
   SLIST_ENTRY (mitm_entry) next;
};

static char *mitm_args = "";

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

/*
 * register a new mitm method in the table 
 */
void mitm_add(struct mitm_method *mm)
{
   struct mitm_entry *e;

   SAFE_CALLOC(e, 1, sizeof(struct mitm_entry));

   /* copy the mm struct */
   SAFE_CALLOC(e->mm, 1, sizeof(struct mitm_method));
   
   memcpy(e->mm, mm, sizeof(struct mitm_method));
   
   SLIST_INSERT_HEAD(&mitm_table, e, next);
   
}


/*
 * set the 'selected' flag in the table
 * used by ec_parse.c
 */
int mitm_set(char *name)
{
   struct mitm_entry *e;

   if ((mitm_args = strchr(name, ':')) != NULL) {
      *mitm_args = '\0';
      mitm_args ++;
   } else {
      mitm_args = "";
   }
   
   DEBUG_MSG("mitm_set: %s (%s)", name, mitm_args);
   
   /* search the name and set it */
   SLIST_FOREACH(e, &mitm_table, next) {
      if (!strcasecmp(e->mm->name, name)) {
         e->selected = 1;
         return E_SUCCESS;
      }
   }

   return -E_NOTFOUND;
}

/*
 * return 1 if the mitm method is active
 */
int is_mitm_active(char *name)
{
   struct mitm_entry *e;
   
   /* search the name and set it */
   SLIST_FOREACH(e, &mitm_table, next)
      if (!strcasecmp(e->mm->name, name))
         return e->started;
         
   return 0;
}

/* 
 * starts all the method with the selected flag set.
 * it is possible to start multiple method simultaneusly
 */
int mitm_start(void)
{
   struct mitm_entry *e;

   /* reading from file we won't start mitm */
   if (GBL_OPTIONS->read || GBL_OPTIONS->unoffensive) {
      DEBUG_MSG("mitm_start: skipping");
      return -E_INVALID;
   }

      
   DEBUG_MSG("mitm_start");
   
   /* start all the selected methods */
   SLIST_FOREACH(e, &mitm_table, next) {
      if (e->selected && !e->started) {
   
         /* cant use -R with mitm methods */
         if (GBL_OPTIONS->reversed)
            SEMIFATAL_ERROR("Reverse target matching can't be used with MITM attacks");
  
         if (!GBL_IFACE->is_ready)
            SEMIFATAL_ERROR("MITM attacks can't be used on unconfigured interfaces");
         
         DEBUG_MSG("mitm_start: starting %s", e->mm->name);

         /* 
          * if the mitm method does not start correctly,
          * deselect it !
          */
         if (e->mm->start(mitm_args) == E_SUCCESS)
            e->started = 1;
         else
            e->selected = 0;
      }
   }

   return E_SUCCESS;
}


/*
 * stop all the previously started method
 */
void mitm_stop(void)
{
   struct mitm_entry *e;

   DEBUG_MSG("mitm_stop");
   
   /* stop all the started methods */
   SLIST_FOREACH(e, &mitm_table, next) {
      if (e->started) {
         DEBUG_MSG("mitm_stop: stopping %s", e->mm->name);
         e->mm->stop();
         e->started = 0;
         e->selected = 0;
      }
   }
   
}


/*
 * keep the process running until the user exits
 */
void only_mitm(void)
{
   char ch = 0;
   
   /* build the list of active hosts */
   build_hosts_list();
   
   /* start the mitm attack */
   mitm_start();

   INSTANT_USER_MSG("Activated the mitm attack only... (press 'q' to exit)\n");

   if (GBL_UI->type == UI_DAEMONIZE)
       LOOP {
           ec_usleep(SEC2MICRO(1));
       }
  
   /* wait for user to exit */
   while (ch != 'q' && ch != 'Q') {
      /* if there is a pending char to be read */
      if ( ec_poll_in(fileno(stdin), 1) || ec_poll_buffer(GBL_OPTIONS->script) ) {
         /* get the input from the stdin or the buffer */
         if (ec_poll_buffer(GBL_OPTIONS->script))
            ch = getchar_buffer(&GBL_OPTIONS->script);
         else
            ch = getchar();
      }
   }
   
   INSTANT_USER_MSG("Exiting...\n\n");

   /* stop the process */
   mitm_stop();

   /* perform a clean exit */
   clean_exit(0);
}

/* EOF */

// vim:ts=3:expandtab