|
@@ -0,0 +1,139 @@
|
|
|
|
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.menuBar;
|
|
|
|
+
|
|
|
|
+import java.util.LinkedList;
|
|
|
|
+import java.util.Observable;
|
|
|
|
+import java.util.Observer;
|
|
|
|
+
|
|
|
|
+import javax.swing.JMenu;
|
|
|
|
+import javax.swing.JMenuItem;
|
|
|
|
+
|
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
|
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.NetworkController;
|
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
|
|
|
|
+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.SmartDevice;
|
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Menu which allows simple insertion of anomalies into the network
|
|
|
|
+ *
|
|
|
|
+ * @author Andreas T. Meyer-Berg
|
|
|
|
+ */
|
|
|
|
+public class MenuBarInsertAnomalies extends JMenu implements Observer {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ private static final long serialVersionUID = 2522406981845530518L;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Controller for manipulating the network
|
|
|
|
+ */
|
|
|
|
+ private Controller controller;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Menu for denial of service attacks
|
|
|
|
+ */
|
|
|
|
+ private JMenu mnDos;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Menu for letting the device crash
|
|
|
|
+ */
|
|
|
|
+ private JMenu mnCrash;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Create a new MenuBar for anomaly insertion
|
|
|
|
+ * @param controller main Controller of the framework
|
|
|
|
+ */
|
|
|
|
+ public MenuBarInsertAnomalies(Controller controller) {
|
|
|
|
+ super("Insert Example Anomaly");
|
|
|
|
+ this.controller = controller;
|
|
|
|
+ initialize();
|
|
|
|
+ this.update(null, null);
|
|
|
|
+ this.controller.addObserver(this);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void initialize() {
|
|
|
|
+ mnDos = new JMenu("Denial of Service");
|
|
|
|
+ this.add(mnDos);
|
|
|
|
+
|
|
|
|
+ mnCrash = new JMenu("Crash Device");
|
|
|
|
+ this.add(mnCrash);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void update(Observable o, Object arg) {
|
|
|
|
+ mnDos.removeAll();
|
|
|
|
+ mnCrash.removeAll();
|
|
|
|
+ for(SmartDevice d : controller.getNetworkController().getVisibleSmartDevices()) {
|
|
|
|
+ JMenu mntmDosSource = new JMenu("Source: "+d.getName());
|
|
|
|
+ for(SmartDevice t: controller.getNetworkController().getVisibleSmartDevices()){
|
|
|
|
+ if(d==t)continue;
|
|
|
|
+ JMenuItem mntmDosTarget = new JMenuItem("Destination: " +t.getName());
|
|
|
|
+ mntmDosTarget.addActionListener(a->runDosAttack(d,t));
|
|
|
|
+ mntmDosSource.add(mntmDosTarget);
|
|
|
|
+ }
|
|
|
|
+ mnDos.add(mntmDosSource);
|
|
|
|
+ JMenuItem crashDevice = new JMenuItem(d.getName());
|
|
|
|
+ crashDevice.addActionListener(a->crashDevice(d));
|
|
|
|
+ mnCrash.add(crashDevice);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Crash Device, which wont send packets any longer
|
|
|
|
+ * @param d Device to be crashed
|
|
|
|
+ */
|
|
|
|
+ private void crashDevice(SmartDevice d) {
|
|
|
|
+ for(Port p:d.getPorts()) {
|
|
|
|
+ p.setStatus(Port.CLOSED);
|
|
|
|
+ }
|
|
|
|
+ controller.notifyObservers();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Inserts DOS attack into the network
|
|
|
|
+ * @param source
|
|
|
|
+ * @param target
|
|
|
|
+ */
|
|
|
|
+ private void runDosAttack(SmartDevice source, SmartDevice target) {
|
|
|
|
+ NetworkController networkController = controller.getNetworkController();
|
|
|
|
+ Connection dosConnection = new ConnectionPrecision();
|
|
|
|
+ dosConnection.setName("DOS: "+source.getName()+"-"+target.getName());
|
|
|
|
+ networkController.addConnectionToLink(dosConnection, getCommonLink(source, target));
|
|
|
|
+ dosConnection.setProtocol(new SimpleProtocol());
|
|
|
|
+ Port pSource = new Port(source, (short)80, 1L);
|
|
|
|
+ pSource.setStatus(Port.SENDING);
|
|
|
|
+ networkController.addDeviceToConnectionAndProtocol(pSource, dosConnection, 0);
|
|
|
|
+ Port pTarget = new Port(target, (short)80, 1000L);
|
|
|
|
+ pTarget.setStatus(Port.OPEN);
|
|
|
|
+ networkController.addDeviceToConnectionAndProtocol(pSource, dosConnection, 0);
|
|
|
|
+ networkController.addDeviceToConnectionAndProtocol(pTarget, dosConnection, 1);
|
|
|
|
+ networkController.addConnection(dosConnection);
|
|
|
|
+ controller.notifyObservers();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * Returns a common Link of the given devices
|
|
|
|
+ * @param a
|
|
|
|
+ * @param b
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private Link getCommonLink(SmartDevice a, SmartDevice b) {
|
|
|
|
+ LinkedList<Link> l = new LinkedList<Link>(a.getLinks());
|
|
|
|
+ l.addAll(b.getLinks());
|
|
|
|
+ if(l.isEmpty()) {
|
|
|
|
+ NetworkController net = controller.getNetworkController();
|
|
|
|
+ Link newLink = new PrecisionLink("Direct Link: " + a.getName()+"-"+b.getName());
|
|
|
|
+ net.addLinkToDevice(newLink, a);
|
|
|
|
+ net.addLinkToDevice(newLink, b);
|
|
|
|
+ net.addLink(newLink);
|
|
|
|
+ return newLink;
|
|
|
|
+ }else
|
|
|
|
+ return l.getFirst();
|
|
|
|
+ }
|
|
|
|
+}
|