1 package edu.uci.iotproject;
3 import edu.uci.iotproject.analysis.TcpConversationUtils;
4 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
5 import edu.uci.iotproject.io.TriggerTimesFileReader;
6 import org.pcap4j.core.*;
7 import org.pcap4j.packet.namednumber.DataLinkType;
9 import java.io.EOFException;
10 import java.net.UnknownHostException;
11 import java.time.Instant;
13 import java.util.concurrent.TimeoutException;
16 * This is a system that reads PCAP files to compare
17 * patterns of DNS hostnames, packet sequences, and packet
18 * lengths with training data to determine certain events
19 * or actions for smart home devices.
21 * @author Janus Varmarken
22 * @author Rahmadi Trimananda (rtrimana@uci.edu)
28 public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException, TimeoutException, UnknownHostException {
29 // -------------------------------------------------------------------------------------------------------------
30 // ------------ # Code for extracting traffic generated by a device within x seconds of a trigger # ------------
31 // Paths to input and output files (consider supplying these as arguments instead) and IP of the device for
32 // which traffic is to be extracted:
33 // D-Link July 26 experiment
34 final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink.wlan1.local.pcap";
35 final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink-processed.pcap";
36 final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/dlink/dlink-july-26-2018.timestamps";
37 final String deviceIp = "192.168.1.246";
38 // TP-Link July 25 experiment
39 // final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink.wlan1.local.pcap";
40 // final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-processed.pcap";
41 // final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-july-25-2018.timestamps";
42 // final String deviceIp = "192.168.1.159";
44 TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
45 List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
46 TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
47 final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
48 DnsMap dnsMap = new DnsMap();
49 TcpReassembler tcpReassembler = new TcpReassembler();
50 tte.performExtraction(pkt -> {
53 } catch (NotOpenException e) {
56 }, dnsMap, tcpReassembler);
60 // Extract all conversations present in the filtered trace.
61 List<Conversation> allConversations = tcpReassembler.getTcpConversations();
62 // Group conversations by hostname.
63 Map<String, List<Conversation>> convsByHostname = TcpConversationUtils.groupConversationsByHostname(allConversations, dnsMap);
64 System.out.println("Grouped conversations by hostname.");
65 // For each hostname, count the frequencies of packet lengths exchanged with that hostname.
66 final Map<String, Map<Integer, Integer>> pktLenFreqsByHostname = new HashMap<>();
67 convsByHostname.forEach((host, convs) -> pktLenFreqsByHostname.put(host, TcpConversationUtils.countPacketLengthFrequencies(convs)));
68 System.out.println("Counted frequencies of packet lengths exchanged with each hostname.");
69 // For each hostname, count the frequencies of packet sequences (i.e., count how many conversations exchange a
70 // sequence of packets of some specific lengths).
71 final Map<String, Map<String, Integer>> pktSeqFreqsByHostname = new HashMap<>();
72 convsByHostname.forEach((host, convs) -> pktSeqFreqsByHostname.put(host, TcpConversationUtils.countPacketSequenceFrequencies(convs)));
73 System.out.println("Counted frequencies of packet sequences exchanged with each hostname.");
74 // For each hostname, count frequencies of packet pairs exchanged with that hostname across all conversations
75 final Map<String, Map<String, Integer>> pktPairFreqsByHostname =
76 TcpConversationUtils.countPacketPairFrequenciesByHostname(allConversations, dnsMap);
77 System.out.println("Counted frequencies of packet pairs per hostname");
78 // -------------------------------------------------------------------------------------------------------------
79 // -------------------------------------------------------------------------------------------------------------
85 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)