【问题标题】:get a "raw" request\response from MITM Proxy从 MITM 代理获取“原始”请求\响应
【发布时间】:2014-02-24 17:06:37
【问题描述】:

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

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

有人知道更好的方法吗?我正在使用:

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

要访问被拦截的请求或响应,我没有更改任何内容,只是观察。 目前代理在 8080 上,稍后将成为 80 和 443 上的透明代理。 如果有人在我之前做过,如果你能分享一些信息,我会很高兴。

【问题讨论】:

    标签: http python-2.7 httprequest httpresponse mitmproxy


    【解决方案1】:

    对于那些想要将请求/响应数据复制到剪贴板而最终在这里的人:

    ## 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

    【讨论】:

      【解决方案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 命名。这几乎解决了我的问题。 对提琴手的一些检查表明该请求稍后是可重现的,这正是我所需要的。

      【讨论】:

      • 感谢 codeSciber!我注意到 req_assemble() 的输出在请求的第一行中不包含所请求资源的 fullURL,它是相对路径。我该如何解决?
      • 自己打印主机变量。一旦在办公室,我可以复制粘贴我用来执行此操作的行,您可以像上面那样在您自己的脚本中执行此操作,或者使用您自己的类更改他们的代码。
      • 嗨。我是 mitmproxy 模块的完整新手。需要stackoverflow.com/questions/32195882/… 的帮助。有人可以指导我正确的方向吗?同样在@codeScriber 的上述答案中,我从哪里调用 request() ?那里的上下文和流参数是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多