Internals of mitmΒΆ
Quick guide on the internal structure of mitm.
Make sure to check out the documentation for asyncio streams before reading this section.
CoreΒΆ
- class mitm.core.HostΒΆ
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.- readerΒΆ
The
asyncio.StreamReaderobject for the host.
- writerΒΆ
The
asyncio.StreamWriterobject for the host.
- mitm_managedΒΆ
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.
- ipΒΆ
The IP address of the host.
- portΒΆ
The port of the host.
Hostis a dataclass that is defined like so:@dataclass class Host: reader: Optional[asyncio.StreamReader] = None writer: Optional[asyncio.StreamWriter] = None mitm_managed: Optional[bool] = True
- class mitm.core.ConnectionΒΆ
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.- clientΒΆ
The client
mitm.core.Host. The client host connects to themitm.
- serverΒΆ
The server
mitm.core.Host. A server host is connected to themitmon behalf of the client host.
- protocolΒΆ
The
mitm.core.Protocolobject for the connection.
Connectionis a dataclass that is defined like so:@dataclass class Connection: client: Host server: Host protocol: Optional[Protocol] = None
ExtensionsΒΆ
- class mitm.core.MiddlewareΒΆ
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.- async static mitm_started(host: str, port: int)ΒΆ
Called when the
mitmserver boots-up.
- async static client_connected(connection: Connection)ΒΆ
Called when a client connects to the
mitmserver. Note that themitm.core.Connectionobject is not fully initialized yet, and only contains a valid clientmitm.core.Host.
- async static server_connected(connection: Connection)ΒΆ
Called when the
mitmconnects with the destination server. At this point themitm.core.Connectionobject is fully initialized.
- async static client_data(connection: Connection, data: bytes) bytesΒΆ
Raw TLS/SSL handshake is not sent through this method. Everything should be decrypted beforehand.
- Note:
This method must return back data. Modified or not.
- async static server_data(connection: Connection, data: bytes) bytesΒΆ
Called when the
mitmreceives data from the destination server. Data that comes through this hook can be modified and returned to themitmas new data to be sent to the client.- Note:
This method must return back data. Modified or not.
- async static client_disconnected(connection: Connection)ΒΆ
Called when the client disconnects.
- async static server_disconnected(connection: Connection)ΒΆ
Called when the server disconnects.
- class mitm.core.ProtocolΒΆ
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.- bytes_neededΒΆ
Specifies how many bytes are 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.
Note that the attributes above must be set per-protocol basis.
- async resolve(connection: Connection, data: bytes) Tuple[str, int, bool]ΒΆ
Resolves the destination of the connection. Returns a tuple containing the host, port, and bool that indicates if the connection is encrypted.
- async connect(connection: Connection, host: str, port: int, data: bytes)ΒΆ
Attempts to connect to destination server using the given data. Returns
Trueif the connection was successful, raisesInvalidProtocolif the connection failed.
- async handle(connection: Connection)ΒΆ
Handles the connection between a client and a server.