1 package edu.uci.iotproject.detection.layer3;
3 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
4 import edu.uci.iotproject.analysis.UserAction;
5 import edu.uci.iotproject.detection.AbstractClusterMatcher;
6 import edu.uci.iotproject.detection.ClusterMatcherObserver;
7 import edu.uci.iotproject.io.PcapHandleReader;
8 import edu.uci.iotproject.util.PrintUtils;
9 import org.jgrapht.GraphPath;
10 import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
11 import org.jgrapht.graph.DefaultWeightedEdge;
12 import org.jgrapht.graph.SimpleDirectedWeightedGraph;
13 import org.pcap4j.core.*;
15 import java.time.Duration;
16 import java.time.ZoneId;
17 import java.time.format.DateTimeFormatter;
18 import java.time.format.FormatStyle;
20 import java.util.function.Consumer;
23 * Detects an event signature that spans one or multiple TCP connections.
25 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
26 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
28 public class SignatureDetector implements PacketListener, ClusterMatcherObserver {
31 public static void main(String[] args) throws PcapNativeException, NotOpenException {
32 // if (args.length < 3) {
33 // String errMsg = String.format("Usage: %s inputPcapFile onSignatureFile offSignatureFile",
34 // SignatureDetector.class.getSimpleName());
35 // System.out.println(errMsg);
38 // final String inputPcapFile = args[0];
39 // final String onSignatureFile = args[1];
40 // final String offSignatureFile = args[2];
42 String path = "/scratch/July-2018"; // Rahmadi
43 // String path = "/Users/varmarken/temp/UCI IoT Project/experiments"; // Janus
44 // String path = "/home/jvarmark/iot_project/datasets"; // Hera (server)
45 // String path = "/raid/varmarken/iot_project/datasets"; // Zeus (server)
48 //final String inputPcapFile = path + "/evaluation/no-activity/no-activity.wlan1.pcap";
50 // D-Link Siren experiment
51 // final String inputPcapFile = path + "/evaluation/dlink-siren/dlink-siren.data.wlan1.pcap";
52 // final String inputPcapFile = path + "/evaluation/dlink-siren/dlink-siren.eth0.local.pcap";
53 // D-Link Siren DEVICE signatures
54 // final String onSignatureFile = path + "/2018-08/dlink-siren/onSignature-DLink-Siren-device.sig";
55 // final String offSignatureFile = path + "/2018-08/dlink-siren/offSignature-DLink-Siren-device.sig";
56 // D-Link Siren PHONE signatures
57 // final String onSignatureFile = path + "/2018-08/dlink-siren/onSignature-DLink-Siren-phone.sig";
58 // final String offSignatureFile = path + "/2018-08/dlink-siren/offSignature-DLink-Siren-phone.sig";
59 // TODO: EXPERIMENT - November 19, 2018
60 // Hue Bulb experiment
61 // final String inputPcapFile = path + "/2018-08/hue-bulb/hue-bulb.wlan1.local.pcap";
62 // Hue Bulb PHONE signatures
63 // final String onSignatureFile = path + "/experimental_result/standalone/hue-bulb/signatures/hue-bulb-onSignature-phone-side.sig";
64 // final String offSignatureFile = path + "/experimental_result/standalone/hue-bulb/signatures/hue-bulb-offSignature-phone-side.sig";
67 // Kwikset Doorlock Sep 12 experiment
68 // final String inputPcapFile = path + "/evaluation/kwikset-doorlock/kwikset-doorlock.data.wlan1.pcap";
69 final String inputPcapFile = path + "/evaluation/kwikset-doorlock/kwikset-doorlock.data.eth0.pcap";
70 // // Kwikset Doorlock PHONE signatures
71 final String onSignatureFile = path + "/2018-08/kwikset-doorlock/onSignature-Kwikset-Doorlock-phone-new.sig";
72 final String offSignatureFile = path + "/2018-08/kwikset-doorlock/offSignature-Kwikset-Doorlock-phone-new.sig";
75 // D-Link Plug experiment
76 //final String inputPcapFile = path + "/evaluation/dlink/dlink-plug.data.wlan1.pcap";
77 // final String inputPcapFile = path + "/evaluation/dlink/dlink-plug.data.eth0.pcap";
79 // D-Link Plug DEVICE signatures
80 // final String onSignatureFile = path + "/2018-07/dlink/onSignature-DLink-Plug-device.sig";
81 // final String offSignatureFile = path + "/2018-07/dlink/offSignature-DLink-Plug-device.sig";
82 // D-Link Plug PHONE signatures
83 // final String onSignatureFile = path + "/2018-07/dlink/onSignature-DLink-Plug-phone.sig";
84 // final String offSignatureFile = path + "/2018-07/dlink/offSignature-DLink-Plug-phone.sig";
86 // TODO: The following are negative tests against the PCAP file from UNSW
87 // final String inputPcapFile = path + "/UNSW/16-10-04.pcap"; // TODO: Seems to be broken! Zero-payload!
88 // final String inputPcapFile = path + "/UNSW/16-10-12.pcap";
90 // final String inputPcapFile = path + "/UNSW/16-09-28.pcap"; // TODO: Seems to be broken! Zero-payload!
91 // final String inputPcapFile = path + "/UNSW/16-10-02.pcap"; // TODO: Seems to be broken!
92 // final String inputPcapFile = path + "/UNSW/16-10-03.pcap"; // TODO: Seems to be broken!
93 // final String inputPcapFile = path + "/UNSW/16-10-04-a.pcap"; // TODO: Seems to be broken! Zero-payload!
94 // final String inputPcapFile = path + "/UNSW/16-10-04-b.pcap"; // TODO: Seems to be broken! Zero-payload!
95 // final String inputPcapFile = path + "/UNSW/16-10-07.pcap"; // TODO: Seems to be broken!
96 // final String inputPcapFile = path + "/UNSW/16-10-08.pcap"; // TODO: Seems to be broken!
97 // final String inputPcapFile = path + "/UNSW/16-10-09.pcap"; // TODO: Seems to be broken!
98 // final String inputPcapFile = path + "/UNSW/16-10-10.pcap"; // TODO: Seems to be broken!
99 // final String inputPcapFile = path + "/UNSW/16-10-11.pcap"; // TODO: Seems to be broken!
100 // TODO: The following one is very long!!! - Split into smaller files!
101 // final String inputPcapFile = path + "/UNSW/16-10-12-a.pcap";
102 // final String inputPcapFile = path + "/UNSW/16-10-12-b.pcap";
103 // final String inputPcapFile = path + "/UNSW/16-10-12-c.pcap";
104 // final String inputPcapFile = path + "/UNSW/16-10-12-d.pcap";
106 // final String inputPcapFile = path + "/UNSW/16-09-23.pcap";
107 // final String inputPcapFile = path + "/UNSW/16-09-24.pcap";
108 // final String inputPcapFile = path + "/UNSW/16-09-25.pcap";
109 // final String inputPcapFile = path + "/UNSW/16-09-26.pcap";
110 // final String inputPcapFile = path + "/UNSW/16-09-27.pcap";
111 // final String inputPcapFile = path + "/UNSW/16-09-29.pcap";
112 // final String inputPcapFile = path + "/UNSW/16-10-01.pcap";
113 // final String inputPcapFile = path + "/UNSW/16-10-06.pcap";
114 // Negative test: dataset from UNB
115 final String inputPcapFile = path + "/evaluation/negative-datasets/UNB/Monday-WorkingHours_one-local-endpoint-001.pcap";
117 // TODO: The following are tests for signatures against training data
119 // D-Link Plug experiment
120 // final String inputPcapFile = path + "/training/dlink-plug/wlan1/dlink-plug.wlan1.local.pcap";
121 // D-Link Plug DEVICE signatures
122 // final String onSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-onSignature-device-side.sig";
123 // final String offSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-offSignature-device-side.sig";
124 // D-Link Plug PHONE signatures
125 // final String onSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-onSignature-phone-side.sig";
126 // final String offSignatureFile = path + "/training/dlink-plug/signatures/dlink-plug-offSignature-phone-side.sig";
128 // TODO: EXPERIMENT - November 7, 2018
129 // D-Link Plug experiment
130 //final String inputPcapFile = path + "/experimental_result/standalone/dlink-plug/wlan1/dlink-plug.wlan1.local.pcap";
131 //final String inputPcapFile = path + "/experimental_result/smarthome/dlink-plug/wlan1/dlink-plug.wlan1.detection.pcap";
132 //final String inputPcapFile = path + "/experimental_result/smarthome/dlink-plug/eth0/dlink-plug.eth0.detection.pcap";
133 // D-Link Plug DEVICE signatures
134 // final String onSignatureFile = path + "/experimental_result/standalone/dlink-plug/signatures/dlink-plug-onSignature-device-side.sig";
135 // final String offSignatureFile = path + "/experimental_result/standalone/dlink-plug/signatures/dlink-plug-offSignature-device-side.sig";
136 // D-Link Plug PHONE signatures
137 // final String onSignatureFile = path + "/experimental_result/standalone/dlink-plug/signatures/dlink-plug-onSignature-phone-side.sig";
138 // final String offSignatureFile = path + "/experimental_result/standalone/dlink-plug/signatures/dlink-plug-offSignature-phone-side.sig";
140 // TODO: EXPERIMENT - November 9, 2018
141 // D-Link Siren experiment
142 //final String inputPcapFile = path + "/experimental_result/standalone/dlink-siren/wlan1/dlink-siren.wlan1.local.pcap";
143 //final String inputPcapFile = path + "/experimental_result/smarthome/dlink-siren/wlan1/dlink-siren.wlan1.detection.pcap";
144 // final String inputPcapFile = path + "/experimental_result/smarthome/dlink-siren/eth0/dlink-siren.eth0.detection.pcap";
145 // D-Link Siren DEVICE signatures
146 // TODO: The device signature does not have pairs---only one packet which is 216, so we don't consider this as a signature
147 // final String onSignatureFile = path + "/experimental_result/standalone/dlink-siren/signatures/dlink-siren-onSignature-device-side.sig";
148 // final String offSignatureFile = path + "/experimental_result/standalone/dlink-siren/signatures/dlink-siren-offSignature-device-side.sig";
149 // D-Link Siren PHONE signatures
150 // final String onSignatureFile = path + "/experimental_result/standalone/dlink-siren/signatures/dlink-siren-onSignature-phone-side.sig";
151 // final String offSignatureFile = path + "/experimental_result/standalone/dlink-siren/signatures/dlink-siren-offSignature-phone-side.sig";
152 // final String onSignatureFile = path + "/training/signatures/dlink-siren/dlink-siren-onSignature-phone-side.sig";
153 // final String offSignatureFile = path + "/training/signatures/dlink-siren/dlink-siren-offSignature-phone-side.sig";
155 // TP-Link Plug experiment
156 //// final String inputPcapFile = path + "/training/tplink-plug/wlan1/tplink-plug.wlan1.local.pcap";
157 //// final String inputPcapFile = path + "/experimental_result/wifi-Sniffer/tests2/airtool_2019-01-04_11.08.45.AM.pcap";
158 // final String inputPcapFile = path + "/experimental_result/wifi-Sniffer/tests2/command-frames-only.pcap";
159 // // TP-Link Plug DEVICE signatures
160 // final String onSignatureFile = path + "/training/tplink-plug/signatures/tplink-plug-onSignature-device-side.sig";
161 // final String offSignatureFile = path + "/training/tplink-plug/signatures/tplink-plug-offSignature-device-side.sig";
162 // TODO: EXPERIMENT - November 8, 2018
163 // TP-Link Plug experiment
164 // final String inputPcapFile = path + "/experimental_result/standalone/tplink-plug/wlan1/tplink-plug.wlan1.local.pcap";
165 // final String inputPcapFile = path + "/experimental_result/standalone/tplink-plug/eth0/tplink-plug.eth0.local.pcap";
166 // final String inputPcapFile = path + "/experimental_result/smarthome/tplink-plug/wlan1/tplink-plug.wlan1.detection.pcap";
167 //final String inputPcapFile = path + "/experimental_result/smarthome/tplink-plug/eth0/tplink-plug.eth0.detection.pcap";
168 // TP-Link Plug DEVICE signatures
169 // final String onSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-onSignature-device-side.sig";
170 // final String offSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-offSignature-device-side.sig";
171 // final String onSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-onSignature-device-side-outbound.sig";
172 // final String offSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-offSignature-device-side-outbound.sig";
173 // TP-Link Plug PHONE signatures
174 // final String onSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-onSignature-phone-side.sig";
175 // final String offSignatureFile = path + "/experimental_result/standalone/tplink-plug/signatures/tplink-plug-offSignature-phone-side.sig";
177 // Arlo camera experiment
178 // final String inputPcapFile = path + "/training/arlo-camera/wlan1/arlo-camera.wlan1.local.pcap";
179 //// // TP-Link Plug DEVICE signatures
180 // final String onSignatureFile = path + "/training/arlo-camera/signatures/arlo-camera-onSignature-phone-side.sig";
181 // final String offSignatureFile = path + "/training/arlo-camera/signatures/arlo-camera-offSignature-phone-side.sig";
182 // TODO: EXPERIMENT - November 13, 2018
183 // Arlo Camera experiment
184 // final String inputPcapFile = path + "/experimental_result/standalone/arlo-camera/wlan1/arlo-camera.wlan1.local.pcap";
185 // final String inputPcapFile = path + "/experimental_result/standalone/arlo-camera/eth0/arlo-camera.eth0.local.pcap";
186 // final String inputPcapFile = path + "/experimental_result/smarthome/arlo-camera/wlan1/arlo-camera.wlan1.detection.pcap";
187 // final String inputPcapFile = path + "/experimental_result/smarthome/arlo-camera/eth0/arlo-camera.eth0.detection.pcap";
188 // final String inputPcapFile = path + "/training/arlo-camera/eth0/arlo-camera.eth0.local.pcap";
189 // Arlo Camera PHONE signatures
190 // final String onSignatureFile = path + "/experimental_result/standalone/arlo-camera/signatures/arlo-camera-onSignature-phone-side.sig";
191 // final String offSignatureFile = path + "/experimental_result/standalone/arlo-camera/signatures/arlo-camera-offSignature-phone-side.sig";
193 // Amazon Alexa experiment
194 // final String inputPcapFile = path + "/training/amazon-alexa/wlan1/alexa2.wlan1.local.pcap";
195 // // TP-Link Plug DEVICE signatures
196 // final String onSignatureFile = path + "/training/amazon-alexa/signatures/amazon-alexa-onSignature-device-side.sig";
197 // final String offSignatureFile = path + "/training/amazon-alexa/signatures/amazon-alexa-offSignature-device-side.sig";
199 // SmartThings Plug experiment
200 // final String inputPcapFile = path + "/training/st-plug/wlan1/st-plug.wlan1.local.pcap";
201 // // SmartThings Plug DEVICE signatures
202 // //final String onSignatureFile = path + "/training/st-plug/signatures/st-plug-onSignature-device-side.sig";
203 // //final String offSignatureFile = path + "/training/st-plug/signatures/st-plug-offSignature-device-side.sig";
204 // // SmartThings Plug PHONE signatures
205 // final String onSignatureFile = path + "/training/st-plug/signatures/st-plug-onSignature-phone-side.sig";
206 // final String offSignatureFile = path + "/training/st-plug/signatures/st-plug-offSignature-phone-side.sig";
207 // TODO: EXPERIMENT - November 12, 2018
208 // SmartThings Plug experiment
209 // final String inputPcapFile = path + "/experimental_result/standalone/st-plug/wlan1/st-plug.wlan1.local.pcap";
210 // final String inputPcapFile = path + "/experimental_result/standalone/st-plug/eth0/st-plug.eth0.local.pcap";
211 // //final String inputPcapFile = path + "/experimental_result/smarthome/st-plug/wlan1/st-plug.wlan1.detection.pcap";
212 // final String inputPcapFile = path + "/experimental_result/smarthome/st-plug/eth0/st-plug.eth0.detection.pcap";
213 // // SmartThings Plug PHONE signatures
214 // final String onSignatureFile = path + "/experimental_result/standalone/st-plug/signatures/st-plug-onSignature-phone-side.sig";
215 // final String offSignatureFile = path + "/experimental_result/standalone/st-plug/signatures/st-plug-offSignature-phone-side.sig";
216 // final String onSignatureFile = path + "/training/signatures/st-plug/st-plug-onSignature-phone-side.sig";
217 // final String offSignatureFile = path + "/training/signatures/st-plug/st-plug-offSignature-phone-side.sig";
219 // TODO: EXPERIMENT - January 9, 2018
220 // Blossom Sprinkler experiment
221 // final String inputPcapFile = path + "/experimental_result/standalone/blossom-sprinkler/wlan1/blossom-sprinkler.wlan1.local.pcap";
222 // final String inputPcapFile = path + "/experimental_result/smarthome/blossom-sprinkler/eth0/blossom-sprinkler.eth0.detection.pcap";
223 // final String inputPcapFile = path + "/experimental_result/smarthome/blossom-sprinkler/wlan1/blossom-sprinkler.wlan1.detection.pcap";
224 // Blossom Sprinkler DEVICE signatures
225 final String onSignatureFile = path + "/experimental_result/standalone/blossom-sprinkler/signatures/blossom-sprinkler-onSignature-device-side.sig";
226 final String offSignatureFile = path + "/experimental_result/standalone/blossom-sprinkler/signatures/blossom-sprinkler-offSignature-device-side.sig";
227 // Blossom Sprinkler PHONE signatures
228 // final String onSignatureFile = path + "/experimental_result/standalone/blossom-sprinkler/signatures/blossom-sprinkler-onSignature-phone-side.sig";
229 // final String offSignatureFile = path + "/experimental_result/standalone/blossom-sprinkler/signatures/blossom-sprinkler-offSignature-phone-side.sig";
231 // LiFX Bulb experiment
232 // final String inputPcapFile = path + "/training/lifx-bulb/wlan1/lifx-bulb.wlan1.local.pcap";
233 // // LiFX Bulb DEVICE signatures
234 // final String onSignatureFile = path + "/training/lifx-bulb/signatures/lifx-bulb-onSignature-device-side.sig";
235 // final String offSignatureFile = path + "/training/lifx-bulb/signatures/lifx-bulb-offSignature-device-side.sig";
236 // LiFX Bulb PHONE signatures
237 // final String onSignatureFile = path + "/training/lifx-bulb/signatures/lifx-bulb-onSignature-phone-side.sig";
238 // final String offSignatureFile = path + "/training/lifx-bulb/signatures/lifx-bulb-offSignature-phone-side.sig";
240 // Blossom Sprinkler experiment
241 // //final String inputPcapFile = path + "/training/blossom-sprinkler/wlan1/blossom-sprinkler.wlan1.local.pcap";
242 // final String inputPcapFile = path + "/2018-08/blossom/blossom.wlan1.local.pcap";
243 // //final String inputPcapFile = path + "/training/blossom-sprinkler/eth0/blossom-sprinkler.eth0.local.pcap";
244 // // Blossom Sprinkler DEVICE signatures
245 // final String onSignatureFile = path + "/training/blossom-sprinkler/signatures/blossom-sprinkler-onSignature-device-side.sig";
246 // final String offSignatureFile = path + "/training/blossom-sprinkler/signatures/blossom-sprinkler-offSignature-device-side.sig";
248 // Nest Thermostat experiment
249 // final String inputPcapFile = path + "/training/nest-thermostat/wlan1/nest-thermostat.wlan1.local.pcap";
250 // // Nest Thermostat DEVICE signatures
251 //// final String onSignatureFile = path + "/training/nest-thermostat/signatures/nest-thermostat-onSignature-device-side.sig";
252 //// final String offSignatureFile = path + "/training/nest-thermostat/signatures/nest-thermostat-offSignature-device-side.sig";
253 // // Nest Thermostat PHONE signatures
254 // final String onSignatureFile = path + "/training/nest-thermostat/signatures/nest-thermostat-onSignature-phone-side.sig";
255 // final String offSignatureFile = path + "/training/nest-thermostat/signatures/nest-thermostat-offSignature-phone-side.sig";
256 // TODO: EXPERIMENT - November 15, 2018
257 // Nest Thermostat experiment
258 // final String inputPcapFile = path + "/experimental_result/standalone/nest-thermostat/wlan1/nest-thermostat.wlan1.local.pcap";
259 //// final String inputPcapFile = path + "/experimental_result/standalone/nest-thermostat/eth0/nest-thermostat.eth0.local.pcap";
260 //// final String inputPcapFile = path + "/experimental_result/smarthome/nest-thermostat/wlan1/nest-thermostat.wlan1.detection.pcap";
261 // final String inputPcapFile = path + "/experimental_result/smarthome/nest-thermostat/eth0/nest-thermostat.eth0.detection.pcap";
262 //// // Nest Thermostat PHONE signatures
263 // final String onSignatureFile = path + "/experimental_result/standalone/nest-thermostat/signatures/nest-thermostat-onSignature-phone-side.sig";
264 // final String offSignatureFile = path + "/experimental_result/standalone/nest-thermostat/signatures/nest-thermostat-offSignature-phone-side.sig";
267 // Hue Bulb experiment
268 final String inputPcapFile = path + "/training/hue-bulb/wlan1/hue-bulb.wlan1.local.pcap";
269 // Hue Bulb PHONE signatures
270 final String onSignatureFile = path + "/training/hue-bulb/signatures/hue-bulb-onSignature-phone-side.sig";
271 final String offSignatureFile = path + "/training/hue-bulb/signatures/hue-bulb-offSignature-phone-side.sig";
276 // TP-Link Bulb experiment
277 // final String inputPcapFile = path + "/training/tplink-bulb/wlan1/tplink-bulb.wlan1.local.pcap";
278 // // TP-Link Bulb PHONE signatures
279 // final String onSignatureFile = path + "/training/tplink-bulb/signatures/tplink-bulb-onSignature-phone-side.sig";
280 // final String offSignatureFile = path + "/training/tplink-bulb/signatures/tplink-bulb-offSignature-phone-side.sig";
281 // TODO: EXPERIMENT - November 16, 2018
282 // TP-Link Bulb experiment
283 // final String inputPcapFile = path + "/experimental_result/standalone/tplink-bulb/wlan1/tplink-bulb.wlan1.local.pcap";
284 // final String inputPcapFile = path + "/experimental_result/standalone/tplink-bulb/eth0/tplink-bulb.eth0.local.pcap";
285 // final String inputPcapFile = path + "/experimental_result/smarthome/tplink-bulb/wlan1/tplink-bulb.wlan1.detection.pcap";
286 //// final String inputPcapFile = path + "/experimental_result/smarthome/tplink-bulb/eth0/tplink-bulb.eth0.detection.pcap";
287 // // TP-Link Bulb PHONE signatures
288 // final String onSignatureFile = path + "/experimental_result/standalone/tplink-bulb/signatures/tplink-bulb-onSignature-phone-side.sig";
289 // final String offSignatureFile = path + "/experimental_result/standalone/tplink-bulb/signatures/tplink-bulb-offSignature-phone-side.sig";
292 // WeMo Plug experiment
293 final String inputPcapFile = path + "/training/wemo-plug/wlan1/wemo-plug.wlan1.local.pcap";
294 // WeMo Plug PHONE signatures
295 final String onSignatureFile = path + "/training/wemo-plug/signatures/wemo-plug-onSignature-device-side.sig";
296 final String offSignatureFile = path + "/training/wemo-plug/signatures/wemo-plug-offSignature-device-side.sig";
298 // TODO: EXPERIMENT - November 20, 2018
299 // WeMo Plug experiment
300 // final String inputPcapFile = path + "/experimental_result/standalone/wemo-plug/wlan1/wemo-plug.wlan1.local.pcap";
301 // final String inputPcapFile = path + "/experimental_result/standalone/wemo-plug/eth0/wemo-plug.eth0.local.pcap";
302 // TODO: WE HAVE 4 ADDITIONAL EVENTS (TRIGGERED MANUALLY), SO WE JUST IGNORE THEM BECAUSE THEY HAPPENED BEFORE
303 // TODO: THE ACTUAL TRIGGERS
304 // final String inputPcapFile = path + "/experimental_result/smarthome/wemo-plug/wlan1/wemo-plug.wlan1.detection.pcap";
305 //// final String inputPcapFile = path + "/experimental_result/smarthome/wemo-plug/eth0/wemo-plug.eth0.detection.pcap";
306 // // WeMo Plug PHONE signatures
307 // final String onSignatureFile = path + "/experimental_result/standalone/wemo-plug/signatures/wemo-plug-onSignature-phone-side.sig";
308 // final String offSignatureFile = path + "/experimental_result/standalone/wemo-plug/signatures/wemo-plug-offSignature-phone-side.sig";
311 // WeMo Insight Plug experiment
312 final String inputPcapFile = path + "/training/wemo-insight-plug/wlan1/wemo-insight-plug.wlan1.local.pcap";
313 // WeMo Insight Plug PHONE signatures
314 final String onSignatureFile = path + "/training/wemo-insight-plug/signatures/wemo-insight-plug-onSignature-device-side.sig";
315 final String offSignatureFile = path + "/training/wemo-insight-plug/signatures/wemo-insight-plug-offSignature-device-side.sig";
317 // TODO: EXPERIMENT - November 21, 2018
318 // WeMo Insight Plug experiment
319 // final String inputPcapFile = path + "/experimental_result/standalone/wemo-insight-plug/wlan1/wemo-insight-plug.wlan1.local.pcap";
320 // final String inputPcapFile = path + "/experimental_result/standalone/wemo-insight-plug/eth0/wemo-insight-plug.eth0.local.pcap";
321 // TODO: WE HAVE 1 ADDITIONAL EVENT (FROM WEMO PLUG)
322 // final String inputPcapFile = path + "/experimental_result/smarthome/wemo-insight-plug/wlan1/wemo-insight-plug.wlan1.detection.pcap";
323 // final String inputPcapFile = path + "/experimental_result/smarthome/wemo-insight-plug/eth0/wemo-insight-plug.eth0.detection.pcap";
324 // WeMo Insight Plug PHONE signatures
325 // final String onSignatureFile = path + "/experimental_result/standalone/wemo-insight-plug/signatures/wemo-insight-plug-onSignature-phone-side.sig";
326 // final String offSignatureFile = path + "/experimental_result/standalone/wemo-insight-plug/signatures/wemo-insight-plug-offSignature-phone-side.sig";
329 // Kwikset Doorlock Sep 12 experiment
330 // final String inputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset3.wlan1.local.pcap";
331 // // Kwikset Doorlock PHONE signatures
332 // final String onSignatureFile = path + "/2018-08/kwikset-doorlock/onSignature-Kwikset-Doorlock-phone.sig";
333 // final String offSignatureFile = path + "/2018-08/kwikset-doorlock/offSignature-Kwikset-Doorlock-phone.sig";
334 // TODO: EXPERIMENT - November 10, 2018
335 // Kwikset Door lock experiment
336 // final String inputPcapFile = path + "/experimental_result/standalone/kwikset-doorlock/wlan1/kwikset-doorlock.wlan1.local.pcap";
337 // //final String inputPcapFile = path + "/experimental_result/smarthome/kwikset-doorlock/wlan1/kwikset-doorlock.wlan1.detection.pcap";
338 // final String inputPcapFile = path + "/experimental_result/smarthome/kwikset-doorlock/eth0/kwikset-doorlock.eth0.detection.pcap";
339 //// // Kwikset Door lock PHONE signatures
340 // final String onSignatureFile = path + "/experimental_result/standalone/kwikset-doorlock/signatures/kwikset-doorlock-onSignature-phone-side.sig";
341 // final String offSignatureFile = path + "/experimental_result/standalone/kwikset-doorlock/signatures/kwikset-doorlock-offSignature-phone-side.sig";
342 // final String onSignatureFile = path + "/training/signatures/kwikset-doorlock/kwikset-doorlock-onSignature-phone-side.sig";
343 // final String offSignatureFile = path + "/training/signatures/kwikset-doorlock/kwikset-doorlock-offSignature-phone-side.sig";
347 // D-Link Siren experiment
348 // final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap";
349 // D-Link Siren DEVICE signatures
350 //final String onSignatureFile = path + "/2018-08/dlink-siren/onSignature-DLink-Siren-device.sig";
351 //final String offSignatureFile = path + "/2018-08/dlink-siren/offSignature-DLink-Siren-device.sig";
352 // D-Link Siren PHONE signatures
353 // final String onSignatureFile = path + "/2018-08/dlink-siren/onSignature-DLink-Siren-phone.sig";
354 // final String offSignatureFile = path + "/2018-08/dlink-siren/offSignature-DLink-Siren-phone.sig";
357 // Output file names used (to make it easy to catch if one forgets to change them)
358 System.out.println("ON signature file in use is " + onSignatureFile);
359 System.out.println("OFF signature file in use is " + offSignatureFile);
360 System.out.println("PCAP file that is the target of detection is " + inputPcapFile);
362 List<List<List<PcapPacket>>> onSignature = PrintUtils.deserializeSignatureFromFile(onSignatureFile);
363 List<List<List<PcapPacket>>> offSignature = PrintUtils.deserializeSignatureFromFile(offSignatureFile);
366 // SignatureDetector onDetector = new SignatureDetector(onSignature, null);
367 // SignatureDetector offDetector = new SignatureDetector(offSignature, null);
369 SignatureDetector onDetector = new SignatureDetector(onSignature, "128.195.205.105");
370 SignatureDetector offDetector = new SignatureDetector(offSignature, "128.195.205.105");
372 final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).
373 withLocale(Locale.US).withZone(ZoneId.of("America/Los_Angeles"));
375 // Outputs information about a detected event to std.out
376 final Consumer<UserAction> outputter = ua -> {
377 String eventDescription;
378 switch (ua.getType()) {
380 eventDescription = "ON";
383 eventDescription = "OFF";
386 throw new AssertionError("unhandled event type");
388 //String output = String.format("[ !!! %s SIGNATURE DETECTED at %s !!! ]",
389 // eventDescription, dateTimeFormatter.format(ua.getTimestamp()));
390 String output = String.format("%s",
391 dateTimeFormatter.format(ua.getTimestamp()));
392 System.out.println(output);
395 // Let's create observers that construct a UserAction representing the detected event.
396 final List<UserAction> detectedEvents = new ArrayList<>();
397 onDetector.addObserver((searched, match) -> {
398 PcapPacket firstPkt = match.get(0).get(0);
399 detectedEvents.add(new UserAction(UserAction.Type.TOGGLE_ON, firstPkt.getTimestamp()));
401 offDetector.addObserver((searched, match) -> {
402 PcapPacket firstPkt = match.get(0).get(0);
403 detectedEvents.add(new UserAction(UserAction.Type.TOGGLE_OFF, firstPkt.getTimestamp()));
408 handle = Pcaps.openOffline(inputPcapFile, PcapHandle.TimestampPrecision.NANO);
409 } catch (PcapNativeException pne) {
410 handle = Pcaps.openOffline(inputPcapFile);
412 PcapHandleReader reader = new PcapHandleReader(handle, p -> true, onDetector, offDetector);
413 reader.readFromHandle();
415 // TODO: need a better way of triggering detection than this...
416 onDetector.mClusterMatchers.forEach(cm -> cm.performDetection());
417 offDetector.mClusterMatchers.forEach(cm -> cm.performDetection());
419 // Sort the list of detected events by timestamp to make it easier to compare it line-by-line with the trigger
421 Collections.sort(detectedEvents, Comparator.comparing(UserAction::getTimestamp));
423 // Output the detected events
424 detectedEvents.forEach(outputter);
426 System.out.println("Number of detected events of type " + UserAction.Type.TOGGLE_ON + ": " +
427 detectedEvents.stream().filter(ua -> ua.getType() == UserAction.Type.TOGGLE_ON).count());
428 System.out.println("Number of detected events of type " + UserAction.Type.TOGGLE_OFF + ": " +
429 detectedEvents.stream().filter(ua -> ua.getType() == UserAction.Type.TOGGLE_OFF).count());
432 // TODO: Temporary clean up until we clean the pipeline
433 // List<UserAction> cleanedDetectedEvents = SignatureDetector.removeDuplicates(detectedEvents);
434 // cleanedDetectedEvents.forEach(outputter);
438 * The signature that this {@link SignatureDetector} is searching for.
440 private final List<List<List<PcapPacket>>> mSignature;
443 * The {@link Layer3ClusterMatcher}s in charge of detecting each individual sequence of packets that together make up the
446 private final List<Layer3ClusterMatcher> mClusterMatchers;
449 * For each {@code i} ({@code i >= 0 && i < pendingMatches.length}), {@code pendingMatches[i]} holds the matches
450 * found by the {@link Layer3ClusterMatcher} at {@code mClusterMatchers.get(i)} that have yet to be "consumed", i.e.,
451 * have yet to be included in a signature detected by this {@link SignatureDetector} (a signature can be encompassed
452 * of multiple packet sequences occurring shortly after one another on multiple connections).
454 private final List<List<PcapPacket>>[] pendingMatches;
457 * Maps a {@link Layer3ClusterMatcher} to its corresponding index in {@link #pendingMatches}.
459 private final Map<Layer3ClusterMatcher, Integer> mClusterMatcherIds;
461 private final List<SignatureDetectionObserver> mObservers = new ArrayList<>();
464 * Remove duplicates in {@code List} of {@code UserAction} objects. We need to clean this up for user actions
465 * that appear multiple times.
466 * TODO: This static method is probably just for temporary and we could get rid of this after we clean up
469 * @param listUserAction A {@link List} of {@code UserAction}.
472 public static List<UserAction> removeDuplicates(List<UserAction> listUserAction) {
474 // Iterate and check for duplicates (check timestamps)
475 Set<Long> epochSecondSet = new HashSet<>();
476 // Create a target list for cleaned up list
477 List<UserAction> listUserActionClean = new ArrayList<>();
478 for(UserAction userAction : listUserAction) {
479 // Don't insert if any duplicate is found
480 if(!epochSecondSet.contains(userAction.getTimestamp().getEpochSecond())) {
481 listUserActionClean.add(userAction);
482 epochSecondSet.add(userAction.getTimestamp().getEpochSecond());
485 return listUserActionClean;
488 public SignatureDetector(List<List<List<PcapPacket>>> searchedSignature, String routerWanIp) {
489 // note: doesn't protect inner lists from changes :'(
490 mSignature = Collections.unmodifiableList(searchedSignature);
491 // Generate corresponding/appropriate ClusterMatchers based on the provided signature
492 List<Layer3ClusterMatcher> clusterMatchers = new ArrayList<>();
493 for (List<List<PcapPacket>> cluster : mSignature) {
494 clusterMatchers.add(new Layer3ClusterMatcher(cluster, routerWanIp, this));
496 mClusterMatchers = Collections.unmodifiableList(clusterMatchers);
499 pendingMatches = new List[mClusterMatchers.size()];
500 for (int i = 0; i < pendingMatches.length; i++) {
501 pendingMatches[i] = new ArrayList<>();
503 Map<Layer3ClusterMatcher, Integer> clusterMatcherIds = new HashMap<>();
504 for (int i = 0; i < mClusterMatchers.size(); i++) {
505 clusterMatcherIds.put(mClusterMatchers.get(i), i);
507 mClusterMatcherIds = Collections.unmodifiableMap(clusterMatcherIds);
510 public void addObserver(SignatureDetectionObserver observer) {
511 mObservers.add(observer);
514 public boolean removeObserver(SignatureDetectionObserver observer) {
515 return mObservers.remove(observer);
519 public void gotPacket(PcapPacket packet) {
520 // simply delegate packet reception to all ClusterMatchers.
521 mClusterMatchers.forEach(cm -> cm.gotPacket(packet));
525 public void onMatch(AbstractClusterMatcher clusterMatcher, List<PcapPacket> match) {
526 // Add the match at the corresponding index
527 pendingMatches[mClusterMatcherIds.get(clusterMatcher)].add(match);
528 checkSignatureMatch();
531 private void checkSignatureMatch() {
532 // << Graph-based approach using Balint's idea. >>
533 // This implementation assumes that the packets in the inner lists (the sequences) are ordered by asc timestamp.
535 // There cannot be a signature match until each Layer3ClusterMatcher has found a match of its respective sequence.
536 if (Arrays.stream(pendingMatches).noneMatch(l -> l.isEmpty())) {
538 final SimpleDirectedWeightedGraph<Vertex, DefaultWeightedEdge> graph =
539 new SimpleDirectedWeightedGraph<>(DefaultWeightedEdge.class);
540 // Add a vertex for each match found by all ClusterMatchers
541 // And maintain an array to keep track of what cluster matcher each vertex corresponds to
542 final List<Vertex>[] vertices = new List[pendingMatches.length];
543 for (int i = 0; i < pendingMatches.length; i++) {
544 vertices[i] = new ArrayList<>();
545 for (List<PcapPacket> sequence : pendingMatches[i]) {
546 Vertex v = new Vertex(sequence);
547 vertices[i].add(v); // retain reference for later when we are to add edges
548 graph.addVertex(v); // add to vertex to graph
551 // Add dummy source and sink vertices to facilitate search.
552 final Vertex source = new Vertex(null);
553 final Vertex sink = new Vertex(null);
554 graph.addVertex(source);
555 graph.addVertex(sink);
556 // The source is connected to all vertices that wrap the sequences detected by Layer3ClusterMatcher at index 0.
557 // Note: zero cost edges as this is just a dummy link to facilitate search from a common start node.
558 for (Vertex v : vertices[0]) {
559 DefaultWeightedEdge edge = graph.addEdge(source, v);
560 graph.setEdgeWeight(edge, 0.0);
562 // Similarly, all vertices that wrap the sequences detected by the last Layer3ClusterMatcher of the signature
563 // are connected to the sink node.
564 for (Vertex v : vertices[vertices.length-1]) {
565 DefaultWeightedEdge edge = graph.addEdge(v, sink);
566 graph.setEdgeWeight(edge, 0.0);
568 // Now link sequences detected by Layer3ClusterMatcher at index i to sequences detected by Layer3ClusterMatcher at index
569 // i+1 if they obey the timestamp constraint (i.e., that the latter is later in time than the former).
570 for (int i = 0; i < vertices.length; i++) {
572 if (j < vertices.length) {
573 for (Vertex iv : vertices[i]) {
574 PcapPacket ivLast = iv.sequence.get(iv.sequence.size()-1);
575 for (Vertex jv : vertices[j]) {
576 PcapPacket jvFirst = jv.sequence.get(jv.sequence.size()-1);
577 if (ivLast.getTimestamp().isBefore(jvFirst.getTimestamp())) {
578 DefaultWeightedEdge edge = graph.addEdge(iv, jv);
579 // The weight is the duration of the i'th sequence plus the duration between the i'th
580 // and i+1'th sequence.
581 Duration d = Duration.
582 between(iv.sequence.get(0).getTimestamp(), jvFirst.getTimestamp());
583 // Unfortunately weights are double values, so must convert from long to double.
584 // TODO: need nano second precision? If so, use d.toNanos().
585 // TODO: risk of overflow when converting from long to double..?
586 graph.setEdgeWeight(edge, Long.valueOf(d.toMillis()).doubleValue());
588 // Alternative version if we cannot assume that sequences are ordered by timestamp:
589 // if (iv.sequence.stream().max(Comparator.comparing(PcapPacket::getTimestamp)).get()
590 // .getTimestamp().isBefore(jv.sequence.stream().min(
591 // Comparator.comparing(PcapPacket::getTimestamp)).get().getTimestamp())) {
598 // Graph construction complete, run shortest-path to find a (potential) signature match.
599 DijkstraShortestPath<Vertex, DefaultWeightedEdge> dijkstra = new DijkstraShortestPath<>(graph);
600 GraphPath<Vertex, DefaultWeightedEdge> shortestPath = dijkstra.getPath(source, sink);
601 if (shortestPath != null) {
602 // The total weight is the duration between the first packet of the first sequence and the last packet
603 // of the last sequence, so we simply have to compare the weight against the timeframe that we allow
604 // the signature to span. For now we just use the inclusion window we defined for training purposes.
605 // Note however, that we must convert back from double to long as the weight is stored as a double in
607 if (((long)shortestPath.getWeight()) < TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS) {
608 // There's a signature match!
609 // Extract the match from the vertices
610 List<List<PcapPacket>> signatureMatch = new ArrayList<>();
611 for(Vertex v : shortestPath.getVertexList()) {
612 if (v == source || v == sink) {
613 // Skip the dummy source and sink nodes.
616 signatureMatch.add(v.sequence);
617 // As there is a one-to-one correspondence between vertices[] and pendingMatches[], we know that
618 // the sequence we've "consumed" for index i of the matched signature is also at index i in
619 // pendingMatches. We must remove it from pendingMatches so that we don't use it to construct
620 // another signature match in a later call.
621 pendingMatches[signatureMatch.size()-1].remove(v.sequence);
623 // Declare success: notify observers
624 mObservers.forEach(obs -> obs.onSignatureDetected(mSignature,
625 Collections.unmodifiableList(signatureMatch)));
632 * Used for registering for notifications of signatures detected by a {@link SignatureDetector}.
634 interface SignatureDetectionObserver {
637 * Invoked when the {@link SignatureDetector} detects the presence of a signature in the traffic that it's
639 * @param searchedSignature The signature that the {@link SignatureDetector} reporting the match is searching
641 * @param matchingTraffic The actual traffic trace that matches the searched signature.
643 void onSignatureDetected(List<List<List<PcapPacket>>> searchedSignature,
644 List<List<PcapPacket>> matchingTraffic);
648 * Encapsulates a {@code List<PcapPacket>} so as to allow the list to be used as a vertex in a graph while avoiding
649 * the expensive {@link AbstractList#equals(Object)} calls when adding vertices to the graph.
650 * Using this wrapper makes the incurred {@code equals(Object)} calls delegate to {@link Object#equals(Object)}
651 * instead of {@link AbstractList#equals(Object)}. The net effect is a faster implementation, but the graph will not
652 * recognize two lists that contain the same items--from a value and not reference point of view--as the same
653 * vertex. However, this is fine for our purposes -- in fact restricting it to reference equality seems more
656 private static class Vertex {
657 private final List<PcapPacket> sequence;
658 private Vertex(List<PcapPacket> wrappedSequence) {
659 sequence = wrappedSequence;