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 // D-Link July 26 experiment
38 // final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink.wlan1.local.pcap";
39 // final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink-processed.pcap";
40 // final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink-july-26-2018.timestamps";
41 // final String deviceIp = "192.168.1.246"; // .246 == phone; .199 == dlink plug?
42 // TP-Link July 25 experiment
43 final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink.wlan1.local.pcap";
44 final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-processed.pcap";
45 final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-july-25-2018.timestamps";
46 final String deviceIp = "192.168.1.159";
48 TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
49 List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
50 // Tag each trigger with "ON" or "OFF", assuming that the first trigger is an "ON" and that they alternate.
51 List<UserAction> userActions = new ArrayList<>();
52 for (int i = 0; i < triggerTimes.size(); i++) {
53 userActions.add(new UserAction(i % 2 == 0 ? Type.TOGGLE_ON : Type.TOGGLE_OFF, triggerTimes.get(i)));
55 TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
56 final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
57 DnsMap dnsMap = new DnsMap();
58 TcpReassembler tcpReassembler = new TcpReassembler();
59 TrafficLabeler trafficLabeler = new TrafficLabeler(userActions);
60 tte.performExtraction(pkt -> {
63 } catch (NotOpenException e) {
66 }, dnsMap, tcpReassembler, trafficLabeler);
70 if (tte.getPacketsIncludedCount() != trafficLabeler.getTotalPacketCount()) {
72 throw new AssertionError(String.format("mismatch between packet count in %s and %s",
73 TriggerTrafficExtractor.class.getSimpleName(), TrafficLabeler.class.getSimpleName()));
76 // Extract all conversations present in the filtered trace.
77 List<Conversation> allConversations = tcpReassembler.getTcpConversations();
78 // Group conversations by hostname.
79 Map<String, List<Conversation>> convsByHostname = TcpConversationUtils.groupConversationsByHostname(allConversations, dnsMap);
80 System.out.println("Grouped conversations by hostname.");
81 // For each hostname, count the frequencies of packet lengths exchanged with that hostname.
82 final Map<String, Map<Integer, Integer>> pktLenFreqsByHostname = new HashMap<>();
83 convsByHostname.forEach((host, convs) -> pktLenFreqsByHostname.put(host, TcpConversationUtils.countPacketLengthFrequencies(convs)));
84 System.out.println("Counted frequencies of packet lengths exchanged with each hostname.");
85 // For each hostname, count the frequencies of packet sequences (i.e., count how many conversations exchange a
86 // sequence of packets of some specific lengths).
87 final Map<String, Map<String, Integer>> pktSeqFreqsByHostname = new HashMap<>();
88 convsByHostname.forEach((host, convs) -> pktSeqFreqsByHostname.put(host, TcpConversationUtils.countPacketSequenceFrequencies(convs)));
89 System.out.println("Counted frequencies of packet sequences exchanged with each hostname.");
90 // For each hostname, count frequencies of packet pairs exchanged with that hostname across all conversations
91 final Map<String, Map<String, Integer>> pktPairFreqsByHostname =
92 TcpConversationUtils.countPacketPairFrequenciesByHostname(allConversations, dnsMap);
93 System.out.println("Counted frequencies of packet pairs per hostname");
94 // -------------------------------------------------------------------------------------------------------------
95 // -------------------------------------------------------------------------------------------------------------
101 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)
102 // frame.len >= 556 && frame.len <= 558 && ip.addr == 192.168.1.159