1 package edu.uci.iotproject;
3 import static edu.uci.iotproject.analysis.UserAction.Type;
5 import edu.uci.iotproject.analysis.TcpConversationUtils;
6 import edu.uci.iotproject.analysis.TrafficLabeler;
7 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
8 import edu.uci.iotproject.analysis.UserAction;
9 import edu.uci.iotproject.comparison.seqalignment.ExtractedSequence;
10 import edu.uci.iotproject.comparison.seqalignment.SequenceAlignment;
11 import edu.uci.iotproject.comparison.seqalignment.SequenceExtraction;
12 import edu.uci.iotproject.io.TriggerTimesFileReader;
13 import org.pcap4j.core.*;
14 import org.pcap4j.packet.namednumber.DataLinkType;
16 import java.io.EOFException;
17 import java.net.UnknownHostException;
18 import java.time.Instant;
20 import java.util.concurrent.TimeoutException;
23 * This is a system that reads PCAP files to compare
24 * patterns of DNS hostnames, packet sequences, and packet
25 * lengths with training data to determine certain events
26 * or actions for smart home devices.
28 * @author Janus Varmarken
29 * @author Rahmadi Trimananda (rtrimana@uci.edu)
35 public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException, TimeoutException, UnknownHostException {
36 // -------------------------------------------------------------------------------------------------------------
37 // ------------ # Code for extracting traffic generated by a device within x seconds of a trigger # ------------
38 // Paths to input and output files (consider supplying these as arguments instead) and IP of the device for
39 // which traffic is to be extracted:
40 // String path = "/scratch/July-2018"; // Rahmadi
41 String path = "/Users/varmarken/temp/UCI IoT Project/experiments"; // Janus
43 // 1) D-Link July 26 experiment
44 // final String inputPcapFile = path + "/2018-07/dlink/dlink.wlan1.local.pcap";
45 // final String outputPcapFile = path + "/2018-07/dlink/dlink-processed.pcap";
46 // final String triggerTimesFile = path + "/2018-07/dlink/dlink-july-26-2018.timestamps";
47 // final String deviceIp = "192.168.1.199"; // .246 == phone; .199 == dlink plug?
49 // 2) TP-Link July 25 experiment
50 // final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.pcap";
51 // final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.pcap";
52 // final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.timestamps";
53 // final String deviceIp = "192.168.1.159";
55 // 2b) TP-Link July 25 experiment TRUNCATED:
56 // Only contains "true local" events, i.e., before the behavior changes to remote-like behavior.
57 // Last included event is at July 25 10:38:11; file filtered to only include packets with arrival time <= 10:38:27.
58 final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.truncated.pcap";
59 final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.truncated.pcap";
60 final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.truncated.timestamps";
61 final String deviceIp = "192.168.1.159";
63 // 3) SmartThings Plug July 25 experiment
64 // final String inputPcapFile = path + "/2018-07/stplug/stplug.wlan1.local.pcap";
65 // final String outputPcapFile = path + "/2018-07/stplug/stplug-processed.pcap";
66 // final String triggerTimesFile = path + "/2018-07/stplug/smartthings-july-25-2018.timestamps";
67 // final String deviceIp = "192.168.1.246"; // .246 == phone; .142 == SmartThings Hub (note: use eth0 capture for this!)
69 // 4) Wemo July 30 experiment
70 // final String inputPcapFile = path + "/2018-07/wemo/wemo.wlan1.local.pcap";
71 // final String outputPcapFile = path + "/2018-07/wemo/wemo-processed.pcap";
72 // final String triggerTimesFile = path + "/2018-07/wemo/wemo-july-30-2018.timestamps";
73 // final String deviceIp = "192.168.1.145";
75 // 5) Wemo Insight July 31 experiment
76 // final String inputPcapFile = path + "/2018-07/wemoinsight/wemoinsight.wlan1.local.pcap";
77 // final String outputPcapFile = path + "/2018-07/wemoinsight/wemoinsight-processed.pcap";
78 // final String triggerTimesFile = path + "/2018-07/wemoinsight/wemo-insight-july-31-2018.timestamps";
79 // final String deviceIp = "192.168.1.135";
81 // 6) TP-Link Bulb August 1 experiment
82 // final String inputPcapFile = path + "/2018-08/tplink-bulb/tplinkbulb.wlan1.local.pcap";
83 // final String outputPcapFile = path + "/2018-08/tplink-bulb/tplinkbulb-processed.pcap";
84 // final String triggerTimesFile = path + "/2018-08/tplink-bulb/tplink-bulb-aug-3-2018.timestamps";
85 // final String deviceIp = "192.168.1.140";
87 // 7) Kwikset Doorlock August 6 experiment
88 // final String inputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock.wlan1.local.pcap";
89 // final String outputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock-processed.pcap";
90 // final String triggerTimesFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock-aug-6-2018.timestamps";
91 // final String deviceIp = "192.168.1.246"; // .246 == phone; .142 == SmartThings Hub (note: use eth0 capture for this!)
93 // 8) Hue Bulb August 7 experiment
94 // final String inputPcapFile = path + "/2018-08/hue-bulb/hue-bulb.wlan1.local.pcap";
95 // final String outputPcapFile = path + "/2018-08/hue-bulb/hue-bulb-processed.pcap";
96 // final String triggerTimesFile = path + "/2018-08/hue-bulb/hue-bulb-aug-7-2018.timestamps";
97 // final String deviceIp = "192.168.1.246";
99 // 9) Lifx Bulb August 8 experiment
100 // final String inputPcapFile = path + "/2018-08/lifx-bulb/lifx-bulb.wlan1.local.pcap";
101 // final String outputPcapFile = path + "/2018-08/lifx-bulb/lifx-bulb-processed.pcap";
102 // final String triggerTimesFile = path + "/2018-08/lifx-bulb/lifx-bulb-aug-8-2018.timestamps";
103 // final String deviceIp = "192.168.1.246"; // .246 == phone; .231 == Lifx
105 // 10) Amcrest Camera August 9 experiment
106 // final String inputPcapFile = path + "/2018-08/amcrest-camera/amcrest-camera.wlan1.local.pcap";
107 // final String outputPcapFile = path + "/2018-08/amcrest-camera/amcrest-camera-processed.pcap";
108 // final String triggerTimesFile = path + "/2018-08/amcrest-camera/amcrest-camera-aug-9-2018.timestamps";
109 // final String deviceIp = "192.168.1.246"; // .246 == phone; .235 == camera
111 // 11) Arlo Camera August 10 experiment
112 // final String inputPcapFile = path + "/2018-08/arlo-camera/arlo-camera.wlan1.local.pcap";
113 // final String outputPcapFile = path + "/2018-08/arlo-camera/arlo-camera-processed.pcap";
114 // final String triggerTimesFile = path + "/2018-08/arlo-camera/arlo-camera-aug-10-2018.timestamps";
115 // final String deviceIp = "192.168.1.140"; // .246 == phone; .140 == camera
117 // 12) Blossom sprinkler August 13 experiment
118 // final String inputPcapFile = path + "/2018-08/blossom/blossom.wlan1.local.pcap";
119 // final String outputPcapFile = path + "/2018-08/blossom/blossom-processed.pcap";
120 // final String triggerTimesFile = path + "/2018-08/blossom/blossom-aug-13-2018.timestamps";
121 // final String deviceIp = "192.168.1.229"; // .246 == phone; .229 == sprinkler
123 // 13) DLink siren August 14 experiment
124 // final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap";
125 // final String outputPcapFile = path + "/2018-08/dlink-siren/dlink-siren-processed.pcap";
126 // final String triggerTimesFile = path + "/2018-08/dlink-siren/dlink-siren-aug-14-2018.timestamps";
127 // final String deviceIp = "192.168.1.183"; // .246 == phone; .183 == siren
129 // 14) Nest thermostat August 15 experiment
130 // final String inputPcapFile = path + "/2018-08/nest/nest.wlan1.local.pcap";
131 // final String outputPcapFile = path + "/2018-08/nest/nest-processed.pcap";
132 // final String triggerTimesFile = path + "/2018-08/nest/nest-aug-15-2018.timestamps";
133 // final String deviceIp = "192.168.1.246"; // .246 == phone; .127 == Nest thermostat
135 // 15) Alexa August 16 experiment
136 // final String inputPcapFile = path + "/2018-08/alexa/alexa.wlan1.local.pcap";
137 // final String outputPcapFile = path + "/2018-08/alexa/alexa-processed.pcap";
138 // final String triggerTimesFile = path + "/2018-08/alexa/alexa-aug-16-2018.timestamps";
139 // final String deviceIp = "192.168.1.225"; // .246 == phone; .225 == Alexa
141 // final String inputPcapFile = path + "/2018-08/alexa/alexa2.wlan1.local.pcap";
142 // final String outputPcapFile = path + "/2018-08/alexa/alexa2-processed.pcap";
143 // final String triggerTimesFile = path + "/2018-08/alexa/alexa-aug-17-2018.timestamps";
144 // final String deviceIp = "192.168.1.225"; // .246 == phone; .225 == Alexa
146 TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
147 List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
148 // Tag each trigger with "ON" or "OFF", assuming that the first trigger is an "ON" and that they alternate.
149 List<UserAction> userActions = new ArrayList<>();
150 for (int i = 0; i < triggerTimes.size(); i++) {
151 userActions.add(new UserAction(i % 2 == 0 ? Type.TOGGLE_ON : Type.TOGGLE_OFF, triggerTimes.get(i)));
153 TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
154 final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
155 DnsMap dnsMap = new DnsMap();
156 TcpReassembler tcpReassembler = new TcpReassembler();
157 TrafficLabeler trafficLabeler = new TrafficLabeler(userActions);
158 tte.performExtraction(pkt -> {
161 } catch (NotOpenException e) {
164 }, dnsMap, tcpReassembler, trafficLabeler);
168 if (tte.getPacketsIncludedCount() != trafficLabeler.getTotalPacketCount()) {
169 // Sanity/debug check
170 throw new AssertionError(String.format("mismatch between packet count in %s and %s",
171 TriggerTrafficExtractor.class.getSimpleName(), TrafficLabeler.class.getSimpleName()));
174 // Extract all conversations present in the filtered trace.
175 List<Conversation> allConversations = tcpReassembler.getTcpConversations();
176 // Group conversations by hostname.
177 Map<String, List<Conversation>> convsByHostname = TcpConversationUtils.groupConversationsByHostname(allConversations, dnsMap);
178 System.out.println("Grouped conversations by hostname.");
179 // For each hostname, count the frequencies of packet lengths exchanged with that hostname.
180 final Map<String, Map<Integer, Integer>> pktLenFreqsByHostname = new HashMap<>();
181 convsByHostname.forEach((host, convs) -> pktLenFreqsByHostname.put(host, TcpConversationUtils.countPacketLengthFrequencies(convs)));
182 System.out.println("Counted frequencies of packet lengths exchanged with each hostname.");
183 // For each hostname, count the frequencies of packet sequences (i.e., count how many conversations exchange a
184 // sequence of packets of some specific lengths).
185 final Map<String, Map<String, Integer>> pktSeqFreqsByHostname = new HashMap<>();
186 convsByHostname.forEach((host, convs) -> pktSeqFreqsByHostname.put(host, TcpConversationUtils.countPacketSequenceFrequencies(convs)));
187 System.out.println("Counted frequencies of packet sequences exchanged with each hostname.");
188 // For each hostname, count frequencies of packet pairs exchanged with that hostname across all conversations
189 final Map<String, Map<String, Integer>> pktPairFreqsByHostname =
190 TcpConversationUtils.countPacketPairFrequenciesByHostname(allConversations, dnsMap);
191 System.out.println("Counted frequencies of packet pairs per hostname");
192 // For each user action, reassemble the set of TCP connections occurring shortly after
193 final Map<UserAction, List<Conversation>> userActionToConversations = trafficLabeler.getLabeledReassembledTcpTraffic();
194 final Map<UserAction, Map<String, List<Conversation>>> userActionsToConvsByHostname = trafficLabeler.getLabeledReassembledTcpTraffic(dnsMap);
195 System.out.println("Reassembled TCP conversations occurring shortly after each user event");
199 // Contains all ON events: hostname -> sequence identifier -> list of conversations with that sequence
200 Map<String, Map<String, List<Conversation>>> ons = new HashMap<>();
201 // Contains all OFF events: hostname -> sequence identifier -> list of conversations with that sequence
202 Map<String, Map<String, List<Conversation>>> offs = new HashMap<>();
203 userActionsToConvsByHostname.forEach((ua, hostnameToConvs) -> {
204 Map<String, Map<String, List<Conversation>>> outer = ua.getType() == Type.TOGGLE_ON ? ons : offs;
205 hostnameToConvs.forEach((host, convs) -> {
206 Map<String, List<Conversation>> seqsToConvs = TcpConversationUtils.
207 groupConversationsByPacketSequence(convs);
208 outer.merge(host, seqsToConvs, (oldMap, newMap) -> {
209 newMap.forEach((sequence, cs) -> oldMap.merge(sequence, cs, (list1, list2) -> {
218 // ================================================================================================
219 // <<< Some work-in-progress/explorative code that extracts a "representative" sequence >>>
221 // Currently need to know relevant hostname in advance :(
222 String hostname = "events.tplinkra.com";
223 // Conversations with 'hostname' for ON events.
224 List<Conversation> onsForHostname = new ArrayList<>();
225 // Conversations with 'hostname' for OFF events.
226 List<Conversation> offsForHostname = new ArrayList<>();
227 // "Unwrap" sequence groupings in ons/offs maps.
228 ons.get(hostname).forEach((k,v) -> onsForHostname.addAll(v));
229 offs.get(hostname).forEach((k,v) -> offsForHostname.addAll(v));
230 // Extract representative sequence for ON and OFF by providing the list of conversations with
231 // 'hostname' observed for each event type (the training data).
232 SequenceExtraction seqExtraction = new SequenceExtraction();
233 ExtractedSequence extractedSequenceForOn = seqExtraction.extract(onsForHostname);
234 ExtractedSequence extractedSequenceForOff = seqExtraction.extract(offsForHostname);
235 // Let's check how many ONs align with OFFs and vice versa (that is, how many times an event is incorrectly
237 int onsLabeledAsOff = 0;
238 Integer[] representativeOnSeq = TcpConversationUtils.getPacketLengthSequence(extractedSequenceForOn.getRepresentativeSequence());
239 Integer[] representativeOffSeq = TcpConversationUtils.getPacketLengthSequence(extractedSequenceForOff.getRepresentativeSequence());
240 SequenceAlignment<Integer> seqAlg = seqExtraction.getAlignmentAlgorithm();
241 for (Conversation c : onsForHostname) {
242 Integer[] onSeq = TcpConversationUtils.getPacketLengthSequence(c);
243 if (seqAlg.calculateAlignment(representativeOffSeq, onSeq) <= extractedSequenceForOff.getMaxAlignmentCost()) {
247 int offsLabeledAsOn = 0;
248 for (Conversation c : offsForHostname) {
249 Integer[] offSeq = TcpConversationUtils.getPacketLengthSequence(c);
250 if (seqAlg.calculateAlignment(representativeOnSeq, offSeq) <= extractedSequenceForOn.getMaxAlignmentCost()) {
254 System.out.println("");
255 // ================================================================================================
258 // -------------------------------------------------------------------------------------------------------------
259 // -------------------------------------------------------------------------------------------------------------
265 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)
266 // frame.len >= 556 && frame.len <= 558 && ip.addr == 192.168.1.159