1 package edu.uci.iotproject.detection;
3 import edu.uci.iotproject.Conversation;
4 import edu.uci.iotproject.TcpReassembler;
5 import edu.uci.iotproject.analysis.TcpConversationUtils;
6 import edu.uci.iotproject.util.PcapPacketUtils;
7 import org.pcap4j.core.PacketListener;
8 import org.pcap4j.core.PcapPacket;
10 import java.util.List;
13 * TODO add class documentation.
15 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
16 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
18 public class SignatureDetector implements PacketListener {
21 * The signature that this {@link SignatureDetector} is trying to detect in the observed traffic.
23 private final List<List<PcapPacket>> mSignature;
26 * For reassembling the observed traffic into TCP connections.
28 private final TcpReassembler mTcpReassembler = new TcpReassembler();
30 public SignatureDetector(List<List<PcapPacket>> signature) {
31 mSignature = signature;
36 public void gotPacket(PcapPacket packet) {
37 // Present packet to TCP reassembler so that it can be mapped to a connection (if it is a TCP packet).
38 mTcpReassembler.gotPacket(packet);
42 // private void performDetection() {
43 // // Let's start out simple by building a version that only works for signatures that do not span across multiple
44 // // TCP conversations...
45 // for (Conversation c : mTcpReassembler.getTcpConversations()) {
46 // boolean matchFound = isSequenceInConversation(c);
51 * Examine if a {@link Conversation} contains a given sequence of packets. Note: the current implementation actually
52 * searches for a substring as it does not allow for interleaved packets in {@code c} that are not in
53 * {@code sequence}; for example, if {@code sequence} consists of packet lengths [2, 3, 5] and {@code c} consists of
54 * packet lengths [2, 3, 4, 5], the result will be {@code false}. If we are to allow interleaved packets, we need
55 * a modified version of <a href="https://stackoverflow.com/a/20545604/1214974">this</a>.
56 * @param sequence The sequence to look for.
57 * @param c The {@link Conversation} to search for {@code sequence} in.
58 * @return {@code true} if {@code c} contains {@code sequence}, {@code false} otherwise.
60 private boolean isSequenceInConversation(List<PcapPacket> sequence, Conversation c) {
61 // The packets we match against differ depending on whether the signature is a TLS or non-TLS signature.
62 boolean tlsSequence = isTlsSequence(sequence);
63 if (tlsSequence && !c.isTls()) {
64 // If we're looking for a TLS signature and this conversation does not appear to be a TLS conversation, we
65 // are done. Note: this assumes that they do NOT start performing TLS on new ports that are not captured in
66 // Conversation.isTls()
69 // Based on TLS or non-TLS signature, fetch the corresponding list of packets to match against.
70 List<PcapPacket> packets = tlsSequence ? c.getTlsApplicationDataPackets() : c.getPackets();
71 // If sequence is longer than the conversation, it can obviously not be contained in the conversation.
72 if (packets.size() < sequence.size()) {
77 while (convIdx < packets.size()) {
78 PcapPacket seqPkt = sequence.get(seqIdx);
79 PcapPacket convPkt = packets.get(convIdx);
80 if (convPkt.getOriginalLength() == seqPkt.getOriginalLength()) {
81 // TODO should also check direction of packets -- how to?
82 // A match, advance both indices to consider next packet in sequence vs. next packet in conversation
85 if (seqIdx == sequence.size()) {
86 // we managed to match the full sequence in the conversation.
93 * If we managed to match parts of sequence, we restart the search for sequence in c at the index of
94 * c where the current mismatch occurred. I.e., we must reset seqIdx, but leave convIdx untouched.
99 * First packet of sequence didn't match packet at convIdx of conversation, so we move forward in
100 * conversation, i.e., we continue the search for sequence in c starting at index convIdx+1 of c.
109 private boolean isTlsSequence(List<PcapPacket> sequence) {
110 // NOTE: Assumes ALL packets in sequence pertain to the same TCP connection!
111 PcapPacket firstPkt = sequence.get(0);
112 int srcPort = PcapPacketUtils.getSourcePort(firstPkt);
113 int dstPort = PcapPacketUtils.getDestinationPort(firstPkt);
114 return TcpConversationUtils.isTlsPort(srcPort) || TcpConversationUtils.isTlsPort(dstPort);