NETWORK ATTACKS FRAMEWORK  1.0.0
A NETwork Attacks framework. Making network attacks impact evaluation easier!
NA_Attack.cc
Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2013, NESG (Network Engineering and Security Group), http://nesg.ugr.es,
00003 // - Gabriel Maciá Fernández (gmacia@ugr.es)
00004 // - Leovigildo Sánchez Casado (sancale@ugr.es)
00005 // - Rafael A. Rodríguez Gómez (rodgom@ugr.es)
00006 // - Roberto Magán Carrión (rmagan@ugr.es)
00007 // - Pedro García Teodoro (pgteodor@ugr.es)
00008 // - José Camacho Páez (josecamacho@ugr.es)
00009 // - Jesús E. Díaz Verdejo (jedv@ugr.es)
00010 //
00011 // This file is part of NETA.
00012 //
00013 //    NETA is free software: you can redistribute it and/or modify
00014 //    it under the terms of the GNU General Public License as published by
00015 //    the Free Software Foundation, either version 3 of the License, or
00016 //    (at your option) any later version.
00017 //
00018 //    NETA is distributed in the hope that it will be useful,
00019 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 //    GNU General Public License for more details.
00022 //
00023 //    You should have received a copy of the GNU General Public License
00024 //    along with NETA.  If not, see <http://www.gnu.org/licenses/>.
00025 //
00026 
00027 #include "NA_Attack.h"
00028 #include "NA_NesgLog.h"
00029 #include "NA_HackedModule.h"
00030 #include "NA_IPv4.h"
00031 
00032 Define_Module (NA_Attack);
00033 
00034 void NA_Attack::initialize() {
00035 
00036     // Get the type of attack to be launched
00037     attackType = (char*) par(NA_ATTACK_TYPE).stringValue();
00038 
00039     // Activate the attack only if defined in the active parameter in module (.ned)
00040     if (par(NA_ATTACK_ACTIVE).boolValue() == true) {
00041 
00042         getAttackModules();
00043         scheduleAttack();
00044     }
00045 }
00046 
00047 void NA_Attack::getAttackModules() {
00048     cTopology topo; //Used to discover the topology of the node and find modules for activating the attack
00049     cTopology::Node *node;
00050     string nodeName;
00051 
00052     // extract all modules with the @attackType property set in the simulation
00053     topo.extractByProperty(par(NA_ATTACK_TYPE).stringValue());
00054 
00055     LOG << "------------------------------------\n";
00056     LOG << "Found " << topo.getNumNodes() << " possible modules for attack\n";
00057     LOG << "------------------------------------\n";
00058 
00059     // Now, only the modules contained in the parent module of this NA_Attack object are activated.
00060     string prefix = this->getParentModule()->getFullPath(); // First we get the full path of the parent node
00061     int numModules = 0;
00062     for (int i = 0; i < topo.getNumNodes(); i++) {
00063         node = topo.getNode(i);
00064         nodeName = node->getModule()->getFullPath();
00065         if (not nodeName.compare(0, prefix.size(), prefix)) {
00066 
00067             LOG << "--->Inserting module in list: " << nodeName << "\n";
00068             modList.push_back(node->getModule());
00069             numModules++;
00070         }
00071     }
00072     LOG << "-----------------------------------\n";
00073     LOG << "Inserted " << numModules << " modules in list\n";
00074     LOG << "-----------------------------------\n";
00075 }
00076 
00077 void NA_Attack::scheduleAttack() {
00078     cMessage *msg = new cMessage(NA_ATTACK_START_MESSAGE);
00079     LOG << "Scheduling the attack \n";
00080     scheduleAt(par(NA_ATTACK_START_TIME).doubleValue(), msg);
00081     if (par("endTime").doubleValue()) //When the value differs from 0
00082     {
00083         cMessage *msgEnd = new cMessage(NA_ATTACK_END_MESSAGE);
00084         scheduleAt(par(NA_ATTACK_END_TIME).doubleValue(), msgEnd);
00085     }
00086 }
00087 
00088 void NA_Attack::handleMessage(cMessage *msg) {
00089     LOG << "Message received: " << msg->getFullName() << "\n";
00090     if (not strcmp(msg->getFullName(), NA_ATTACK_START_MESSAGE)) {
00091         activateModules();
00092     } else {
00093         deactivateModules();
00094     }
00095     delete (msg);
00096 }
00097 
00098 void NA_Attack::activateModules() {
00099     char msgCaption[30];
00100 
00101     // Concatenate the <attackType> + Activate
00102     opp_strcpy(msgCaption, attackType);
00103     strcat(msgCaption, NA_ATTACK_ACTIVATE_TAG);
00104 
00105     // Generate the specific attack controller message.
00106     // This method belongs to the specific attack controller.
00107     cMessage *msg = check_and_cast<cMessage *>(
00108             generateAttackMessage(msgCaption));
00109     EV << "\n\n";
00110     LOG << "-----------------------------------\n";
00111     LOG << "ACTIVATING HACKED MODULES\n";
00112     LOG << "-----------------------------------\n";
00113 
00114     sendMessageToHackedModules(msg);
00115 }
00116 
00117 void NA_Attack::deactivateModules() {
00118 
00119     char msgCaption[30];
00120 
00121     // Concatenate the <attackType> + Activate
00122     opp_strcpy(msgCaption, attackType);
00123     strcat(msgCaption, NA_ATTACK_DEACTIVATE_TAG);
00124 
00125     // Generate the specific attack controller message.
00126     // This method belongs to the specific attack controller.
00127     cMessage *msg = check_and_cast<cMessage *>(
00128             generateAttackMessage(msgCaption));
00129 
00130     EV << "\n\n";
00131     LOG << "-----------------------------------\n";
00132     LOG << "DEACTIVATING HACKED MODULES\n";
00133     LOG << "-----------------------------------\n";
00134 
00135     sendMessageToHackedModules(msg);
00136 }
00137 
00138 void NA_Attack::sendMessageToHackedModules(cMessage *msg) {
00139 
00140     unsigned int i;
00141     NA_HackedModule *modHacked;
00142 
00143     for (i = 0; i < modList.size(); i++) {
00144         LOG << "Module: " << modList[i]->getFullPath() << "\n";
00145         if (modList[i]->isSimple()) { // Activation is only done in simple modules (implemented in C++ classes).
00146 
00147             modHacked = dynamic_cast<NA_HackedModule *>(modList[i]);
00148 
00149             LOG << "--> Sending message: " << msg->getFullName() << "\n";
00150             // Send the message to the specific hacked module
00151             modHacked->handleMessageFromAttackController(msg);
00152         } else {
00153             LOG << "--> Message not sent. Not a simple module.\n";
00154         }
00155     }
00156     LOG << "-----------------------------------\n";
00157 }
00158 
00159 cMessage *NA_Attack::generateAttackMessage(const char *name) {
00160 
00161     LOG << "ERROR: EN NA_ATTACK GENERATE ATTACK MESSAGE\n";
00162     cMessage *msg = new cMessage(name);
00163     return msg;
00164 }
 All Classes Files Functions Variables Typedefs Enumerator Defines