import org.pcap4j.core.*;
import java.time.Duration;
+import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.*;
import java.util.function.Consumer;
+import java.util.stream.Collectors;
/**
* Detects an event signature that spans one or multiple TCP connections.
// Test client
public static void main(String[] args) throws PcapNativeException, NotOpenException {
- // String path = "/scratch/July-2018"; // Rahmadi
- String path = "/Users/varmarken/temp/UCI IoT Project/experiments"; // Janus
- final String inputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset3.wlan1.local.pcap";
- final String onSignatureFile = path + "/2018-08/kwikset-doorlock/onSignature-Kwikset-Doorlock-phone.sig";
- final String offSignatureFile = path + "/2018-08/kwikset-doorlock/offSignature-Kwikset-Doorlock-phone.sig";
+// String path = "/scratch/July-2018"; // Rahmadi
+// String path = "/Users/varmarken/temp/UCI IoT Project/experiments"; // Janus
+// String path = "/home/jvarmark/iot_project/datasets"; // Hera (server)
+ String path = "/raid/varmarken/iot_project/datasets"; // Zeus (server)
+
+ // No activity test
+ //final String inputPcapFile = path + "/evaluation/no-activity/no-activity.wlan1.pcap";
+
+ // D-Link Siren experiment
+// final String inputPcapFile = path + "/evaluation/dlink-siren/dlink-siren.data.wlan1.pcap";
+// final String inputPcapFile = path + "/evaluation/dlink-siren/dlink-siren.eth0.local.pcap";
+ // D-Link Siren DEVICE signatures
+// final String onSignatureFile = path + "/2018-08/dlink-siren/onSignature-DLink-Siren-device.sig";
+// final String offSignatureFile = path + "/2018-08/dlink-siren/offSignature-DLink-Siren-device.sig";
+ // D-Link Siren PHONE signatures
+// final String onSignatureFile = path + "/2018-08/dlink-siren/onSignature-DLink-Siren-phone.sig";
+// final String offSignatureFile = path + "/2018-08/dlink-siren/offSignature-DLink-Siren-phone.sig";
+ // TODO: EXPERIMENT - November 19, 2018
+ // Hue Bulb experiment
+// final String inputPcapFile = path + "/2018-08/hue-bulb/hue-bulb.wlan1.local.pcap";
+ // Hue Bulb PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/hue-bulb/signatures/hue-bulb-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/hue-bulb/signatures/hue-bulb-offSignature-phone-side.sig";
+
+ /*
+ // Kwikset Doorlock Sep 12 experiment
+// final String inputPcapFile = path + "/evaluation/kwikset-doorlock/kwikset-doorlock.data.wlan1.pcap";
+ final String inputPcapFile = path + "/evaluation/kwikset-doorlock/kwikset-doorlock.data.eth0.pcap";
+// // Kwikset Doorlock PHONE signatures
+ final String onSignatureFile = path + "/2018-08/kwikset-doorlock/onSignature-Kwikset-Doorlock-phone-new.sig";
+ final String offSignatureFile = path + "/2018-08/kwikset-doorlock/offSignature-Kwikset-Doorlock-phone-new.sig";
+ */
+
+ // D-Link Plug experiment
+ //final String inputPcapFile = path + "/evaluation/dlink/dlink-plug.data.wlan1.pcap";
+// final String inputPcapFile = path + "/evaluation/dlink/dlink-plug.data.eth0.pcap";
+
+ // D-Link Plug DEVICE signatures
+// final String onSignatureFile = path + "/2018-07/dlink/onSignature-DLink-Plug-device.sig";
+// final String offSignatureFile = path + "/2018-07/dlink/offSignature-DLink-Plug-device.sig";
+ // D-Link Plug PHONE signatures
+// final String onSignatureFile = path + "/2018-07/dlink/onSignature-DLink-Plug-phone.sig";
+// final String offSignatureFile = path + "/2018-07/dlink/offSignature-DLink-Plug-phone.sig";
+
+ // TODO: The following are negative tests against the PCAP file from UNSW
+// final String inputPcapFile = path + "/UNSW/16-09-28.pcap"; // TODO: Seems to be broken!
+// final String inputPcapFile = path + "/UNSW/16-10-02.pcap"; // TODO: Seems to be broken!
+// final String inputPcapFile = path + "/UNSW/16-10-03.pcap"; // TODO: Seems to be broken!
+// final String inputPcapFile = path + "/UNSW/16-10-04.pcap"; // TODO: Seems to be broken!
+// final String inputPcapFile = path + "/UNSW/16-10-07.pcap"; // TODO: Seems to be broken!
+// final String inputPcapFile = path + "/UNSW/16-10-08.pcap"; // TODO: Seems to be broken!
+// final String inputPcapFile = path + "/UNSW/16-10-09.pcap"; // TODO: Seems to be broken!
+// final String inputPcapFile = path + "/UNSW/16-10-10.pcap"; // TODO: Seems to be broken!
+// final String inputPcapFile = path + "/UNSW/16-10-11.pcap"; // TODO: Seems to be broken!
+
+// final String inputPcapFile = path + "/UNSW/16-09-23.pcap";
+// final String inputPcapFile = path + "/UNSW/16-09-24.pcap";
+// final String inputPcapFile = path + "/UNSW/16-09-25.pcap";
+// final String inputPcapFile = path + "/UNSW/16-09-26.pcap";
+// final String inputPcapFile = path + "/UNSW/16-09-27.pcap";
+// final String inputPcapFile = path + "/UNSW/16-09-29.pcap";
+// final String inputPcapFile = path + "/UNSW/16-10-01.pcap";
+// final String inputPcapFile = path + "/UNSW/16-10-06.pcap";
+ // Negative test: dataset from UNB
+ final String inputPcapFile = path + "/evaluation/negative-datasets/UNB/Monday-WorkingHours_one-local-endpoint.pcap";
+
+ // TODO: The following one is very long!!!
+// final String inputPcapFile = path + "/UNSW/16-10-12.pcap";
+
+ // TODO: The following are tests for signatures against training data
+
+ // D-Link Plug experiment
+// final String inputPcapFile = path + "/training/dlink-plug/wlan1/dlink-plug.wlan1.local.pcap";
+ // D-Link Plug DEVICE signatures
+// final String onSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-onSignature-device-side.sig";
+// final String offSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-offSignature-device-side.sig";
+ // D-Link Plug PHONE signatures
+// final String onSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-offSignature-phone-side.sig";
+
+ // TODO: EXPERIMENT - November 7, 2018
+ // D-Link Plug experiment
+ //final String inputPcapFile = path + "/experimental_result/standalone/dlink-plug/wlan1/dlink-plug.wlan1.local.pcap";
+ //final String inputPcapFile = path + "/experimental_result/smarthome/dlink-plug/wlan1/dlink-plug.wlan1.detection.pcap";
+ //final String inputPcapFile = path + "/experimental_result/smarthome/dlink-plug/eth0/dlink-plug.eth0.detection.pcap";
+ // D-Link Plug DEVICE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/dlink-plug/signatures/dlink-plug-onSignature-device-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/dlink-plug/signatures/dlink-plug-offSignature-device-side.sig";
+ // D-Link Plug PHONE signatures
+ //final String onSignatureFile = path + "/experimental_result/standalone/dlink-plug/signatures/dlink-plug-onSignature-phone-side.sig";
+ //final String offSignatureFile = path + "/experimental_result/standalone/dlink-plug/signatures/dlink-plug-offSignature-phone-side.sig";
+
+ // TODO: EXPERIMENT - November 9, 2018
+ // D-Link Siren experiment
+ //final String inputPcapFile = path + "/experimental_result/standalone/dlink-siren/wlan1/dlink-siren.wlan1.local.pcap";
+ //final String inputPcapFile = path + "/experimental_result/smarthome/dlink-siren/wlan1/dlink-siren.wlan1.detection.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/dlink-siren/eth0/dlink-siren.eth0.detection.pcap";
+ // D-Link Siren DEVICE signatures
+ // TODO: The device signature does not have pairs---only one packet which is 216, so we don't consider this as a signature
+// final String onSignatureFile = path + "/experimental_result/standalone/dlink-siren/signatures/dlink-siren-onSignature-device-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/dlink-siren/signatures/dlink-siren-offSignature-device-side.sig";
+ // D-Link Siren PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/dlink-siren/signatures/dlink-siren-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/dlink-siren/signatures/dlink-siren-offSignature-phone-side.sig";
+// final String onSignatureFile = path + "/training/signatures/dlink-siren/dlink-siren-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/training/signatures/dlink-siren/dlink-siren-offSignature-phone-side.sig";
+
+ // TP-Link Plug experiment
+ //final String inputPcapFile = path + "/training/tplink-plug/wlan1/tplink-plug.wlan1.local.pcap";
+ // TP-Link Plug DEVICE signatures
+ //final String onSignatureFile = path + "/training/tplink-plug/signatures/tplink-plug-onSignature-device-side.sig";
+ //final String offSignatureFile = path + "/training/tplink-plug/signatures/tplink-plug-offSignature-device-side.sig";
+ // TODO: EXPERIMENT - November 8, 2018
+ // TP-Link Plug experiment
+// final String inputPcapFile = path + "/experimental_result/standalone/tplink-plug/wlan1/tplink-plug.wlan1.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/standalone/tplink-plug/eth0/tplink-plug.eth0.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/tplink-plug/wlan1/tplink-plug.wlan1.detection.pcap";
+ //final String inputPcapFile = path + "/experimental_result/smarthome/tplink-plug/eth0/tplink-plug.eth0.detection.pcap";
+ // TP-Link Plug DEVICE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-onSignature-device-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-offSignature-device-side.sig";
+// final String onSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-onSignature-device-side-outbound.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-offSignature-device-side-outbound.sig";
+ // TP-Link Plug PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-offSignature-phone-side.sig";
+
+ // Arlo camera experiment
+// final String inputPcapFile = path + "/training/arlo-camera/wlan1/arlo-camera.wlan1.local.pcap";
+//// // TP-Link Plug DEVICE signatures
+// final String onSignatureFile = path + "/training/arlo-camera/signatures/arlo-camera-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/training/arlo-camera/signatures/arlo-camera-offSignature-phone-side.sig";
+ // TODO: EXPERIMENT - November 13, 2018
+ // Arlo Camera experiment
+// final String inputPcapFile = path + "/experimental_result/standalone/arlo-camera/wlan1/arlo-camera.wlan1.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/standalone/arlo-camera/eth0/arlo-camera.eth0.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/arlo-camera/wlan1/arlo-camera.wlan1.detection.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/arlo-camera/eth0/arlo-camera.eth0.detection.pcap";
+// final String inputPcapFile = path + "/training/arlo-camera/eth0/arlo-camera.eth0.local.pcap";
+ // Arlo Camera PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/arlo-camera/signatures/arlo-camera-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/arlo-camera/signatures/arlo-camera-offSignature-phone-side.sig";
+
+ // Amazon Alexa experiment
+// final String inputPcapFile = path + "/training/amazon-alexa/wlan1/alexa2.wlan1.local.pcap";
+// // TP-Link Plug DEVICE signatures
+// final String onSignatureFile = path + "/training/amazon-alexa/signatures/amazon-alexa-onSignature-device-side.sig";
+// final String offSignatureFile = path + "/training/amazon-alexa/signatures/amazon-alexa-offSignature-device-side.sig";
+
+ // SmartThings Plug experiment
+// final String inputPcapFile = path + "/training/st-plug/wlan1/st-plug.wlan1.local.pcap";
+// // SmartThings Plug DEVICE signatures
+// //final String onSignatureFile = path + "/training/st-plug/signatures/st-plug-onSignature-device-side.sig";
+// //final String offSignatureFile = path + "/training/st-plug/signatures/st-plug-offSignature-device-side.sig";
+// // SmartThings Plug PHONE signatures
+// final String onSignatureFile = path + "/training/st-plug/signatures/st-plug-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/training/st-plug/signatures/st-plug-offSignature-phone-side.sig";
+ // TODO: EXPERIMENT - November 12, 2018
+ // SmartThings Plug experiment
+// final String inputPcapFile = path + "/experimental_result/standalone/st-plug/wlan1/st-plug.wlan1.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/standalone/st-plug/eth0/st-plug.eth0.local.pcap";
+// //final String inputPcapFile = path + "/experimental_result/smarthome/st-plug/wlan1/st-plug.wlan1.detection.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/st-plug/eth0/st-plug.eth0.detection.pcap";
+// // SmartThings Plug PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/st-plug/signatures/st-plug-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/st-plug/signatures/st-plug-offSignature-phone-side.sig";
+
+
+ // LiFX Bulb experiment
+// final String inputPcapFile = path + "/training/lifx-bulb/wlan1/lifx-bulb.wlan1.local.pcap";
+// // LiFX Bulb DEVICE signatures
+// final String onSignatureFile = path + "/training/lifx-bulb/signatures/lifx-bulb-onSignature-device-side.sig";
+// final String offSignatureFile = path + "/training/lifx-bulb/signatures/lifx-bulb-offSignature-device-side.sig";
+ // LiFX Bulb PHONE signatures
+// final String onSignatureFile = path + "/training/lifx-bulb/signatures/lifx-bulb-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/training/lifx-bulb/signatures/lifx-bulb-offSignature-phone-side.sig";
+
+
+ /*
+ // Blossom Sprinkler experiment
+ //final String inputPcapFile = path + "/training/blossom-sprinkler/wlan1/blossom-sprinkler.wlan1.local.pcap";
+ final String inputPcapFile = path + "/training/blossom-sprinkler/eth0/blossom-sprinkler.eth0.local.pcap";
+ // Blossom Sprinkler DEVICE signatures
+ final String onSignatureFile = path + "/training/blossom-sprinkler/signatures/blossom-sprinkler-onSignature-device-side.sig";
+ final String offSignatureFile = path + "/training/blossom-sprinkler/signatures/blossom-sprinkler-offSignature-device-side.sig";
+ */
+
+ // Nest Thermostat experiment
+// final String inputPcapFile = path + "/training/nest-thermostat/wlan1/nest-thermostat.wlan1.local.pcap";
+// // Nest Thermostat DEVICE signatures
+//// final String onSignatureFile = path + "/training/nest-thermostat/signatures/nest-thermostat-onSignature-device-side.sig";
+//// final String offSignatureFile = path + "/training/nest-thermostat/signatures/nest-thermostat-offSignature-device-side.sig";
+// // Nest Thermostat PHONE signatures
+// final String onSignatureFile = path + "/training/nest-thermostat/signatures/nest-thermostat-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/training/nest-thermostat/signatures/nest-thermostat-offSignature-phone-side.sig";
+ // TODO: EXPERIMENT - November 15, 2018
+ // Nest Thermostat experiment
+// final String inputPcapFile = path + "/experimental_result/standalone/nest-thermostat/wlan1/nest-thermostat.wlan1.local.pcap";
+//// final String inputPcapFile = path + "/experimental_result/standalone/nest-thermostat/eth0/nest-thermostat.eth0.local.pcap";
+//// final String inputPcapFile = path + "/experimental_result/smarthome/nest-thermostat/wlan1/nest-thermostat.wlan1.detection.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/nest-thermostat/eth0/nest-thermostat.eth0.detection.pcap";
+//// // Nest Thermostat PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/nest-thermostat/signatures/nest-thermostat-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/nest-thermostat/signatures/nest-thermostat-offSignature-phone-side.sig";
+
+ /*
+ // Hue Bulb experiment
+ final String inputPcapFile = path + "/training/hue-bulb/wlan1/hue-bulb.wlan1.local.pcap";
+ // Hue Bulb PHONE signatures
+ final String onSignatureFile = path + "/training/hue-bulb/signatures/hue-bulb-onSignature-phone-side.sig";
+ final String offSignatureFile = path + "/training/hue-bulb/signatures/hue-bulb-offSignature-phone-side.sig";
+ */
+
+
+ // TP-Link Bulb experiment
+// final String inputPcapFile = path + "/training/tplink-bulb/wlan1/tplink-bulb.wlan1.local.pcap";
+// // TP-Link Bulb PHONE signatures
+// final String onSignatureFile = path + "/training/tplink-bulb/signatures/tplink-bulb-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/training/tplink-bulb/signatures/tplink-bulb-offSignature-phone-side.sig";
+ // TODO: EXPERIMENT - November 16, 2018
+ // TP-Link Bulb experiment
+// final String inputPcapFile = path + "/experimental_result/standalone/tplink-bulb/wlan1/tplink-bulb.wlan1.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/standalone/tplink-bulb/eth0/tplink-bulb.eth0.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/tplink-bulb/wlan1/tplink-bulb.wlan1.detection.pcap";
+//// final String inputPcapFile = path + "/experimental_result/smarthome/tplink-bulb/eth0/tplink-bulb.eth0.detection.pcap";
+// // TP-Link Bulb PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/tplink-bulb/signatures/tplink-bulb-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/tplink-bulb/signatures/tplink-bulb-offSignature-phone-side.sig";
+
+ /*
+ // WeMo Plug experiment
+ final String inputPcapFile = path + "/training/wemo-plug/wlan1/wemo-plug.wlan1.local.pcap";
+ // WeMo Plug PHONE signatures
+ final String onSignatureFile = path + "/training/wemo-plug/signatures/wemo-plug-onSignature-device-side.sig";
+ final String offSignatureFile = path + "/training/wemo-plug/signatures/wemo-plug-offSignature-device-side.sig";
+ */
+ // TODO: EXPERIMENT - November 20, 2018
+ // WeMo Plug experiment
+// final String inputPcapFile = path + "/experimental_result/standalone/wemo-plug/wlan1/wemo-plug.wlan1.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/standalone/wemo-plug/eth0/wemo-plug.eth0.local.pcap";
+ // TODO: WE HAVE 4 ADDITIONAL EVENTS (TRIGGERED MANUALLY), SO WE JUST IGNORE THEM BECAUSE THEY HAPPENED BEFORE
+ // TODO: THE ACTUAL TRIGGERS
+// final String inputPcapFile = path + "/experimental_result/smarthome/wemo-plug/wlan1/wemo-plug.wlan1.detection.pcap";
+//// final String inputPcapFile = path + "/experimental_result/smarthome/wemo-plug/eth0/wemo-plug.eth0.detection.pcap";
+// // WeMo Plug PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/wemo-plug/signatures/wemo-plug-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/wemo-plug/signatures/wemo-plug-offSignature-phone-side.sig";
+
+ /*
+ // WeMo Insight Plug experiment
+ final String inputPcapFile = path + "/training/wemo-insight-plug/wlan1/wemo-insight-plug.wlan1.local.pcap";
+ // WeMo Insight Plug PHONE signatures
+ final String onSignatureFile = path + "/training/wemo-insight-plug/signatures/wemo-insight-plug-onSignature-device-side.sig";
+ final String offSignatureFile = path + "/training/wemo-insight-plug/signatures/wemo-insight-plug-offSignature-device-side.sig";
+ */
+ // TODO: EXPERIMENT - November 21, 2018
+ // WeMo Insight Plug experiment
+// final String inputPcapFile = path + "/experimental_result/standalone/wemo-insight-plug/wlan1/wemo-insight-plug.wlan1.local.pcap";
+// final String inputPcapFile = path + "/experimental_result/standalone/wemo-insight-plug/eth0/wemo-insight-plug.eth0.local.pcap";
+ // TODO: WE HAVE 1 ADDITIONAL EVENT (FROM WEMO PLUG)
+// final String inputPcapFile = path + "/experimental_result/smarthome/wemo-insight-plug/wlan1/wemo-insight-plug.wlan1.detection.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/wemo-insight-plug/eth0/wemo-insight-plug.eth0.detection.pcap";
+ // WeMo Insight Plug PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/wemo-insight-plug/signatures/wemo-insight-plug-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/wemo-insight-plug/signatures/wemo-insight-plug-offSignature-phone-side.sig";
+
+
+ // Kwikset Doorlock Sep 12 experiment
+// final String inputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset3.wlan1.local.pcap";
+// // Kwikset Doorlock PHONE signatures
+// final String onSignatureFile = path + "/2018-08/kwikset-doorlock/onSignature-Kwikset-Doorlock-phone.sig";
+// final String offSignatureFile = path + "/2018-08/kwikset-doorlock/offSignature-Kwikset-Doorlock-phone.sig";
+ // TODO: EXPERIMENT - November 10, 2018
+ // Kwikset Door lock experiment
+// final String inputPcapFile = path + "/experimental_result/standalone/kwikset-doorlock/wlan1/kwikset-doorlock.wlan1.local.pcap";
+// //final String inputPcapFile = path + "/experimental_result/smarthome/kwikset-doorlock/wlan1/kwikset-doorlock.wlan1.detection.pcap";
+// final String inputPcapFile = path + "/experimental_result/smarthome/kwikset-doorlock/eth0/kwikset-doorlock.eth0.detection.pcap";
+//// // Kwikset Door lock PHONE signatures
+// final String onSignatureFile = path + "/experimental_result/standalone/kwikset-doorlock/signatures/kwikset-doorlock-onSignature-phone-side.sig";
+// final String offSignatureFile = path + "/experimental_result/standalone/kwikset-doorlock/signatures/kwikset-doorlock-offSignature-phone-side.sig";
+ final String onSignatureFile = path + "/training/signatures/kwikset-doorlock/kwikset-doorlock-onSignature-phone-side.sig";
+ final String offSignatureFile = path + "/training/signatures/kwikset-doorlock/kwikset-doorlock-offSignature-phone-side.sig";
+
+
+
+ // D-Link Siren experiment
+// final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap";
+ // D-Link Siren DEVICE signatures
+ //final String onSignatureFile = path + "/2018-08/dlink-siren/onSignature-DLink-Siren-device.sig";
+ //final String offSignatureFile = path + "/2018-08/dlink-siren/offSignature-DLink-Siren-device.sig";
+ // D-Link Siren PHONE signatures
+// final String onSignatureFile = path + "/2018-08/dlink-siren/onSignature-DLink-Siren-phone.sig";
+// final String offSignatureFile = path + "/2018-08/dlink-siren/offSignature-DLink-Siren-phone.sig";
+
+
+ // Output file names used (to make it easy to catch if one forgets to change them)
+ System.out.println("ON signature file in use is " + onSignatureFile);
+ System.out.println("OFF signature file in use is " + offSignatureFile);
+ System.out.println("PCAP file that is the target of detection is " + inputPcapFile);
List<List<List<PcapPacket>>> onSignature = PrintUtils.deserializeSignatureFromFile(onSignatureFile);
List<List<List<PcapPacket>>> offSignature = PrintUtils.deserializeSignatureFromFile(offSignatureFile);
+ // LAN
SignatureDetector onDetector = new SignatureDetector(onSignature, null);
SignatureDetector offDetector = new SignatureDetector(offSignature, null);
+ // WAN
+// SignatureDetector onDetector = new SignatureDetector(onSignature, "128.195.205.105");
+// SignatureDetector offDetector = new SignatureDetector(offSignature, "128.195.205.105");
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).
withLocale(Locale.US).withZone(ZoneId.of("America/Los_Angeles"));
default:
throw new AssertionError("unhandled event type");
}
- String output = String.format("[ !!! %s SIGNATURE DETECTED at %s !!! ]",
- eventDescription, dateTimeFormatter.format(ua.getTimestamp()));
+ //String output = String.format("[ !!! %s SIGNATURE DETECTED at %s !!! ]",
+ // eventDescription, dateTimeFormatter.format(ua.getTimestamp()));
+ String output = String.format("%s",
+ dateTimeFormatter.format(ua.getTimestamp()));
System.out.println(output);
};
// Sort the list of detected events by timestamp to make it easier to compare it line-by-line with the trigger
// times file.
Collections.sort(detectedEvents, Comparator.comparing(UserAction::getTimestamp));
+
// Output the detected events
detectedEvents.forEach(outputter);
+
+ System.out.println("Number of detected events of type " + UserAction.Type.TOGGLE_ON + ": " +
+ detectedEvents.stream().filter(ua -> ua.getType() == UserAction.Type.TOGGLE_ON).count());
+ System.out.println("Number of detected events of type " + UserAction.Type.TOGGLE_OFF + ": " +
+ detectedEvents.stream().filter(ua -> ua.getType() == UserAction.Type.TOGGLE_OFF).count());
+
+
+ // TODO: Temporary clean up until we clean the pipeline
+// List<UserAction> cleanedDetectedEvents = SignatureDetector.removeDuplicates(detectedEvents);
+// cleanedDetectedEvents.forEach(outputter);
}
/**
private final List<SignatureDetectionObserver> mObservers = new ArrayList<>();
+ /**
+ * Remove duplicates in {@code List} of {@code UserAction} objects. We need to clean this up for user actions
+ * that appear multiple times.
+ * TODO: This static method is probably just for temporary and we could get rid of this after we clean up
+ * TODO: the pipeline
+ *
+ * @param listUserAction A {@link List} of {@code UserAction}.
+ *
+ */
+ public static List<UserAction> removeDuplicates(List<UserAction> listUserAction) {
+
+ // Iterate and check for duplicates (check timestamps)
+ Set<Long> epochSecondSet = new HashSet<>();
+ // Create a target list for cleaned up list
+ List<UserAction> listUserActionClean = new ArrayList<>();
+ for(UserAction userAction : listUserAction) {
+ // Don't insert if any duplicate is found
+ if(!epochSecondSet.contains(userAction.getTimestamp().getEpochSecond())) {
+ listUserActionClean.add(userAction);
+ epochSecondSet.add(userAction.getTimestamp().getEpochSecond());
+ }
+ }
+ return listUserActionClean;
+ }
+
public SignatureDetector(List<List<List<PcapPacket>>> searchedSignature, String routerWanIp) {
// note: doesn't protect inner lists from changes :'(
mSignature = Collections.unmodifiableList(searchedSignature);
public void onMatch(ClusterMatcher clusterMatcher, List<PcapPacket> match) {
// Add the match at the corresponding index
pendingMatches[mClusterMatcherIds.get(clusterMatcher)].add(match);
- checkSignatureMatch3();
-
-
- // INITIAL
-// // No need to check for signature presence until all ClusterMatchers have found a match.
-// if (Arrays.stream(pendingMatches).noneMatch(l -> l.isEmpty())) {
-// // There's potentially a signature match...
-// // TODO need to check if all matches are within X seconds of one another
-//
-// List<List<PcapPacket>> signatureMatch = new ArrayList<>();
-// for (int i = 0; i < pendingMatches.length; i++) {
-// if (signatureMatch.size() != i) {
-// // Didn't manage to add sequence at previous index to signature match, so not a signature match.
-// // TODO: clear array?
-// return;
-// }
-// if (i == 0) {
-// // Special case with no preceding sequence as this is the first sequence of the signature.
-// // TODO...
-// signatureMatch.add(pendingMatches[i].get(0)); // TODO: pick earliest or latest match?
-// } else {
-// // Fetch the sequence in the signature that precedes this sequence
-// List<PcapPacket> prev = signatureMatch.get(i-1);
-// // And get a hold of it's latest packet; note that a match should never be empty so .get() is safe.
-// PcapPacket prevLatestPkt = prev.stream().max(Comparator.comparing(PcapPacket::getTimestamp)).get();
-// /*
-// * Do any of the matches of the sequence at the current index of the signature lie later in time
-// * than the match of the sequence that precedes it? If so, we are good and can proceed, otherwise we
-// * do not have a signature match.
-// */
-// Optional<List<PcapPacket>> curr = pendingMatches[i].stream().filter(pkts -> pkts.stream().allMatch(
-// pkt -> pkt.getTimestamp().isAfter(prevLatestPkt.getTimestamp()))).findFirst();
-// if (curr.isPresent()) {
-// // So far so good, keep going.
-// signatureMatch.add(curr.get());
-// } else {
-// // Bummer, not a signature match.
-// // TODO: clear array?
-// return;
-// }
-// }
-// }
-// // If we make it out of the loop, it means that we have managed to construct a match of the signature.
-// // Notify observers of the match.
-// // TODO: clear array? At the very least we need to remove those entries that we used for this match so they are not reused later.
-// mObservers.forEach(obs -> obs.onSignatureDetected(mSignature, signatureMatch));
-// }
-
+ checkSignatureMatch();
}
- private void checkSignatureMatch3() {
+ private void checkSignatureMatch() {
// << Graph-based approach using Balint's idea. >>
// This implementation assumes that the packets in the inner lists (the sequences) are ordered by asc timestamp.
}
}
- private void checkSignatureMatch2() {
- /*
- * In this implementation, we assume that the packets in the inner lists (the sequences) are ordered by
- * timestamp (ascending) AND that the outer list is ordered by timestamp of the most recent packet of each inner
- * list (i.e., the last packet of the inner list).
- */
- if (Arrays.stream(pendingMatches).noneMatch(l -> l.isEmpty())) {
- /*
- * The signature match consisting of one (or a set of) sequence(s) observed on (potentially multiple)
- * separate TCP connections. The signature match is reconstructed from the matches found by the individual
- * ClusterMatchers that each look for a separate sequence of packets occurring on one TCP connection.
- * Invariant used below: if all entries are non-null, we have a match; initially all entries are null.
- */
- List<PcapPacket>[] signatureMatch = new List[pendingMatches.length];
- // List<List<PcapPacket>> signatureMatch = new ArrayList<>();
- /*
- * For the first sequence of the signature, we give preference to the later samples as that strategy makes
- * it more likely that the full set of sequences that make up the signature fit in the time window that
- * dictates the maximum time between the sequences of the signature.
- */
- for (int i = pendingMatches[0].size()-1; i >= 0; i--) {
- signatureMatch[0] = pendingMatches[0].get(i);
- // Having selected the most recent sequence
- for (int j = 1; j < pendingMatches.length; j++) {
- List<List<PcapPacket>> entry = pendingMatches[j];
-
- }
-
- }
-
-
- /*
- // First sort by duration
- Stream<List<PcapPacket>> sortedByDuration = pendingMatches[0].stream().sorted((l1, l2) -> {
- Instant l1Max = l1.get(l1.size()-1).getTimestamp();
- Instant l1Min = l1.get(0).getTimestamp();
- Instant l2Max = l2.get(l2.size()-1).getTimestamp();
- Instant l2Min = l2.get(0).getTimestamp();
- Duration l1Duration = Duration.between(l1Min, l1Max);
- Duration l2Duration = Duration.between(l2Min, l2Max);
-
- return l1Duration.compareTo(l2Duration);
- });
- for (int i = 1; i < pendingMatches.length; i++) {
- pendingMatches[i].stream()
- }
- */
- }
-
- }
-
- /*
- private void checkSignatureMatch() {
- // There cannot be a signature match until each ClusterMatcher has found a match of its respective sequence.
- if (Arrays.stream(pendingMatches).noneMatch(l -> l.isEmpty())) {
- List<List<PcapPacket>> sigMatch = new ArrayList<>();
- for (int i = 0; i < pendingMatches.length; i++) {
- if (i + 1 < pendingMatches.length) {
- // We want to select the current element that is the latest, yet lies before the next element.
- // Start by fetching the matches at the next index.
- List<List<PcapPacket>> nextIdxMatches = pendingMatches[i+1];
- // Create a stream that contains the minimum packet timestamp of each inner list of nextIdMatches
- Stream<PcapPacket> nextMinTimestamps = nextIdxMatches.stream().
- map(l -> l.stream().min(Comparator.comparing(PcapPacket::getTimestamp)).get());
- // Create a stream that contains the maximum packet timestamps of each inner list of current index
- Stream<PcapPacket> currMaxTimestamps = pendingMatches[i].stream().
- map(ps -> ps.stream().max(Comparator.comparing(PcapPacket::getTimestamp)).get());
- currMaxTimestamps.filter(p1 -> nextMinTimestamps.anyMatch(p2 -> p2.getTimestamp().isAfter(p1.getTimestamp())));
-
-
-
- //pendingMatches[i].stream().filter(ps -> ps.stream().map(p1 -> ))
-
-
-
-
- pendingMatches[i].stream().filter(ps -> ps.stream().allMatch(p -> p.getTimestamp().isBefore(
- )))
-
-
- pendingMatches[i].stream().filter(ps -> ps.stream().allMatch(p -> p.getTimestamp().isBefore(
-
- )))
-
- Stream<PcapPacket> currMaxTimestamps = pendingMatches[i].stream().
- map(ps -> ps.stream().max(Comparator.comparing(PcapPacket::getTimestamp)));
-
-
-// pendingMatches[i].stream().filter(ps -> ps.stream().allMatch(p -> p.getTimestamp().isBefore(
-// // which match (item) in 'next' do we consider?
-// next.stream().
-// )))
- }
-
- }
- }
- }
- */
+ /**
+ * Used for registering for notifications of signatures detected by a {@link SignatureDetector}.
+ */
interface SignatureDetectionObserver {
- // TODO: add argument that points to the packets matching the signature
+
+ /**
+ * Invoked when the {@link SignatureDetector} detects the presence of a signature in the traffic that it's
+ * examining.
+ * @param searchedSignature The signature that the {@link SignatureDetector} reporting the match is searching
+ * for.
+ * @param matchingTraffic The actual traffic trace that matches the searched signature.
+ */
void onSignatureDetected(List<List<List<PcapPacket>>> searchedSignature,
List<List<PcapPacket>> matchingTraffic);
}