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

发布于 2021-01-29 16:18:01

对特定网址(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,

我需要截取并将“ isAdmin”更改为true。

对特定网址(https://test.com/profiles/
{Random_Numbers} / id})的GET请求具有“响应” [decoded gzip] JSON

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

我需要将“ 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)`

如何为我的senario实现此功能?

也许使用libmproxy获得http请求和响应可能是一个更好的主意。

关注者
0
被浏览
154
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您发布的脚本和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)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看