一.原来的基础上添加代码

"""
This inline script allows conditional TLS Interception based
on a user-defined strategy.
Example:
> mitmdump -s tls_passthrough.py
1. curl --proxy http://localhost:8080 https://example.com --insecure
// works - we'll also see the contents in mitmproxy
2. curl --proxy http://localhost:8080 https://example.com --insecure
// still works - we'll also see the contents in mitmproxy
3. curl --proxy http://localhost:8080 https://example.com
// fails with a certificate error, which we will also see in mitmproxy
4. curl --proxy http://localhost:8080 https://example.com
// works again, but mitmproxy does not intercept and we do *not* see the contents
Authors: Maximilian Hils, Matthew Tuusberg
"""
import collections
import random from enum import Enum import mitmproxy
from mitmproxy import ctx
from mitmproxy.exceptions import TlsProtocolException
from mitmproxy.proxy.protocol import TlsLayer, RawTCPLayer class InterceptionResult(Enum):
success = True
failure = False
skipped = None class _TlsStrategy:
"""
Abstract base class for interception strategies.
""" def __init__(self):
# A server_address -> interception results mapping
self.history = collections.defaultdict(lambda: collections.deque(maxlen=200)) def should_intercept(self, server_address):
"""
Returns:
True, if we should attempt to intercept the connection.
False, if we want to employ pass-through instead.
"""
raise NotImplementedError() def record_success(self, server_address):
self.history[server_address].append(InterceptionResult.success) def record_failure(self, server_address):
self.history[server_address].append(InterceptionResult.failure) def record_skipped(self, server_address):
self.history[server_address].append(InterceptionResult.skipped) class ConservativeStrategy(_TlsStrategy):
"""
Conservative Interception Strategy - only intercept if there haven't been any failed attempts
in the history.
""" def should_intercept(self, server_address):
if InterceptionResult.failure in self.history[server_address]:
return False
return True class ProbabilisticStrategy(_TlsStrategy):
"""
Fixed probability that we intercept a given connection.
""" def __init__(self, p):
self.p = p
super(ProbabilisticStrategy, self).__init__() def should_intercept(self, server_address):
return random.uniform(0, 1) < self.p class TlsFeedback(TlsLayer):
"""
Monkey-patch _establish_tls_with_client to get feedback if TLS could be established
successfully on the client connection (which may fail due to cert pinning).
""" def _establish_tls_with_client(self):
server_address = self.server_conn.address try:
super(TlsFeedback, self)._establish_tls_with_client()
except TlsProtocolException as e:
tls_strategy.record_failure(server_address)
raise e
else:
tls_strategy.record_success(server_address) # inline script hooks below. tls_strategy = None def load(l):
l.add_option(
"tlsstrat", int, 0, "TLS passthrough strategy (0-100)",
) def configure(updated):
global tls_strategy
if ctx.options.tlsstrat > 0:
tls_strategy = ProbabilisticStrategy(float(ctx.options.tlsstrat) / 100.0)
else:
tls_strategy = ConservativeStrategy() def next_layer(next_layer):
"""
This hook does the actual magic - if the next layer is planned to be a TLS layer,
we check if we want to enter pass-through mode instead.
"""
if isinstance(next_layer, TlsLayer) and next_layer._client_tls:
server_address = next_layer.server_conn.address if tls_strategy.should_intercept(server_address):
# We try to intercept.
# Monkey-Patch the layer to get feedback from the TLSLayer if interception worked.
next_layer.__class__ = TlsFeedback
else:
# We don't intercept - reply with a pass-through layer and add a "skipped" entry.
mitmproxy.ctx.log("TLS passthrough for %s" % repr(next_layer.server_conn.address), "info")
next_layer_replacement = RawTCPLayer(next_layer.ctx, ignore=True)
next_layer.reply.send(next_layer_replacement)
tls_strategy.record_skipped(server_address)

最新文章

  1. Lua 学习笔记(四)语句与控制结构
  2. Day Tips:Remote 服务器黑屏
  3. Shell 变量
  4. function foo(){}、(function(){})、(function(){}())等函数区别分析
  5. UCOS-信号标志组(学习笔记)
  6. 新型信用卡MasterPass
  7. JS实现常用的分享到按钮
  8. CSS的box-sizing属性
  9. 【省带宽、压成本专题】从产品架构来看,PCDN如何节流50%
  10. Confluence 6 选择一个外部数据库
  11. 大牛推荐的30本经典编程书籍,从Python到前端全系列。
  12. Visio画流程图风格设置
  13. 进程理论 阻塞非阻塞 同步异步 I/O操作
  14. 一次jenkins的错误
  15. eclipse连接VisualSVN Server
  16. 关于Maven配置的一些标签含义(后续逐渐补充)
  17. Ubuntu 16.04 安装搜狗输入法
  18. 当今最流行的Web项目管理工具精选
  19. [Python 多线程] threading.local类 (六)
  20. mongodb3.4 远程连接认证失败

热门文章

  1. 2.Git知识
  2. scrapy 开发流程
  3. VNC怎么和宿主机共享粘贴板
  4. 十 Restful风格
  5. #写一个登陆的程序 ( 1.最多登录失败3次 2.登陆成功,提示欢迎XX登录,今天的日期是XXX,程序结束 3.要检验输入是否为空,账户和密码不能为空 4.账户不区分大小写)
  6. mybatis官方中文文档
  7. 谁说5G网络无敌?第六代Wi-Fi表示不服
  8. linux环境下查看tomcat日志
  9. 「JSOI2008」Blue Mary的旅行
  10. 【Unity】关于屏幕自适应的思路