0

I want to modify the response body, a json string to append extra site information for current user. For example, the original body might look like below.

[
  {
    "provider_assignments":[
      {
        "user_uuid":"c2d18512-298d-4035-9068-123cabdd2cd4",
        "environment":{
          "uuid":"b4156425-1afd-40ef-a3af-00e2887f7c91",
          "name":"Flik_Test (DEV)"
        },
        "roles":[
          {
            "oid":"SITEMODE",
            "name":"Site Mode User"
          }
        ],
        "sites":[
          {
            "uuid":"c7e56fb2-d93e-4d61-9822-805643c0773e",
            "name":"Shanghai Office"
          }
        ]
      }
    ],
    ...
  }
]

I expect the body could be modified as below.

[
  {
    "provider_assignments":[
      {
        "user_uuid":"c2d18512-298d-4035-9068-123cabdd2cd4",
        "environment":{
          "uuid":"ffb520d4-09fb-45b5-a292-b31fcfdeb2e6",
          "name":"ACE-536-MDS-002"
        },
        "roles":[
          {
            "oid":"SITEMODE",
            "name":"Site Mode User"
          }
        ],
        "sites":[
          {
            "uuid":"e83ff461-756d-43e6-8129-210fa065be41",
            "name":"Cabrini Hospital"
          }
        ]
      },
      {
        "user_uuid":"c2d18512-298d-4035-9068-123cabdd2cd4",
        "environment":{
          "uuid":"b4156425-1afd-40ef-a3af-00e2887f7c91",
          "name":"Flik_Test (DEV)"
        },
        "roles":[
          {
            "oid":"SITEMODE",
            "name":"Site Mode User"
          }
        ],
        "sites":[
          {
            "uuid":"c7e56fb2-d93e-4d61-9822-805643c0773e",
            "name":"Shanghai Office"
          }
        ]
      }
    ],
    ...
  }
]

However, the user_uuid will be different when another user logs in and I try below regex when adding modify-body option. I have tries $1, \$1, \1 and \\1 but without luck none of them works.

--modify-body "/~d test\.com & ~bs provider_assignments/\"provider_assignments\":\[\{\"user_uuid\":\"([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})\"/\"provider_assignments\":[{\"user_uuid\":\"\1\",\"environment\":{\"uuid\":\"ffb520d4-09fb-45b5-a292-b31fcfdeb2e6\",\"name\":\"ACE-536-MDS-002\"},\"roles\":[{\"oid\":\"SITEMODE\",\"name\":\"Site Mode User\"}],\"sites\":[{\"uuid\":\"e83ff461-756d-43e6-8129-210fa065be41\",\"name\":\"Cabrini Hospital\"}]},{\"user_uuid\":\"\1\""

Please advice. Thanks a lot.

Flik Shen
  • 27
  • 1
  • 10

2 Answers2

0

Using the group reference syntax mentioned in python regex instantly replace groups, group reference should be like \g<1> then it solves my problem perfect.

Flik Shen
  • 27
  • 1
  • 10
0

For this specific use case, I would recommend you write your own addon that modifies the JSON content directly. Untested, but something like the following should work:

import json
from mitmproxy import http

def response(flow: http.HTTPFlow):
    data = flow.response.json()

    data["foo"] = "bar"  # modify your JSON data with custom Python code.

    flow.response.text = json.dumps(data)
Maximilian Hils
  • 6,309
  • 3
  • 27
  • 46