Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. import javax.xml.crypto.Data;
  2.         import java.util.*;
  3.  
  4. /**
  5.  * Created by  on 6/22/2017.
  6.  */
  7. public class DosDetection {
  8.     public static void main(String[] args) {
  9.         //Temporary until we get a real data feed
  10.         boolean DataFeedWorking = true;
  11.  
  12.         //Linked list that stores the data fields
  13.         LinkedList<TestPacket> data = new LinkedList<TestPacket>();
  14.  
  15.         //Create Sample Data (ONLY TILL I GET SOME REAL DATA TO TRY AND IMPORT)
  16.         TestPacket p1  = new TestPacket(1,  0.000000,"","A","",1,"");
  17.         TestPacket p2  = new TestPacket(1,  1.100000,"","A","",1,"");
  18.         TestPacket p3  = new TestPacket(1,  2.200000,"","B","",1,"");
  19.         TestPacket p4  = new TestPacket(1,  3.300000,"","A","",1,"");
  20.         TestPacket p5  = new TestPacket(1,  6.400000,"","A","",1,"");
  21.         TestPacket p6  = new TestPacket(1,  7.500000,"","A","",1,"");
  22.         TestPacket p7  = new TestPacket(1, 10.600000,"","A","",1,"");
  23.         TestPacket p8  = new TestPacket(2, 11.100000,"","A","",1,"");
  24.         TestPacket p9  = new TestPacket(3, 12.400000,"","B","",1,"");
  25.         TestPacket p10 = new TestPacket(4, 44.800000,"","A","",1,"");
  26.         TestPacket p11 = new TestPacket(5, 88.100000,"","C","",1,"");
  27.         TestPacket p12 = new TestPacket(6, 99.700000,"","B","",1,"");
  28.         TestPacket p13 = new TestPacket(7,115.800000,"","A","",1,"");
  29.         TestPacket p14 = new TestPacket(8,120.000000,"","B","",1,"");
  30.         TestPacket p15 = new TestPacket(9,125.100000,"","C","",1,"");
  31.  
  32.         //Add Sample Data
  33.         data.addAll(Arrays.asList(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15));
  34.  
  35.         //Map that stores the window length worth of data
  36.         Map<String,Integer> attackCount = new HashMap<String,Integer>();
  37.  
  38.         //window of time that the packets are kept in the count
  39.         double windowLengthSeconds = 120;
  40.  
  41.         //Current packet in the list
  42.         int curPacketNum = 0;
  43.  
  44.         //Loop that runs while there is data to look at)
  45.         while (DataFeedWorking){
  46.             TestPacket curPacket = data.get(curPacketNum);
  47.  
  48.             //Add the current packet to the count
  49.             if(!attackCount.containsKey(curPacket.getDestinationIP())){
  50.                 //If destination IP does not exist in the map add it
  51.                 attackCount.put(curPacket.getDestinationIP(), 1);
  52.             } else {
  53.                 attackCount.put(curPacket.getDestinationIP(), attackCount.get(curPacket.getDestinationIP()) + 1);
  54.             }
  55.             //Remove any packets that have passed the window length
  56.             removeOutdatedPacketsNumber(data, curPacket, windowLengthSeconds, attackCount);
  57.  
  58.             //Move to the next packet
  59.             curPacketNum++;
  60.  
  61.             //Safety to protect from infinite loop
  62.             if (curPacketNum >= data.size()){
  63.                 DataFeedWorking = false;
  64.             }
  65.         }
  66.         System.out.println("final: " + attackCount);
  67.     }
  68.     private static void removeOutdatedPacketsNumber(LinkedList<TestPacket> data, TestPacket curPacket, double windowLengthSeconds, Map<String,Integer> attackCount){
  69.         //Is the next packet in the queue older the the time window?
  70.         if(data.element().getTime() < curPacket.getTime() - windowLengthSeconds){
  71.             System.out.println("Remove one from :" + data.element().getDestinationIP());
  72.  
  73.             //remove one from the count
  74.             attackCount.put(data.element().getDestinationIP(), attackCount.get(data.element().getDestinationIP()) - 1);
  75.  
  76.             //remove the packet from memory
  77.             data.remove();
  78.  
  79.             //search next packet
  80.             removeOutdatedPacketsNumber(data, curPacket, windowLengthSeconds, attackCount);
  81.         }
  82.     }
  83. }
  84.  
  85. /**
  86.  * Created by  on 6/22/2017.
  87.  */
  88. public class TestPacket {
  89.     private int num;
  90.     private double time;
  91.     private String sourceIP;
  92.     private String destinationIP;
  93.     private String protocol;
  94.     private int length;
  95.     private String info;
  96.  
  97.     public TestPacket(int num, double time, String sourceIP, String destinationIP, String protocol, int length, String info) {
  98.         this.num = num;
  99.         this.time = time;
  100.         this.sourceIP = sourceIP;
  101.         this.destinationIP = destinationIP;
  102.         this.protocol = protocol;
  103.         this.length = length;
  104.         this.info = info;
  105.     }
  106.  
  107.     public int getNum() {
  108.         return num;
  109.     }
  110.  
  111.     public void setNum(int num) {
  112.         this.num = num;
  113.     }
  114.  
  115.     public double getTime() {
  116.         return time;
  117.     }
  118.  
  119.     public void setTime(double time) {
  120.         this.time = time;
  121.     }
  122.  
  123.     public String getSourceIP() {
  124.         return sourceIP;
  125.     }
  126.  
  127.     public void setSourceIP(String sourceIP) {
  128.         this.sourceIP = sourceIP;
  129.     }
  130.  
  131.     public String getDestinationIP() {
  132.         return destinationIP;
  133.     }
  134.  
  135.     public void setDestinationIP(String destinationIP) {
  136.         this.destinationIP = destinationIP;
  137.     }
  138.  
  139.     public String getProtocol() {
  140.         return protocol;
  141.     }
  142.  
  143.     public void setProtocol(String protocol) {
  144.         this.protocol = protocol;
  145.     }
  146.  
  147.     public int getLength() {
  148.         return length;
  149.     }
  150.  
  151.     public void setLength(int length) {
  152.         this.length = length;
  153.     }
  154.  
  155.     public String getInfo() {
  156.         return info;
  157.     }
  158.  
  159.     public void setInfo(String info) {
  160.         this.info = info;
  161.     }
  162.  
  163.  
  164.     @Override
  165.     public String toString() {
  166.         return "TestPacket{" +
  167.                 "num=" + num +
  168.                 ", time=" + time +
  169.                 ", sourceIP='" + sourceIP + '\'' +
  170.                 ", destinationIP='" + destinationIP + '\'' +
  171.                 ", protocol='" + protocol + '\'' +
  172.                 ", length=" + length +
  173.                 ", info='" + info + '\'' +
  174.                 '}';
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement