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

Ric*_*rd3 3 python regex proxy inline-scripting mitmproxy

对某个网址(http://test.com)的POST请求如下:

{

"messageType": "OK",
"city": {
    "Name": "Paris",
    "Views": {
        "1231": {
            "id": 4234,
             "enableView": false
        },
    },
    "Views": [5447, 8457],
    "messages": [{
        "id": "message_6443",
        "eTag": 756754338
    }]
},
"client": {
    "Id": 53,
    "email": "test@test.us",
    "firstName": "test",
    "lastName": "test",
    "id": 52352352,
    "uuid": "5631f-grdeh4",
    "isAdmin": false,
Run Code Online (Sandbox Code Playgroud)

我需要拦截它并将"isAdmin"更改为true.

对某个网址(https://test.com/profiles/ {Random_Numbers}/id})的GET请求有一个'响应'[decode gzip] JSON

{
"id": 0, 
"Code": "Admin", 
"display": "RRRR"
}
Run Code Online (Sandbox Code Playgroud)

我需要将"id"值更改为5.

所以基本上我需要编写一个可以完成这两个的脚本.

到目前为止,我已经尝试在Github中获取示例代码的帮助,但我没有预期的结果.(我是一个完整的菜鸟:\)并希望有人可以帮助我开始.提前致谢!

编辑:根据Github中的示例代码,modify_response_body.py:

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)`
Run Code Online (Sandbox Code Playgroud)

我如何为我的senario实现这个?

可能使用libmproxy获取http请求和响应可能是一个更好的主意.

Max*_*ils 7

您发布的脚本和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)
Run Code Online (Sandbox Code Playgroud)