从MITM代理获得“原始”请求\响应

时间:2014-01-31 20:39:34

标签: http python-2.7 httprequest httpresponse mitmproxy

i',脚本mitm代理(http://mitmproxy.org/index.html)根据IP(每个客户端可以访问它自己的请求\响应)来写入HTTP和HTTPS请求以及对文件的响应,以进行移动设备的单元测试。 / p>

据我现在所知,我不能只使用str(Flow.request)或repr(Flow.request)获取响应\请求的“原始”打印,就像我进入提琴手一样,我需要从Request和Response对象的内部数据重建它。

谁知道更好的方法?我正在使用:

def response(ScriptContext, Flow):
    Flow.request....
    Flow.response....

要访问被拦截的请求或响应,我不会改变任何东西,只是观察。 现在代理是在8080上,稍后是80和443上的透明代理。 如果你能分享一些信息,如果有人在我开心之前就已经这样做了。

2 个答案:

答案 0 :(得分:4)

对于那些希望将rquest / response数据复制到剪贴板而在这里结束的人:

## export the current request/response as curl/httpie/raw/request/response to clipboard
# press colon : and input one of commands and enter
export.clip curl @focus
export.clip httpie @focus
export.clip raw @focus
export.clip raw_request @focus
export.clip raw_response @focus

Mitmproxy:5.0.1

Source code

答案 1 :(得分:2)

几件事。 首先,你可以使用str(flow.request.headers)和request.httpversion等自己构建原始响应。 但似乎_assemble()和_assemble_headers()似乎没问题。

基本上是这样的:

def request(context, flow):
req = flow.request;
try:
    print("Request: -----------------");
    print(req._assemble());
    print("--------------------------");
except Exception as ee:
    print(str(ee));

def response(context, flow):
    res = flow.response;
    try:
        print("Response: -----------------");
    print(res._assemble());

    if res.content:
        size = len(res.content);
        size  = min(size, 20);
        if res.content[0:size] != res.get_decoded_content()[0:size]:
            print("\n\n");
            print(res.get_decoded_content());
    print("--------------------------");
except Exception as ee:
    print(str(ee));

你可以看到解码后的主体是否与非解码主体不相似(我可以检查gzip内容类型)我也在打印解码后的消息。 这应该根据当前日期保存到文件中,并且每个文件都是在从request \ response.client_conn对象获取的客户端ip之后命名的。这几乎解决了我的问题。 对fiddler的一些检查表明,请求可以在以后重现,这正是我所需要的。

相关问题