Skip to main content

Reverse engineering utilities for several popular network protocols

Project description

pynetkit

Reverse engineering utilities for several popular network protocols.

Introduction

pynetkit allows running custom servers, that listen and answer to several network protocols. The servers can be configured programmatically, which lets you easily add network handlers, change their configuration, etc. Additionally, the servers receive and broadcast events about what's happening, so that you can respond to the current state.

The unconventional style of the network servers is particularly useful for reverse engineering embedded/IoT devices and finding various kinds of vulnerabilities.

Implemented modules

Below are the modules currently implemented in the project.

  • base - Common utilities and base classes for all modules.
  • dhcp - DHCP server that can give out dynamic or static leases from a specific IP address range.
  • dns - DNS server that can answer queries based on a set of RegEx patterns, as well as forward queries to an upstream server.
  • http - HTTP/HTTPS server that can call request handlers based on various parameters of the request. SSL is supported by using certificates or PSK authentication.
  • mqtt - MQTT broker that will also listen to incoming messages and call message handlers based on their parameters.
  • network - Network interface configuration module, that can list network interfaces, read and change their IP configuration and ping hosts.
  • proxy - TCP/TLS/HTTP proxy that can redirect traffic to a different IP address/port based on the requested host name (TLS SNI or HTTP Host header).
  • wifi - Wi-Fi configuration module, that can scan for Wi-Fi networks, connect to a network and create an access point (SoftAP).

The network and wifi modules are currently Windows-only - the Linux implementation is not written yet.

A brief on modules

All pynetkit modules follow the same pattern - they have their own thread (or several threads) that can be started or stopped using AsyncIO method calls. In order to receive events, the caller class should inherit from ModuleBase.

ModuleBase has an async run() method, which is executed on the module's thread. All threads created by ModuleBase start their work in entrypoint(); it's not recommended to override this function. However, if unusual configuration is needed before starting the thread (such as starting a few of them), the async start() method can be overridden.

Example

An example class that starts an HTTP server and redirect all DNS queries to it:

import asyncio
import logging
from ipaddress import IPv4Address
from logging import DEBUG

import pynetkit.modules.http as httpm
from pynetkit.modules.base import BaseEvent, ModuleBase, subscribe
from pynetkit.modules.dns import DnsModule
from pynetkit.modules.http import HttpModule, Request, Response


class Example(ModuleBase):
    dns: DnsModule
    http: HttpModule

    def __init__(self):
        super().__init__()
        self.dns = DnsModule()
        self.http = HttpModule()

    async def run(self) -> None:
        self.register_subscribers()
        await self.event_loop_thread_start()

        self.dns.add_record(".*", "A", IPv4Address("0.0.0.0"))
        await self.dns.start()

        self.http.configure(
            address=IPv4Address("0.0.0.0"),
            http=80,
            https=0,
        )
        self.http.add_handlers(self)
        await self.http.start()

        while True:
            await asyncio.sleep(1.0)

    async def cleanup(self) -> None:
        await super().cleanup()
        await self.http.stop()
        await self.dns.stop()

    @subscribe(BaseEvent)
    async def on_event(self, event) -> None:
        self.info(f"EVENT: {event}")

    @httpm.get("/hello")
    async def on_hello(self, request: Request) -> Response:
        return {
            "Hello": "World",
            "Headers": request.headers,
        }

    @httpm.get("/.*")
    async def on_http(self, request: Request) -> Response:
        return {
            "Error": "Not Found",
            "Path": request.path,
        }


def main():
    logger = logging.getLogger()
    logger.level = DEBUG
    example = Example()
    example.entrypoint()


if __name__ == "__main__":
    main()

License

MIT License

Copyright (c) 2024 Kuba Szczodrzyński

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynetkit-1.0.0.tar.gz (66.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynetkit-1.0.0-py3-none-any.whl (99.9 kB view details)

Uploaded Python 3

File details

Details for the file pynetkit-1.0.0.tar.gz.

File metadata

  • Download URL: pynetkit-1.0.0.tar.gz
  • Upload date:
  • Size: 66.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.16 Linux/6.8.0-1021-azure

File hashes

Hashes for pynetkit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3b45814f2e6253e0e781f5997ea78e27a3785c27eab18f63e09bdbff8a51aea0
MD5 849d03d8d68d9be4f3a634bde8cbb38d
BLAKE2b-256 abf6903ac0b681db573d3308c1ac912e6a3dbe1457906c567df3af90501b806c

See more details on using hashes here.

File details

Details for the file pynetkit-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pynetkit-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 99.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.16 Linux/6.8.0-1021-azure

File hashes

Hashes for pynetkit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5d5742b72cee9677b0781d55b47a9c98c4e3e38cde49f447a1c70d6411f7f73
MD5 4484f28569879712ace126f8cbc30106
BLAKE2b-256 aa08a610aa260b6f8f095b25aef0287af4c94ea9ef7218e9419cfc1ada8bde28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page