이번엔 Python Addon을 통해 Response 데이터 보는 방법과 Response 데이터 수정하는 방법을 알아보자
$ mitmproxy -s {파이썬모듈경로/test.py} --set block_global=false
# test.py
from mitmproxy import ctx
class TestAddon:
def __init__(self):
pass
def response(self, flow):
if "myrealtrip" in flow.request.pretty_url :
ctx.log.info(flow.response)
# 결과 : info: Response(200, application/json;charset=UTF-8, 1.35k)
def response(self, flow):
""" 압축되지 않고 디코딩된 HTTP 메시지 본문을 텍스트로 보기 """
if "myrealtrip" in flow.request.pretty_url :
ctx.log.info(flow.response.get_text())
# 결과 : {"data":[{"code":"RENTCAR_LIST_BANNER","banners":[{"id":857,"imageUrl":"https://d2ur7st6jjikze.cloudfront.net/cms/1729_original_1643098924.jpg?1643098924","mobileImageUrl":"https://d2ur7st6jjikze.cloudfront.net/cms /1730_original_1643098925.jpg?1643098925","webImageUrl":"https://d2ur7st6jjikze.cloudfront.net/cms/1729_original_1643098924.jpg?1643098924","link":"https://www.myrealtrip.com/promotions/rentacar_coupon_main","linkForWeb" :"https://www.myrealtrip.com/promotions/rentacar_coupon_main","linkForMobileWeb":"https://www.myrealtrip.com/promotions/rentacar_coupon_main","linkForIos":"mrt://web?url=https%3A%2F%2Fwww.myrealtrip.com%2Finstants%2Fprom otions%2Frentacar_coupon_main","linkForAndroid":"mrt://web?url=https%3A%2F%2Fwww.myrealtrip.com%2Finstants%2Fpromotions%2Frentacar_coupon_main"}]}],"meta":{},"result":{"status":200,"message":"SUCCESS","code":"success"}}
def response(self, flow):
""" 압축되지 않고 디코딩된 HTTP 메시지 본문을 바이트로 보기 """
if "myrealtrip" in flow.request.pretty_url :
ctx.log.info(flow.response.get_content())
# 결과 : b'{"data":{"available":true,"salePrice":50400,"basePrice":201600,"stock":{"amount":2,"text":"2\xeb\x8c\x80\xeb\x82\xa8\xec\x95\x98\xec\x96\xb4\xec\x9a\x94.","highlight":true}}
def response(self, flow):
""" Response Json 객체 수정하기 """
if "sampleURL" in flow.request.url:
data = json.loads(flow.response.get_text()) # get_text로 Json 객체 로드
data["data"]["vid"] = 9999 # json 객체 내 value 수정
flow.response.text = json.dumps(data) # 수정한 데이터 리턴
# 결과 : {"data": {"vid": 9999}}