1 package edu.uci.iotproject.detection.layer2;
3 import edu.uci.iotproject.trafficreassembly.layer2.Layer2FlowReassembler;
4 import edu.uci.iotproject.trafficreassembly.layer2.Layer2Flow;
5 import edu.uci.iotproject.trafficreassembly.layer2.Layer2FlowReassemblerObserver;
6 import edu.uci.iotproject.detection.AbstractClusterMatcher;
7 import edu.uci.iotproject.trafficreassembly.layer2.Layer2FlowObserver;
8 import org.pcap4j.core.*;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
14 import java.util.function.Function;
17 * Attempts to detect members of a cluster (packet sequence mutations) in layer 2 flows.
19 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
20 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
22 public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Layer2FlowReassemblerObserver, Layer2FlowObserver {
25 * Maps from a flow to a table of {@link Layer2SequenceMatcher}s for that particular flow. The table {@code t} is
26 * structured such that {@code t[i][j]} is a {@link Layer2SequenceMatcher} that attempts to match member {@code i}
27 * of {@link #mCluster} and has so far matched {@code j} packets of that particular sequence.
29 private final Map<Layer2Flow, Layer2SequenceMatcher[][]> mPerFlowSeqMatchers = new HashMap<>();
31 private final Function<Layer2Flow, Boolean> mFlowFilter;
34 * Create a new {@link Layer2ClusterMatcher} that attempts to find occurrences of {@code cluster}'s members.
35 * @param cluster The sequence mutations that the new {@link Layer2ClusterMatcher} should search for.
37 public Layer2ClusterMatcher(List<List<PcapPacket>> cluster) {
38 // Consider all flows if no flow filter specified.
39 this(cluster, flow -> true);
43 * Create a new {@link Layer2ClusterMatcher} that attempts to find occurrences of {@code cluster}'s members.
44 * @param cluster The sequence mutations that the new {@link Layer2ClusterMatcher} should search for.
45 * @param flowFilter A filter that defines what {@link Layer2Flow}s the new {@link Layer2ClusterMatcher} should
46 * search for {@code cluster}'s members in. If {@code flowFilter} returns {@code true}, the flow
47 * will be included (searched). Note that {@code flowFilter} is only queried once for each flow,
48 * namely when the {@link Layer2FlowReassembler} notifies the {@link Layer2ClusterMatcher} about
49 * the new flow. This functionality may for example come in handy when one only wants to search
50 * for matches in the subset of flows that involves a specific (range of) MAC(s).
52 public Layer2ClusterMatcher(List<List<PcapPacket>> cluster, Function<Layer2Flow, Boolean> flowFilter) {
54 mFlowFilter = flowFilter;
58 public void onNewPacket(Layer2Flow flow, PcapPacket newPacket) {
59 if (mPerFlowSeqMatchers.get(flow) == null) {
60 // If this is the first time we encounter this flow, we need to set up sequence matchers for it.
61 // All sequences of the cluster have the same length, so we only need to compute the length of the nested
62 // arrays once. We want to make room for a cluster matcher in each state, including the initial empty state
63 // but excluding the final "full match" state (as there is no point in keeping a terminated sequence matcher
64 // around), so the length of the inner array is simply the sequence length.
65 Layer2SequenceMatcher[][] matchers = new Layer2SequenceMatcher[mCluster.size()][mCluster.get(0).size()];
66 // Prepare a "state 0" sequence matcher for each sequence variation in the cluster.
67 for (int i = 0; i < matchers.length; i++) {
68 matchers[i][0] = new Layer2SequenceMatcher(mCluster.get(i));
70 // Associate the new sequence matcher table with the new flow
71 mPerFlowSeqMatchers.put(flow, matchers);
73 // Fetch table that contains sequence matchers for this flow.
74 Layer2SequenceMatcher[][] matchers = mPerFlowSeqMatchers.get(flow);
75 // Present the packet to all sequence matchers.
76 for (int i = 0; i < matchers.length; i++) {
77 // Present packet to the sequence matchers that has advanced the most first. This is to prevent discarding
78 // the sequence matchers that have advanced the most in the special case where the searched sequence
79 // contains two packets of the same length going in the same direction.
80 for (int j = matchers[i].length - 1; j >= 0 ; j--) {
81 Layer2SequenceMatcher sm = matchers[i][j];
83 // There is currently no sequence matcher that has managed to match j packets.
86 boolean matched = sm.matchPacket(newPacket);
88 if (sm.getMatchedPacketsCount() == sm.getTargetSequencePacketCount()) {
89 // Sequence matcher has a match. Report it to observers.
90 mObservers.forEach(o -> o.onMatch(this, sm.getMatchedPackets()));
91 // Remove the now terminated sequence matcher.
92 matchers[i][j] = null;
94 // Sequence matcher advanced one step, so move it to its corresponding new position iff the
95 // packet that advanced it has a later timestamp than that of the last matched packet of the
96 // sequence matcher at the new index, if any. In most traces, a small amount of the packets
97 // appear out of order (with regards to their timestamp), which is why this check is required.
98 // Obviously it would not be needed if packets where guaranteed to be processed in timestamp
100 if (matchers[i][j+1] == null ||
101 newPacket.getTimestamp().isAfter(matchers[i][j+1].getLastPacket().getTimestamp())) {
102 matchers[i][j+1] = sm;
105 // We always want to have a sequence matcher in state 0, regardless of if the one that advanced
106 // from state zero completed its matching or if it replaced a different one in state 1 or not.
107 if (sm.getMatchedPacketsCount() == 1) {
108 matchers[i][j] = new Layer2SequenceMatcher(sm.getTargetSequence());
117 protected List<List<PcapPacket>> pruneCluster(List<List<PcapPacket>> cluster) {
118 // Note: we assume that all sequences in the input cluster are of the same length and that their packet
119 // directions are identical.
120 List<List<PcapPacket>> prunedCluster = new ArrayList<>();
121 for (List<PcapPacket> originalClusterSeq : cluster) {
122 boolean alreadyPresent = prunedCluster.stream().anyMatch(pcPkts -> {
123 for (int i = 0; i < pcPkts.size(); i++) {
124 if (pcPkts.get(i).getOriginalLength() != originalClusterSeq.get(i).getOriginalLength()) {
130 if (!alreadyPresent) {
131 // Add the sequence if not already present in the pruned cluster.
132 prunedCluster.add(originalClusterSeq);
135 return prunedCluster;
138 private static final boolean DEBUG = false;
141 public void onNewFlow(Layer2FlowReassembler reassembler, Layer2Flow newFlow) {
142 // New flow detected. Check if we should consider it when searching for cluster member matches.
143 if (mFlowFilter.apply(newFlow)) {
145 System.out.println(">>> ACCEPTING FLOW: " + newFlow + " <<<");
147 // Subscribe to the new flow to get updates whenever a new packet pertaining to the flow is processed.
148 newFlow.addFlowObserver(this);
150 System.out.println(">>> IGNORING FLOW: " + newFlow + " <<<");