主题

   修改request或者response内容

介绍

  mitmdump无交互界面的命令,与python脚本对接,来源于mitmproxy支持inline script,这里的script指的是python脚本,inline script提供了http、Websocket、tcp等各个时间点事件(events)的hook函数,如http中的request、response等

主要events一览表

   需要修改各种事件内容时,重写以下对应方法,这里主要用的是request、response方法

import typing

import mitmproxy.addonmanager

import mitmproxy.connections

import mitmproxy.http

import mitmproxy.log

import mitmproxy.tcp

import mitmproxy.websocket

import mitmproxy.proxy.protocol

class Events:

# HTTP lifecycle

def http_connect(self, flow: mitmproxy.http.HTTPFlow):

"""

An HTTP CONNECT request was received. Setting a non 2xx response on

the flow will return the response to the client abort the

connection. CONNECT requests and responses do not generate the usual

HTTP handler events. CONNECT requests are only valid in regular and

upstream proxy modes.

"""

def requestheaders(self, flow: mitmproxy.http.HTTPFlow):
"""
HTTP request headers were successfully read. At this point, the body
is empty.
""" def request(self, flow: mitmproxy.http.HTTPFlow):
"""
The full HTTP request has been read.
""" def responseheaders(self, flow: mitmproxy.http.HTTPFlow):
"""
HTTP response headers were successfully read. At this point, the body
is empty.
""" def response(self, flow: mitmproxy.http.HTTPFlow):
"""
The full HTTP response has been read.
""" def error(self, flow: mitmproxy.http.HTTPFlow):
"""
An HTTP error has occurred, e.g. invalid server responses, or
interrupted connections. This is distinct from a valid server HTTP
error response, which is simply a response with an HTTP error code.
""" # TCP lifecycle
def tcp_start(self, flow: mitmproxy.tcp.TCPFlow):
"""
A TCP connection has started.
""" def tcp_message(self, flow: mitmproxy.tcp.TCPFlow):
"""
A TCP connection has received a message. The most recent message
will be flow.messages[-1]. The message is user-modifiable.
""" def tcp_error(self, flow: mitmproxy.tcp.TCPFlow):
"""
A TCP error has occurred.
""" def tcp_end(self, flow: mitmproxy.tcp.TCPFlow):
"""
A TCP connection has ended.
""" # Websocket lifecycle
def websocket_handshake(self, flow: mitmproxy.http.HTTPFlow):
"""
Called when a client wants to establish a WebSocket connection. The
WebSocket-specific headers can be manipulated to alter the
handshake. The flow object is guaranteed to have a non-None request
attribute.
""" def websocket_start(self, flow: mitmproxy.websocket.WebSocketFlow):
"""
A websocket connection has commenced.
""" def websocket_message(self, flow: mitmproxy.websocket.WebSocketFlow):
"""
Called when a WebSocket message is received from the client or
server. The most recent message will be flow.messages[-1]. The
message is user-modifiable. Currently there are two types of
messages, corresponding to the BINARY and TEXT frame types.
""" def websocket_error(self, flow: mitmproxy.websocket.WebSocketFlow):
"""
A websocket connection has had an error.
""" def websocket_end(self, flow: mitmproxy.websocket.WebSocketFlow):
"""
A websocket connection has ended.
""" # Network lifecycle
def clientconnect(self, layer: mitmproxy.proxy.protocol.Layer):
"""
A client has connected to mitmproxy. Note that a connection can
correspond to multiple HTTP requests.
""" def clientdisconnect(self, layer: mitmproxy.proxy.protocol.Layer):
"""
A client has disconnected from mitmproxy.
""" def serverconnect(self, conn: mitmproxy.connections.ServerConnection):
"""
Mitmproxy has connected to a server. Note that a connection can
correspond to multiple requests.
""" def serverdisconnect(self, conn: mitmproxy.connections.ServerConnection):
"""
Mitmproxy has disconnected from a server.
""" def next_layer(self, layer: mitmproxy.proxy.protocol.Layer):
"""
Network layers are being switched. You may change which layer will
be used by returning a new layer object from this event.
""" # General lifecycle
def configure(self, updated: typing.Set[str]):
"""
Called when configuration changes. The updated argument is a
set-like object containing the keys of all changed options. This
event is called during startup with all options in the updated set.
""" def done(self):
"""
Called when the addon shuts down, either by being removed from
the mitmproxy instance, or when mitmproxy itself shuts down. On
shutdown, this event is called after the event loop is
terminated, guaranteeing that it will be the final event an addon
sees. Note that log handlers are shut down at this point, so
calls to log functions will produce no output.
""" def load(self, entry: mitmproxy.addonmanager.Loader):
"""
Called when an addon is first loaded. This event receives a Loader
object, which contains methods for adding options and commands. This
method is where the addon configures itself.
""" def log(self, entry: mitmproxy.log.LogEntry):
"""
Called whenever a new log entry is created through the mitmproxy
context. Be careful not to log from this event, which will cause an
infinite loop!
""" def running(self):
"""
Called when the proxy is completely up and running. At this point,
you can expect the proxy to be bound to a port, and all addons to be
loaded.
""" def update(self, flows: typing.Sequence[mitmproxy.flow.Flow]):
"""
Update is called when one or more flow objects have been modified,
usually from a different addon.
"""

