Python Addon을 통해 Request 데이터 보는 방법과 Request 데이터 수정하는 방법을 알아보자
mitmproxy -s {파이썬모듈경로/test.py} --set block_global=false
# test.py
from mitmproxy import ctx
class TestAddon:
def __init__(self):
pass
def request(self, flow):
""" Host, User-Agent, Content-type을 포함한 모든 헤더 정보 얻기 """
flow.request.headers
ctx.log.info(flow.request.headers)
# 결과 : Headers[(b'cache-control', b'max-age=0'), (b'sec-ch-ua', b'" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"'), (b'sec-ch-ua-mobile', b'?0'), (b'sec-ch-ua-platform', b'"macOS"')
def request(self, flow):
""" 쿼리파라미터를 포함한 Request URL 얻기 """
flow.request.url
ctx.log.info(flow.request.url) # 로그 출력
# 결과 : https://www.myrealtrip.com/lodgings/products?startDate=2022-03-21&endDate=2022-03-22&location[key]=Jeju&location[name]=%EC%A0%9C%EC%A3%BC%EB%8F%84&adults=2&children=0
def request(self, flow):
""" 도메인 네임 얻기 """
flow.request.host
ctx.log.info(flow.request.host)
# 결과 : www.myrealtrip.com
def request(self, flow):
""" Request 스킴 얻기(http/s) """
flow.request.scheme
ctx.log.info(flow.request.scheme)
# 결과 : https
def request(self, flow):
""" Request path 얻기 """
flow.request.path
ctx.log.info(flow.request.path)
# 결과 : /lodgings/products?startDate=2022-03-21&endDate=2022-03-22&location[key]=Jeju&location[name]=%EC%A0%9C%EC%A3%BC%EB%8F%84&adults=2&children=0
def request(self, flow):
""" Request Body(Payload) 얻기 """
flow.request.get_text()
ctx.log.info(flow.request.get_text())
# 결과 : {"productType":"ACCOMMODATION","gids":["1005595"],"sort":"-recommendScore","page":1,"pageSize":10,"imaged":null,"score":null,"travelPurposes":[]}
def request(self, flow):
""" Request Body(Payload) byte type으로 얻기 """
flow.request.get_content() # flow.request.get_text()과 결과는 동일
ctx.log.info(flow.request.get_content())
# 결과 : b'{"productType":"ACCOMMODATION","gids":["1005595"],"sort":"-recommendScore","page":1,"pageSize":10,"imaged":null,"score":null,"travelPurposes":[]}'
def request(self, flow):
""" 쿼리파라미터 얻기 (MultiDict type)"""
flow.request.query
ctx.log.info(flow.request.query)
# 결과 : [('startDate', '2022-03-21'), ('endDate', '2022-03-22'), ('location[key]', 'Jeju'), ('location[name]', '제주도'), ('adults', '2'), ('children', '0')]
def request(self, flow):
""" 쿼리파라미터 Key로 Value 얻기 """
flow.request.query["startDate"]
# 결과 : '2022-03-21'
def request(self, flow):
""" 쿼리파라미터 수정하기 """
flow.request.query.set_all("startDate", ["2022-03-22"])
flow.request.query.set_all("endDate", ["2022-03-25"])
def request(self, flow):
""" Request Host 수정하기 """
if flow.request.host == "www.myrealtrip.com":
flow.request.host = "www.naver.com"