5c04c23c4828b08652bf471b606ac80c3fbbbe62
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / detection / SignatureDetector.java
1 package edu.uci.iotproject.detection;
2
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;
9
10 import java.util.List;
11
12 /**
13  * TODO add class documentation.
14  *
15  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
16  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
17  */
18 public class SignatureDetector implements PacketListener {
19
20     /**
21      * The signature that this {@link SignatureDetector} is trying to detect in the observed traffic.
22      */
23     private final List<List<PcapPacket>> mSignature;
24
25     /**
26      * For reassembling the observed traffic into TCP connections.
27      */
28     private final TcpReassembler mTcpReassembler = new TcpReassembler();
29
30     public SignatureDetector(List<List<PcapPacket>> signature) {
31         mSignature = signature;
32     }
33
34
35     @Override
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);
39
40     }
41
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);
47 //        }
48 //    }
49
50     /**
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.
59      */
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()
67             return false;
68         }
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()) {
73             return false;
74         }
75         int seqIdx = 0;
76         int convIdx = 0;
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
83                 seqIdx++;
84                 convIdx++;
85                 if (seqIdx == sequence.size()) {
86                     // we managed to match the full sequence in the conversation.
87                     return true;
88                 }
89             } else {
90                 // Mismatch.
91                 if (seqIdx > 0) {
92                     /*
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.
95                      */
96                     seqIdx = 0;
97                 } else {
98                     /*
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.
101                      */
102                     convIdx++;
103                 }
104             }
105         }
106         return false;
107     }
108
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);
115     }
116
117 }