1 package edu.uci.iotproject.detection.layer2;
3 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
4 import edu.uci.iotproject.util.PcapPacketUtils;
5 import org.pcap4j.core.PcapPacket;
6 import org.pcap4j.util.MacAddress;
8 import java.util.ArrayList;
12 * Attempts to detect the presence of a specific packet sequence in the set of packets provided through multiple calls
13 * to {@link #matchPacket(PcapPacket)}, considering only layer 2 information.
15 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
16 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
18 public class Layer2SequenceMatcher extends Layer2AbstractMatcher {
21 * The sequence this {@link Layer2SequenceMatcher} is searching for.
23 private final List<PcapPacket> mSequence;
26 * Create a {@code Layer2SequenceMatcher}.
27 * @param sequence The sequence to match against (search for).
29 public Layer2SequenceMatcher(List<PcapPacket> sequence) {
32 // Compute packet directions for sequence.
33 for (int i = 0; i < sequence.size(); i++) {
35 // No previous packet; boolean parameter is ignored in this special case.
36 mPacketDirections[i] = getPacketDirection(null, true, sequence.get(i));
38 // Base direction marker on direction of previous packet.
39 PcapPacket prevPkt = mSequence.get(i-1);
40 boolean prevPktDirection = mPacketDirections[i-1];
41 mPacketDirections[i] = getPacketDirection(prevPkt, prevPktDirection, sequence.get(i));
47 * Attempt to advance this {@code Layer2SequenceMatcher} by matching {@code packet} against the packet that this
48 * {@code Layer2SequenceMatcher} expects as the next packet of the sequence it is searching for.
50 * @return {@code true} if this {@code Layer2SequenceMatcher} could advance by adding {@code packet} to its set of
51 * matched packets, {@code false} otherwise.
53 public boolean matchPacket(PcapPacket packet) {
54 if (getMatchedPacketsCount() == getTargetSequencePacketCount()) {
55 // We already matched the entire sequence, so we can't match any more packets.
59 // Verify that new packet pertains to same flow as previously matched packets, if any.
60 if (getMatchedPacketsCount() > 0) {
61 MacAddress pktSrc = PcapPacketUtils.getEthSrcAddr(packet);
62 MacAddress pktDst = PcapPacketUtils.getEthDstAddr(packet);
63 MacAddress earlierPktSrc = PcapPacketUtils.getEthSrcAddr(mMatchedPackets.get(0));
64 MacAddress earlierPktDst = PcapPacketUtils.getEthDstAddr(mMatchedPackets.get(0));
65 if (!(pktSrc.equals(earlierPktSrc) && pktDst.equals(earlierPktDst) ||
66 pktSrc.equals(earlierPktDst) && pktDst.equals(earlierPktSrc))) {
71 // Get representative of the packet we expect to match next.
72 PcapPacket expected = mSequence.get(mMatchedPackets.size());
73 // First verify if the received packet has the length we're looking for.
74 if (packet.getOriginalLength() == expected.getOriginalLength()) {
75 // If this is the first packet, we only need to verify that its length is correct. Time constraints are
76 // obviously satisfied as there are no previous packets. Furthermore, direction matches by definition as we
77 // don't know the MAC of the device (or phone) in advance, so we can't enforce a rule saying "first packet
78 // must originate from this particular MAC".
79 if (getMatchedPacketsCount() == 0) {
80 // Store packet as matched and advance.
81 mMatchedPackets.add(packet);
84 // Check if direction of packet matches expected direction.
85 boolean actualDirection = getPacketDirection(mMatchedPackets.get(getMatchedPacketsCount()-1),
86 mPacketDirections[getMatchedPacketsCount()-1], packet);
87 boolean expectedDirection = mPacketDirections[getMatchedPacketsCount()];
88 if (actualDirection != expectedDirection) {
91 // Next apply timing constraints:
92 // 1: to be a match, the packet must have a later timestamp than any other packet currently matched
93 // 2: does adding the packet cause the max allowed time between first packet and last packet to be exceeded?
94 if (!packet.getTimestamp().isAfter(mMatchedPackets.get(getMatchedPacketsCount()-1).getTimestamp())) {
97 if (packet.getTimestamp().isAfter(mMatchedPackets.get(0).getTimestamp().
98 plusMillis(TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS))) {
101 // If we made it here, it means that this packet has the expected length, direction, and obeys the timing
102 // constraints, so we store it and advance.
103 mMatchedPackets.add(packet);
104 if (mMatchedPackets.size() == mSequence.size()) {
105 // TODO report (to observers?) that we are done?
112 public int getTargetSequencePacketCount() {
113 return mSequence.size();
116 public List<PcapPacket> getTargetSequence() {