From: Janus Varmarken Date: Fri, 31 Aug 2018 22:18:07 +0000 (-0700) Subject: TcpConversationUtils.java: add method for grouping conversations by TLS Application... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=1dc2ecbbf701ec5ca3611f4f73499ff2622957ad;p=pingpong.git TcpConversationUtils.java: add method for grouping conversations by TLS Application Data sequence; add method for getting packet length sequence for TLS Application data. --- diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TcpConversationUtils.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TcpConversationUtils.java index f43077d..dc38358 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TcpConversationUtils.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TcpConversationUtils.java @@ -9,6 +9,7 @@ import org.pcap4j.packet.IpV4Packet; import org.pcap4j.packet.TcpPacket; import java.util.*; +import java.util.stream.Collectors; /** * Utility functions for analyzing and structuring (sets of) {@link Conversation}s. @@ -242,6 +243,13 @@ public class TcpConversationUtils { return result; } + public static Map> groupConversationsByTlsApplicationDataPacketSequence(Collection conversations) { + return conversations.stream().collect(Collectors.groupingBy( + c -> c.getTlsApplicationDataPackets().stream().map(p -> Integer.toString(p.getOriginalLength())). + reduce("", (s1, s2) -> s1.length() == 0 ? s2 : s1 + " " + s2)) + ); + } + /** * Given a {@link Conversation}, counts the frequencies of each unique packet length seen as part of the * {@code Conversation}. @@ -304,14 +312,35 @@ public class TcpConversationUtils { * packet lengths in the returned array are ordered by packet timestamp. */ public static Integer[] getPacketLengthSequence(Conversation c) { - List packets = c.getPackets(); - Integer[] packetLengthSequence = new Integer[packets.size()]; - for (int i = 0; i < packetLengthSequence.length; i++) { - packetLengthSequence[i] = packets.get(i).getOriginalLength(); + return getPacketLengthSequence(c.getPackets()); + } + + + /** + * Given a {@link Conversation}, extract its packet length sequence, but only include packet lengths of those + * packets that carry TLS Application Data. + * @param c The {@link Conversation} from which a TLS Application Data packet length sequence is to be extracted. + * @return An {@code Integer[]} that holds the packet lengths of all packets in {@code c} that carry TLS Application + * Data. The packet lengths in the returned array are ordered by packet timestamp. + */ + public static Integer[] getPacketLengthSequenceTlsAppDataOnly(Conversation c) { + if (!c.isTls()) { + throw new IllegalArgumentException("Provided " + c.getClass().getSimpleName() + " was not a TLS session"); } - return packetLengthSequence; + return getPacketLengthSequence(c.getTlsApplicationDataPackets()); + } + + /** + * Given a list of packets, extract the packet lengths and wrap them in an array such that the packet lengths in the + * resulting array appear in the same order as their corresponding packets in the input list. + * @param packets The list of packets for which the packet lengths are to be extracted. + * @return An array containing the packet lengths in the same order as their corresponding packets in the input list. + */ + private static Integer[] getPacketLengthSequence(List packets) { + return packets.stream().map(pkt -> pkt.getOriginalLength()).toArray(Integer[]::new); } + /** * Appends a space to {@code sb} iff {@code sb} already contains some content. * @param sb A {@link StringBuilder} that should have a space appended iff it is not empty.