X-Git-Url: http://plrg.eecs.uci.edu/git/?p=pingpong.git;a=blobdiff_plain;f=Code%2FProjects%2FPacketLevelSignatureExtractor%2Fsrc%2Fmain%2Fjava%2Fedu%2Fuci%2Fiotproject%2Fdetection%2Flayer2%2FLayer2ClusterMatcher.java;h=7b576be3d79debd87329944a8a6b2884e5e8c400;hp=e2a4aeab6f2db7f6f09ff83f1b65a692bb1f0db7;hb=987ea910fed24a1f3f51ded41b6aa98c4e2618ae;hpb=92a31b0967b3acff9e473ce62c136e84298b3aab diff --git a/Code/Projects/PacketLevelSignatureExtractor/src/main/java/edu/uci/iotproject/detection/layer2/Layer2ClusterMatcher.java b/Code/Projects/PacketLevelSignatureExtractor/src/main/java/edu/uci/iotproject/detection/layer2/Layer2ClusterMatcher.java index e2a4aea..7b576be 100644 --- a/Code/Projects/PacketLevelSignatureExtractor/src/main/java/edu/uci/iotproject/detection/layer2/Layer2ClusterMatcher.java +++ b/Code/Projects/PacketLevelSignatureExtractor/src/main/java/edu/uci/iotproject/detection/layer2/Layer2ClusterMatcher.java @@ -1,16 +1,19 @@ package edu.uci.iotproject.detection.layer2; +import edu.uci.iotproject.analysis.TriggerTrafficExtractor; import edu.uci.iotproject.trafficreassembly.layer2.Layer2FlowReassembler; import edu.uci.iotproject.trafficreassembly.layer2.Layer2Flow; import edu.uci.iotproject.trafficreassembly.layer2.Layer2FlowReassemblerObserver; import edu.uci.iotproject.detection.AbstractClusterMatcher; import edu.uci.iotproject.trafficreassembly.layer2.Layer2FlowObserver; +import org.jetbrains.annotations.NotNull; import org.pcap4j.core.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Function; /** @@ -27,7 +30,8 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye * of {@link #mCluster} and has so far matched {@code j} packets of that particular sequence. */ private final Map mPerFlowSeqMatchers = new HashMap<>(); - private final Map mPerFlowRangeMatcher = new HashMap<>(); +// private final Map mPerFlowRangeMatcher = new HashMap<>(); + private final Map> mPerFlowRangeMatcher = new HashMap<>(); private final Function mFlowFilter; @@ -41,13 +45,22 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye */ private final double mEps; + private int mInclusionTimeMillis; + + /** + * Keeping track of maximum number of skipped packets + */ + //private int mMaxSkippedPackets; + private List mMaxSkippedPackets; + /** * Create a new {@link Layer2ClusterMatcher} that attempts to find occurrences of {@code cluster}'s members. * @param cluster The sequence mutations that the new {@link Layer2ClusterMatcher} should search for. */ - public Layer2ClusterMatcher(List> cluster, boolean isRangeBased, double eps) { + public Layer2ClusterMatcher(List> cluster, int inclusionTimeMillis, + boolean isRangeBased, double eps) { // Consider all flows if no flow filter specified. - this(cluster, flow -> true, isRangeBased, eps); + this(cluster, flow -> true, inclusionTimeMillis, isRangeBased, eps); } /** @@ -59,15 +72,20 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye * namely when the {@link Layer2FlowReassembler} notifies the {@link Layer2ClusterMatcher} about * the new flow. This functionality may for example come in handy when one only wants to search * for matches in the subset of flows that involves a specific (range of) MAC(s). + * @param inclusionTimeMillis Packet inclusion time limit for matching. * @param isRangeBased The boolean that decides if it is range-based vs. strict matching. * @param eps The epsilon value used in the DBSCAN algorithm. */ public Layer2ClusterMatcher(List> cluster, Function flowFilter, - boolean isRangeBased, double eps) { + int inclusionTimeMillis, boolean isRangeBased, double eps) { super(cluster, isRangeBased); mFlowFilter = flowFilter; mRangeBased = isRangeBased; mEps = eps; + mInclusionTimeMillis = + inclusionTimeMillis == 0 ? TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS : inclusionTimeMillis; + //mMaxSkippedPackets = 0; + mMaxSkippedPackets = new ArrayList<>(); } @Override @@ -89,7 +107,7 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye Layer2SequenceMatcher[][] matchers = new Layer2SequenceMatcher[mCluster.size()][mCluster.get(0).size()]; // Prepare a "state 0" sequence matcher for each sequence variation in the cluster. for (int i = 0; i < matchers.length; i++) { - matchers[i][0] = new Layer2SequenceMatcher(mCluster.get(i)); + matchers[i][0] = new Layer2SequenceMatcher(mCluster.get(i), mInclusionTimeMillis); } // Associate the new sequence matcher table with the new flow mPerFlowSeqMatchers.put(flow, matchers); @@ -110,6 +128,8 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye boolean matched = sm.matchPacket(newPacket); if (matched) { if (sm.getMatchedPacketsCount() == sm.getTargetSequencePacketCount()) { + // Update maximum skipped packets + updateMaxSkippedPackets(flow.getPackets(), sm.getMatchedPackets()); // Sequence matcher has a match. Report it to observers. mObservers.forEach(o -> o.onMatch(this, sm.getMatchedPackets())); // Remove the now terminated sequence matcher. @@ -129,60 +149,82 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye // We always want to have a sequence matcher in state 0, regardless of if the one that advanced // from state zero completed its matching or if it replaced a different one in state 1 or not. if (sm.getMatchedPacketsCount() == 1) { - matchers[i][j] = new Layer2SequenceMatcher(sm.getTargetSequence()); + matchers[i][j] = new Layer2SequenceMatcher(sm.getTargetSequence(), mInclusionTimeMillis); } } } } } + // Update the maximum number of skipped packets + private void updateMaxSkippedPackets(List flowPackets, List matchedPackets) { + // Count number of skipped packets by looking into + // the difference of indices of two matched packets + for(int i = 1; i < matchedPackets.size(); ++i) { + int currIndex = flowPackets.indexOf(matchedPackets.get(i-1)); + int nextIndex = flowPackets.indexOf(matchedPackets.get(i)); + int skippedPackets = nextIndex - currIndex; +// if (mMaxSkippedPackets < skippedPackets) { +// mMaxSkippedPackets = skippedPackets; +// } + mMaxSkippedPackets.add(skippedPackets); + } + } + private void rangeBasedMatching(Layer2Flow flow, PcapPacket newPacket) { - // TODO: For range-based matching, we only care about matching a range; therefore it is a matcher array. + // TODO: For range-based matching, we need to create a new matcher every time we see the first element of + // the sequence (between lower and upper bounds). if (mPerFlowRangeMatcher.get(flow) == null) { - // If this is the first time we encounter this flow, we need to set up a sequence matcher. - // All sequences of the cluster have the same length, so we only need to compute the length of the - // arrays once. We want to make room for a cluster matcher in each state, including the initial empty state - // but excluding the final "full match" state (as there is no point in keeping a terminated sequence matcher - // around), so the length of the array is simply the sequence length. - Layer2RangeMatcher[] matcher = new Layer2RangeMatcher[mCluster.get(0).size()]; + // If this is the first time we encounter this flow, we need to set up a list of sequence matchers. + List listMatchers = new ArrayList<>(); // Prepare a "state 0" sequence matcher. - matcher[0] = new Layer2RangeMatcher(mCluster.get(0), mCluster.get(1), mEps); + Layer2RangeMatcher matcher = new Layer2RangeMatcher(mCluster.get(0), mCluster.get(1), + mInclusionTimeMillis, mEps); + listMatchers.add(matcher); // Associate the new sequence matcher table with the new flow. - mPerFlowRangeMatcher.put(flow, matcher); + mPerFlowRangeMatcher.put(flow, listMatchers); } // Fetch table that contains sequence matchers for this flow. - Layer2RangeMatcher[] matcher = mPerFlowRangeMatcher.get(flow); - // Present packet to the sequence matcher. - for (int j = matcher.length - 1; j >= 0; j--) { - Layer2RangeMatcher sm = matcher[j]; - if (sm == null) { - // There is currently no sequence matcher that has managed to match j packets. - continue; + List listMatchers = mPerFlowRangeMatcher.get(flow); + // Add a new matcher if all matchers have already advanced to the next stage. + // We always need a new matcher to match from NO packets. + boolean addOneArray = true; + for(Layer2RangeMatcher matcher : listMatchers) { + if (matcher.getMatchedPacketsCount() == 0) { + addOneArray = false; } - boolean matched = sm.matchPacket(newPacket); - if (matched) { - if (sm.getMatchedPacketsCount() == sm.getTargetSequencePacketCount()) { - // Sequence matcher has a match. Report it to observers. - mObservers.forEach(o -> o.onMatch(this, sm.getMatchedPackets())); - // Remove the now terminated sequence matcher. - matcher[j] = null; - } else { - // Sequence matcher advanced one step, so move it to its corresponding new position iff the - // packet that advanced it has a later timestamp than that of the last matched packet of the - // sequence matcher at the new index, if any. In most traces, a small amount of the packets - // appear out of order (with regards to their timestamp), which is why this check is required. - // Obviously it would not be needed if packets where guaranteed to be processed in timestamp - // order here. - if (matcher[j+1] == null || - newPacket.getTimestamp().isAfter(matcher[j+1].getLastPacket().getTimestamp())) { - matcher[j+1] = sm; + } + // Add the new matcher into the list + if (addOneArray) { + Layer2RangeMatcher newMatcher = new Layer2RangeMatcher(mCluster.get(0), mCluster.get(1), + mInclusionTimeMillis, mEps); + listMatchers.add(newMatcher); + } + // Present packet to the sequence matchers. + // Make a shallow copy of the list so that we can clean up the actual list when a matcher is terminated. + // Otherwise, we would get an exception for changing the list while iterating on it. + List listMatchersCopy = new ArrayList<>(listMatchers); + for(Layer2RangeMatcher matcher : listMatchersCopy) { + Layer2RangeMatcher sm = matcher; + // Check if no packets are matched yet or if there are matched packets, the next packet to be matched + // has to be later than the last matched packet. + // In most traces, a small amount of the packets appear out of order (with regards to their timestamp), + // which is why this check is required. + // Obviously it would not be needed if packets where guaranteed to be processed in timestamp + // order here. + if (sm.getMatchedPacketsCount() == 0 || + newPacket.getTimestamp().isAfter(sm.getLastPacket().getTimestamp())) { + boolean matched = sm.matchPacket(newPacket); + if (matched) { + if (sm.getMatchedPacketsCount() == sm.getTargetSequencePacketCount()) { + // Update maximum skipped packets + updateMaxSkippedPackets(flow.getPackets(), sm.getMatchedPackets()); + // Sequence matcher has a match. Report it to observers. + mObservers.forEach(o -> o.onMatch(this, sm.getMatchedPackets())); + // Terminate sequence matcher since matching is complete. + listMatchers.remove(matcher); } } - // We always want to have a sequence matcher in state 0, regardless of if the one that advanced - // from state zero completed its matching or if it replaced a different one in state 1 or not. - if (sm.getMatchedPacketsCount() == 1) { - matcher[j] = new Layer2RangeMatcher(sm.getTargetLowerBound(), sm.getTargetUpperBound(), mEps); - } } } } @@ -224,4 +266,14 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye System.out.println(">>> IGNORING FLOW: " + newFlow + " <<<"); } } + + /** + * Return the maximum number of skipped packets. + */ +// public int getMaxSkippedPackets() { +// return mMaxSkippedPackets; +// } + public List getMaxSkippedPackets() { + return mMaxSkippedPackets; + } }