Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.xml.crypto.Data;
- import java.util.*;
- /**
- * Created by on 6/22/2017.
- */
- public class DosDetection {
- public static void main(String[] args) {
- //Temporary until we get a real data feed
- boolean DataFeedWorking = true;
- //Linked list that stores the data fields
- LinkedList<TestPacket> data = new LinkedList<TestPacket>();
- //Create Sample Data (ONLY TILL I GET SOME REAL DATA TO TRY AND IMPORT)
- TestPacket p1 = new TestPacket(1, 0.000000,"","A","",1,"");
- TestPacket p2 = new TestPacket(1, 1.100000,"","A","",1,"");
- TestPacket p3 = new TestPacket(1, 2.200000,"","B","",1,"");
- TestPacket p4 = new TestPacket(1, 3.300000,"","A","",1,"");
- TestPacket p5 = new TestPacket(1, 6.400000,"","A","",1,"");
- TestPacket p6 = new TestPacket(1, 7.500000,"","A","",1,"");
- TestPacket p7 = new TestPacket(1, 10.600000,"","A","",1,"");
- TestPacket p8 = new TestPacket(2, 11.100000,"","A","",1,"");
- TestPacket p9 = new TestPacket(3, 12.400000,"","B","",1,"");
- TestPacket p10 = new TestPacket(4, 44.800000,"","A","",1,"");
- TestPacket p11 = new TestPacket(5, 88.100000,"","C","",1,"");
- TestPacket p12 = new TestPacket(6, 99.700000,"","B","",1,"");
- TestPacket p13 = new TestPacket(7,115.800000,"","A","",1,"");
- TestPacket p14 = new TestPacket(8,120.000000,"","B","",1,"");
- TestPacket p15 = new TestPacket(9,125.100000,"","C","",1,"");
- //Add Sample Data
- data.addAll(Arrays.asList(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15));
- //Map that stores the window length worth of data
- Map<String,Integer> attackCount = new HashMap<String,Integer>();
- //window of time that the packets are kept in the count
- double windowLengthSeconds = 120;
- //Current packet in the list
- int curPacketNum = 0;
- //Loop that runs while there is data to look at)
- while (DataFeedWorking){
- TestPacket curPacket = data.get(curPacketNum);
- //Add the current packet to the count
- if(!attackCount.containsKey(curPacket.getDestinationIP())){
- //If destination IP does not exist in the map add it
- attackCount.put(curPacket.getDestinationIP(), 1);
- } else {
- attackCount.put(curPacket.getDestinationIP(), attackCount.get(curPacket.getDestinationIP()) + 1);
- }
- //Remove any packets that have passed the window length
- removeOutdatedPacketsNumber(data, curPacket, windowLengthSeconds, attackCount);
- //Move to the next packet
- curPacketNum++;
- //Safety to protect from infinite loop
- if (curPacketNum >= data.size()){
- DataFeedWorking = false;
- }
- }
- System.out.println("final: " + attackCount);
- }
- private static void removeOutdatedPacketsNumber(LinkedList<TestPacket> data, TestPacket curPacket, double windowLengthSeconds, Map<String,Integer> attackCount){
- //Is the next packet in the queue older the the time window?
- if(data.element().getTime() < curPacket.getTime() - windowLengthSeconds){
- System.out.println("Remove one from :" + data.element().getDestinationIP());
- //remove one from the count
- attackCount.put(data.element().getDestinationIP(), attackCount.get(data.element().getDestinationIP()) - 1);
- //remove the packet from memory
- data.remove();
- //search next packet
- removeOutdatedPacketsNumber(data, curPacket, windowLengthSeconds, attackCount);
- }
- }
- }
- /**
- * Created by on 6/22/2017.
- */
- public class TestPacket {
- private int num;
- private double time;
- private String sourceIP;
- private String destinationIP;
- private String protocol;
- private int length;
- private String info;
- public TestPacket(int num, double time, String sourceIP, String destinationIP, String protocol, int length, String info) {
- this.num = num;
- this.time = time;
- this.sourceIP = sourceIP;
- this.destinationIP = destinationIP;
- this.protocol = protocol;
- this.length = length;
- this.info = info;
- }
- public int getNum() {
- return num;
- }
- public void setNum(int num) {
- this.num = num;
- }
- public double getTime() {
- return time;
- }
- public void setTime(double time) {
- this.time = time;
- }
- public String getSourceIP() {
- return sourceIP;
- }
- public void setSourceIP(String sourceIP) {
- this.sourceIP = sourceIP;
- }
- public String getDestinationIP() {
- return destinationIP;
- }
- public void setDestinationIP(String destinationIP) {
- this.destinationIP = destinationIP;
- }
- public String getProtocol() {
- return protocol;
- }
- public void setProtocol(String protocol) {
- this.protocol = protocol;
- }
- public int getLength() {
- return length;
- }
- public void setLength(int length) {
- this.length = length;
- }
- public String getInfo() {
- return info;
- }
- public void setInfo(String info) {
- this.info = info;
- }
- @Override
- public String toString() {
- return "TestPacket{" +
- "num=" + num +
- ", time=" + time +
- ", sourceIP='" + sourceIP + '\'' +
- ", destinationIP='" + destinationIP + '\'' +
- ", protocol='" + protocol + '\'' +
- ", length=" + length +
- ", info='" + info + '\'' +
- '}';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement