Python3自定义http/https请求拦截mitmproxy脚本实例

 更新时间:2020年05月11日 09:11:52   作者:天外归云  
这篇文章主要介绍了Python3自定义http/https请求拦截mitmproxy脚本实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

脚本内容

代码如下:

from mitmproxy import http, ctx
from multiprocessing import Lock


class Filter:
  def __init__(self, filter_info):
    self.log_info = ""
    self.mutex = Lock()
    self.filter_info = filter_info
    self.response_file = None
    self.switch_on = False
    self.log_file = "log.txt"

  def log(self, info) -> None:
    self.log_info += f"{info}\n\n"

  def write_log(self, mode="w+") -> None:
    self.mutex.acquire()
    with open(self.log_file, mode) as f:
      f.write(self.log_info)
    self.mutex.release()

  def is_target_flow(self, flow: http.HTTPFlow) -> bool:
    for info in self.filter_info:
      if info["str_in_url"] in flow.request.url:
        self.log_file = info["log_file"]
        self.switch_on = info["switch_on"]
        if info["response_file"] != None:
          self.response_file = info["response_file"]
        return True
    else:
      return False

  def modify_response(self, flow: http.HTTPFlow) -> http.HTTPFlow:
    if self.switch_on and self.response_file:
      with open(self.response_file, "r") as f:
        flow.response.content = f.read().encode()
    return flow

  def request(self, flow: http.HTTPFlow) -> None:
    if self.is_target_flow(flow):
      self.log_info = ""
      self.log(f"——METHOD——\n{flow.request.method}")
      self.log(f"——HOST——\n{flow.request.pretty_host}")
      self.log(f"——URL——\n{flow.request.pretty_url}")
      query = [i + ":" + flow.request.query[i] + "\n" for i in flow.request.query]
      self.log(f"——QUERY STRING——\n{''.join(query)}")
      if flow.request.urlencoded_form:
        form = [i + ":" + flow.request.urlencoded_form[i] + "\n" for i in flow.request.urlencoded_form]
        self.log(f"——FORM——\n{''.join(form)}")
      self.write_log()

  def response(self, flow: http.HTTPFlow) -> None:
    if self.is_target_flow(flow):
      self.log_info = ""
      self.log(f"——RESPONSE before modified——\n{flow.response.content.decode()}")
      flow = self.modify_response(flow)
      self.log(f"——RESPONSE after modified——\n{flow.response.content.decode()}")
      self.write_log(mode="a")


filter_info = [
  {
    "str_in_url": "getSimpleNews",
    "log_file": "getSimpleNews_log.txt",
    "switch_on": True,
    "response_file": "getSimpleNews_response.txt",
  },
  {
    "str_in_url": "getQQNewsComment",
    "log_file": "getQQNewsComment_log.txt",
    "switch_on": True,
    "response_file": None,
  }
]
addons = [
  Filter(filter_info)
]

使用方法

运行mitmproxy指定使用该脚本和端口号即可:

mitmproxy -p 6666 -s xxx.py

在mitmproxy运行时:

1. 会拦截url中包含str_in_url字符串的请求

2. 会把response.content修改为当前mitm运行所在目录下的response_file文件中的内容

3. 打印信息在当前mitm运行所在目录下的log_file文件中

4. 如果无需修改response设置switch_on为False即为开关关闭

5. 如果不修改response的话response_file需要写None

补充知识:mitmproxy 监听指定端口

安装

使用python3的安装方式

https://mitmproxy.org/

监听指定端口

例子:Presto SQL请求的监听

Presto地址:http://datacenter4:18080

mitmproxy命令(端口8484)
mitmproxy \
--mode reverse:http://datacenter4:18080 \
--listen-host datacenter4 \
--listen-port 8484 \
--replacements :~s:\/\/datacenter4/:\/\/datacenter4:18080/

然后JDBC访问Presto使用:jdbc:presto://datacenter4:8484

效果

以上这篇Python3自定义http/https请求拦截mitmproxy脚本实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python调用百度语音识别api

    python调用百度语音识别api

    这篇文章主要介绍了python调用百度语音识别api,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • python正则表达式之作业计算器

    python正则表达式之作业计算器

    这篇文章主要为大家详细介绍了python正则表达式之作业计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们
    2016-03-03
  • Python Process多进程实现过程

    Python Process多进程实现过程

    这篇文章主要介绍了Python Process多进程实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • python中封包建立过程实例

    python中封包建立过程实例

    在本篇文章里小编给大家分享的是一篇关于python中封包建立过程实例内容,有兴趣的朋友们可以学习参考下。
    2021-02-02
  • Python分割单词和转换命名法的实现

    Python分割单词和转换命名法的实现

    本文主要介绍了Python分割单词和转换命名法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Python数组并集交集补集代码实例

    Python数组并集交集补集代码实例

    这篇文章主要介绍了Python数组并集交集补集代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • flask 实现上传图片并缩放作为头像的例子

    flask 实现上传图片并缩放作为头像的例子

    今天小编就为大家分享一篇flask 实现上传图片并缩放作为头像的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • 基于Python实现的恋爱对话小程序详解

    基于Python实现的恋爱对话小程序详解

    这篇文章主要介绍了基于Python制作一个恋爱对话小程序,文章详细介绍了小程序的实现过程,感兴趣的小伙伴可以跟随小编一起学习学习
    2022-01-01
  • python使用minimize() 函数替代matlab的fmincon函数

    python使用minimize() 函数替代matlab的fmincon函数

    这篇文章主要介绍了python使用minimize()函数替代matlab的fmincon函数,在matlab中,fmincon函数可以用于求解带约束的非线性多变量函数的最小值,即可以用来求解非线性规划问题
    2022-09-09
  • 简单谈谈Python中的几种常见的数据类型

    简单谈谈Python中的几种常见的数据类型

    Python 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。在 Python 中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型。
    2017-02-02

最新评论