4

A POST request sent to a certain URL (http://test.com) looks like this:

{
    "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
    }
}

I need to intercept the request and change isAdmin to true.

And a GET request to a certain URL (https://test.com/profiles/{Random_Numbers}/{id}) has a (encoded) response like this:

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

I need to change id to 5.

So basically I need to write one script that will do both of these actions.

So far I have tried to take advantage of some examples on GitHub, but I haven't gotten it so far:

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)`

How do I implement this for my scenario?

Probably using the libmproxy to get http-request and response would be a better idea, maybe.

zcoop98
  • 2,590
  • 1
  • 18
  • 31
Richard3
  • 139
  • 1
  • 1
  • 7

1 Answers1

10

The script you posted and Python's JSON module should get you pretty far:

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)
Maximilian Hils
  • 6,309
  • 3
  • 27
  • 46
  • 1
    I used this solution but it does not work for me ... I can see the response function triggered in the event logs, but the if statement seems to never success ... Any idea ? – programmersn Aug 14 '18 at 12:21