TcpConversationUtils.java and Main.java: add support for counting packet pair frequen...
[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) 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";
43
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 -> {
51             try {
52                 outputter.dump(pkt);
53             } catch (NotOpenException e) {
54                 e.printStackTrace();
55             }
56         }, dnsMap, tcpReassembler);
57         outputter.flush();
58         outputter.close();
59
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         // -------------------------------------------------------------------------------------------------------------
80     }
81
82 }
83
84
85 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)