[
https://issues.apache.org/jira/browse/DRILL-7443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16979830#comment-16979830
]
ASF GitHub Bot commented on DRILL-7443:
---------------------------------------
cgivre commented on pull request #1898: DRILL-7443: Enable PCAP Plugin to
Reassemble TCP Streams
URL: https://github.com/apache/drill/pull/1898#discussion_r349429229
##########
File path:
exec/java-exec/src/main/java/org/apache/drill/exec/store/pcap/decoder/TcpSession.java
##########
@@ -0,0 +1,334 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.drill.exec.store.pcap.decoder;
+
+import org.joda.time.Instant;
+import org.joda.time.Period;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Collections;
+
+import static
org.apache.drill.exec.store.pcap.PcapFormatUtils.parseBytesToASCII;
+
+/**
+ * This class is the representation of a TCP session.
+ */
+public class TcpSession {
+
+ private ArrayList<Packet> packetsFromSender;
+ private ArrayList<Packet> packetsFromReceiver;
+
+ private long startTime;
+ private long endTime;
+ private long sessionLength;
+ private int packetCount;
+ private InetAddress srcIP;
+ private InetAddress dstIP;
+ private int srcPort;
+ private int dstPort;
+ private String srcMac;
+ private String dstMac;
+ private long sessionID;
+ private TcpHandshake handshake;
+ private long synTime;
+ private long ackTime;
+ private long connectTime;
+ private byte[] sentData;
+ private byte[] receivedData;
+ private int sentDataSize;
+ private int receivedDataSize;
+ private boolean hasCorruptedData = false;
+
+
+ private static final Logger logger =
LoggerFactory.getLogger(TcpSession.class);
+
+ public TcpSession (long sessionID) {
+ packetsFromSender = new ArrayList<>();
+ packetsFromReceiver = new ArrayList<>();
+
+ handshake = new TcpHandshake();
+ this.sessionID = sessionID;
+ }
+
+ /**
+ * This function adds a packet to the TCP session.
+ * @param p The Packet to be added to the session
+ */
+ public void addPacket(Packet p) {
+
+ // Only attempt to add TCP packets to session
+ if (!p.getPacketType().equalsIgnoreCase("TCP")) {
+ return;
+ }
+
+ // These variables should be consistent within a TCP session
+ if (packetCount == 0) {
+ srcIP = p.getSrc_ip();
+ dstIP = p.getDst_ip();
+
+ srcPort = p.getSrc_port();
+ dstPort = p.getDst_port();
+
+ srcMac = p.getEthernetSource();
+ dstMac = p.getEthernetDestination();
+ startTime = p.getTimestamp();
+ } else if (p.getSessionHash() != sessionID) {
+ logger.warn("Attempting to add session {} to incorrect TCP session.",
sessionID);
+ return;
+ }
+
+ // Add packet to appropriate list and increment the data size counter
+ if
(p.getSrc_ip().getHostAddress().equalsIgnoreCase(srcIP.getHostAddress())) {
+ packetsFromSender.add(p);
+ // Increment the data size counters
+ if (p.getData() != null) {
+ sentDataSize += p.getData().length;
+ }
+
+ } else {
+ packetsFromReceiver.add(p);
+ if (p.getData() != null) {
+ receivedDataSize += p.getData().length;
+ }
+ }
+
+ // Check flags if connection is not established
+ if (!handshake.isConnected()) {
+ if (p.getSynFlag() &&
p.getSrc_ip().getHostAddress().equalsIgnoreCase(srcIP.getHostAddress())) {
+ // This is part 1 of the TCP session handshake
+ // The host sends the first SYN packet
+ handshake.syn = true;
+ handshake.setSyn();
+ synTime = p.getTimestamp();
+ } else if (p.getSynFlag() && p.getAckFlag() &&
p.getSrc_ip().getHostAddress().equalsIgnoreCase(dstIP.getHostAddress())) {
+ // This condition represents the second part of the TCP Handshake,
+ // where the receiver sends a frame with the SYN/ACK flags set to the
originator
+ handshake.synAck = true;
+ handshake.setAck();
+ } else if (p.getAckFlag() &&
p.getSrc_ip().getHostAddress().equalsIgnoreCase(srcIP.getHostAddress())) {
+ // Finally, this condition represents a successful opening of a TCP
session, when the originator sends a frame with only the ACK flag set.
+ // At this point we finalize the session object and clear out the
flags.
+ handshake.ack = true;
+ handshake.setAck();
+ ackTime = p.getTimestamp();
+ connectTime = ackTime - synTime;
+ //handshake.setConnected(sessionID);
+ }
+ } else {
+ /* Check for flags to close connection. Closing a TCP session is more
difficult than opening a session and there are
+ * a lot of ways that it can go wrong. See
https://accedian.com/enterprises/blog/close-tcp-sessions-diagnose-disconnections/
for references on
Review comment:
Fixed
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Enable PCAP Plugin to Reassemble TCP Streams
> --------------------------------------------
>
> Key: DRILL-7443
> URL: https://issues.apache.org/jira/browse/DRILL-7443
> Project: Apache Drill
> Issue Type: Improvement
> Components: Storage - Other
> Affects Versions: 1.16.0
> Reporter: Charles Givre
> Assignee: Charles Givre
> Priority: Major
> Fix For: 1.17.0
>
>
> One common task in network forensics is reassembling TCP streams from
> captured network data. This PR adds this capability to Drill.
> h2. Usage
> To enable TCP re-sessionization, in the configuration for the PCAP reader,
> simply set the variable: {{sessionizeTCPStreams}} to {{true}}.
> This can also be accomplished at query time by using the {{table()}} method.
> {{SELECT * FROM table(dfs.test.`attack-trace.pcap` (type => 'pcap',
> sessionizeTCPStreams=> true))}}
> h3. Results
> *When this option is enabled, Drill will ignore all packets that are not TCP
> packets.*
> Executing a query with this option enables changes the results Drill will
> return from PCAP files.
> You will get the following columns:
> * session_start_time: The start time of the session
> * session_end_time: The ending time of the session
> * session_duration: The duration of the session. This will be a Drill PERIOD
> datatype.
> * total_packet_count: The number of packets in the session
> * connection_time: The amount of time it took for the TCP handshake to be
> completed. Useful for network diagnostics
> * src_ip: The IP address of the initiating machine
> * dst_ip: The IP address of the remote machine
> * src_port: The port of the originating machine
> * dst_port: The port of the remote machine
> * src_mac_address: The MAC address of the originating machine
> * dst_mac_address: The MAC address of the remote machine
> * tcp_session: This is the session hash for the TCP session. (Long)
> * is_corrupt: True/false if the session contains corrupted packets
> * data_from_originator: The data sent from the originator
> * data_from_remote: The data sent from the remote machine
> * data_volume_from_remote: The number of bytes sent from the remote host
> * data_volume_from_origin: The number of bytes sent from the originating
> machine
> * packet_count_from_origin: The number of packets sent from the originating
> machine
> * packet_count_from_remote: The number of packets sent from the remote
> machine
>
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)