Search code examples
mitmproxy

Is it possible to use captured group in replacement when setting modify-body option for mitmproxy?


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.


Solution

  • 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.