【问题标题】:How to make a request inside a simple mitmproxy script?如何在简单的 mitmproxy 脚本中发出请求?
【发布时间】:2020-04-01 02:58:44
【问题描述】:

早安,

我目前正试图找出一种在 mitmproxy 的简单脚本中发出非阻塞请求的方法,但文档对我来说似乎并不清楚。

如果我显示我当前的代码并在下面描述我的问题,我认为这可能是最简单的:

from copy import copy
from mitmproxy import http

def request(flow: http.HTTPFlow):
    headers = copy(flow.request.headers)
    headers.update({"Authorization": "<removed>", "Requested-URI": flow.request.pretty_url})
    req = http.HTTPRequest(
        first_line_format="origin_form",
        scheme=flow.request.scheme,
        port=443,
        path="/",
        http_version=flow.request.http_version,
        content=flow.request.content,
        host="my.api.xyz",
        headers=headers,
        method=flow.request.method
    )
    print(req.get_text())
    flow.response = http.HTTPResponse.make(
        200, req.content,
    )

基本上,我想拦截任何已完成的 HTTP(S) 请求,并向https://my.api.xyz/ 的 API 端点发出非阻塞请求,该端点应采用所有原始标头并返回原始请求 URL 的 png 屏幕截图。

但是上面的代码产生了一个空的内容并且打印也没有返回任何内容。

我的问题似乎与:mtmproxy http get request in scriptResubmitting a request from a response in mitmproxy 有关,但我仍然无法找到在 mitmproxy 中发送请求的正确方法。

【问题讨论】:

    标签: python networking scripting mitmproxy


    【解决方案1】:

    以下代码可能符合您的要求:

    from copy import copy
    from mitmproxy import http
    from mitmproxy import ctx
    from mitmproxy.addons import clientplayback
    
    
    def request(flow: http.HTTPFlow):
        ctx.log.info("Inside request")
    
        if hasattr(flow.request, 'is_custom'):
            return
    
        headers = copy(flow.request.headers)
        headers.update({"Authorization": "<removed>", "Requested-URI": flow.request.pretty_url})
    
        req = http.HTTPRequest(
            first_line_format="origin_form",
            scheme='http',
            port=8000,
            path="/",
            http_version=flow.request.http_version,
            content=flow.request.content,
            host="localhost",
            headers=headers,
            method=flow.request.method
        )
    
        req.is_custom = True
        playback = ctx.master.addons.get('clientplayback')
        f = flow.copy()
        f.request = req
        playback.start_replay([f])
    

    它使用clientplayback 插件来发送请求。当这个新请求被发送时,这将产生另一个request 事件,这将是一个无限循环。这就是我在请求中添加is_custom 属性的原因。如果生成此事件的请求是我们创建的请求,那么我们不想从它创建新请求。

    【讨论】:

      猜你喜欢
      • 2016-07-04
      • 2020-02-15
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      相关资源
      最近更新 更多