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%2FLayer2SignatureDetector.java;h=5e9d8a053a2f2c07016cd9d27a43b47fc9f59e00;hp=2be6de3c3116cfa0519408272d04afe46fdd9c3e;hb=321d6c31113ec547c99d782a06635969d773232c;hpb=4a4f956b06431ba99ff4c0676d6c4bde5d7610e7 diff --git a/Code/Projects/PacketLevelSignatureExtractor/src/main/java/edu/uci/iotproject/detection/layer2/Layer2SignatureDetector.java b/Code/Projects/PacketLevelSignatureExtractor/src/main/java/edu/uci/iotproject/detection/layer2/Layer2SignatureDetector.java index 2be6de3..5e9d8a0 100644 --- a/Code/Projects/PacketLevelSignatureExtractor/src/main/java/edu/uci/iotproject/detection/layer2/Layer2SignatureDetector.java +++ b/Code/Projects/PacketLevelSignatureExtractor/src/main/java/edu/uci/iotproject/detection/layer2/Layer2SignatureDetector.java @@ -39,6 +39,17 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb */ private static boolean DUPLICATE_OUTPUT_TO_STD_OUT = true; + /** + * Router's MAC. + * This is only useful for the filter for direction when it is a WAN signature (Phone-Cloud or Device-Cloud). + * Phone-Device signatures do not have router MAC address in it. + */ + // TODO: We can remove the following constants if we do remove router's MAC filtering for directions + private static String TRAINING_ROUTER_WLAN_MAC = null; + private static String ROUTER_WLAN_MAC = null; + //private static String TRAINING_ROUTER_WLAN_MAC = "b0:b9:8a:73:69:8e"; + //private static String ROUTER_WLAN_MAC = "00:c1:b1:14:eb:31"; + private static List> parseSignatureMacFilters(String filtersString) { List> filters = new ArrayList<>(); String[] filterRegexes = filtersString.split(";"); @@ -53,14 +64,20 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb public static void main(String[] args) throws PcapNativeException, NotOpenException, IOException { // Parse required parameters. if (args.length < 8) { - String errMsg = String.format("Usage: %s inputPcapFile onAnalysisFile offAnalysisFile onSignatureFile offSignatureFile resultsFile" + + String errMsg = String.format("SPECTO version 1.0\n" + + "Copyright (C) 2018-2019 Janus Varmarken and Rahmadi Trimananda.\n" + + "University of California, Irvine.\n" + + "All rights reserved.\n\n" + + "Usage: %s inputPcapFile onAnalysisFile offAnalysisFile onSignatureFile offSignatureFile " + + "resultsFile signatureDuration eps onMaxSkippedPackets offMaxSkippedPackets" + "\n inputPcapFile: the target of the detection" + "\n onAnalysisFile: the file that contains the ON clusters analysis" + "\n offAnalysisFile: the file that contains the OFF clusters analysis" + "\n onSignatureFile: the file that contains the ON signature to search for" + "\n offSignatureFile: the file that contains the OFF signature to search for" + "\n resultsFile: where to write the results of the detection" + - "\n signatureDuration: the maximum duration of signature detection", + "\n signatureDuration: the maximum duration of signature detection" + + "\n eps: the epsilon value for the DBSCAN algorithm", Layer2SignatureDetector.class.getSimpleName()); System.out.println(errMsg); String optParamsExplained = "Above are the required, positional arguments. In addition to these, the " + @@ -73,11 +90,13 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb "that matches the vendor's prefix).\n" + " '-offmacfilters ;;...;' works exactly the same as onmacfilters, but " + "applies to the OFF signature instead of the ON signature.\n" + - " '-sout ' true/false literal indicating if output should also be printed to std out; default is true."; + " '-sout ' true/false literal indicating if output should also be printed to std out; default is true.\n" + + " '-vpn ' router's MAC address; this is to simulate a VPN that combines all flows even when the traffic is not a VPN traffic.\n" + + " '-onskipped ' the maximum duration of ON signature detection.\n" + + " '-offskipped ' the maximum duration of OFF signature detection.\n"; System.out.println(optParamsExplained); return; } - // TODO: We could take 7 inputs if we decided to use the cluster analyses. final String pcapFile = args[0]; final String onClusterAnalysisFile = args[1]; final String offClusterAnalysisFile = args[2]; @@ -89,7 +108,10 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb // Parse optional parameters. List> onSignatureMacFilters = null, offSignatureMacFilters = null; - final int optParamsStartIdx = 7; + String vpnClientMacAddress = null; + int onMaxSkippedPackets = -1; + int offMaxSkippedPackets = -1; + final int optParamsStartIdx = 8; if (args.length > optParamsStartIdx) { for (int i = optParamsStartIdx; i < args.length; i++) { if (args[i].equalsIgnoreCase("-onMacFilters")) { @@ -101,6 +123,16 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb } else if (args[i].equalsIgnoreCase("-sout")) { // Next argument is a boolean true/false literal. DUPLICATE_OUTPUT_TO_STD_OUT = Boolean.parseBoolean(args[i+1]); + } else if (args[i].equalsIgnoreCase("-vpn")) { + vpnClientMacAddress = args[i+1]; + } else if (args[i].equalsIgnoreCase("-onskipped")) { + if (i+2 > args.length - 1 || !args[i+2].equalsIgnoreCase("-offskipped")) { + throw new Error("Please make sure that the -onskipped and -offskipped options are both used at the same time..."); + } + if (args[i+2].equalsIgnoreCase("-offskipped")) { + onMaxSkippedPackets = Integer.parseInt(args[i+1]); + offMaxSkippedPackets = Integer.parseInt(args[i+3]); + } } } } @@ -142,11 +174,16 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb offSignature = PcapPacketUtils.useRangeBasedMatching(offSignature, offClusterAnalysis); } Layer2SignatureDetector onDetector = onSignatureMacFilters == null ? - new Layer2SignatureDetector(onSignature, signatureDuration, isRangeBasedForOn, eps) : - new Layer2SignatureDetector(onSignature, onSignatureMacFilters, signatureDuration, isRangeBasedForOn, eps); + new Layer2SignatureDetector(onSignature, TRAINING_ROUTER_WLAN_MAC, ROUTER_WLAN_MAC, signatureDuration, + isRangeBasedForOn, eps, onMaxSkippedPackets, vpnClientMacAddress) : + new Layer2SignatureDetector(onSignature, TRAINING_ROUTER_WLAN_MAC, ROUTER_WLAN_MAC, + onSignatureMacFilters, signatureDuration, isRangeBasedForOn, eps, onMaxSkippedPackets, + vpnClientMacAddress); Layer2SignatureDetector offDetector = offSignatureMacFilters == null ? - new Layer2SignatureDetector(offSignature, signatureDuration, isRangeBasedForOff, eps) : - new Layer2SignatureDetector(offSignature, offSignatureMacFilters, signatureDuration, isRangeBasedForOff, eps); + new Layer2SignatureDetector(offSignature, TRAINING_ROUTER_WLAN_MAC, ROUTER_WLAN_MAC, signatureDuration, + isRangeBasedForOff, eps, offMaxSkippedPackets, vpnClientMacAddress) : + new Layer2SignatureDetector(offSignature, TRAINING_ROUTER_WLAN_MAC, ROUTER_WLAN_MAC, offSignatureMacFilters, + signatureDuration, isRangeBasedForOff, eps, offMaxSkippedPackets, vpnClientMacAddress); final List detectedEvents = new ArrayList<>(); onDetector.addObserver((signature, match) -> { UserAction event = new UserAction(UserAction.Type.TOGGLE_ON, match.get(0).get(0).getTimestamp()); @@ -174,9 +211,23 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb detectedEvents.stream().filter(ua -> ua.getType() == UserAction.Type.TOGGLE_ON).count(); String resultOff = "# Number of detected events of type " + UserAction.Type.TOGGLE_OFF + ": " + detectedEvents.stream().filter(ua -> ua.getType() == UserAction.Type.TOGGLE_OFF).count(); + String onMaximumSkippedPackets = "# Maximum number of skipped packets in ON signature " + + Integer.toString(onDetector.getMaxSkippedPackets()); + String offMaximumSkippedPackets = "# Maximum number of skipped packets in OFF signature " + + Integer.toString(offDetector.getMaxSkippedPackets()); PrintWriterUtils.println(resultOn, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT); PrintWriterUtils.println(resultOff, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT); - + // Perform the skipped packet analysis if needed + if (onMaxSkippedPackets != -1 && offMaxSkippedPackets != -1) { + PrintWriterUtils.println(onMaximumSkippedPackets, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT); + for (Integer skippedPackets : onDetector.getSkippedPackets()) { + PrintWriterUtils.println(skippedPackets, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT); + } + PrintWriterUtils.println(offMaximumSkippedPackets, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT); + for (Integer skippedPackets : offDetector.getSkippedPackets()) { + PrintWriterUtils.println(skippedPackets, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT); + } + } // Flush output to results file and close it. resultsWriter.flush(); resultsWriter.close(); @@ -209,18 +260,31 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb /** * In charge of reassembling layer 2 packet flows. */ - private final Layer2FlowReassembler mFlowReassembler = new Layer2FlowReassembler(); + private Layer2FlowReassembler mFlowReassembler; private final List mObservers = new ArrayList<>(); private int mInclusionTimeMillis; - public Layer2SignatureDetector(List>> searchedSignature, int signatureDuration, boolean isRangeBased, double eps) { - this(searchedSignature, null, signatureDuration, isRangeBased, eps); + /** + * Skipped-packet analysis. + */ + private int mMaxSkippedPackets; + private List mSkippedPackets; + + + + public Layer2SignatureDetector(List>> searchedSignature, String trainingRouterWlanMac, + String routerWlanMac, int signatureDuration, boolean isRangeBased, double eps, + int limitSkippedPackets, String vpnClientMacAddress) { + this(searchedSignature, trainingRouterWlanMac, routerWlanMac, null, signatureDuration, isRangeBased, + eps, limitSkippedPackets, vpnClientMacAddress); } - public Layer2SignatureDetector(List>> searchedSignature, List> flowFilters, int inclusionTimeMillis, boolean isRangeBased, double eps) { + public Layer2SignatureDetector(List>> searchedSignature, String trainingRouterWlanMac, + String routerWlanMac, List> flowFilters, + int inclusionTimeMillis, boolean isRangeBased, double eps, int limitSkippedPackets, + String vpnClientMacAddress) { if (flowFilters != null && flowFilters.size() != searchedSignature.size()) { throw new IllegalArgumentException("If flow filters are used, there must be a flow filter for each cluster " + "of the signature."); @@ -230,8 +294,10 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb for (int i = 0; i < mSignature.size(); i++) { List> cluster = mSignature.get(i); Layer2ClusterMatcher clusterMatcher = flowFilters == null ? - new Layer2ClusterMatcher(cluster, inclusionTimeMillis, isRangeBased, eps) : - new Layer2ClusterMatcher(cluster, flowFilters.get(i), inclusionTimeMillis, isRangeBased, eps); + new Layer2ClusterMatcher(cluster, trainingRouterWlanMac, routerWlanMac, inclusionTimeMillis, + isRangeBased, eps, limitSkippedPackets) : + new Layer2ClusterMatcher(cluster, trainingRouterWlanMac, routerWlanMac, flowFilters.get(i), + inclusionTimeMillis, isRangeBased, eps, limitSkippedPackets); clusterMatcher.addObserver(this); clusterMatchers.add(clusterMatcher); } @@ -246,9 +312,27 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb } mClusterMatcherIds = Collections.unmodifiableMap(clusterMatcherIds); // Register all cluster matchers to receive a notification whenever a new flow is encountered. + if (vpnClientMacAddress != null) { + mFlowReassembler = new Layer2FlowReassembler(vpnClientMacAddress); + } else { + mFlowReassembler = new Layer2FlowReassembler(); + } mClusterMatchers.forEach(cm -> mFlowReassembler.addObserver(cm)); mInclusionTimeMillis = inclusionTimeMillis == 0 ? TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS : inclusionTimeMillis; + mMaxSkippedPackets = 0; + mSkippedPackets = new ArrayList<>(); + } + + public int getMaxSkippedPackets() { + return mMaxSkippedPackets; + } + + public List getSkippedPackets() { + for (Layer2ClusterMatcher matcher : mClusterMatchers) { + mSkippedPackets.addAll(matcher.getSkippedPackets()); + } + return mSkippedPackets; } @Override @@ -264,6 +348,10 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb // Add the match at the corresponding index mPendingMatches[mClusterMatcherIds.get(clusterMatcher)].add(match); checkSignatureMatch(); + // Update maximum number of skipped packets + if (mMaxSkippedPackets < ((Layer2ClusterMatcher) clusterMatcher).getMaxSkippedPackets()) { + mMaxSkippedPackets = ((Layer2ClusterMatcher) clusterMatcher).getMaxSkippedPackets(); + } } }