Search code examples
python-3.xmitmproxy

mimtproxy running python script: "XX Module not imported"


So im running mitmproxy for windows and im trying to run a script that saves responses and requests into postgresql, for this im using sqlalchemy

But i cannot make it work with mimtproxy for some reason, when running seems like its using another python interpreter and my code is not working. Does mitmproxy use a different interpreter appart from the one you have installed?

Command running from mimtmproxy/bin folder:

mitmdump.exe -s C:\users\etc\{FULL_PATH}\mitmproxy.py

im getting

"No module instaled named SQLAlchemy"

i already tried to installing via pip and pip3 the module is telling me im missing(sqlalchemy) but its already installed

enter image description here

mimtproxy.py

from mitmproxy import http
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

from entities.models.Request import RequestModel
from entities.models.Response import ResponseModel
from entities.models.Session import SessionModel

server = 'www.XXX.com'
world = 'XXX'
user = 'XXX'
version = None

engine = create_engine('postgresql://XXX:XXX@localhost:5432/XXX')
Base = declarative_base()


def createSession():

    with Session(engine) as session:
        http_session = SessionModel(server=server,world=world,version=version,user=user)
        session.add(http_session)
        # We add created object to our DB
        session.flush()
        # At this point, the object  has been pushed to the DB,
        # and has been automatically assigned a unique primary key id
        session.refresh(http_session)
        # refresh updates given object in the session with its state in the DB
        # (and can also only refresh certain attributes - search for documentation)
        return  http_session



session_object = createSession()
with Session(engine) as session:
    session.add(session_object)
    session.commit()


def request(flow: http.HTTPFlow) -> None:

    if flow.request.headers['x-ig-client-version'] and session_object.version == None:
        session_object.version = flow.request.headers['x-ig-client-version']
        with Session(engine) as session:
            session.commit()

    request_url = flow.request.url
    request_cookies = None
    if flow.request.cookies:
        request_cookies = flow.request.cookies

    Request = RequestModel(method=flow.request.method,url=request_url)
    Request.headers = flow.request.headers
    Request.cookies = request_cookies
    Request.body = flow.request.content
    Request.timestamp_start = flow.request.timestamp_start
    Request.timestamp_end = flow.request.timestamp_end
    Request.size = len(flow.request.content)

    Response = ResponseModel(headers=flow.response.headers,
                             status_code=flow.response.status_code,body=flow.response.content)
    Response.cookies = None
    if flow.response.cookies:
        Response.cookies = flow.response.cookies

    Request.response = Response
    session_object.requests.append([Request])
    with Session(engine) as session:
        session.commit()

all sqlalchemy models are here: AttributeError: 'set' object has no attribute '_sa_instance_state' - SQLAlchemy


Solution

  • If you want to use Python packages that are not included in mitmproxy's own installation, you need to install mitmproxy via pip or pipx. The normal binaries include their own Python environment.

    Source: https://docs.mitmproxy.org/stable/overview-installation/#installation-from-the-python-package-index-pypi.