针对http,常用的API

http.HTTPFlow 实例 flow

flow.request.headers #获取所有头信息,包含Host、User-Agent、Content-type等字段

flow.request.url #完整的请求地址,包含域名及请求参数,但是不包含放在body里面的请求参数

flow.request.pretty_url #同flow.request.url目前没看出什么差别

flow.request.host #域名

flow.request.method #请求方式。POST、GET等

flow.request.scheme #什么请求 ,如https

flow.request.path # 请求的路径,url除域名之外的内容

flow.request.get_text() #请求中body内容,有一些http会把请求参数放在body里面,那么可通过此方法获取,返回字典类型

flow.request.query #返回MultiDictView类型的数据,url直接带的键值参数

flow.request.get_content()#bytes,结果如flow.request.get_text()

flow.request.raw_content #bytes,结果如flow.request.get_content()

flow.request.urlencoded_form #MultiDictView,content-type:application/x-www-form-urlencoded时的请求参数,不包含url直接带的键值参数

flow.request.multipart_form #MultiDictView,content-type:multipart/form-data

时的请求参数,不包含url直接带的键值参数

以上均为获取request信息的一些常用方法,对于response,同理

flow.response.status_code #状态码

flow.response.text#返回内容,已解码

flow.response.content #返回内容,二进制

flow.response.setText()#修改返回内容,不需要转码

以上为不完全列举

示例

修改response内容,这里是服务器已经有返回了结果,再更改,也可以做不经过服务器处理,直接返回,看需求

def response(flow:http.HTTPFlow)-> None:

#特定接口需要返回1001结果

interface_list=["page/**"] #由于涉及公司隐私问题,隐藏实际的接口

url_path=flow.request.path
if url_path.split("?")[0] in interface_list:
ctx.log.info("#"*50)
ctx.log.info("待修改路径的内容:"+url_path)
ctx.log.info("修改成:1001错误返回")
ctx.log.info("修改前:\n")
ctx.log.info(flow.response.text)
flow.response.set_text(json.dumps({"result":"1001","message":"服务异常"}))#修改,使用set_text不用转码
ctx.log.info("修改后:\n")
ctx.log.info(flow.response.text)
ctx.log.info("#"*50)
elif flow.request.host in host_list:#host_list 域名列表,作为全局变量,公司有多个域名,也隐藏
ctx.log.info("response= "+flow.response.text)

应用

   移动app测试中,为了测试app的容错能力,在不改动数据库或者折腾服务器的情况下,脚本修改request或者response内容【这里也可以选择第三方工具,如fiddler同样支持,看个人需求】,查看app的表现;亦或是根据接口定义检查app的接口请求情况

作者:小蜗牛的成长

链接:https://www.jianshu.com/p/a495cc016682

來源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

