Sfoglia il codice sorgente

Adds Improved DDoS Creation & adds Label to previous DoS attack

* Copys protocol to send similar packets
* Slightly slower sending interval
* Label 1
* All devices of the largest Connection/Link attack
Andreas T. Meyer-Berg 5 anni fa
parent
commit
f6d3da6548

+ 136 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/menuBar/MenuBarInsertAnomalies.java

@@ -1,6 +1,7 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar;
 
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Observable;
 import java.util.Observer;
 
@@ -14,9 +15,11 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.BoolSensor;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices.FloatSensor;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.NormalDistributionHandler;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
 
 /**
@@ -41,6 +44,11 @@ public class MenuBarInsertAnomalies extends JMenu implements Observer {
 	 */
 	private JMenu mnDos;
 	
+	/**
+	 * Menu for distributed denial of service attacks
+	 */
+	private JMenu mnDDos;
+	
 	/**
 	 * Menu for letting the device crash
 	 */
@@ -67,6 +75,9 @@ public class MenuBarInsertAnomalies extends JMenu implements Observer {
 		mnDos = new JMenu("Denial of Service");
 		this.add(mnDos);
 		
+		mnDDos = new JMenu("Distributed Denial of Service");
+		this.add(mnDDos);
+		
 		mnCrash = new JMenu("Crash Device");
 		this.add(mnCrash);
 		
@@ -77,9 +88,20 @@ public class MenuBarInsertAnomalies extends JMenu implements Observer {
 	@Override
 	public void update(Observable o, Object arg) {
 		mnDos.removeAll();
+		mnDDos.removeAll();
 		mnCrash.removeAll();
 		mnValueAnomaly.removeAll();
+		
 		for(SmartDevice d : controller.getNetworkController().getVisibleSmartDevices()) {
+			/**
+			 * DDos Target Menu Item, which opens a DDosCreation Menu
+			 */
+			JMenuItem mntmDDosTarget = new JMenuItem("Destination: "+d.getName());
+			mntmDDosTarget.addActionListener(a->openDDosCreationMenu(d, (List<SmartDevice>)controller.getNetworkController().getVisibleSmartDevices()));
+			mnDDos.add(mntmDDosTarget);
+			/**
+			 * Dos creation menus
+			 */
 			JMenu mntmDosSource = new JMenu("Source: "+d.getName());
 			for(SmartDevice t: controller.getNetworkController().getVisibleSmartDevices()){
 				if(d==t)continue;
@@ -88,9 +110,15 @@ public class MenuBarInsertAnomalies extends JMenu implements Observer {
 				mntmDosSource.add(mntmDosTarget);
 			}
 			mnDos.add(mntmDosSource);
+			/**
+			 * Menus to crash a device
+			 */
 			JMenuItem crashDevice = new JMenuItem(d.getName());
 			crashDevice.addActionListener(a->crashDevice(d));
 			mnCrash.add(crashDevice);
+			/**
+			 * Value Anomalies menus
+			 */
 			if(d instanceof BoolSensor) {
 				BoolSensor sensor = (BoolSensor)d;
 				JMenuItem itm = new JMenuItem(d.getName());
@@ -105,11 +133,118 @@ public class MenuBarInsertAnomalies extends JMenu implements Observer {
 				mnValueAnomaly.add(itm);
 			}
 		}
+		/**
+		 * Only enable not empty menus
+		 */
 		mnCrash.setEnabled(mnCrash.getMenuComponentCount()!=0);
 		mnDos.setEnabled(mnDos.getMenuComponentCount()!=0);
+		mnDDos.setEnabled(mnDDos.getMenuComponentCount()!=0);
 		mnValueAnomaly.setEnabled(mnValueAnomaly.getMenuComponentCount()!=0);
 	}
 
+	/**
+	 * Opens a DDosCreation Menu which targets the given Device
+	 * So far only a DDoS Attack is created
+	 * @param d Device which should be targeted
+	 */
+	private void openDDosCreationMenu(SmartDevice d, List<SmartDevice> sources) {
+		/**
+		 * Port of the largest connected connection to disguise DDoS
+		 */
+		Port example = null;
+		for(Port p: d.getPorts()) {
+			if(example == null || example.getConnection() == null || example.getConnection().getProtocol()==null) {
+				example = p;
+			} else {
+				if(p.getConnection() == null || p.getConnection().getProtocol() == null) {
+					continue;
+				} else if(p.getConnection().getParticipants().size()>example.getConnection().getParticipants().size()){
+					example = p;
+				}
+			}
+		}
+		if(example == null || example.getConnection() == null || example.getConnection().getProtocol()==null)
+			example = null;
+		/**
+		 * Role of the target device
+		 */
+		int targetRole = 1;
+		if(example != null) {
+			targetRole = example.getConnection().getProtocol().getRoleOfDevice(example);
+			if(targetRole == -1)
+				targetRole = 1;
+		}
+		/**
+		 * Attack Interval per device
+		 */
+		long attackInterval = 1;
+		if(example!=null) {
+			int numDevices = 0;
+			for(Port exP:example.getConnection().getParticipants()) {
+				attackInterval+=exP.getTriggerInterval();
+				numDevices++;
+			}
+			attackInterval /= numDevices == 0 ? 1 : numDevices;
+			/**
+			 * Frequency Less or equal
+			 */
+			attackInterval *= 2;
+		}
+		
+		/**
+		 * Link of the DDoS attack
+		 */
+		Link link = null;
+		if(example !=null)
+			link = example.getConnection().getLink();
+		if(link == null) {
+			if(d.getLinks().isEmpty()) {
+				System.out.println("WARNING: Could not create DDos, as Device "+d.getName()+" is not connected to any Link");
+				return;
+			}
+			link = d.getLinks().get(0);
+		}
+		NetworkController networkController = controller.getNetworkController();
+		Connection ddosConnection = new ConnectionPrecision();
+		ddosConnection.setLabel((short)1);
+		ddosConnection.setName("DDOS against "+d.getName());
+		networkController.addConnectionToLink(ddosConnection, link);
+		try {
+			@SuppressWarnings("unchecked")
+			Class<Protocol> exampleProtocol = (Class<Protocol>) example.getConnection().getProtocol().getClass();
+			ddosConnection.setProtocol(exampleProtocol.newInstance());	
+		} catch (Exception e) {
+			ddosConnection.setProtocol(new SimpleProtocol());			
+		}
+		LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>(sources);
+		devices.retainAll(link.getDevices());
+		
+		
+		Port pTarget = new Port(d, (short)80, 1000L);
+		pTarget.setStatus(Port.OPEN);
+		if(!networkController.addDeviceToConnectionAndProtocol(pTarget, ddosConnection, targetRole)) {
+			System.out.println("WARNING: Could not add DDoS Target to role "+targetRole);
+		}
+			
+		for(SmartDevice src: devices) {
+			if(src==d)continue;
+			Port pSource = new Port(src, (short)80, 1L);
+			pSource.setTriggerHandler(new NormalDistributionHandler(attackInterval, attackInterval*0.05));
+			pSource.setStatus(Port.SENDING);
+			// Ten tries of assigning random role
+			for(int i=0; i<10;i++) {
+				int sourceRole = (int) Math.floor(Math.random()*ddosConnection.getProtocol().getNumberOfRoles());
+				if(sourceRole==targetRole)
+					continue;
+				if(networkController.addDeviceToConnectionAndProtocol(pSource, ddosConnection, sourceRole)) 
+					break;
+			}
+		}
+		networkController.addConnection(ddosConnection);
+		
+		controller.notifyObservers();
+	}
+	
 	/**
 	 * Crash Device, which wont send packets any longer
 	 * @param d Device to be crashed
@@ -129,6 +264,7 @@ public class MenuBarInsertAnomalies extends JMenu implements Observer {
 	private void runDosAttack(SmartDevice source, SmartDevice target) {
 		NetworkController networkController = controller.getNetworkController();
 		Connection dosConnection = new ConnectionPrecision();
+		dosConnection.setLabel((short)1);
 		dosConnection.setName("DOS: "+source.getName()+"-"+target.getName());
 		networkController.addConnectionToLink(dosConnection, getCommonLink(source, target));
 		dosConnection.setProtocol(new SimpleProtocol());