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.io.TriggerTimesFileReader;
10 import org.pcap4j.core.*;
11 import org.pcap4j.packet.namednumber.DataLinkType;
13 import java.io.EOFException;
14 import java.net.UnknownHostException;
15 import java.time.Instant;
17 import java.util.concurrent.TimeoutException;
20 * This is a system that reads PCAP files to compare
21 * patterns of DNS hostnames, packet sequences, and packet
22 * lengths with training data to determine certain events
23 * or actions for smart home devices.
25 * @author Janus Varmarken
26 * @author Rahmadi Trimananda (rtrimana@uci.edu)
32 public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException, TimeoutException, UnknownHostException {
33 // -------------------------------------------------------------------------------------------------------------
34 // ------------ # Code for extracting traffic generated by a device within x seconds of a trigger # ------------
35 // Paths to input and output files (consider supplying these as arguments instead) and IP of the device for
36 // which traffic is to be extracted:
37 String path = "/scratch/July-2018"; // Rahmadi
38 //String path = "/Users/varmarken/temp/UCI IoT Project/experiments"; // Janus
40 // D-Link July 26 experiment
41 // final String inputPcapFile = path + "/2018-07/dlink/dlink.wlan1.local.pcap";
42 // final String outputPcapFile = path + "/2018-07/dlink/dlink-processed.pcap";
43 // final String triggerTimesFile = path + "/2018-07/dlink/dlink-july-26-2018.timestamps";
44 // final String deviceIp = "192.168.1.199"; // .246 == phone; .199 == dlink plug?
46 // TP-Link July 25 experiment
47 // final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.pcap";
48 // final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.pcap";
49 // final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.timestamps";
50 // final String deviceIp = "192.168.1.159";
52 // SmartThings Plug July 25 experiment
53 // final String inputPcapFile = path + "/2018-07/stplug/stplug.wlan1.local.pcap";
54 // final String outputPcapFile = path + "/2018-07/stplug/stplug-processed.pcap";
55 // final String triggerTimesFile = path + "/2018-07/stplug/smartthings-july-25-2018.timestamps";
56 // final String deviceIp = "192.168.1.246"; // .246 == phone; .142 == SmartThings Hub (note: use eth0 capture for this!)
58 // Wemo July 30 experiment
59 // final String inputPcapFile = path + "/2018-07/wemo/wemo.wlan1.local.pcap";
60 // final String outputPcapFile = path + "/2018-07/wemo/wemo-processed.pcap";
61 // final String triggerTimesFile = path + "/2018-07/wemo/wemo-july-30-2018.timestamps";
62 // final String deviceIp = "192.168.1.145";
64 // Wemo Insight July 31 experiment
65 // final String inputPcapFile = path + "/2018-07/wemoinsight/wemoinsight.wlan1.local.pcap";
66 // final String outputPcapFile = path + "/2018-07/wemoinsight/wemoinsight-processed.pcap";
67 // final String triggerTimesFile = path + "/2018-07/wemoinsight/wemo-insight-july-31-2018.timestamps";
68 // final String deviceIp = "192.168.1.135";
70 // TP-Link Bulb August 1 experiment
71 // final String inputPcapFile = path + "/2018-08/tplink-bulb/tplinkbulb.wlan1.local.pcap";
72 // final String outputPcapFile = path + "/2018-08/tplink-bulb/tplinkbulb-processed.pcap";
73 // final String triggerTimesFile = path + "/2018-08/tplink-bulb/tplink-bulb-aug-3-2018.timestamps";
74 // final String deviceIp = "192.168.1.140";
76 // Kwikset Doorlock August 6 experiment
77 // final String inputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock.wlan1.local.pcap";
78 // final String outputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock-processed.pcap";
79 // final String triggerTimesFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock-aug-6-2018.timestamps";
80 // final String deviceIp = "192.168.1.246"; // .246 == phone; .142 == SmartThings Hub (note: use eth0 capture for this!)
82 // Hue Bulb August 7 experiment
83 // final String inputPcapFile = path + "/2018-08/hue-bulb/hue-bulb.wlan1.local.pcap";
84 // final String outputPcapFile = path + "/2018-08/hue-bulb/hue-bulb-processed.pcap";
85 // final String triggerTimesFile = path + "/2018-08/hue-bulb/hue-bulb-aug-7-2018.timestamps";
86 // final String deviceIp = "192.168.1.246";
88 // Lifx Bulb August 8 experiment
89 // final String inputPcapFile = path + "/2018-08/lifx-bulb/lifx-bulb.wlan1.local.pcap";
90 // final String outputPcapFile = path + "/2018-08/lifx-bulb/lifx-bulb-processed.pcap";
91 // final String triggerTimesFile = path + "/2018-08/lifx-bulb/lifx-bulb-aug-8-2018.timestamps";
92 // final String deviceIp = "192.168.1.246"; // .246 == phone; .231 == Lifx
94 // Amcrest Camera August 9 experiment
95 // final String inputPcapFile = path + "/2018-08/amcrest-camera/amcrest-camera.wlan1.local.pcap";
96 // final String outputPcapFile = path + "/2018-08/amcrest-camera/amcrest-camera-processed.pcap";
97 // final String triggerTimesFile = path + "/2018-08/amcrest-camera/amcrest-camera-aug-9-2018.timestamps";
98 // final String deviceIp = "192.168.1.246"; // .246 == phone; .235 == camera
100 // Arlo Camera August 10 experiment
101 // final String inputPcapFile = path + "/2018-08/arlo-camera/arlo-camera.wlan1.local.pcap";
102 // final String outputPcapFile = path + "/2018-08/arlo-camera/arlo-camera-processed.pcap";
103 // final String triggerTimesFile = path + "/2018-08/arlo-camera/arlo-camera-aug-10-2018.timestamps";
104 // final String deviceIp = "192.168.1.140"; // .246 == phone; .140 == camera
106 // Blossom sprinkler August 13 experiment
107 // final String inputPcapFile = path + "/2018-08/blossom/blossom.wlan1.local.pcap";
108 // final String outputPcapFile = path + "/2018-08/blossom/blossom-processed.pcap";
109 // final String triggerTimesFile = path + "/2018-08/blossom/blossom-aug-13-2018.timestamps";
110 // final String deviceIp = "192.168.1.229"; // .246 == phone; .229 == sprinkler
112 // DLink siren August 14 experiment
113 final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap";
114 final String outputPcapFile = path + "/2018-08/dlink-siren/dlink-siren-processed.pcap";
115 final String triggerTimesFile = path + "/2018-08/dlink-siren/dlink-siren-aug-14-2018.timestamps";
116 final String deviceIp = "192.168.1.183"; // .246 == phone; .183 == siren
118 TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
119 List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
120 // Tag each trigger with "ON" or "OFF", assuming that the first trigger is an "ON" and that they alternate.
121 List<UserAction> userActions = new ArrayList<>();
122 for (int i = 0; i < triggerTimes.size(); i++) {
123 userActions.add(new UserAction(i % 2 == 0 ? Type.TOGGLE_ON : Type.TOGGLE_OFF, triggerTimes.get(i)));
125 TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
126 final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
127 DnsMap dnsMap = new DnsMap();
128 TcpReassembler tcpReassembler = new TcpReassembler();
129 TrafficLabeler trafficLabeler = new TrafficLabeler(userActions);
130 tte.performExtraction(pkt -> {
133 } catch (NotOpenException e) {
136 }, dnsMap, tcpReassembler, trafficLabeler);
140 if (tte.getPacketsIncludedCount() != trafficLabeler.getTotalPacketCount()) {
141 // Sanity/debug check
142 throw new AssertionError(String.format("mismatch between packet count in %s and %s",
143 TriggerTrafficExtractor.class.getSimpleName(), TrafficLabeler.class.getSimpleName()));
146 // Extract all conversations present in the filtered trace.
147 List<Conversation> allConversations = tcpReassembler.getTcpConversations();
148 // Group conversations by hostname.
149 Map<String, List<Conversation>> convsByHostname = TcpConversationUtils.groupConversationsByHostname(allConversations, dnsMap);
150 System.out.println("Grouped conversations by hostname.");
151 // For each hostname, count the frequencies of packet lengths exchanged with that hostname.
152 final Map<String, Map<Integer, Integer>> pktLenFreqsByHostname = new HashMap<>();
153 convsByHostname.forEach((host, convs) -> pktLenFreqsByHostname.put(host, TcpConversationUtils.countPacketLengthFrequencies(convs)));
154 System.out.println("Counted frequencies of packet lengths exchanged with each hostname.");
155 // For each hostname, count the frequencies of packet sequences (i.e., count how many conversations exchange a
156 // sequence of packets of some specific lengths).
157 final Map<String, Map<String, Integer>> pktSeqFreqsByHostname = new HashMap<>();
158 convsByHostname.forEach((host, convs) -> pktSeqFreqsByHostname.put(host, TcpConversationUtils.countPacketSequenceFrequencies(convs)));
159 System.out.println("Counted frequencies of packet sequences exchanged with each hostname.");
160 // For each hostname, count frequencies of packet pairs exchanged with that hostname across all conversations
161 final Map<String, Map<String, Integer>> pktPairFreqsByHostname =
162 TcpConversationUtils.countPacketPairFrequenciesByHostname(allConversations, dnsMap);
163 System.out.println("Counted frequencies of packet pairs per hostname");
164 // For each user action, reassemble the set of TCP connections occurring shortly after
165 final Map<UserAction, List<Conversation>> userActionToConversations = trafficLabeler.getLabeledReassembledTcpTraffic();
166 final Map<UserAction, Map<String, List<Conversation>>> userActionsToConvsByHostname = trafficLabeler.getLabeledReassembledTcpTraffic(dnsMap);
167 System.out.println("Reassembled TCP conversations occurring shortly after each user event");
171 // Contains all ON events: hostname -> sequence identifier -> list of conversations with that sequence
172 Map<String, Map<String, List<Conversation>>> ons = new HashMap<>();
173 // Contains all OFF events: hostname -> sequence identifier -> list of conversations with that sequence
174 Map<String, Map<String, List<Conversation>>> offs = new HashMap<>();
175 userActionsToConvsByHostname.forEach((ua, hostnameToConvs) -> {
176 Map<String, Map<String, List<Conversation>>> outer = ua.getType() == Type.TOGGLE_ON ? ons : offs;
177 hostnameToConvs.forEach((host, convs) -> {
178 Map<String, List<Conversation>> seqsToConvs = TcpConversationUtils.
179 groupConversationsByPacketSequence(convs);
180 outer.merge(host, seqsToConvs, (oldMap, newMap) -> {
181 newMap.forEach((sequence, cs) -> oldMap.merge(sequence, cs, (list1, list2) -> {
190 System.out.println("");
192 // -------------------------------------------------------------------------------------------------------------
193 // -------------------------------------------------------------------------------------------------------------
199 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)
200 // frame.len >= 556 && frame.len <= 558 && ip.addr == 192.168.1.159