pcapng_utils.tshark.protocols package
Submodules
pcapng_utils.tshark.protocols.http1 module
- class pcapng_utils.tshark.protocols.http1.Http1Traffic(traffic)[source]
Bases:
object
Class to represent HTTP1 network traffic.
This class is the entry point for parsing HTTP1 network traffic.
The format of JSON data from tshark is as follows for a single HTTP request:
GET /spi/v2/platforms/ HTTP/1.1rn: Contains the HTTP method, URI, and version.
http.request.version: The HTTP version used.
http.request.line: A list of HTTP headers sent with the request.
http.host: The Host header value.
http.request.full_uri: The full URI including the scheme (e.g., https).
http.request_number: The request number.
http.response_in: The response number associated with this request.
The format of JSON data from tshark is as follows for a single HTTP response:
HTTP/1.1 200 OKrn: Contains the HTTP version, status code, and status phrase.
http.content_type: The Content-Type header value.
http.response.line: A list of HTTP headers sent with the response.
http.content_encoding: The Content-Encoding header value.
http.response_number: The response number.
http.time: The time taken for the response.
http.request_in: The request number associated with this response.
http.response_for.uri: The URI for which this response is generated.
http.file_data_raw: The data in hexadecimal format (requires -x flag).
- get_har_entries()[source]
Convert the HTTP network traffic to HTTP Archive (HAR) format. :return: the HTTP network traffic in HAR format
- parse_traffic()[source]
Parse the HTTP network traffic and extract the request-response pairs.
Identify each HTTP request and its associated HTTP response by following these steps:
Iterate through packets: It loops through all packets obtained from the traffic object.
Check protocols: It checks if the packet contains the http protocol by examining the frame.protocols field.
Identify http requests: It checks if the packet contains an HTTP request by looking for the http.request key in the http layer.
Find associated response: If the packet is an HTTP request and contains the http.response_in key, it retrieves the corresponding response packet using the get_packet_by_number method with the response number.
Create conversation: It creates an HttpConversation object with the request and response packets and appends it to the conversations list.
- class pcapng_utils.tshark.protocols.http1.HttpConversation(request_layers, response_layers)[source]
Bases:
object
Class to represent an HTTP conversation composed of a request and a response.
- class pcapng_utils.tshark.protocols.http1.HttpRequest(packet)[source]
Bases:
HttpRequestResponse
Class to represent an HTTP request.
- to_har()[source]
Convert the HTTP request to HTTP Archive (HAR) format. :return: the HTTP request in HAR format
- class pcapng_utils.tshark.protocols.http1.HttpRequestResponse(packet)[source]
Bases:
ABC
Base class for HTTP request and response packets. It wraps the packet data and provides methods to access the relevant information.
- class pcapng_utils.tshark.protocols.http1.HttpResponse(packet)[source]
Bases:
HttpRequestResponse
Class to represent an HTTP response.
- to_har()[source]
Convert the HTTP response to HTTP Archive (HAR) format. :return: the HTTP response in HAR format
pcapng_utils.tshark.protocols.http2 module
- class pcapng_utils.tshark.protocols.http2.Http2Helper[source]
Bases:
object
- classmethod get_headers_and_data(substreams)[source]
Identify the headers and data substreams and return them.
The substreams are identified by their types: - Headers substream: type 1 - Data substream: type 0 We ignore the rest of the substreams.
Note that (flag & 0x01) identify the end of stream, usually it happens for a data-stream but it may also happen for a header-stream (trailers in gRPC), or even never happen.
- Parameters:
substreams (Sequence[Http2Substream]) – the substreams of a HTTP2 stream
- Returns:
the headers and data substreams regardless if it is a request or a response
- static get_data(data_substreams)[source]
Extract the data from the substreams (precondition: all substreams are data substreams).
- Parameters:
data_substreams (Sequence[Http2Substream]) – the data substreams to be analyzed
- Returns:
the reassembled data
- Return type:
- static get_headers(substream)[source]
Extract the headers from the substream (precondition: it is a header substream).
- Parameters:
substream (Http2Substream) – the substream to be analyzed
- Returns:
the headers of the substream
- Return type:
- static substream_is_data(substream)[source]
Returns whether substream is a data substream.
- Return type:
- class pcapng_utils.tshark.protocols.http2.Http2Request(substreams)[source]
Bases:
Http2RequestResponse
Class to represent a HTTP2 request. It contains the headers and data of the request.
- class pcapng_utils.tshark.protocols.http2.Http2RequestResponse(substreams)[source]
Bases:
object
Base class to represent a HTTP2 request or response. It contains the headers and data of the request or response. Implements the common properties of a HTTP2 request or response.
- class pcapng_utils.tshark.protocols.http2.Http2Response(substreams)[source]
Bases:
Http2RequestResponse
Class to represent a HTTP2 response. It contains the headers and data of the response.
<!> May be empty for convenience (response never received)
- class pcapng_utils.tshark.protocols.http2.Http2Stream(tcp_stream_id, http2_stream_id, community_id)[source]
Bases:
object
Class to represent an entire HTTP2 stream (multiple substreams). It contains the request and response objects. Http2Stream represents a single HTTP2 stream that can contain multiple substreams as follows:
+-------------------------------------- (tcp stream, http2 stream) | Http2SubStream 1 | Request headers (type: 1) | Http2SubStream ... | Request data (type: 0, flags: 0x0) - partial data | Http2SubStream 3 | Request data (type: 0, flags: 0x1) - end of stream, contains reassembled data | (Http2SubStream 4 | Request trailers (type: 1)) +-------------------------------------- | Http2SubStream 5 | Response headers (type: 1) | Http2SubStream ... | Response data (type: 0, flags: 0x0) - partial data | Http2SubStream 7 | Response data (type: 0, flags: 0x1) - end of stream, contains reassembled data | (Http2SubStream 8 | Response trailers (type: 1)) +-------------------------------------- Each HTTP2 stream is uniquely identified by a tuple (tcp stream index, http2 stream index) and contains both request and response objects.
- __init__(tcp_stream_id, http2_stream_id, community_id)[source]
Defines a HTTP2 stream for the given TCP stream and HTTP2 stream.
- append(raw_http2_substream, all_layers)[source]
Append a new substream to the HTTP2 stream.
- Parameters:
substream – the substream to be added
frame – the frame containing the substream. A frame can contain multiple substreams.
- har_entry()[source]
Create a HAR entry for the HTTP2 stream. It contains the request and response objects.
- process()[source]
Process the substreams and create the request and response objects accordingly. Substreams are processed in order, the first substreams are request headers, followed by request data, and finally the response headers and data. The reassembled data is used to create the request and response objects.
Request substreams are identified by the presence of the ‘http2.request.full_uri’ key in the raw stream. If no response substream is found, the request object is created with the first substreams.
It retrieves the source and destination IP addresses from the first substream to identify the substreams that belong to the request. The response substreams are identified by checking their source IP address matches the destination IP address of the first substream.
- class pcapng_utils.tshark.protocols.http2.Http2Substream(raw_http2_substream, all_layers)[source]
Bases:
object
Class to represent a HTTP2 substream. It contains the layers of the packet and the metadata of the substream. Wrap the raw HTTP2 substream and the frame layers to extract the relevant information.
- class pcapng_utils.tshark.protocols.http2.Http2Traffic(traffic)[source]
Bases:
object
Class to represent the HTTP2 traffic. It contains the HTTP2 streams and the parsed traffic data.
In HTTP/2, frames are the smallest unit of communication. Each frame has a specific type and can have associated flags.
HTTP/2 frame types and flags:
HTTP/2 Frame Types:
DATA (0x0): carries arbitrary, variable-length sequences of octets associated with a stream.
HEADERS (0x1): used to open a stream and carry a header block fragment.
PRIORITY (0x2): specifies the sender-advised priority of a stream.
RST_STREAM (0x3): abruptly terminates a stream.
SETTINGS (0x4): used to communicate configuration parameters.
PUSH_PROMISE (0x5): used to notify the peer endpoint in advance of streams the sender intends to initiate.
PING (0x6): used to measure round-trip time and ensure the connection is still active.
GOAWAY (0x7): informs the peer to stop creating streams on this connection.
WINDOW_UPDATE (0x8): used to implement flow control.
CONTINUATION (0x9): used to continue a sequence of header block fragments.
HTTP/2 Frame Flags:
END_STREAM (0x1): indicates that the frame is the last one for the current stream.
END_HEADERS (0x4): indicates that the frame contains the entire header block.
PADDED (0x8): indicates that the frame contains padding.
PRIORITY (0x20): indicates that the frame contains priority information.
TCP stream ID and the HTTP/2 stream ID The TCP stream ID identifies a unique TCP connection. Each TCP connection is assigned a unique stream ID, which is used to track the packets that belong to that connection. The HTTP/2 stream ID, within a single TCP connection, multiple HTTP/2 streams can exist. Each HTTP/2 stream is identified by a unique stream ID within the context of that TCP connection. These stream IDs are used to multiplex multiple HTTP/2 requests and responses over a single TCP connection.
A single TCP stream (connection) can contain multiple HTTP/2 streams. Each HTTP/2 stream is uniquely identified within the context of its TCP stream. The combination of the TCP stream ID and the HTTP/2 stream ID uniquely identifies an HTTP/2 stream within the network traffic.
- parse_traffic()[source]
Parse the traffic and extract the HTTP2 streams. It creates a dictionary for each HTTP2 stream. Each key is a tuple with the TCP stream ID and the HTTP2 stream ID.
Identify each HTTP2 request and its associated HTTP2 response by following these steps:
Iterate through packets: it loops through all packets obtained from the traffic object.
Extract protocols: for each packet, it extracts the protocols from the frame.protocols field.
Check for HTTP2 protocol: it checks if the packet contains the http2 protocol.
Extract the TCP stream ID: it retrieves the TCP stream ID from the tcp.stream field.
Handle HTTP2 layer: it ensures the http2 layer is a list of HTTP2 stream objects.
Process each HTTP2 stream: for each HTTP2 stream in the http2 layer:
extract stream information: it retrieves the stream type and stream ID.
filter relevant streams: it ignores streams that are not data (type 0) or headers (type 1).
create or update stream pair: it creates a new tuple of (tcp_stream_id, http2_stream_id) if it does not exist and appends the substream to the list.
Process streams: after assembling the HTTP2 streams, it processes each stream to create the request and response objects.
Module contents
- class pcapng_utils.tshark.protocols.Http1Traffic(traffic)[source]
Bases:
object
Class to represent HTTP1 network traffic.
This class is the entry point for parsing HTTP1 network traffic.
The format of JSON data from tshark is as follows for a single HTTP request:
GET /spi/v2/platforms/ HTTP/1.1rn: Contains the HTTP method, URI, and version.
http.request.version: The HTTP version used.
http.request.line: A list of HTTP headers sent with the request.
http.host: The Host header value.
http.request.full_uri: The full URI including the scheme (e.g., https).
http.request_number: The request number.
http.response_in: The response number associated with this request.
The format of JSON data from tshark is as follows for a single HTTP response:
HTTP/1.1 200 OKrn: Contains the HTTP version, status code, and status phrase.
http.content_type: The Content-Type header value.
http.response.line: A list of HTTP headers sent with the response.
http.content_encoding: The Content-Encoding header value.
http.response_number: The response number.
http.time: The time taken for the response.
http.request_in: The request number associated with this response.
http.response_for.uri: The URI for which this response is generated.
http.file_data_raw: The data in hexadecimal format (requires -x flag).
- get_har_entries()[source]
Convert the HTTP network traffic to HTTP Archive (HAR) format. :return: the HTTP network traffic in HAR format
- parse_traffic()[source]
Parse the HTTP network traffic and extract the request-response pairs.
Identify each HTTP request and its associated HTTP response by following these steps:
Iterate through packets: It loops through all packets obtained from the traffic object.
Check protocols: It checks if the packet contains the http protocol by examining the frame.protocols field.
Identify http requests: It checks if the packet contains an HTTP request by looking for the http.request key in the http layer.
Find associated response: If the packet is an HTTP request and contains the http.response_in key, it retrieves the corresponding response packet using the get_packet_by_number method with the response number.
Create conversation: It creates an HttpConversation object with the request and response packets and appends it to the conversations list.
- class pcapng_utils.tshark.protocols.Http2Traffic(traffic)[source]
Bases:
object
Class to represent the HTTP2 traffic. It contains the HTTP2 streams and the parsed traffic data.
In HTTP/2, frames are the smallest unit of communication. Each frame has a specific type and can have associated flags.
HTTP/2 frame types and flags:
HTTP/2 Frame Types:
DATA (0x0): carries arbitrary, variable-length sequences of octets associated with a stream.
HEADERS (0x1): used to open a stream and carry a header block fragment.
PRIORITY (0x2): specifies the sender-advised priority of a stream.
RST_STREAM (0x3): abruptly terminates a stream.
SETTINGS (0x4): used to communicate configuration parameters.
PUSH_PROMISE (0x5): used to notify the peer endpoint in advance of streams the sender intends to initiate.
PING (0x6): used to measure round-trip time and ensure the connection is still active.
GOAWAY (0x7): informs the peer to stop creating streams on this connection.
WINDOW_UPDATE (0x8): used to implement flow control.
CONTINUATION (0x9): used to continue a sequence of header block fragments.
HTTP/2 Frame Flags:
END_STREAM (0x1): indicates that the frame is the last one for the current stream.
END_HEADERS (0x4): indicates that the frame contains the entire header block.
PADDED (0x8): indicates that the frame contains padding.
PRIORITY (0x20): indicates that the frame contains priority information.
TCP stream ID and the HTTP/2 stream ID The TCP stream ID identifies a unique TCP connection. Each TCP connection is assigned a unique stream ID, which is used to track the packets that belong to that connection. The HTTP/2 stream ID, within a single TCP connection, multiple HTTP/2 streams can exist. Each HTTP/2 stream is identified by a unique stream ID within the context of that TCP connection. These stream IDs are used to multiplex multiple HTTP/2 requests and responses over a single TCP connection.
A single TCP stream (connection) can contain multiple HTTP/2 streams. Each HTTP/2 stream is uniquely identified within the context of its TCP stream. The combination of the TCP stream ID and the HTTP/2 stream ID uniquely identifies an HTTP/2 stream within the network traffic.
- parse_traffic()[source]
Parse the traffic and extract the HTTP2 streams. It creates a dictionary for each HTTP2 stream. Each key is a tuple with the TCP stream ID and the HTTP2 stream ID.
Identify each HTTP2 request and its associated HTTP2 response by following these steps:
Iterate through packets: it loops through all packets obtained from the traffic object.
Extract protocols: for each packet, it extracts the protocols from the frame.protocols field.
Check for HTTP2 protocol: it checks if the packet contains the http2 protocol.
Extract the TCP stream ID: it retrieves the TCP stream ID from the tcp.stream field.
Handle HTTP2 layer: it ensures the http2 layer is a list of HTTP2 stream objects.
Process each HTTP2 stream: for each HTTP2 stream in the http2 layer:
extract stream information: it retrieves the stream type and stream ID.
filter relevant streams: it ignores streams that are not data (type 0) or headers (type 1).
create or update stream pair: it creates a new tuple of (tcp_stream_id, http2_stream_id) if it does not exist and appends the substream to the list.
Process streams: after assembling the HTTP2 streams, it processes each stream to create the request and response objects.