深入了解mitmproxy(二)的更多相关文章

  1. MITMProxy如何配置二次代理

    MITMProxy如何配置二次代理 0.2172018.09.05 11:13:15字数 232阅读 2609 前序: mitmproxy真的很强大,或许是大家都各自使用,或者没有相关的需求,导致我废 ...

  2. 双网卡+mitmproxy+iptables搭建SSL中间人(支持非HTTPS协议)

    "想要解决一个问题,最根本方法的就是了解这一切是如何工作的,而不是玄学." --ASCII0X03 最近学习发现现在很多现成的安卓SSL中间人工具和教程都只针对HTTPS流量,比如 ...

  3. Python爬虫入门教程 47-100 mitmproxy安装与安卓模拟器的配合使用-手机APP爬虫部分

    1. 准备下载软件 介绍一款爬虫辅助工具mitmproxy ,mitmproxy 就是用于MITM的proxy,MITM中间人攻击.说白了就是服务器和客户机中间通讯多增加了一层.跟Fiddler和Ch ...

  4. mitmproxy(中间人攻击工具)安装及使用

    一.安装 执行命令,报错 pip install mitmproxy 根据提示安装vc++14后重新执行正常安装. 查看版本号: mitmproxy --version Error: mitmprox ...

  5. Python3+mitmproxy安装使用教程(Windows)

    一.安装 1.1 安装mitmproxy 直接使用pip安装即可 pip install mitmproxy pip本质上会一是安装mitmproxy库的相关代码,二是安装mitmproxy.exe/ ...

  6. 13.App爬取相关库的安装(Charles,Mitmproxy,Appium)

    由于App没有像浏览器一样直观的后台请求工具,主要用一些抓包技术抓取数据.(目前也在学习安装,参考书籍.) 首先呢,一些简单的接口通过Charles或mitmproxy分析,找出规律,直接用程序去抓取 ...

  7. App爬虫神器mitmproxy和mitmdump的使用

    原文 mitmproxy是一个支持HTTP和HTTPS的抓包程序,有类似Fiddler.Charles的功能,只不过它是一个控制台的形式操作. mitmproxy还有两个关联组件.一个是mitmdum ...

  8. [转]使用 mitmproxy + python 做拦截代理

    使用 mitmproxy + python 做拦截代理   本文是一个较为完整的 mitmproxy 教程,侧重于介绍如何开发拦截脚本,帮助读者能够快速得到一个自定义的代理工具. 本文假设读者有基本的 ...

  9. 网络安全、Web安全、渗透测试之笔经面经总结(二)

    这篇文章涉及的知识点有如下几方面: 1.SSL Strip(SSp)攻击到底是什么? 2.中间人攻击——ARP欺骗的原理.实战及防御 3会话劫持原理 4.CC攻击 5.添加时间戳防止重放攻击 6.浅析 ...

随机推荐

  1. NSP4——Network Simulator for P4

    NSP4--Network Simulator for P4 一.前言 NSP4旨在为P4开发者,创建一个可视化的P4流表管理及拓扑建立工具,帮助P4开发者,更好的调试自己的P4程序.此开发工具是基于 ...

  2. 对MP4一些概念的理解

    首先,对视频一些基本概念的理解: I帧:i帧又称为内编码帧,是一种自带全部信息的独立帧,可独立解码,可理解为一张静态图片,视频序列中的第一个帧始终是i帧,因为它是关键帧. P帧:P帧又称为帧间预测编码 ...

  3. _stdcall 和 _cdecl

    今天遇到一个问题用C++编写一个动态链接库生成的文件为dll.dll,用在visual stdio 2010调用这个dll 调用形式:[DllImport("dll.dll")] ...

  4. Docker 将一堆镜像 导成一个文件

    docker save istio/galley istio/citadel istio/mixer istio/sidecar_injector istio/proxy_init istio/pro ...

  5. Android 获取加速传感器的值,并去除杂音

    1.注册和注销传感器 private void registerSensor() { manager.registerListener(this, manager.getDefaultSensor(S ...

  6. NGINX.conf配置文件支持pathinfo

    # power by www.php.cn #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/e ...

  7. Python模块-pandas

    目录 数据读取 数据探索 数据清洗 数据清洗 类型转换 缺失值 重复值 值替换 修改表结构 新增列 删除列 删除行 修改列名 数据分组(数值变量) 数据分列(分类变量) 设置索引 排序 数据筛选/切片 ...

  8. [COGS 2551] 新型武器

    图片加载可能有点慢,请跳过题面先看题解,谢谢 这个题好多解法啊... 可以主席树,可以按深度将操作排序离线做 我这里是动态开点线段树,对每一个深度种一棵线段树,下标是节点的\(dfs\)序 然后这个做 ...

  9. VS2010 代码突然改变字体 解决办法

    sfsfsddffs dffsfsfsfsf 如上,第一行是突然变成宽体的字体,第二行是恢复后的字体,方法就是: shift+空格键,一起按就会在这两种字体之间变换~

  10. Linux系统Web网站目录和文件安全权限设置

    查看Linux文件的权限:ls -l 文件名称查看linux文件夹的权限:ls -ld 文件夹名称(所在目录)例如: drwxr-xr-x 2 root root 4096 2009-01-14 17 ...