ec_main.c
上传用户:nilegod
上传日期:2007-01-08
资源大小:220k
文件大小:13k
- /*
- ettercap -- a ncurses-based sniffer/interceptor utility for switched LAN
- Copyright (C) 2001 ALoR <alor@users.sourceforge.net>, NaGA <crwm@freemail.it>
- 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 <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include "include/ec_main.h"
- #include "include/ec_error.h"
- #include "include/ec_inet.h"
- #include "include/ec_simple.h"
- #include "include/ec_signal.h"
- #ifdef HAVE_GETOPT_H
- #include <getopt.h>
- #else
- #include "missing/getopt.h"
- #endif
- #ifdef HAVE_NCURSES
- #include "include/ec_interface.h"
- #endif
- #ifdef DEBUG
- #include "include/ec_debug.h"
- #endif
- // global variables
- char *program_argv0;
- HOST *Host_In_LAN = NULL; // ec_main.h
- int number_of_hosts_in_lan;
- CONNECTION *Conn_Between_Hosts = NULL; // ec_main.h
- int number_of_connections;
- HOST Host_Source;
- HOST Host_Dest;
- char *Execute_Plugin;
- OPTIONS Options;
- int pipe_with_illithid;
- int pipe_with_illithid_data;
- int pipe_inject_stod[2];
- int pipe_inject_dtos[2];
- char active_dissector = 1; // ec_main.h
- // protos...
- void Main_Usage(void);
- void Main_Interactive(void);
- void Main_Normal(void);
- void Main_CheckForRun(void);
- void Main_ParseParameters(char *first, char *second, char *third, char *fourth);
- //-----------------------------------
- int main(int argc, char *argv[])
- {
- int c;
- static struct option long_options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version", no_argument, NULL, 'v' },
- { "simple", no_argument, NULL, 'N' },
- { "list", no_argument, NULL, 'l' },
- { "arpsniff", no_argument, NULL, 'a' },
- { "sniff", no_argument, NULL, 's' },
- { "macsniff", no_argument, NULL, 'm' },
- { "iface", required_argument, NULL, 'i' },
- { "netmask", required_argument, NULL, 'n' },
- { "check", no_argument, NULL, 'c' },
- { "plugin", required_argument, NULL, 'p' },
- { "hexview", no_argument, NULL, 'x' },
- { "silent", no_argument, NULL, 'z' },
- { "udp", no_argument, NULL, 'u' },
- { "fingerprint", no_argument, NULL, 'f' },
- { "linktype", no_argument, NULL, 't' },
- { "collect", no_argument, NULL, 'C' },
- { "broadping", no_argument, NULL, 'b' },
- { 0 , 0 , 0 , 0}
- };
- program_argv0 = argv[0];
- #ifdef DEBUG
- Debug_Init();
- Debug_msg("Main");
- #endif
- Main_CheckForRun(); // it is ok ?
- Signal_SigBuster(); // signal masking
- #ifdef PERMIT_PLUGINS
- while ((c = getopt_long (argc, argv, "hvNlasmci:p:xzuftCbn:",long_options, (int *)0)) != EOF) {
- #else
- while ((c = getopt_long (argc, argv, "hvNlasmci:xzuftCbn:",long_options, (int *)0)) != EOF) { // no plugin
- #endif
- switch (c) {
- case 'h':
- Main_Usage();
- break;
- case 'v':
- fprintf(stdout,"%sn",VERSION);
- exit(0);
- break;
- case 'N': Options.normal = 1; break;
- case 'l': Options.list = 1; break;
- case 'a': Options.arpsniff = 1; break;
- case 's': Options.sniff = 1; break;
- case 'm': Options.macsniff = 1; break;
- case 'c': Options.check = 1; break;
- case 'x': Options.hexview = 1; break;
- case 'z': Options.silent = 1; break;
- case 'u': Options.udp = 1; break;
- case 'f': Options.finger = 1; break;
- case 't': Options.link = 1; break;
- case 'C': Options.collect = 1; break;
- case 'b': Options.broadping = 1; break;
- #ifdef PERMIT_PLUGINS
- case 'p':
- Options.plugin = 1;
- Execute_Plugin = strdup(optarg);
- break;
- #endif
- case 'i':
- strncpy(Options.netiface, optarg, 10);
- break;
- case 'n':
- strncpy(Options.netmask, optarg, 16);
- break;
- case ':': // missing parameter
- Main_Usage();
- break;
- case '?': // unknown option
- Main_Usage();
- break;
- }
- }
- Main_ParseParameters(argv[optind], argv[optind+1], argv[optind+2], argv[optind+3]);
- if (!strcmp(Options.netiface, "")) // set the default interface
- if (Inet_FindIFace(Options.netiface) == -1)
- Error_msg("No suitable Network Interface found !!");
- if ( Inet_CorrectIface(Options.netiface) < 0)
- Error_msg("Network Interface %s is invalid !!", Options.netiface);
- if (Options.normal)
- Main_Normal();
- else
- Main_Interactive();
- return 0;
- }
- void Main_Usage(void)
- {
- #ifdef DEBUG
- Debug_msg("Main_Usage");
- #endif
- fprintf (stdout, "n 33[01m 33[1m%s %s (c) 2001 %s 33[0mnn", PROGRAM, VERSION, AUTHORS);
- fprintf (stdout, "nUsage: %s [OPTION] [HOST:PORT] [HOST:PORT] [MAC] [MAC]nn", PROGRAM);
- fprintf (stdout, " -N, --simple NON interactive mode (without ncurses)nn");
- fprintf (stdout, " -z, --silent silent mode (no arp storm on start up)nn");
- fprintf (stdout, " -b, --broadping broadcast ping instead of arp storm on start upnn");
- fprintf (stdout, " -u, --udp sniff only udp connection (default is tcp)nn");
- fprintf (stdout, " -a, --arpsniff arp based sniffing (must specify two host)n");
- fprintf (stdout, " in silent mode : must specify two IP and two MACn");
- fprintf (stdout, " i.e.: ettercap -za IP IP MAC MACnn");
- fprintf (stdout, " -s, --sniff ip based sniffingn");
- fprintf (stdout, " you can specify null ip that means ALL hostsnn");
- fprintf (stdout, " -m, --macsniff mac based sniffing (must specify two host)n");
- fprintf (stdout, " in silent mode : must specify two MAC addressn");
- fprintf (stdout, " i.e.: ettercap -zm MAC MACnn");
- fprintf (stdout, " -l, --list list all hosts in the lannn");
- fprintf (stdout, " -f, --fingerprint HOST do OS fingerprinting on HOSTnn");
- fprintf (stdout, " -C, --collect collect users and passwords onlyn");
- fprintf (stdout, " this options must be used with a sniffing methodn");
- fprintf (stdout, " Es: ettercap -NCzsnn");
- fprintf (stdout, " -x, --hexview display data in hex modenn");
- fprintf (stdout, " -i, --iface IFACE network interface to be usednn");
- fprintf (stdout, " -n, --netmask NETMASK the netmask used to scan the lannn");
- fprintf (stdout, " -c, --check check for other poisoners in the LANnn");
- fprintf (stdout, " -t, --linktype am I in a switched LAN or not ?nn");
- #ifdef PERMIT_PLUGINS
- fprintf (stdout, " -p, --plugin [name] run the "name" plugin ("list" for available ones)nn");
- #endif
- fprintf (stdout, " -v, --version print versionnn");
- fprintf (stdout, " -h, --help print this helpnn");
- fprintf (stdout, "n");
- exit (0);
- }
- void Main_Interactive(void)
- {
- #ifdef HAVE_NCURSES
- struct winsize ws = {0, 0, 0, 0};
- #endif
- #ifdef DEBUG
- Debug_msg("Main_Interactive");
- #endif
- #ifdef HAVE_NCURSES
- ioctl(0, TIOCGWINSZ, &ws); // syscall for the window size
- if ( (ws.ws_row < 25) || (ws.ws_col < 80) )
- Error_msg("Screen must be at least 25x80 !!");
- if (!Options.silent) printf("Building host list for netmask %s, please wait...n", Inet_MySubnet());
- number_of_hosts_in_lan = Inet_HostInLAN();
- Interface_InitTitle(Host_In_LAN[0].ip, Host_In_LAN[0].mac, Inet_MySubnet());
- Interface_InitScreen();
- Interface_Run();
- #else
- #ifdef DEBUG
- Debug_msg("Ncurses not supported -- turning to non interactive mode...");
- #endif
- Main_Normal();
- #endif
- #ifdef DEBUG
- Debug_msg("Main_Interactive_END");
- #endif
- }
- void Main_Normal(void)
- {
- #ifdef DEBUG
- Debug_msg("Main_Normal");
- #endif
- printf ("n 33[01m 33[1m%s %s (c) 2001 %s 33[0mnn", PROGRAM, VERSION, AUTHORS);
- printf ("Your IP: %s with MAC: %s on Iface: %sn", Inet_MyIPAddress(), Inet_MyMACAddress(), Options.netiface);
- if (Options.list || Options.check || Options.arpsniff || Options.sniff || Options.macsniff || Options.link)
- {
- if (!Options.silent) printf("Building host list for netmask %s, please wait...n", Inet_MySubnet());
- number_of_hosts_in_lan = Inet_HostInLAN();
- }
- if (Options.list)
- Simple_HostList();
- if (Options.check)
- Simple_CheckForPoisoner();
- if (Options.link)
- Simple_CheckForSwitch();
- if (Options.finger)
- Simple_FingerPrint();
- #ifdef PERMIT_PLUGINS
- if (Options.plugin)
- Simple_Plugin();
- #endif
- if (Options.arpsniff || Options.sniff || Options.macsniff)
- Simple_Run();
- printf("n");
- #ifdef DEBUG
- Debug_msg("Main_Normal_END");
- #endif
- exit(0);
- }
- void Main_CheckForRun(void)
- {
- #ifdef DEBUG
- Debug_msg("Main_CheckForRun");
- #endif
- #ifdef PERMIT_SUID
- if (geteuid() != 0)
- Error_msg("Sorry EUID %d, must be EUID = 0 to run %s !!", geteuid(), PROGRAM);
- #else
- if (getuid() != 0)
- Error_msg("Sorry UID %d, must be root to run %s !!", getuid(), PROGRAM);
- #endif
- }
- void Main_ParseParameters(char *first, char *second, char *third, char *fourth)
- {
- #ifdef DEBUG
- Debug_msg("Main_ParseParameters");
- #endif
- #define R(a,b,c) (a & b) | ((a ^ b) & c) // returns true if more than one was selected
- if ( R(Options.arpsniff, Options.sniff, Options.macsniff) )
- Error_msg("Please select only one sniffing method !!nn");
- if (Options.silent && Options.broadping)
- Error_msg("Please select only one start up method !!nn");
- if (Options.silent)
- {
- if (Options.macsniff)
- {
- int i=0;
- if (first)
- {
- i++;
- sscanf(first, "%17s", Host_Dest.mac);
- if (second)
- {
- i++;
- sscanf(second, "%17s", Host_Source.mac);
- }
- }
- if (i < 2) Error_msg("Please specify both source and destination MAC !!");
- }
- if (Options.arpsniff)
- {
- int i=0;
- if (first)
- {
- i++;
- sscanf(first, "%128[^:]:%d", Host_Dest.name, &Host_Dest.port);
- if (second)
- {
- i++;
- sscanf(second, "%128[^:]:%d", Host_Source.name, &Host_Source.port);
- if (third)
- {
- i++;
- sscanf(third, "%17s", Host_Dest.mac);
- if (fourth)
- {
- i++;
- sscanf(fourth, "%17s", Host_Source.mac);
- }
- }
- }
- }
- if (i < 4) Error_msg("Please specify both source and destination IP and MAC !!");
- }
- if (Options.sniff || Options.plugin)
- {
- if (first)
- {
- sscanf(first, "%128[^:]:%d", Host_Dest.name, &Host_Dest.port);
- if (second) sscanf(second, "%128[^:]:%d", Host_Source.name, &Host_Source.port);
- }
- }
- if (Options.check) Error_msg("You can't check for poisoners in silent mode !!");
- if (Options.list) Error_msg("You can't make the list in silent mode !!");
- }
- else // !silent
- {
- if (first)
- {
- sscanf(first, "%128[^:]:%d", Host_Dest.name, &Host_Dest.port);
- if (second) sscanf(second, "%128[^:]:%d", Host_Source.name, &Host_Source.port);
- }
- if (Options.arpsniff || Options.macsniff )
- if ( (!strcmp(Host_Source.name, "")) || (!strcmp(Host_Dest.name, "")) )
- Error_msg("Please specify both source and destination host !!");
- }
- if (strcmp(Host_Source.name, ""))
- strcpy(Host_Source.ip, Inet_NameToIp(Host_Source.name));
- if (strcmp(Host_Dest.name, ""))
- strcpy(Host_Dest.ip, Inet_NameToIp(Host_Dest.name));
- #ifdef DEBUG
- Debug_msg("Main_ParseParameters - name - [%s][%s]", Host_Dest.name, Host_Source.name);
- Debug_msg("Main_ParseParameters - IP - [%s][%s]", Host_Dest.ip, Host_Source.ip);
- Debug_msg("Main_ParseParameters - MAC - [%s][%s]", Host_Dest.mac, Host_Source.mac);
- #endif
- }
- /* EOF */