Main.java: change filepaths to point to TPLink july experiment.
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / Main.java
1 package edu.uci.iotproject;
2
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;
8
9 import java.io.EOFException;
10 import java.net.UnknownHostException;
11 import java.time.Instant;
12 import java.util.*;
13 import java.util.concurrent.TimeoutException;
14
15 /**
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.
20  *
21  * @author Janus Varmarken
22  * @author Rahmadi Trimananda (rtrimana@uci.edu)
23  * @version 0.1
24  */
25 public class Main {
26
27
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/experiments/2018-07/tplink/tplink.wlan1.local.pcap";
33         final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-processed.pcap";
34         final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-july-25-2018.timestamps";
35         // IP of the device for which traffic is to be extracted
36         final String deviceIp = "192.168.1.159";
37
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 -> {
45             try {
46                 outputter.dump(pkt);
47             } catch (NotOpenException e) {
48                 e.printStackTrace();
49             }
50         }, dnsMap, tcpReassembler);
51         outputter.flush();
52         outputter.close();
53
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.");
68
69
70
71         // -------------------------------------------------------------------------------------------------------------
72         // -------------------------------------------------------------------------------------------------------------
73     }
74
75 }
76
77
78 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)