core¶
from mitm import core
Core components of the MITM framework.
- class Host[source]¶
Dataclass representing an
mitmhost.A host is a pair of
asyncio.StreamReaderandasyncio.StreamWriterobjects that are used to communicate with the remote host. There are two types of hosts: a client, and a server. A client host is one that is connected to themitm, and a server host is one that themitmconnected to on behalf of the client.The
mitm_managedattribute is used to determine whether themitmis responsible for closing the connection with the host. Ifmitm_managedis True, themitmwill close the connection with the host when it is done with it. Ifmitm_managedis set to False, themitmwill not close the connection with the host, and instead, the developer must close the connection with the host manually. This is useful for situations where themitmis running as a seperate utility and the developer wants to keep the connection open with the host after themitmis done with it.Note
See more on dataclasses.
- Parameters:
reader (
Optional[StreamReader]) – The reader of the host.writer (
Optional[StreamWriter]) – The writer of the host.mitm_managed (
Optional[bool]) – Whether or not the host is managed by themitm.
- reader¶
The reader of the host.
- Type:
asyncio.streams.StreamReader | None
- writer¶
The writer of the host.
- Type:
asyncio.streams.StreamWriter | None
- mitm_managed¶
Whether or not the host is managed by the
mitm.- Type:
bool | None
- ip¶
The IP address of the host.
- port¶
The port of the host.
Example
reader, writer = await asyncio.open_connection(...) server = Host(reader, writer)
- class Connection[source]¶
Dataclass representing a standard
mitmconnection.A connection is a pair of
Hostobjects that themitmrelays data between. When a connection is created the server host is not resolved until the data is intercepted and the protocol and destination server is figured out.Note
See more on dataclasses.
- Parameters:
Example
client = Host(...) server = Host(...) connection = Connection(client, server, Protocol.HTTP)
- class Flow(enum.Enum)[source]¶
Enum representing the flow of the connection.
Can be used within the appropriate locations to determine the flow of the connection. Not used by the core
mitmframework, but used by the HTTP extension.- Parameters:
CLIENT_TO_SERVER – The client is sending data to the server.
SERVER_TO_CLIENT – The server is sending data to the client.
- class Middleware(abc.ABC)[source]¶
Event-driven hook extension for the
mitm.A middleware is a class that is used to extend the
mitmframework by allowing event-driven hooks to be added to themitmand executed when the appropriate event occurs. Built-in middlewares can be found in themitm.middlewaremodule.- abstract async client_connected(connection)[source]¶
Called when the connection is established with the client.
Note
Note that the
mitm.core.Connectionobject is not fully initialized yet, and only contains a valid clientmitm.core.Host.
- abstract async server_connected(connection)[source]¶
Called when the connection is established with the server.
Note
At this point the
mitm.core.Connectionobject is fully initialized.
- abstract async client_data(connection, data)[source]¶
Called when data is received from the client.
Note
Raw TLS/SSL handshake is not sent through this method. Everything should be decrypted beforehand.
- Parameters:
request – The request received from the client.
- Return type:
bytes- Returns:
The request to send to the server.
- class InvalidProtocol(Exception)[source]¶
Exception raised when the protocol did not work.
This is the only error that
mitm.MITMwill catch. Throwing this error will continue the search for a valid protocol.
- class Protocol[source]¶
Custom protocol implementation.
Protocols are implementations on how the data flows between the client and server. Application-layer protocols are implemented by subclassing this class. Built-in protocols can be found in the
mitm.extensionpackage.- Parameters:
bytes_needed – Minimum number of bytes needed to determine the protocol.
buffer_size – The size of the buffer to use when reading data.
timeout – The timeout to use when reading data.
keep_alive – Whether or not to keep the connection alive.
- __init__(certificate_authority, middlewares)[source]¶
Initializes the protocol.
- Parameters:
certificate_authority (
Optional[CertificateAuthority]) – The certificate authority to use for the connection.middlewares (
Optional[List[Middleware]]) – The middlewares to use for the connection.
- abstract async resolve(connection, data)[source]¶
Resolves the destination of the connection.
- Parameters:
connection (
Connection) – Connection object containing a client host.data (
bytes) – The initial incoming data from the client.
- Return type:
Tuple[str,int,bool]- Returns:
A tuple containing the host, port, and bool that indicates if the connection is encrypted.
- Raises:
InvalidProtocol – If the connection failed.
- abstract async connect(connection, host, port, tls, data)[source]¶
Attempts to connect to destination server using the given data. Returns
Trueif the connection was successful, raisesInvalidProtocolif the connection failed.- Parameters:
connection (
Connection) – Connection object containing a client host.data (
bytes) – The initial incoming data from the client.
- Raises:
InvalidProtocol – If the connection failed.
- abstract async handle(connection)[source]¶
Handles the connection between a client and a server.
- Parameters:
connection (
Connection) – Client/server connection to relay.