"Fossies" - the Fresh Open Source Software Archive 
Member "ettercap-0.8.3.1/src/interfaces/curses/ec_curses_mitm.c" (1 Aug 2020, 14959 Bytes) of package /linux/privat/ettercap-0.8.3.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "ec_curses_mitm.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
0.8.3_vs_0.8.3.1.
1 /*
2 ettercap -- curses GUI
3
4 Copyright (C) ALoR & NaGA
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 */
21
22 #include <ec.h>
23 #include <wdg.h>
24 #include <ec_curses.h>
25 #include <ec_mitm.h>
26 #include <ec_redirect.h>
27
28 /* proto */
29
30 static void curses_arp_poisoning(void);
31 static void curses_icmp_redir(void);
32 static void curses_port_stealing(void);
33 static void curses_dhcp_spoofing(void);
34 #ifdef WITH_IPV6
35 static void curses_ndp_poisoning(void);
36 #endif
37 static void curses_start_mitm(void);
38 static void curses_mitm_stop(void);
39
40 static void curses_sslredir_show(void);
41 static void curses_sslredir_create_lists(void);
42 static void curses_sslredir_destroy(void);
43 static void curses_sslredir_update(void);
44 static void curses_sslredir_add_list(struct redir_entry *re);
45 static void curses_sslredir_add_service(struct serv_entry *se);
46 static void curses_sslredir_add(void *dummy);
47 static void curses_sslredir_add_rule(void);
48 static void curses_sslredir_del(void *dummy);
49 static void curses_sslredir_help(void *dummy);
50
51 /* globals */
52
53 #define PARAMS_LEN 64
54 #define MAX_DESC_LEN 75
55
56 static char params[PARAMS_LEN];
57
58 struct wdg_menu menu_mitm[] = { {"Mitm", 'M', "", NULL},
59 {"ARP poisoning...", 0, "", curses_arp_poisoning},
60 {"ICMP redirect...", 0, "", curses_icmp_redir},
61 {"PORT stealing...", 0, "", curses_port_stealing},
62 {"DHCP spoofing...", 0, "", curses_dhcp_spoofing},
63 #ifdef WITH_IPV6
64 {"NDP poisoning...", 0, "", curses_ndp_poisoning},
65 #endif
66 {"-", 0, "", NULL},
67 {"Stop mitm attack(s)", 0, "", curses_mitm_stop},
68 {"-", 0, "", NULL},
69 {"SSL Intercept", 0, "", curses_sslredir_show},
70 {NULL, 0, NULL, NULL},
71 };
72
73 static wdg_t *wdg_redirect = NULL;
74 static struct wdg_list *wdg_redirect_elements = NULL;
75 static struct wdg_list *wdg_redirect_services = NULL;
76 static size_t n_redir = 0;
77 static size_t n_serv = 0;
78 static char redir_proto[5] = "ipv4";
79 static char redir_name[50] = "ftps";
80 static char redir_destination[MAX_ASCII_ADDR_LEN] = "0.0.0.0/0";
81
82
83 /*******************************************/
84
85 static void curses_arp_poisoning(void)
86 {
87 char *method = "arp:";
88 char *default_param = "remote";
89 size_t len = strlen(method);
90
91 DEBUG_MSG("curses_arp_poisoning");
92
93 snprintf(params, PARAMS_LEN, "%s%s", method, default_param);
94
95 curses_input("Parameters :", params + len, PARAMS_LEN - len - 1, curses_start_mitm);
96 }
97
98 static void curses_icmp_redir(void)
99 {
100 char *method = "icmp:";
101 size_t len = strlen(method);
102
103 DEBUG_MSG("curses_icmp_redir");
104
105 strncpy(params, method, len);
106
107 curses_input("Parameters :", params + len, PARAMS_LEN - len - 1, curses_start_mitm);
108 }
109
110 static void curses_port_stealing(void)
111 {
112 char *method = "port:";
113 size_t len = strlen(method);
114
115 DEBUG_MSG("curses_port_stealing");
116
117 strncpy(params, method, len);
118
119 curses_input("Parameters :", params + len, PARAMS_LEN - len - 1, curses_start_mitm);
120 }
121
122 static void curses_dhcp_spoofing(void)
123 {
124 char *method = "dhcp:";
125 size_t len = strlen(method);
126
127 DEBUG_MSG("curses_dhcp_spoofing");
128
129 strncpy(params, method, len);
130
131 curses_input("Parameters :", params + len, PARAMS_LEN - len - 1, curses_start_mitm);
132 }
133
134 #ifdef WITH_IPV6
135 static void curses_ndp_poisoning(void)
136 {
137 char *method = "ndp:";
138 char *default_param = "remote";
139 size_t len = strlen(method);
140
141 DEBUG_MSG("curses_ndp_poisoning");
142
143 snprintf(params, PARAMS_LEN, "%s%s", method, default_param);
144
145 curses_input("Parameters :", params + len, PARAMS_LEN - len - 1, curses_start_mitm);
146 }
147 #endif
148
149 /*
150 * start the mitm attack by passing the name and parameters
151 */
152 static void curses_start_mitm(void)
153 {
154 DEBUG_MSG("curses_start_mitm");
155
156 mitm_set(params);
157 mitm_start();
158 }
159
160
161 /*
162 * stop all the mitm attack(s)
163 */
164 static void curses_mitm_stop(void)
165 {
166 wdg_t *dlg;
167
168 DEBUG_MSG("curses_mitm_stop");
169
170 /* create the dialog */
171 wdg_create_object(&dlg, WDG_DIALOG, WDG_OBJ_WANT_FOCUS);
172
173 wdg_set_color(dlg, WDG_COLOR_SCREEN, EC_COLOR);
174 wdg_set_color(dlg, WDG_COLOR_WINDOW, EC_COLOR);
175 wdg_set_color(dlg, WDG_COLOR_FOCUS, EC_COLOR_FOCUS);
176 wdg_set_color(dlg, WDG_COLOR_TITLE, EC_COLOR_TITLE);
177 wdg_dialog_text(dlg, WDG_NO_BUTTONS, "Stopping the mitm attack...");
178 wdg_draw_object(dlg);
179
180 wdg_set_focus(dlg);
181
182 wdg_update_screen();
183
184 /* stop the mitm process */
185 mitm_stop();
186
187 wdg_destroy_object(&dlg);
188
189 curses_message("MITM attack(s) stopped");
190 }
191
192 /*
193 * build SSL Redir window
194 */
195 static void curses_sslredir_show(void)
196 {
197 DEBUG_MSG("curses_sslredir_show()");
198
199 /* create the array for the list widget */
200 curses_sslredir_create_lists();
201
202 /* if the object already exists, set the focus to it */
203 if (wdg_redirect) {
204 /* set the new array */
205 wdg_list_set_elements(wdg_redirect, wdg_redirect_elements);
206 return;
207 }
208
209 wdg_create_object(&wdg_redirect, WDG_LIST, WDG_OBJ_WANT_FOCUS);
210
211 wdg_set_size(wdg_redirect, 1, 2, -1, SYSMSG_WIN_SIZE - 1);
212 wdg_set_title(wdg_redirect, "Delete or Insert SSL Intercept rules",
213 WDG_ALIGN_LEFT);
214 wdg_set_color(wdg_redirect, WDG_COLOR_SCREEN, EC_COLOR);
215 wdg_set_color(wdg_redirect, WDG_COLOR_WINDOW, EC_COLOR);
216 wdg_set_color(wdg_redirect, WDG_COLOR_BORDER, EC_COLOR_BORDER);
217 wdg_set_color(wdg_redirect, WDG_COLOR_FOCUS, EC_COLOR_FOCUS);
218 wdg_set_color(wdg_redirect, WDG_COLOR_TITLE, EC_COLOR_TITLE);
219
220 /* set the elements */
221 wdg_list_set_elements(wdg_redirect, wdg_redirect_elements);
222
223 /* add the destroy callback */
224 wdg_add_destroy_key(wdg_redirect, KEY_ESC, curses_sslredir_destroy);
225
226 /* add the insert and delete callback */
227 wdg_list_add_callback(wdg_redirect, KEY_IC, curses_sslredir_add);
228 wdg_list_add_callback(wdg_redirect, KEY_DC, curses_sslredir_del);
229 wdg_list_add_callback(wdg_redirect, ' ', curses_sslredir_help);
230
231 wdg_draw_object(wdg_redirect);
232
233 wdg_set_focus(wdg_redirect);
234
235 }
236
237 static void curses_sslredir_destroy(void)
238 {
239 wdg_redirect = NULL;
240 }
241
242 static void curses_sslredir_help(void *dummy)
243 {
244 /* varable not used */
245 (void) dummy;
246
247 char help[] = "HELP: shortcut list:\n\n"
248 " INSERT - insert a new redirect rule\n"
249 " DELETE - delete a redirect rule";
250
251 curses_message(help);
252 }
253
254 /*
255 * dialog to add new redirect rule
256 */
257 static void curses_sslredir_add(void *dummy)
258 {
259 wdg_t *wdg_input;
260
261 DEBUG_MSG("curses_sslredir_add()");
262
263 /* unused variable */
264 (void) dummy;
265
266 wdg_create_object(&wdg_input, WDG_INPUT,
267 WDG_OBJ_WANT_FOCUS | WDG_OBJ_FOCUS_MODAL);
268
269 wdg_set_color(wdg_input, WDG_COLOR_SCREEN, EC_COLOR);
270 wdg_set_color(wdg_input, WDG_COLOR_WINDOW, EC_COLOR);
271 wdg_set_color(wdg_input, WDG_COLOR_FOCUS, EC_COLOR_FOCUS);
272 wdg_set_color(wdg_input, WDG_COLOR_TITLE, EC_COLOR_MENU);
273 wdg_input_size(wdg_input, strlen("Destination: ") +
274 MAX_ASCII_ADDR_LEN, 6);
275 wdg_input_add(wdg_input, 1, 1, "IP Version: ", redir_proto, 5, 1);
276 wdg_input_add(wdg_input, 1, 3, "Server IP: ", redir_destination,
277 MAX_ASCII_ADDR_LEN, 1);
278 wdg_input_add(wdg_input, 1, 4, "Service: ", redir_name, 10, 1);
279
280 wdg_input_set_callback(wdg_input, curses_sslredir_add_rule);
281
282 wdg_draw_object(wdg_input);
283
284 wdg_set_focus(wdg_input);
285
286
287 }
288
289 /*
290 * callback inserting the actual rule
291 */
292 static void curses_sslredir_add_rule(void)
293 {
294 int ret;
295 size_t len, new_len, i = 0;
296 struct serv_entry *se = NULL;
297 ec_redir_proto_t proto;
298 char *services_available = NULL;
299
300 DEBUG_MSG("curses_sslredir_add_rule()");
301
302 /* check ip version string */
303 if (!strcasecmp(redir_proto, "ipv4"))
304 proto = EC_REDIR_PROTO_IPV4;
305 else if (!strcasecmp(redir_proto, "ipv6"))
306 proto = EC_REDIR_PROTO_IPV6;
307 else {
308 DEBUG_MSG("curses_sslredir_add_rule(): '%s' invalid IP version string",
309 redir_proto);
310 #ifdef WITH_IPV6
311 curses_message("Invalid IP version string. Use either \"ipv4\" or "
312 "\"ipv6\".\n");
313 #else
314 curses_message("Invalid IP version string. Use \"ipv4\".\n");
315 #endif
316 return;
317 }
318
319 /* check service name */
320 if (wdg_redirect_services == NULL) {
321 DEBUG_MSG("curses_sslredir_add_rule(): "
322 "no redirect services registered");
323 INSTANT_USER_MSG("No redirect services registered. "
324 "Is SSL redirection enabled in etter.conf?");
325 return;
326 }
327
328 while (wdg_redirect_services[i].desc != NULL) {
329 if (!strcasecmp(redir_name, wdg_redirect_services[i].desc)) {
330 se = (struct serv_entry *) wdg_redirect_services[i].value;
331 break;
332 }
333 i++;
334 }
335
336 /* redirect name not found - display available redirects */
337 if (se == NULL) {
338 services_available = strdup("Services available: \n");
339 for (i=0; i < n_serv; i++) {
340 len = strlen(services_available);
341 new_len = len+strlen(wdg_redirect_services[i].desc)+4+1;
342 SAFE_REALLOC(services_available, new_len);
343 snprintf(services_available+len, new_len, " * %s\n",
344 wdg_redirect_services[i].desc);
345 }
346 curses_message(services_available);
347 SAFE_FREE(services_available);
348 return;
349 }
350
351
352 /* do the actual redirect insertion */
353 ret = ec_redirect(EC_REDIR_ACTION_INSERT, se->name, proto,
354 redir_destination, se->from_port, se->to_port);
355
356 /* inform user if redirect insertion wasn't successful */
357 if (ret != E_SUCCESS) {
358 DEBUG_MSG("calling ec_redirect('%s', '%s', '%s', '%s', '%d', '%d'"
359 " failed", "insert", se->name, redir_proto,
360 redir_destination, se->from_port, se->to_port);
361
362 INSTANT_USER_MSG("Inserting redirect for %s/%s failed!\n",
363 redir_proto, redir_name);
364 }
365
366 /* update redirect list */
367 curses_sslredir_update();
368 }
369 /*
370 * callback to delete a certain redirect rule
371 */
372 static void curses_sslredir_del(void *dummy)
373 {
374 struct redir_entry *re;
375 int ret;
376
377 DEBUG_MSG("curses_sslredir_del()");
378
379 /* prevent the selection when the list is empty */
380 if (dummy == NULL)
381 return;
382
383 /* remove the redirect */
384 re = (struct redir_entry *)dummy;
385 ret = ec_redirect(EC_REDIR_ACTION_REMOVE, re->name, re->proto,
386 re->destination, re->from_port, re->to_port);
387
388
389 if (ret != E_SUCCESS) {
390 DEBUG_MSG("calling ec_redirect('%s', '%s', '%s', '%s', '%d', '%d'"
391 " failed", "remove", re->name,
392 (re->proto == EC_REDIR_PROTO_IPV4 ? "ipv4" : "ipv6"),
393 re->destination, re->from_port, re->to_port);
394
395 INSTANT_USER_MSG("Removing redirect for %s/%s failed!\n",
396 (re->proto == EC_REDIR_PROTO_IPV4 ? "ipv4" : "ipv6"), re->name);
397
398 return;
399 }
400
401 curses_sslredir_update();
402
403 }
404
405 static void curses_sslredir_create_lists(void)
406 {
407 int res, i = 0;
408
409 DEBUG_MSG("curses_sslredir_create_lists()");
410
411 /* free the array (if allocated */
412 while (wdg_redirect_elements && wdg_redirect_elements[i].desc != NULL) {
413 SAFE_FREE(wdg_redirect_elements[i].desc);
414 i++;
415 }
416 SAFE_FREE(wdg_redirect_elements);
417 n_redir = 0;
418
419 /* walk through the redirect rules */
420 ec_walk_redirects(&curses_sslredir_add_list);
421
422 /* services are only gathered once */
423 if (wdg_redirect_services != NULL)
424 return;
425
426 /* walk through the registered services */
427 res = ec_walk_redirect_services(&curses_sslredir_add_service);
428 if (res == -E_NOTFOUND) {
429 SAFE_CALLOC(wdg_redirect_elements, 1, sizeof(struct wdg_list));
430 wdg_redirect_elements->desc = "No rules found. "
431 "Redirects may be not enabled in etter.conf?";
432 }
433
434 }
435
436 static void curses_sslredir_add_list(struct redir_entry *re)
437 {
438 /* enlarge the array */
439 SAFE_REALLOC(wdg_redirect_elements, (n_redir+1) * sizeof(struct wdg_list));
440
441 /* fill the element */
442 SAFE_CALLOC(wdg_redirect_elements[n_redir].desc, MAX_DESC_LEN,
443 sizeof(char));
444
445 snprintf(wdg_redirect_elements[n_redir].desc, MAX_DESC_LEN,
446 "%s %30s %s",
447 (re->proto == EC_REDIR_PROTO_IPV4 ? "ipv4" : "ipv6"),
448 re->destination,
449 re->name);
450
451 wdg_redirect_elements[n_redir].value = re;
452
453 n_redir++;
454
455 /* allocate new entry in list to move the NULL element */
456 SAFE_REALLOC(wdg_redirect_elements, (n_redir+1) * sizeof(struct wdg_list));
457 wdg_redirect_elements[n_redir].desc = NULL;
458 wdg_redirect_elements[n_redir].value = NULL;
459 }
460
461 /*
462 * populate array for available services
463 */
464 static void curses_sslredir_add_service(struct serv_entry *se)
465 {
466 DEBUG_MSG("curses_sslredir_add_service()");
467
468 /* enlarge the array */
469 SAFE_REALLOC(wdg_redirect_services, (n_serv+1) * sizeof(struct wdg_list));
470
471 /* fill the element */
472 SAFE_CALLOC(wdg_redirect_services[n_serv].desc, MAX_DESC_LEN,
473 sizeof(char));
474
475 snprintf(wdg_redirect_services[n_serv].desc, MAX_DESC_LEN, "%s", se->name);
476
477 wdg_redirect_services[n_serv].value = se;
478
479 n_serv++;
480
481 /* allocate new entry in list to move the NULL element */
482 SAFE_REALLOC(wdg_redirect_services, (n_serv+1) * sizeof(struct wdg_list));
483 wdg_redirect_services[n_serv].desc = NULL;
484 wdg_redirect_services[n_serv].value = NULL;
485 }
486
487 /*
488 * refresh redirects list
489 */
490 static void curses_sslredir_update(void)
491 {
492 int i = 0;
493 DEBUG_MSG("curses_sslredir_update()");
494
495 /* rebuild array */
496 while (wdg_redirect_elements && wdg_redirect_elements[i].desc != NULL) {
497 SAFE_FREE(wdg_redirect_elements[i].desc);
498 i++;
499 }
500 SAFE_FREE(wdg_redirect_elements);
501
502 n_redir = 0;
503 ec_walk_redirects(&curses_sslredir_add_list);
504
505 /* NULL terminate the array in case it's empty */
506 if (wdg_redirect_elements == NULL) {
507 SAFE_CALLOC(wdg_redirect_elements, 1, sizeof(struct wdg_list));
508 wdg_redirect_elements[0].desc = NULL;
509 wdg_redirect_elements[0].value = NULL;
510 }
511
512 /* refresh list widget */
513 wdg_list_set_elements(wdg_redirect, wdg_redirect_elements);
514 wdg_list_refresh(wdg_redirect);
515
516 }
517
518
519
520 /* EOF */
521
522 // vim:ts=3:expandtab
523