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)
32 final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/June2018 experiments/tplink/tplink.wlan1.local.pcap";
33 final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/June2018 experiments/tplink/tplink-filtered.pcap";
34 final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/June2018 experiments/tplink/tplink-june-14-2018-timestamps.txt";
35 // IP of the device for which traffic is to be extracted
36 final String deviceIp = "192.168.1.159";
38 TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
39 List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
40 TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
41 final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
42 DnsMap dnsMap = new DnsMap();
43 TcpReassembler tcpReassembler = new TcpReassembler();
44 tte.performExtraction(pkt -> {
47 } catch (NotOpenException e) {
50 }, dnsMap, tcpReassembler);
54 // Extract all conversations present in the filtered trace.
55 List<Conversation> allConversations = tcpReassembler.getTcpConversations();
56 // Group conversations by hostname.
57 Map<String, List<Conversation>> convsByHostname = TcpConversationUtils.groupConversationsByHostname(allConversations, dnsMap);
58 System.out.println("Grouped conversations by hostname.");
59 // For each hostname, count the frequencies of packet lengths exchanged with that hostname.
60 final Map<String, Map<Integer, Integer>> pktLenFreqsByHostname = new HashMap<>();
61 convsByHostname.forEach((host, convs) -> pktLenFreqsByHostname.put(host, TcpConversationUtils.countPacketLengthFrequencies(convs)));
62 System.out.println("Counted frequencies of packet lengths exchanged with each hostname.");
63 // For each hostname, count the frequencies of packet sequences (i.e., count how many conversations exchange a
64 // sequence of packets of some specific lengths).
65 final Map<String, Map<String, Integer>> pktSeqFreqsByHostname = new HashMap<>();
66 convsByHostname.forEach((host, convs) -> pktSeqFreqsByHostname.put(host, TcpConversationUtils.countPacketSequenceFrequencies(convs)));
67 System.out.println("Counted frequencies of packet sequences exchanged with each hostname.");
71 // -------------------------------------------------------------------------------------------------------------
72 // -------------------------------------------------------------------------------------------------------------
78 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)