当前位置:   article > 正文

mitmdump抓包中遇到的一些问题总结_tls_passthrough.py

tls_passthrough.py

mitmdump是 mitmproxy 的命令行接口,利用它我们可以捕获手机app中的请求并对接 Python 脚本,用 Python 实现监听后的处理。安装方法可以参考相关链接,下面主要讲下遇到的问题及处理方法。


问题:Cannot establish TLS with client报错

在配置完电脑和手机端的证书并且手机代理设置好后,滑动手机捕获请求时cmd会一直闪动并且报Cannot establish TLS with client错误,如下图:

错误原因:mitmproxy缺少https的证书,继而导致mitmproxy解析ssl协议出错

解决办法: 把tls_passthrough.py中的代码整合进我们的监听脚本即可

下面是tls_passthrough.py的全部内容

  1. """
  2. This inline script allows conditional TLS Interception based
  3. on a user-defined strategy.
  4. Example:
  5. > mitmdump -s tls_passthrough.py
  6. 1. curl --proxy http://localhost:8080 https://example.com --insecure
  7. // works - we'll also see the contents in mitmproxy
  8. 2. curl --proxy http://localhost:8080 https://example.com --insecure
  9. // still works - we'll also see the contents in mitmproxy
  10. 3. curl --proxy http://localhost:8080 https://example.com
  11. // fails with a certificate error, which we will also see in mitmproxy
  12. 4. curl --proxy http://localhost:8080 https://example.com
  13. // works again, but mitmproxy does not intercept and we do *not* see the contents
  14. Authors: Maximilian Hils, Matthew Tuusberg
  15. """
  16. import collections
  17. import random
  18. from enum import Enum
  19. import mitmproxy
  20. from mitmproxy import ctx
  21. from mitmproxy.exceptions import TlsProtocolException
  22. from mitmproxy.proxy.protocol import TlsLayer, RawTCPLayer
  23. class InterceptionResult(Enum):
  24. success = True
  25. failure = False
  26. skipped = None
  27. class _TlsStrategy:
  28. """
  29. Abstract base class for interception strategies.
  30. """
  31. def __init__(self):
  32. # A server_address -> interception results mapping
  33. self.history = collections.defaultdict(lambda: collections.deque(maxlen=500))
  34. def should_intercept(self, server_address):
  35. """
  36. Returns:
  37. True, if we should attempt to intercept the connection.
  38. False, if we want to employ pass-through instead.
  39. """
  40. raise NotImplementedError()
  41. def record_success(self, server_address):
  42. self.history[server_address].append(InterceptionResult.success)
  43. def record_failure(self, server_address):
  44. self.history[server_address].append(InterceptionResult.failure)
  45. def record_skipped(self, server_address):
  46. self.history[server_address].append(InterceptionResult.skipped)
  47. class ConservativeStrategy(_TlsStrategy):
  48. """
  49. Conservative Interception Strategy - only intercept if there haven't been any failed attempts
  50. in the history.
  51. """
  52. def should_intercept(self, server_address):
  53. if InterceptionResult.failure in self.history[server_address]:
  54. return False
  55. return True
  56. class ProbabilisticStrategy(_TlsStrategy):
  57. """
  58. Fixed probability that we intercept a given connection.
  59. """
  60. def __init__(self, p):
  61. self.p = p
  62. super(ProbabilisticStrategy, self).__init__()
  63. def should_intercept(self, server_address):
  64. return random.uniform(0, 1) < self.p
  65. class TlsFeedback(TlsLayer):
  66. """
  67. Monkey-patch _establish_tls_with_client to get feedback if TLS could be established
  68. successfully on the client connection (which may fail due to cert pinning).
  69. """
  70. def _establish_tls_with_client(self):
  71. server_address = self.server_conn.address
  72. try:
  73. super(TlsFeedback, self)._establish_tls_with_client()
  74. except TlsProtocolException as e:
  75. tls_strategy.record_failure(server_address)
  76. raise e
  77. else:
  78. tls_strategy.record_success(server_address)
  79. # inline script hooks below.
  80. tls_strategy = None
  81. def load(l):
  82. l.add_option(
  83. "tlsstrat", int, 0, "TLS passthrough strategy (0-100)",
  84. )
  85. def configure(updated):
  86. global tls_strategy
  87. if ctx.options.tlsstrat > 0:
  88. tls_strategy = ProbabilisticStrategy(float(ctx.options.tlsstrat) / 100.0)
  89. else:
  90. tls_strategy = ConservativeStrategy()
  91. def next_layer(next_layer):
  92. """
  93. This hook does the actual magic - if the next layer is planned to be a TLS layer,
  94. we check if we want to enter pass-through mode instead.
  95. """
  96. if isinstance(next_layer, TlsLayer) and next_layer._client_tls:
  97. server_address = next_layer.server_conn.address
  98. if tls_strategy.should_intercept(server_address):
  99. # We try to intercept.
  100. # Monkey-Patch the layer to get feedback from the TLSLayer if interception worked.
  101. next_layer.__class__ = TlsFeedback
  102. else:
  103. # We don't intercept - reply with a pass-through layer and add a "skipped" entry.
  104. mitmproxy.ctx.log("TLS passthrough for %s" % repr(next_layer.server_conn.address), "info")
  105. next_layer_replacement = RawTCPLayer(next_layer.ctx, ignore=True)
  106. next_layer.reply.send(next_layer_replacement)
  107. tls_strategy.record_skipped(server_address)

新建保存上面的tls_passthrough.py代码文件后,使用以下在cmd中执行以下命令,整合进我们的监听脚本即可(这里记得将tls_passthrough.py文件保存到cmd的文件目录下)

mitmproxy -s tls_passthrough.py

相关链接

mitmproxy的安装:mitmproxy 的安装 | 静觅 (cuiqingcai.com)

Cannot establish TLS with client报错:mintproxy跳坑集锦 - SegmentFault 思否

mitmdump控制台使用mitmdump -s script.py命令会No such script的问题:mitmdump控制台如何打开/为何使用mitmdump -s script.py命令会No such script_零零Hua的博客-CSDN博客_mitmdump不是内部或外部命令

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/45243
推荐阅读
相关标签
  

闽ICP备14008679号