ec_simple.c
上传用户:nilegod
上传日期:2007-01-08
资源大小:220k
文件大小:16k
源码类别:

网络截获/分析

开发平台:

C/C++

  1. /*
  2.     ettercap -- module for NON ncurses interface
  3.     Copyright (C) 2001  ALoR <alor@users.sourceforge.net>, NaGA <crwm@freemail.it>
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.     This program is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.     GNU General Public License for more details.
  12.     You should have received a copy of the GNU General Public License
  13.     along with this program; if not, write to the Free Software
  14.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <fcntl.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/time.h>
  23. #include <signal.h>
  24. #include "include/ec_main.h"
  25. #include "include/ec_error.h"
  26. #include "include/ec_inet.h"
  27. #include "include/ec_inet_structures.h"
  28. #include "include/ec_decodedata.h"
  29. #include "include/ec_dissector.h"
  30. #include "include/ec_illithid.h"
  31. #include "include/ec_doppleganger.h"
  32. #include "include/ec_buffer.h"
  33. #include "include/ec_plugins.h"
  34. #include "include/ec_fingerprint.h"
  35. #ifdef DEBUG
  36.    #include "include/ec_debug.h"
  37. #endif
  38. #ifdef HAVE_TERMIOS_H
  39.    #include <termios.h>
  40.    struct termios old_tc;
  41.    struct termios new_tc;
  42.    void set_raw_term(void);
  43.    void reset_term(void);
  44. #endif
  45. #define ASCII_VIEW      0
  46. #define HEX_VIEW        1
  47. extern char *Execute_Plugin;
  48. // protos...
  49. #ifdef PERMIT_PLUGINS
  50.    void Simple_Plugin(void);
  51. #endif
  52. void Simple_HostList(void);
  53. void Simple_Run(void);
  54. void Simple_CheckForPoisoner(void);
  55. void Simple_FingerPrint(void);
  56. void Simple_CheckForSwitch(void);
  57. //---------------------------------
  58. void Simple_HostList(void)
  59. {
  60.    int j;
  61. #ifdef DEBUG
  62.    Debug_msg("Simple_HostList");
  63. #endif
  64.    printf("Host in this LAN:nn");
  65.    for(j=0; j<number_of_hosts_in_lan; j++)
  66.    {
  67.       printf("%3d) %st%st%sn", j+1, Host_In_LAN[j].ip, Host_In_LAN[j].mac, Host_In_LAN[j].name);
  68.    }
  69.    printf("n");
  70. }
  71. void Simple_FingerPrint(void)
  72. {
  73.    int IP;
  74.    char MAC[6];
  75.    char MACs[17];
  76. #ifdef DEBUG
  77.    Debug_msg("Simple_FingerPrint -- [%s]", Host_Dest.ip);
  78. #endif
  79.    IP = inet_addr(Host_Dest.ip);
  80.    memcpy(MAC, Inet_MacFromIP(IP), 6);
  81.    Inet_PutMACinString(MACs, MAC);
  82.    printf("nnFingerprinting %s...nn", Host_Dest.ip);
  83.    printf("n33[36mOperating System:33[0mnn%sn", Fingerprint_OS(Host_Dest.ip) );
  84.    printf("n33[36mNetwork Adapter :33[0mnn%sn", Fingerprint_MAC(MACs) );
  85.    printf("n");
  86. }
  87. void Simple_CheckForSwitch(void)
  88. {
  89.    short type;
  90.    switch(type = Inet_CheckSwitch())
  91.    {
  92.       case 0: printf("n Link Type: unknownn");
  93.               break;
  94.       case 1: printf("n Link Type: HUBn");
  95.               break;
  96.       case 2: printf("n Link Type: SWITCHn");
  97.               break;
  98.    }
  99. }
  100. void Simple_CheckForPoisoner(void)
  101. {
  102.    SniffingHost *SniffList;
  103.    int i;
  104.    short found = 0;
  105. #ifdef DEBUG
  106.    Debug_msg("Simple_CheckForPoisoner");
  107. #endif
  108.    printf("Checking for poisoners...nn");
  109.    SniffList = Inet_NoSniff();
  110.    for (i=0; i<number_of_hosts_in_lan; i++)
  111.    {
  112.       if (SniffList[i].mode == 0) break;
  113.       if (SniffList[i].mode == 1)
  114.       {
  115.          found = 1;
  116.          printf("%s is replying for %s", Host_In_LAN[SniffList[i].Host_Index1].ip, Host_In_LAN[SniffList[i].Host_Index2].ip);
  117.       }
  118.       if (SniffList[i].mode == 2)
  119.       {
  120.          found = 1;
  121.          printf("MAC of %s and %s are identical !",Host_In_LAN[SniffList[i].Host_Index1].ip,Host_In_LAN[SniffList[i].Host_Index2].ip);
  122.       }
  123.    }
  124.    if (!found)
  125.       printf("No poisoners found in this lan (seems to be safe)nn");
  126. }
  127. #ifdef PERMIT_PLUGINS
  128. void Simple_Plugin(void)
  129. {
  130.    char plugin[strlen(Execute_Plugin)+6];
  131.    short i, n, found=-1;
  132.    #include "include/ec_install_path.h"
  133. #ifdef DEBUG
  134.    Debug_msg("Simple_Plugin -- %s", Execute_Plugin);
  135. #endif
  136.    if (strcmp(Host_Source.name, ""))
  137.       strcpy(Host_Source.ip, Inet_NameToIp(Host_Source.name));
  138.    if (strcmp(Host_Dest.name, ""))
  139.       strcpy(Host_Dest.ip, Inet_NameToIp(Host_Dest.name));
  140. #ifdef DEBUG
  141.    Debug_msg("Simple_Plugin -- [%s] [%s] [%s] [%s]", Host_Source.name, Host_Source.ip, Host_Dest.name, Host_Dest.ip );
  142. #endif
  143.    sprintf(plugin, "ec_%s.so", Execute_Plugin);
  144.    n = Plugin_MakePlugList();
  145.    if (!strcmp(Execute_Plugin, "list"))
  146.    {
  147.       #include "include/ec_install_path.h"
  148.       if (n == 0)
  149.          Error_msg("n NO available plugin found in %s or ./ !!n", path);
  150.       fprintf(stdout, "n Available Plugins :nn");
  151.       for(i=0; i<n; i++)
  152.          fprintf(stdout, "%2d) %8s     v %.1f -- %sn", i+1, Plugin_Getname(Plugins_list[i].name), Plugins_list[i].version, Plugins_list[i].description);
  153.       return;
  154.    }
  155.    for(i=0; i<n; i++)
  156.    {
  157.       if ( !strcmp(Plugins_list[i].name, plugin) ) found = i;
  158.    }
  159.    if (found == -1)
  160.    {
  161.       fprintf(stderr, "nPlugin %s NOT found in %s or in ./ !n", plugin, path);
  162.       fprintf(stderr, "nuse -p list to view the available ones.nn");
  163.    }
  164.    else
  165.    {
  166.       char toexec[strlen(plugin)+strlen(Plugins_list[found].path)];
  167.       strcpy(toexec, Plugins_list[found].path);
  168.       strcat(toexec, plugin);
  169.       Plugin_RunPlugIn(toexec, NULL);
  170.    }
  171. }
  172. #endif
  173. #ifdef HAVE_TERMIOS_H
  174. void set_raw_term()        // taken from readchar.c, by M. Andreoli (2000)
  175. {
  176.     tcgetattr(0, &old_tc);
  177.     new_tc = old_tc;
  178.     new_tc.c_lflag &= ~(ECHO | ICANON);   /* raw output */
  179.     new_tc.c_cc[VTIME] = 1;
  180.     tcsetattr(0, TCSANOW, &new_tc);
  181. }
  182. void reset_term()          // taken from readchar.c, by M. Andreoli (2000)
  183. {
  184.     tcsetattr(0, TCSANOW, &old_tc);
  185. }
  186. #endif
  187. void Simple_Run(void)
  188. {
  189.    int Illithid_pid = 0, Dopple_pid = 0, Illithid_pid2 = 0;
  190.    int i, index_source=-1, index_dest=-1;
  191.    char view = ASCII_VIEW;
  192.    char stop = 0;
  193.    fd_set msk_fd;
  194.    struct timeval TimeOut;
  195.    SNIFFED_DATA data_from_illithid;
  196.    CONNECTION data_from_illithid_conn;
  197.    int datalen;
  198.    char source[16], dest[16];
  199.    char proto = 'T';
  200.    if (Options.udp) proto = 'U';
  201. #ifdef DEBUG
  202.    Debug_msg("Simple_Run - name - [%s][%s]", Host_Dest.name, Host_Source.name);
  203.    Debug_msg("Simple_Run -  IP  - [%s][%s]", Host_Dest.ip, Host_Source.ip);
  204.    Debug_msg("Simple_Run -  MAC - [%s][%s]", Host_Dest.mac, Host_Source.mac);
  205.    if (Options.sniff)      Debug_msg("Simple_Run - %c - sniff", proto);
  206.    if (Options.arpsniff)   Debug_msg("Simple_Run - %c - arpsniff", proto);
  207.    if (Options.macsniff)   Debug_msg("Simple_Run - %c - macsniff", proto);
  208. #endif
  209.    if (Options.hexview)
  210.       view = HEX_VIEW;
  211.    if (strcmp(Host_Source.name, ""))
  212.    {
  213.       strcpy(Host_Source.ip, Inet_NameToIp(Host_Source.name));
  214.       strcpy(source, Host_Source.ip);
  215.    }
  216.    else
  217.       strcpy(source, "ANY");
  218.    if (strcmp(Host_Dest.name, ""))
  219.    {
  220.       strcpy(Host_Dest.ip, Inet_NameToIp(Host_Dest.name));
  221.       strcpy(dest, Host_Dest.ip);
  222.    }
  223.    else
  224.       strcpy(dest, "ANY");
  225.    if (!Options.silent)
  226.    {
  227.       for(i=0; i<number_of_hosts_in_lan; i++)
  228.       {
  229.          if ( !strcmp(Host_Source.ip, Host_In_LAN[i].ip) )
  230.          {
  231.             index_source = i;
  232.             strcpy(Host_Source.mac, Host_In_LAN[i].mac);
  233.          }
  234.          if ( !strcmp(Host_Dest.ip, Host_In_LAN[i].ip) )
  235.          {
  236.             index_dest = i;
  237.             strcpy(Host_Dest.mac, Host_In_LAN[i].mac);
  238.          }
  239.       }
  240.       if ( index_source < 0 && strcmp(Host_Source.name, ""))
  241.          Error_msg("Source host %s (%s) not found !!", Host_Source.name, Host_Source.ip);
  242.       if ( index_dest < 0 && strcmp(Host_Dest.name, ""))
  243.          Error_msg("Dest host %s (%s) not found !!", Host_Dest.name, Host_Dest.ip);
  244.    }
  245.    if (!strcmp(Host_Source.ip, Host_Dest.ip) && strcmp(Host_Source.name, ""))
  246.       Error_msg("Source and Dest MUST be different !!");
  247.    if (Options.arpsniff)
  248.       if ( (!strcmp(Host_Source.ip, Inet_MyIPAddress())) || (!strcmp(Host_Dest.ip, Inet_MyIPAddress())) )
  249.          Error_msg("CAN'T arpsniff yourself !!");
  250.    // it is ok...
  251. active_dissector = 0;
  252.    if (Options.arpsniff)      // Doppleganger born...
  253.    {
  254.       char check[6];
  255.       Inet_GetMACfromString(Host_Dest.mac, check );   // check for valid mac
  256.       Inet_GetMACfromString(Host_Source.mac, check ); // check for valid mac
  257.       Dopple_pid = Doppleganger_Run(Options.netiface, Host_Source.ip, Host_Dest.ip, Host_Source.mac, Host_Dest.mac);
  258.    }
  259.    printf("nPress 'h' for help...nn");
  260.    pipe_with_illithid_data = Buffer_Create(1.0e6); // 1Mb in shared memory !!!
  261.    if (Options.arpsniff)
  262.    {
  263.       printf(" Sniffing (ARP based) : %s:%d <--> %s <--> %s:%dnn", source, Host_Source.port, Inet_MyIPAddress(), dest, Host_Dest.port);
  264.       if (!Options.collect) Connection_Mode=0;
  265.       Illithid_pid = Illithid_ARPBased_GetConnections(Options.netiface, Host_Source.ip,  Host_Dest.ip, Host_Source.mac, Host_Dest.mac);
  266.       if (!Options.collect) Illithid_pid2 = Illithid_ARPBased_GetData(Options.netiface, proto, Host_Source.ip, Host_Source.port, Host_Dest.ip, Host_Dest.port, Host_Source.mac, Host_Dest.mac);
  267.    }
  268.    else if (Options.sniff)
  269.    {
  270.       printf(" Sniffing (IP based): %s:%d <--> %s:%dnn", source, Host_Source.port, dest, Host_Dest.port );
  271.       if (!Options.collect) Connection_Mode=0;
  272.       Illithid_pid = Illithid_IPBased_GetConnections(Options.netiface, Host_Source.ip, Host_Dest.ip);
  273.    }
  274.    else if (Options.macsniff)
  275.    {
  276.       char check[6];
  277.       Inet_GetMACfromString(Host_Dest.mac, check );   // check for valid mac
  278.       Inet_GetMACfromString(Host_Source.mac, check ); // check for valid mac
  279.       printf(" Sniffing (MAC based): %s <--> %snn", Host_Dest.mac, Host_Source.mac);
  280.       if (!Options.collect) Connection_Mode=0;
  281.       Illithid_pid = Illithid_MACBased_GetConnections(Options.netiface, Host_Dest.mac, Host_Source.mac);
  282.    }
  283.       memset(&TimeOut, 0, sizeof(TimeOut));  //  timeout = 0
  284.       FD_ZERO(&msk_fd);
  285. #ifdef HAVE_TERMIOS_H
  286.       set_raw_term();   // non blocking stdin... yes this work !!
  287. #else
  288.    #ifdef DEBUG
  289.       Debug_msg("Simple_Run -- NO TERMIOS_H");
  290.    #endif
  291.       fcntl(0, F_SETFL, O_NONBLOCK);   // stdin non blocking... seems to be a non working method for me...
  292. #endif
  293.       loop
  294.       {
  295.          FD_SET(0, &msk_fd);
  296.          select(FOPEN_MAX, &msk_fd, (fd_set *) 0, (fd_set *) 0, &TimeOut);
  297.          if (FD_ISSET(0, &msk_fd))
  298.          {
  299.             char ch = 0;
  300.             ch = getchar();
  301.             switch(ch)
  302.             {
  303.                case 'A':
  304.                case 'a':
  305.                      if (!Options.collect)
  306.                      {
  307.                         printf("nnAscii mode...nn");
  308.                         view = ASCII_VIEW;
  309.                      }
  310.                      break;
  311.                case 'X':
  312.                case 'x':
  313.                      if (!Options.collect)
  314.                      {
  315.                         printf("nnHex mode...nn");
  316.                         view = HEX_VIEW;
  317.                      }
  318.                      break;
  319.                case 'S':
  320.                case 's':
  321.                      if (stop == 1)
  322.                      {
  323.                         printf("nnLet's go...nn");
  324.                         stop = 0;
  325.                      }
  326.                      else
  327.                      {
  328.                         printf("nnStopped...nn");
  329.                         stop = 1;
  330.                      }
  331.                      break;
  332.                case 'H':
  333.                case 'h':
  334.                      printf("nn[qQ]  - quitn");
  335.                      if (!Options.collect)
  336.                      {
  337.                         printf(    "[aA]  - dump data in ascii moden");
  338.                         printf(    "[xX]  - dump data in hex moden");
  339.                      }
  340.                      printf(    "[sS]  - stop/cont sniffingnn");
  341.                      break;
  342.                case 'Q':
  343.                case 'q':
  344. #ifdef HAVE_TERMIOS_H
  345.                      reset_term();
  346. #endif
  347.                      printf("nnnQuitting...nn");
  348.                      kill(Illithid_pid, SIGTERM);
  349.                      if (Illithid_pid2) kill(Illithid_pid2,SIGTERM);
  350.             if (Dopple_pid) kill(Dopple_pid, SIGTERM);
  351.                      exit(0);
  352.                      break;
  353.             }
  354.          }
  355.          if (Options.collect)
  356.             datalen = Buffer_Get(pipe_with_illithid, &data_from_illithid_conn, sizeof(CONNECTION));
  357.          else
  358.             datalen = Buffer_Get(pipe_with_illithid_data, &data_from_illithid, sizeof(SNIFFED_DATA));
  359.          if (datalen>0)
  360.          {
  361.             if (Options.collect)
  362.             {
  363.                int i;
  364.                number_of_connections = Decodedata_MakeConnectionList(data_from_illithid_conn);
  365.                for (i=0; i < number_of_connections; i++)
  366.                {
  367.                   if (Conn_Between_Hosts[i].user[0] != 0 &&  Conn_Between_Hosts[i].pass[0] != 0 &&
  368.                       Conn_Between_Hosts[i].user[0] != 1 &&  Conn_Between_Hosts[i].pass[0] != 1 )
  369.                   {
  370.                      printf("nn%s:%d -> %s:%dtt%snn",
  371.                            data_from_illithid_conn.ip_source,
  372.                            data_from_illithid_conn.source_port,
  373.                            data_from_illithid_conn.ip_dest,
  374.                            data_from_illithid_conn.dest_port,
  375.                            data_from_illithid_conn.type
  376.                            );
  377.                      printf("%sn", Conn_Between_Hosts[i].user);
  378.                      printf("%sn", Conn_Between_Hosts[i].pass);
  379.                      if (strlen(Conn_Between_Hosts[i].info))
  380.                         printf("n%sn", Conn_Between_Hosts[i].info);
  381.                      Conn_Between_Hosts[i].user[0] = 1;  // dont display in the future...
  382.                      Conn_Between_Hosts[i].pass[0] = 1;  // dont display in the future...
  383.                   }
  384.                }
  385.             }
  386.             else
  387.             if (proto == data_from_illithid.proto && Illithid_ToBeSniffed(Host_Source.ip,Host_Source.port,Host_Dest.ip,Host_Dest.port,&data_from_illithid))
  388.             {
  389.                if (!stop )
  390.                   switch (view)
  391.                   {
  392.                      case ASCII_VIEW:
  393.                         printf("nnn%s:%d -> %s:%dnn%s",
  394.                            data_from_illithid.ip_source,
  395.                            data_from_illithid.source_port,
  396.                            data_from_illithid.ip_dest,
  397.                            data_from_illithid.dest_port,
  398.                            Decodedata_GetAsciiData(data_from_illithid.data, data_from_illithid.datasize)
  399.                         );
  400.                         break;
  401.                      case HEX_VIEW:
  402.                         if (data_from_illithid.proto == 'T')
  403.                            printf("nn%s:%d -> %s:%d | seq %lx ack %lx | flags %s |n%s ",
  404.                               data_from_illithid.ip_source,
  405.                               data_from_illithid.source_port,
  406.                               data_from_illithid.ip_dest,
  407.                               data_from_illithid.dest_port,
  408.                               data_from_illithid.seq,
  409.                               data_from_illithid.ack_seq,
  410.                               Decodedata_TCPFlags(data_from_illithid.flags),
  411.                               Decodedata_GetHexData(data_from_illithid.data, data_from_illithid.datasize, 80)
  412.                            );
  413.                         else
  414.                            printf("nn%s:%d -> %s:%d | UDP |n%s ",
  415.                               data_from_illithid.ip_source,
  416.                               data_from_illithid.source_port,
  417.                               data_from_illithid.ip_dest,
  418.                               data_from_illithid.dest_port,
  419.                               Decodedata_GetHexData(data_from_illithid.data, data_from_illithid.datasize, 80)
  420.                            );
  421.                         break;
  422.                   }
  423.                fflush(stdout);
  424.             }
  425.          }
  426.          else
  427.              usleep(1000);
  428.       }
  429. }
  430. /* EOF */