BasicPacketClassifier.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package classifier;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.Map.Entry;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
  11. import weka.core.Attribute;
  12. import weka.core.DenseInstance;
  13. import weka.core.Instance;
  14. import weka.core.Instances;
  15. /**
  16. * Unsupervised Classifier Basis, which contains methods for transforming {@link Packet}s into {@link Instance}s.
  17. *
  18. * @author Andreas T. Meyer-Berg
  19. */
  20. public abstract class BasicPacketClassifier implements PacketSniffer {
  21. /**
  22. * True, if instances should be used for training
  23. */
  24. protected boolean training = true;
  25. /**
  26. * Attributes which should be taken into account
  27. */
  28. protected ArrayList<Attribute> atts = new ArrayList<Attribute>();
  29. /**
  30. * Collected Packets
  31. */
  32. protected Instances dataset;
  33. /**
  34. * CollectedPackets
  35. */
  36. protected HashMap<Link, LinkedList<Packet>> collectedPackets = new HashMap<Link, LinkedList<Packet>>();
  37. /**
  38. * HashMap for calculating transmission delay
  39. */
  40. protected HashMap<Link, LinkedList<Packet>> lastPackets = new HashMap<Link, LinkedList<Packet>>();
  41. /**
  42. * Map for the different Link names
  43. */
  44. protected HashSet<String> link_mappings = new HashSet<String>();
  45. /**
  46. * Map for the difference source device names
  47. */
  48. protected HashSet<String> source_mappings = new HashSet<String>();
  49. /**
  50. * Map for the different destination device names
  51. */
  52. protected HashSet<String> destination_mappings = new HashSet<String>();
  53. /**
  54. * Map for the protocol names
  55. */
  56. protected HashSet<String> protocol_mappings = new HashSet<String>();
  57. protected int NUMBER_OF_PACKETS = 200;
  58. /**
  59. * Initializes the different maps
  60. */
  61. public BasicPacketClassifier() {
  62. // Initialize Attribute list
  63. source_mappings.add("unknown");
  64. link_mappings.add("unknown");
  65. destination_mappings.add("unknown");
  66. protocol_mappings.add("unknown");
  67. }
  68. /**
  69. * Initialize the Algorithm parameters
  70. */
  71. protected abstract void initializeAlgorithm();
  72. @Override
  73. public void processPackets(HashMap<Link, LinkedList<Packet>> packets) {
  74. if(training && !packets.entrySet().isEmpty() && !packets.entrySet().iterator().next().getValue().isEmpty() && packets.entrySet().iterator().next().getValue().getFirst().getTimestamp()>getClassificationStart()) {
  75. training = false;
  76. // Build Clusterer
  77. try {
  78. finishDataCollection();
  79. } catch (Exception e) {
  80. System.out.println("Clustering failed");
  81. e.printStackTrace();
  82. }
  83. }
  84. if(training)
  85. try {
  86. training(packets);
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. else
  91. classify(packets);
  92. }
  93. /**
  94. * Estimates the current Packets per second (depending on the last 100 packets of the link)
  95. * @param link Link which should be checked
  96. * @param packet Packet which should investigated
  97. * @return estimated number of packets per second
  98. */
  99. protected double getEstimatedPacketsPerSecond(Link link, Packet packet) {
  100. /**
  101. * Packets used to calculated the packets per second
  102. */
  103. LinkedList<Packet> list = lastPackets.get(link);
  104. if(list == null) {
  105. /**
  106. * Add list if not present
  107. */
  108. list = new LinkedList<Packet>();
  109. lastPackets.put(link, list);
  110. }
  111. if(list.isEmpty()) {
  112. list.addLast(packet);
  113. // Default 1 packet per second
  114. return 1.0;
  115. }
  116. if(list.size() == NUMBER_OF_PACKETS){
  117. list.removeFirst();
  118. }
  119. list.addLast(packet);
  120. /**
  121. * elapsed time in milliseconds since last packet
  122. */
  123. long elapsed_time = packet.getTimestamp()-list.getFirst().getTimestamp()/list.size();
  124. if(elapsed_time<=0)
  125. return Double.POSITIVE_INFINITY;
  126. /**
  127. * Return number of packets per second
  128. */
  129. return 1000.0/elapsed_time;
  130. }
  131. /**
  132. * Returns the instance representation of the given packet and link
  133. * @param link link the packet was sent on
  134. * @param packet packet which should be transformed
  135. * @param dataset distribution the packet is part of
  136. * @return instance representation
  137. */
  138. protected Instance packet2Instance(Link link, Packet packet, Instances dataset) {
  139. /**
  140. * Instance for the given Packet
  141. */
  142. DenseInstance instance = new DenseInstance(dataset.numAttributes());
  143. instance.setDataset(dataset);
  144. // link
  145. instance.setValue(0, stringToNominal(link_mappings, link.getName()));
  146. // source
  147. if(packet.getSource()==null) {
  148. instance.setValue(1, "unknown");
  149. instance.setValue(2, Double.NEGATIVE_INFINITY);
  150. }else if(packet.getSource().getOwner()==null){
  151. instance.setValue(1, "unknown");
  152. instance.setValue(2, packet.getSource().getPortNumber());
  153. }else {
  154. instance.setValue(1, stringToNominal(source_mappings, packet.getSource().getOwner().getName()));
  155. instance.setValue(2, packet.getSource().getPortNumber());
  156. }
  157. // Destination
  158. if(packet.getDestination()==null) {
  159. instance.setValue(3, "unknown");
  160. instance.setValue(4, Double.NEGATIVE_INFINITY);
  161. }else if(packet.getDestination().getOwner()==null){
  162. instance.setValue(3, "unknown");
  163. instance.setValue(4, packet.getDestination().getPortNumber());
  164. }else {
  165. instance.setValue(3, stringToNominal(destination_mappings, packet.getDestination().getOwner().getName()));
  166. instance.setValue(4, packet.getDestination().getPortNumber());
  167. }
  168. // Protocol name
  169. instance.setValue(5, stringToNominal(protocol_mappings, packet.getProtocolName()));
  170. // Packets per second
  171. instance.setValue(6, getEstimatedPacketsPerSecond(link, packet));
  172. return instance;
  173. }
  174. /**
  175. * Transforms the String into an Number
  176. * @param map
  177. * @param s
  178. * @return
  179. */
  180. protected String stringToNominal(HashSet<String> map, String s) {
  181. return map.contains(s)?s:"unknown";
  182. }
  183. /**
  184. * Train the clusterer by collecting the packets
  185. *
  186. * @param packets packets to be learned
  187. */
  188. protected void training(HashMap<Link, LinkedList<Packet>> packets) {
  189. for(Entry<Link, LinkedList<Packet>> e:packets.entrySet()) {
  190. Link l = e.getKey();
  191. LinkedList<Packet> p = collectedPackets.get(l);
  192. if(p == null)
  193. collectedPackets.put(l, new LinkedList<Packet>(e.getValue()));
  194. else
  195. p.addAll(e.getValue());
  196. }
  197. }
  198. /**
  199. * Finishes the collection and trains the clusterer on the collected packets
  200. *
  201. * @throws Exception
  202. */
  203. protected void finishDataCollection() throws Exception{
  204. atts.add(new Attribute("Link-Name", new LinkedList<String>(link_mappings)));//TODO:??
  205. atts.add(new Attribute("Source-Device", new LinkedList<String>(source_mappings)));
  206. atts.add(new Attribute("Source-Port-number", false));
  207. atts.add(new Attribute("Destination-Device", new LinkedList<String>(destination_mappings)));
  208. atts.add(new Attribute("Destination-Port-number", false));
  209. Attribute pn = new Attribute("Protocol-name", new LinkedList<String>(protocol_mappings));
  210. //pn.setWeight(10);
  211. atts.add(pn);
  212. Attribute pps = new Attribute("Packets-per-second", false);
  213. //pps.setWeight(20);
  214. atts.add(pps);
  215. //atts.add(new Attribute("Anomaly", false));
  216. /*
  217. atts = new ArrayList<Attribute>();
  218. atts.add(new Attribute("LN", new LinkedList<String>(link_mappings)));//TODO:??
  219. atts.add(new Attribute("SD", new LinkedList<String>(source_mappings)));
  220. atts.add(new Attribute("SPN", false));
  221. atts.add(new Attribute("DD", new LinkedList<String>(destination_mappings)));
  222. atts.add(new Attribute("DPN", false));
  223. atts.add(new Attribute("PN", new LinkedList<String>(protocol_mappings)));
  224. atts.add(new Attribute("PPS", false));
  225. atts.add(new Attribute("A", false));*/
  226. dataset = new Instances("Packets", atts, 100000);
  227. //dataset.setClassIndex(7);
  228. /**
  229. * Add Instances to dataset
  230. */
  231. for (Iterator<Entry<Link, LinkedList<Packet>>> it = collectedPackets.entrySet().iterator(); it.hasNext();) {
  232. Entry<Link, LinkedList<Packet>> entry = it.next();
  233. /**
  234. * Link the packet was captured on
  235. */
  236. Link l = entry.getKey();
  237. for (Iterator<Packet> itPacket = entry.getValue().iterator(); itPacket.hasNext();) {
  238. /**
  239. * Packets to be added to the dataset
  240. */
  241. Packet packet = (Packet) itPacket.next();
  242. dataset.add(packet2Instance(l, packet, dataset));
  243. }
  244. }
  245. trainModel(dataset);
  246. }
  247. /**
  248. * Try to classify the given packets and detect anomalies
  249. * @param packets packets to be classified
  250. */
  251. protected void classify(HashMap<Link, LinkedList<Packet>> packets) {
  252. for (Iterator<Entry<Link, LinkedList<Packet>>> it = packets.entrySet().iterator(); it.hasNext();) {
  253. /**
  254. * Link & its packets
  255. */
  256. Entry<Link, LinkedList<Packet>> entry = it.next();
  257. /**
  258. * Link the packets were captured on
  259. */
  260. Link l = entry.getKey();
  261. for (Iterator<Packet> itPacket = entry.getValue().iterator(); itPacket.hasNext();) {
  262. /**
  263. * Packet which should be checked
  264. */
  265. Packet packet = (Packet) itPacket.next();
  266. /**
  267. * Instance Representation
  268. */
  269. Instance packet_instance = packet2Instance(l, packet, dataset);
  270. if(packet_instance == null)continue;
  271. classifyInstance(null, packet);
  272. }
  273. }
  274. }
  275. /**
  276. * Train the model using the given instances
  277. * @param instances training set, which should be learned
  278. */
  279. public abstract void trainModel(Instances instances);
  280. /**
  281. * classifies the given instance
  282. * @param instance instance which should be classified
  283. * @param origin original packet, which was transformed into the instance
  284. */
  285. public abstract void classifyInstance(Instance instance, Packet origin);
  286. /**
  287. * Returns the timestep, after which the classifier should start classifying instead of training.
  288. * @return timestep of the testing begin.
  289. */
  290. public abstract long getClassificationStart();
  291. }