ExampleAnomalyController.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionPrecision;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PrecisionLink;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  11. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.NormalDistributionHandler;
  12. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
  13. /**
  14. * Controller, which allows the user to create a few example anomalies
  15. * @author Andreas T. Meyer-Berg
  16. *
  17. */
  18. public class ExampleAnomalyController {
  19. /**
  20. * Controller which is used to manipulates the network parts
  21. */
  22. private Controller controller;
  23. /**
  24. * Initializes the Anomaly Controller
  25. * @param controller controller to be used
  26. */
  27. public ExampleAnomalyController(Controller controller) {
  28. this.controller = controller;
  29. }
  30. /**
  31. * Opens a DDosCreation Menu which targets the given Device
  32. * So far only a DDoS Attack is created
  33. * @param d Device which should be targeted
  34. * @return DDoS Connection, which was created
  35. */
  36. public Connection openDDosCreationMenu(SmartDevice d, List<SmartDevice> sources) {
  37. /**
  38. * Port of the largest connected connection to disguise DDoS
  39. */
  40. Port example = null;
  41. for(Port p: d.getPorts()) {
  42. if(example == null || example.getConnection() == null || example.getConnection().getProtocol()==null) {
  43. example = p;
  44. } else {
  45. if(p.getConnection() == null || p.getConnection().getProtocol() == null) {
  46. continue;
  47. } else if(p.getConnection().getParticipants().size()>example.getConnection().getParticipants().size()){
  48. example = p;
  49. }
  50. }
  51. }
  52. if(example == null || example.getConnection() == null || example.getConnection().getProtocol()==null)
  53. example = null;
  54. /**
  55. * Role of the target device
  56. */
  57. int targetRole = 0;
  58. if(example != null) {
  59. targetRole = example.getConnection().getProtocol().getRoleOfDevice(example);
  60. if(targetRole == -1)
  61. targetRole = 0;
  62. }
  63. /**
  64. * Attack Interval per device
  65. */
  66. long attackInterval = 1;
  67. if(example!=null) {
  68. int numDevices = 0;
  69. for(Port exP:example.getConnection().getParticipants()) {
  70. attackInterval+=exP.getTriggerInterval();
  71. numDevices++;
  72. }
  73. attackInterval /= numDevices == 0 ? 1 : numDevices;
  74. /**
  75. * Frequency Less or equal
  76. */
  77. attackInterval /= 10.0;
  78. }
  79. /**
  80. * Link of the DDoS attack
  81. */
  82. Link link = null;
  83. if(example !=null)
  84. link = example.getConnection().getLink();
  85. if(link == null) {
  86. if(d.getLinks().isEmpty()) {
  87. System.out.println("WARNING: Could not create DDos, as Device "+d.getName()+" is not connected to any Link");
  88. return null;
  89. }
  90. link = d.getLinks().get(0);
  91. }
  92. NetworkController networkController = controller.getNetworkController();
  93. Connection ddosConnection = new ConnectionPrecision();
  94. ddosConnection.setLabel((short)1);
  95. ddosConnection.setName("DDOS against "+d.getName());
  96. networkController.addConnectionToLink(ddosConnection, link);
  97. try {
  98. @SuppressWarnings("unchecked")
  99. Class<Protocol> exampleProtocol = (Class<Protocol>) example.getConnection().getProtocol().getClass();
  100. ddosConnection.setProtocol(exampleProtocol.newInstance());
  101. } catch (Exception e) {
  102. ddosConnection.setProtocol(new SimpleProtocol());
  103. }
  104. LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>(sources);
  105. devices.retainAll(link.getDevices());
  106. Port pTarget = new Port(d, (short)80, 1000L);
  107. pTarget.setStatus(Port.OPEN);
  108. if(!networkController.addDeviceToConnectionAndProtocol(pTarget, ddosConnection, targetRole)) {
  109. System.out.println("WARNING: Could not add DDoS Target to role "+targetRole);
  110. }
  111. for(SmartDevice src: devices) {
  112. if(src==d)continue;
  113. Port pSource = new Port(src, (short)80, 1L);
  114. pSource.setTriggerHandler(new NormalDistributionHandler(attackInterval, attackInterval*0.05));
  115. pSource.setStatus(Port.SENDING);
  116. // Ten tries of assigning random role
  117. for(int i=0; i<10;i++) {
  118. int sourceRole = (int) Math.floor(Math.random()*ddosConnection.getProtocol().getNumberOfRoles());
  119. if(sourceRole==targetRole)
  120. continue;
  121. if(networkController.addDeviceToConnectionAndProtocol(pSource, ddosConnection, sourceRole))
  122. break;
  123. }
  124. }
  125. networkController.addConnection(ddosConnection);
  126. controller.notifyObservers();
  127. return ddosConnection;
  128. }
  129. /**
  130. * Crash Device, which wont send packets any longer
  131. * @param d Device to be crashed
  132. */
  133. public void crashDevice(SmartDevice d) {
  134. for(Port p:d.getPorts()) {
  135. p.setStatus(Port.CLOSED);
  136. }
  137. controller.notifyObservers();
  138. }
  139. /**
  140. * Inserts DOS attack into the network
  141. * @param source source of the attack
  142. * @param target destination of the attack
  143. * @return DDoS Connection, which was created
  144. */
  145. public Connection runDosAttack(SmartDevice source, SmartDevice target) {
  146. NetworkController networkController = controller.getNetworkController();
  147. Connection dosConnection = new ConnectionPrecision();
  148. dosConnection.setLabel((short)1);
  149. dosConnection.setName("DOS: "+source.getName()+"-"+target.getName());
  150. networkController.addConnectionToLink(dosConnection, getCommonLink(source, target));
  151. dosConnection.setProtocol(new SimpleProtocol());
  152. Port pSource = new Port(source, (short)80, 4L);
  153. pSource.setStatus(Port.SENDING);
  154. networkController.addDeviceToConnectionAndProtocol(pSource, dosConnection, 0);
  155. Port pTarget = new Port(target, (short)80, 1000L);
  156. pTarget.setStatus(Port.OPEN);
  157. networkController.addDeviceToConnectionAndProtocol(pSource, dosConnection, 0);
  158. networkController.addDeviceToConnectionAndProtocol(pTarget, dosConnection, 1);
  159. networkController.addConnection(dosConnection);
  160. controller.notifyObservers();
  161. return dosConnection;
  162. }
  163. /**
  164. * Returns a common Link of the given devices
  165. * @param a
  166. * @param b
  167. * @return Link, both devices are connected to
  168. */
  169. private Link getCommonLink(SmartDevice a, SmartDevice b) {
  170. LinkedList<Link> l = new LinkedList<Link>(a.getLinks());
  171. l.addAll(b.getLinks());
  172. if(l.isEmpty()) {
  173. NetworkController net = controller.getNetworkController();
  174. Link newLink = new PrecisionLink("Direct Link: " + a.getName()+"-"+b.getName());
  175. net.addLinkToDevice(newLink, a);
  176. net.addLinkToDevice(newLink, b);
  177. net.addLink(newLink);
  178. return newLink;
  179. }else
  180. return l.getFirst();
  181. }
  182. }