Skip to content

manmolecular/py-request-interceptor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

py-request-interceptor

Python3 Version License Code Style

Description

🐀 Intercepts your HTTP(S) requests from Python standard libraries. Allows you to catch API calls and HTTP(S) requests from different requests/urllib/http-based libraries (and many others based on the original http.client/socket stack).

Warning

🚧 Note: This is just a WIP/PoC project w/o rocket science in it - just a bunch of dirty monkey patches. So, enjoy it "as is" if you are interested in it. Not properly tested lol.

Proof of Concept

#!/usr/bin/env python3

from src.interceptor import Interceptor
from requests import get

# Initialize 'Interceptor' here, nothing special
intercept = Interceptor()

@intercept.sniff(listener="http://YOUR_LISTENER_ENDPOINT")
@intercept.dump()
@intercept.data(data="GET / HTTP/1.1\r\n ...modified request goes here")
def example_sniff_connect() -> CaseInsensitiveDict:
    """
    Patch request data, dump modified request to the logs, and re-send copy of the request
    to another endpoint
    :return: response headers
    """
    return get(url="http://ORIGINAL_HOST").headers


if __name__ == "__main__":
    example_sniff_connect()

Handlers

Redirect to another target

Definition:

def target(self, host: str, port: int) -> callable:

Example:

intercept = Interceptor()

@intercept.target(host="http://evil.com", port=80)
def my_func():
    ...

Replace raw requests data

Definition:

def data(self, data: Union[str, bytes]) -> callable:

Example:

intercept = Interceptor()

@intercept.data(data="GET / HTTP/1.1\r\n...")
def my_func():
    ...

Log raw requests to the console

Definition:

def dump(self) -> callable:

Example:

intercept = Interceptor()

@intercept.dump()
def my_func():
    ...

Re-send copy of the original raw requests to another endpoint

Definition:

def sniff(self, listener: str) -> callable:

Example:

intercept = Interceptor()

@intercept.sniff(listener="http://requestbin.net/r/YOUR_REQUESTBIN_ID")
def my_func():
    ...

About

🐀 Intercepts your HTTP(S) requests from Python standard library. Allows to redirect, sniff, and modify requests transparently.

Topics

Resources

License

Stars

Watchers

Forks

Languages