Main.java: add paths to Wemo data files
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / Main.java
1 package edu.uci.iotproject;
2
3 import static edu.uci.iotproject.analysis.UserAction.Type;
4
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;
12
13 import java.io.EOFException;
14 import java.net.UnknownHostException;
15 import java.time.Instant;
16 import java.util.*;
17 import java.util.concurrent.TimeoutException;
18
19 /**
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.
24  *
25  * @author Janus Varmarken
26  * @author Rahmadi Trimananda (rtrimana@uci.edu)
27  * @version 0.1
28  */
29 public class Main {
30
31
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
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?
43
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";
49
50         // Wemo July 30 experiment
51         final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo.wlan1.local.pcap";
52         final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo-processed.pcap";
53         final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo-july-30-2018.timestamps";
54         final String deviceIp = "192.168.1.145";
55
56         // TP-Link BULB August 1 experiment
57 //        final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-08/tplink-bulb/tplink-bulb.wlan1.local.pcap";
58 //        final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-08/tplink-bulb/tplink-bulb-processed.pcap";
59 //        final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-08/tplink-bulb/tplink-bulb-aug-1-2018.timestamps";
60 //        final String deviceIp = "192.168.1.140";
61
62         TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
63         List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
64         // Tag each trigger with "ON" or "OFF", assuming that the first trigger is an "ON" and that they alternate.
65         List<UserAction> userActions = new ArrayList<>();
66         for (int i = 0; i < triggerTimes.size(); i++) {
67             userActions.add(new UserAction(i % 2 == 0 ? Type.TOGGLE_ON : Type.TOGGLE_OFF, triggerTimes.get(i)));
68         }
69         TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
70         final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
71         DnsMap dnsMap = new DnsMap();
72         TcpReassembler tcpReassembler = new TcpReassembler();
73         TrafficLabeler trafficLabeler = new TrafficLabeler(userActions);
74         tte.performExtraction(pkt -> {
75             try {
76                 outputter.dump(pkt);
77             } catch (NotOpenException e) {
78                 e.printStackTrace();
79             }
80         }, dnsMap, tcpReassembler, trafficLabeler);
81         outputter.flush();
82         outputter.close();
83
84         if (tte.getPacketsIncludedCount() != trafficLabeler.getTotalPacketCount()) {
85             // Sanity/debug check
86             throw new AssertionError(String.format("mismatch between packet count in %s and %s",
87                     TriggerTrafficExtractor.class.getSimpleName(), TrafficLabeler.class.getSimpleName()));
88         }
89
90         // Extract all conversations present in the filtered trace.
91         List<Conversation> allConversations = tcpReassembler.getTcpConversations();
92         // Group conversations by hostname.
93         Map<String, List<Conversation>> convsByHostname = TcpConversationUtils.groupConversationsByHostname(allConversations, dnsMap);
94         System.out.println("Grouped conversations by hostname.");
95         // For each hostname, count the frequencies of packet lengths exchanged with that hostname.
96         final Map<String, Map<Integer, Integer>> pktLenFreqsByHostname = new HashMap<>();
97         convsByHostname.forEach((host, convs) -> pktLenFreqsByHostname.put(host, TcpConversationUtils.countPacketLengthFrequencies(convs)));
98         System.out.println("Counted frequencies of packet lengths exchanged with each hostname.");
99         // For each hostname, count the frequencies of packet sequences (i.e., count how many conversations exchange a
100         // sequence of packets of some specific lengths).
101         final Map<String, Map<String, Integer>> pktSeqFreqsByHostname = new HashMap<>();
102         convsByHostname.forEach((host, convs) -> pktSeqFreqsByHostname.put(host, TcpConversationUtils.countPacketSequenceFrequencies(convs)));
103         System.out.println("Counted frequencies of packet sequences exchanged with each hostname.");
104         // For each hostname, count frequencies of packet pairs exchanged with that hostname across all conversations
105         final Map<String, Map<String, Integer>> pktPairFreqsByHostname =
106                 TcpConversationUtils.countPacketPairFrequenciesByHostname(allConversations, dnsMap);
107         System.out.println("Counted frequencies of packet pairs per hostname");
108         // -------------------------------------------------------------------------------------------------------------
109         // -------------------------------------------------------------------------------------------------------------
110     }
111
112 }
113
114
115 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)
116 // frame.len >= 556 && frame.len <= 558 && ip.addr == 192.168.1.159