From 4544671abcd2f3848dfde7a094852893c9d45830 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Wed, 12 Sep 2018 08:41:38 -0700 Subject: [PATCH] Printing directions in the toCSV() method in PrintUtils --- .../.idea/modules/SmartPlugDetector_main.iml | 13 ++ .../main/java/edu/uci/iotproject/Main.java | 127 +++--------------- .../iotproject/analysis/PcapPacketPair.java | 31 +++++ .../edu/uci/iotproject/util/PrintUtils.java | 10 +- 4 files changed, 73 insertions(+), 108 deletions(-) diff --git a/Code/Projects/SmartPlugDetector/.idea/modules/SmartPlugDetector_main.iml b/Code/Projects/SmartPlugDetector/.idea/modules/SmartPlugDetector_main.iml index 5134c0a..777fd39 100644 --- a/Code/Projects/SmartPlugDetector/.idea/modules/SmartPlugDetector_main.iml +++ b/Code/Projects/SmartPlugDetector/.idea/modules/SmartPlugDetector_main.iml @@ -14,5 +14,18 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java index 1d3888a..c2f6401 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java @@ -2,14 +2,12 @@ package edu.uci.iotproject; import static edu.uci.iotproject.analysis.UserAction.Type; -import edu.uci.iotproject.analysis.TcpConversationUtils; -import edu.uci.iotproject.analysis.TrafficLabeler; -import edu.uci.iotproject.analysis.TriggerTrafficExtractor; -import edu.uci.iotproject.analysis.UserAction; +import edu.uci.iotproject.analysis.*; import edu.uci.iotproject.comparison.seqalignment.ExtractedSequence; import edu.uci.iotproject.comparison.seqalignment.SequenceAlignment; import edu.uci.iotproject.comparison.seqalignment.SequenceExtraction; import edu.uci.iotproject.io.TriggerTimesFileReader; +import edu.uci.iotproject.util.PrintUtils; import org.pcap4j.core.*; import org.pcap4j.packet.namednumber.DataLinkType; @@ -129,7 +127,7 @@ public class Main { final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap"; final String outputPcapFile = path + "/2018-08/dlink-siren/dlink-siren-processed.pcap"; final String triggerTimesFile = path + "/2018-08/dlink-siren/dlink-siren-aug-14-2018.timestamps"; - final String deviceIp = "192.168.1.183"; // .246 == phone; .183 == siren + final String deviceIp = "192.168.1.246"; // .246 == phone; .183 == siren // 14) Nest thermostat August 15 experiment // final String inputPcapFile = path + "/2018-08/nest/nest.wlan1.local.pcap"; @@ -221,6 +219,7 @@ public class Main { }); + System.out.println("==== ON ===="); // Print out all the pairs into a file for ON events File fileOnEvents = new File(onPairsPath); PrintWriter pwOn = null; @@ -238,58 +237,16 @@ public class Main { for(Conversation conv : listConv) { // Process only if it is a TLS packet if (conv.isTls()) { - List tlsAppDataList = conv.getTlsApplicationDataPackets(); - // Loop and print out packets - int count = 0; - // The direction of the first packet - Conversation.Direction firstDir = null; - // The length of the first packet - int firstLen = 0; - for (PcapPacket pcap : tlsAppDataList) { - boolean isPair = false; - if (count % 2 == 0) { - firstDir = conv.getDirection(pcap); - firstLen = pcap.length(); - } else {// count%2 == 1 - if(conv.getDirection(pcap) != firstDir) { - isPair = true; - pwOn.println(firstLen + ", " + pcap.length()); - //System.out.println(firstDir + ", " + conv.getDirection(pcap)); - //System.out.println(firstLen + ", " + pcap.length()); - } - } - count++; - // If we can't create a pair then just pad it with 0 - if (count == tlsAppDataList.size() && !isPair) { - pwOn.println(firstLen + ", 0"); - } + List tlsAppDataList = TcpConversationUtils.extractTlsAppDataPacketPairs(conv); + for(PcapPacketPair pair: tlsAppDataList) { + System.out.println(PrintUtils.toCsv(pair, dnsMap)); + pwOn.println(PrintUtils.toCsv(pair, dnsMap)); } } else { // Non-TLS conversations - List packetList = conv.getPackets(); - // Loop and print out packets - int count = 0; - // The direction of the first packet - Conversation.Direction firstDir = null; - // The length of the first packet - int firstLen = 0; - for (PcapPacket pcap : packetList) { - boolean isPair = false; - if (count % 2 == 0) { - firstDir = conv.getDirection(pcap); - firstLen = pcap.length(); - } else {// count%2 == 1 - if(conv.getDirection(pcap) != firstDir) { - isPair = true; - pwOn.println(firstLen + ", " + pcap.length()); - System.out.println(firstDir + ", " + conv.getDirection(pcap)); - System.out.println(firstLen + ", " + pcap.length()); - } - } - count++; - // If we can't create a pair then just pad it with 0 - if (count == packetList.size() && !isPair) { - pwOn.println(firstLen + ", 0"); - } + List packetList = TcpConversationUtils.extractPacketPairs(conv); + for(PcapPacketPair pair: packetList) { + System.out.println(PrintUtils.toCsv(pair, dnsMap)); + pwOn.println(PrintUtils.toCsv(pair, dnsMap)); } } } @@ -297,6 +254,7 @@ public class Main { } pwOn.close(); + System.out.println("==== OFF ===="); // Print out all the pairs into a file for ON events File fileOffEvents = new File(offPairsPath); PrintWriter pwOff = null; @@ -314,59 +272,16 @@ public class Main { for(Conversation conv : listConv) { // Process only if it is a TLS packet if (conv.isTls()) { - List tlsAppDataList = conv.getTlsApplicationDataPackets(); - // Loop and print out packets - int count = 0; - // The direction of the first packet - Conversation.Direction firstDir = null; - // The length of the first packet - int firstLen = 0; - for (PcapPacket pcap : tlsAppDataList) { - boolean isPair = false; - if (count % 2 == 0) { - firstDir = conv.getDirection(pcap); - firstLen = pcap.length(); - } else {// count%2 == 1 - if(conv.getDirection(pcap) != firstDir) { - isPair = true; - pwOff.println(firstLen + ", " + pcap.length()); - System.out.println(firstDir + ", " + conv.getDirection(pcap)); - System.out.println(firstLen + ", " + pcap.length()); - } - } - count++; - // If we can't create a pair then just pad it with 0 - if (count == tlsAppDataList.size() && !isPair) { - pwOff.println(firstLen + ", 0"); - } - + List tlsAppDataList = TcpConversationUtils.extractTlsAppDataPacketPairs(conv); + for(PcapPacketPair pair: tlsAppDataList) { + System.out.println(PrintUtils.toCsv(pair, dnsMap)); + pwOff.println(PrintUtils.toCsv(pair, dnsMap)); } } else { // Non-TLS conversations - List packetList = conv.getPackets(); - // Loop and print out packets - int count = 0; - // The direction of the first packet - Conversation.Direction firstDir = null; - // The length of the first packet - int firstLen = 0; - for (PcapPacket pcap : packetList) { - boolean isPair = false; - if (count % 2 == 0) { - firstDir = conv.getDirection(pcap); - firstLen = pcap.length(); - } else {// count%2 == 1 - if(conv.getDirection(pcap) != firstDir) { - isPair = true; - pwOff.println(firstLen + ", " + pcap.length()); - System.out.println(firstDir + ", " + conv.getDirection(pcap)); - System.out.println(firstLen + ", " + pcap.length()); - } - } - count++; - // If we can't create a pair then just pad it with 0 - if (count == packetList.size() && !isPair) { - pwOff.println(firstLen + ", 0"); - } + List packetList = TcpConversationUtils.extractPacketPairs(conv); + for (PcapPacketPair pair : packetList) { + System.out.println(PrintUtils.toCsv(pair, dnsMap)); + pwOff.println(PrintUtils.toCsv(pair, dnsMap)); } } } diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapPacketPair.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapPacketPair.java index f77531f..cec81df 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapPacketPair.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapPacketPair.java @@ -1,8 +1,11 @@ package edu.uci.iotproject.analysis; +import edu.uci.iotproject.util.PcapPacketUtils; import org.apache.commons.math3.ml.clustering.Clusterable; import org.pcap4j.core.PcapPacket; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.Optional; /** @@ -24,8 +27,36 @@ public class PcapPacketPair implements Clusterable { public PcapPacket getFirst() { return mFirst; } + public boolean isFirstClient() { + String firstIp = PcapPacketUtils.getSourceIp(mFirst); + InetAddress ia = null; + try { + ia = InetAddress.getByName(firstIp); + } catch (UnknownHostException ex) { + ex.printStackTrace(); + } + return ia.isSiteLocalAddress(); + } + public Optional getSecond() { return mSecond; } + public boolean isSecondClient() { + // Return the value of the second source if it is not null + if (mSecond.isPresent()) { + String secondIp = PcapPacketUtils.getSourceIp(mSecond.get()); + InetAddress ia = null; + try { + ia = InetAddress.getByName(secondIp); + } catch (UnknownHostException ex) { + ex.printStackTrace(); + } + return ia.isSiteLocalAddress(); + } else { + // When it is null, we always return the opposite of the first source's status + return !isFirstClient(); + } + } + @Override public String toString() { return String.format("%d, %s", diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java index 0183922..ae6b578 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java @@ -69,10 +69,16 @@ public class PrintUtils { // Fall back to IP if we couldn't second pair is present, but we couldn't map to (a) hostname(s). secondSrc = hostnames.isPresent() ? hostnames : secondSrc; - return String.format("%d, %d, %s, %s", packetPair.getFirst().getOriginalLength(), + // Check if the first source is C (client) or S (server) + String firstSrcCorS = packetPair.isFirstClient() ? "C" : "S"; + String secondSrcCorS = packetPair.isSecondClient() ? "C" : "S"; + + return String.format("%d, %d, %s, %s, %s, %s", packetPair.getFirst().getOriginalLength(), packetPair.getSecond().map(pp -> pp.getOriginalLength()).orElse(0), firstSrc, - secondSrc.orElse("null")); + secondSrc.orElse("null"), + firstSrcCorS, + secondSrcCorS); } } -- 2.34.1