Mitmproxy 在一个脚本中篡改 GET 和 POST 请求/响应

2023-12-01

发送到特定 URL 的 POST 请求(http://test.com)看起来像这样:

{
    "messageType": "OK",
    "city": {
        "Name": "Paris",
        "Views": {
            "1231": {
                "id": 4234,
                 "enableView": false
            },
        },
        "Views": [5447, 8457],
        "messages": [{
            "id": "message_6443",
            "eTag": 756754338
        }]
    },
    "client": {
        "Id": 53,
        "email": "[email protected]",
        "firstName": "test",
        "lastName": "test",
        "id": 52352352,
        "uuid": "5631f-grdeh4",
        "isAdmin": false
    }
}

我需要拦截请求并更改isAdmin为真。

以及对某个 URL 的 GET 请求(https://test.com/profiles/{Random_Numbers}/{id})有一个(编码的)响应,如下所示:

{
    "id": 0, 
    "Code": "Admin", 
    "display": "RRRR"
}

我需要改变id to 5.

所以基本上我需要编写一个脚本来执行这两项操作。

到目前为止,我已经尝试利用 GitHub 上的一些示例,但到目前为止我还没有得到它:

from libmproxy.protocol.http import decoded

def start(context, argv):
  if len(argv) != 3:
   raise ValueError('Usage: -s "modify-response-body.py old new"')
    context.old, context.new = argv[1], argv[2]


def response(context, flow):
    with decoded(flow.response):  # automatically decode gzipped responses.
      flow.response.content = flow.response.content.replace(context.old, context.new)`

我如何在我的场景中实现这一点?

也许使用 libmproxy 来获取 http 请求和响应会是一个更好的主意。


您发布的脚本和 Python 的 JSON 模块应该能让您走得很远:

def response(context, flow):
    if flow.request.url == "...": # optionally filter based on some criteria...
        with decoded(flow.response):  # automatically decode gzipped responses.
            data = json.loads(flow.response.content)
            data["foo"] = "bar"
            flow.response.content = json.dumps(data)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mitmproxy 在一个脚本中篡改 GET 和 POST 请求/响应 的相关文章

随机推荐