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:
38 // D-Link July 26 experiment
39 // final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink.wlan1.local.pcap";
40 // final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink-processed.pcap";
41 // final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink-july-26-2018.timestamps";
42 // final String deviceIp = "192.168.1.246"; // .246 == phone; .199 == dlink plug?
44 // TP-Link July 25 experiment
45 // final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink.wlan1.local.pcap";
46 // final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-processed.pcap";
47 // final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-july-25-2018.timestamps";
48 // final String deviceIp = "192.168.1.159";
50 // SmartThings Plug July 25 experiment
51 // final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/stplug/stplug.wlan1.local.pcap";
52 // final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/stplug/stplug-processed.pcap";
53 // final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/stplug/smartthings-july-25-2018.timestamps";
54 // final String deviceIp = "192.168.1.246"; // .246 == phone; .142 == SmartThings Hub (note: use eth0 capture for this!)
56 // Wemo July 30 experiment
57 // final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo.wlan1.local.pcap";
58 // final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo-processed.pcap";
59 // final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo-july-30-2018.timestamps";
60 // final String deviceIp = "192.168.1.145";
62 // Wemo Insight July 31 experiment
63 // final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemoinsight/wemoinsight.wlan1.local.pcap";
64 // final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemoinsight/wemoinsight-processed.pcap";
65 // final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemoinsight/wemo-insight-july-31-2018.timestamps";
66 // final String deviceIp = "192.168.1.135";
68 // TP-Link BULB August 1 experiment
69 final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-08/tplink-bulb/tplinkbulb.wlan1.local.pcap";
70 final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-08/tplink-bulb/tplinkbulb-processed.pcap";
71 final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-08/tplink-bulb/tplink-bulb-aug-3-2018.timestamps";
72 final String deviceIp = "192.168.1.140";
74 TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
75 List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
76 // Tag each trigger with "ON" or "OFF", assuming that the first trigger is an "ON" and that they alternate.
77 List<UserAction> userActions = new ArrayList<>();
78 for (int i = 0; i < triggerTimes.size(); i++) {
79 userActions.add(new UserAction(i % 2 == 0 ? Type.TOGGLE_ON : Type.TOGGLE_OFF, triggerTimes.get(i)));
81 TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
82 final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
83 DnsMap dnsMap = new DnsMap();
84 TcpReassembler tcpReassembler = new TcpReassembler();
85 TrafficLabeler trafficLabeler = new TrafficLabeler(userActions);
86 tte.performExtraction(pkt -> {
89 } catch (NotOpenException e) {
92 }, dnsMap, tcpReassembler, trafficLabeler);
96 if (tte.getPacketsIncludedCount() != trafficLabeler.getTotalPacketCount()) {
98 throw new AssertionError(String.format("mismatch between packet count in %s and %s",
99 TriggerTrafficExtractor.class.getSimpleName(), TrafficLabeler.class.getSimpleName()));
102 // Extract all conversations present in the filtered trace.
103 List<Conversation> allConversations = tcpReassembler.getTcpConversations();
104 // Group conversations by hostname.
105 Map<String, List<Conversation>> convsByHostname = TcpConversationUtils.groupConversationsByHostname(allConversations, dnsMap);
106 System.out.println("Grouped conversations by hostname.");
107 // For each hostname, count the frequencies of packet lengths exchanged with that hostname.
108 final Map<String, Map<Integer, Integer>> pktLenFreqsByHostname = new HashMap<>();
109 convsByHostname.forEach((host, convs) -> pktLenFreqsByHostname.put(host, TcpConversationUtils.countPacketLengthFrequencies(convs)));
110 System.out.println("Counted frequencies of packet lengths exchanged with each hostname.");
111 // For each hostname, count the frequencies of packet sequences (i.e., count how many conversations exchange a
112 // sequence of packets of some specific lengths).
113 final Map<String, Map<String, Integer>> pktSeqFreqsByHostname = new HashMap<>();
114 convsByHostname.forEach((host, convs) -> pktSeqFreqsByHostname.put(host, TcpConversationUtils.countPacketSequenceFrequencies(convs)));
115 System.out.println("Counted frequencies of packet sequences exchanged with each hostname.");
116 // For each hostname, count frequencies of packet pairs exchanged with that hostname across all conversations
117 final Map<String, Map<String, Integer>> pktPairFreqsByHostname =
118 TcpConversationUtils.countPacketPairFrequenciesByHostname(allConversations, dnsMap);
119 System.out.println("Counted frequencies of packet pairs per hostname");
120 // For each user action, reassemble the set of TCP connections occurring shortly after
121 final Map<UserAction, List<Conversation>> userActionToConversations = trafficLabeler.getLabeledReassembledTcpTraffic();
122 final Map<UserAction, Map<String, List<Conversation>>> userActionsToConvsByHostname = trafficLabeler.getLabeledReassembledTcpTraffic(dnsMap);
123 System.out.println("Reassembled TCP conversations occurring shortly after each user event");
129 Map<String, Map<String, Integer>> ons = new HashMap<>();
130 Map<String, Map<String, Integer>> offs = new HashMap<>();
132 userActionsToConvsByHostname.forEach((ua, hostnameToConvs) -> {
133 Map<String, Map<String, Integer>> outer = ua.getType() == Type.TOGGLE_ON ? ons : offs;
134 hostnameToConvs.forEach((host, convs) -> {
135 Map<String, Integer> sequenceCounts = TcpConversationUtils.countPacketSequenceFrequencies(convs);
136 outer.merge(host, sequenceCounts, (existingMap, newMap) -> {
137 newMap.forEach((sequence, count) -> existingMap.merge(sequence, count, (i1, i2) -> i1+i2));
144 // for (Map.Entry<String, Integer> newMapEntry : newMap.entrySet()) {
145 // if (existingMap.get(newMapEntry.getKey()) != null) {
146 // existingMap.put(newMapEntry.getKey(), existingMap.get(newMapEntry.getKey()) + newMapEntry.getValue());
148 // existingMap.put(newMapEntry.getKey(), newMapEntry.getValue());
151 // return existingMap;
153 System.out.println("");
155 // -------------------------------------------------------------------------------------------------------------
156 // -------------------------------------------------------------------------------------------------------------
162 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)
163 // frame.len >= 556 && frame.len <= 558 && ip.addr == 192.168.1.159