文章目录
1、安装mitmproxy
2、事件监听器(demo.py)
import mitmproxy.http
from mitmproxy import ctx
num = 0
def request(flow: mitmproxy.http.HTTPFlow):
global num
num = num + 1
ctx.log.info(u"处理第 %d 个请求" % num)
ctx.log.info(u"cookie数据:")
for cname in flow.request.cookies:
ctx.log.info(cname + ":" + flow.request.cookies[cname])
if flow.request.url.startswith("http://192.168.2.105:8080/save"):
if flow.request.urlencoded_form:
# ctx.log.info(u"请求表单里的数据:" + flow.request.get_text())
ctx.log.info(u"请求表单里的数据:")
for key in flow.request.urlencoded_form:
ctx.log.info(key + ":" + flow.request.urlencoded_form[key])
flow.request.urlencoded_form[u"sname"] = u"哈哈"
ctx.log.info(u"表单数据修改成功:")
for key in flow.request.urlencoded_form:
ctx.log.info(key + ":" + flow.request.urlencoded_form[key])
3、运行mitmproxy
mitmdump -p 9090 --mode reverse:http://192.168.2.105:8080/ -s demo.py
4、修改nginx配置
#动态请求交给mitmproxy处理
location /
{
proxy_pass http://192.168.2.105:9090/;
}
5、运行nginx
6、启动tomcat服务器
7、访问nginx服务器(localhost)
8、查看日志
9、登记学生(填写表单并提交)
10、查看结果
11、nginx的完整配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#动态请求交给mitmproxy处理
location / {
proxy_pass http://192.168.2.105:9090/;
}
#静态文件交给nginx处理
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ico|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
root D:\\yangzc\\ajaxdemoNG\\studentweb\\;
expires 30d;
}
#静态文件交给nginx处理
location ~ .*\.(js|css)?$
{
root D:\\yangzc\\ajaxdemoNG\\studentweb\\;
expires 1h;
}
#设置默认主页
location ~ ^/$
{
index index.html index.htm;
root D:\\yangzc\\ajaxdemoNG\\studentweb\\;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
12、github项目地址
https://github.com/yangzc23/ajaxdemoNG
参考资料
[01] mitmproxy 5种代理模式
[02] mitmproxy 使用mitmdump 过滤请求
[03] 使用 mitmproxy + python 做拦截代理
[04] mitmproxy使用(一)-安装和使用
[05] mitmproxy使用(二)-自定义脚本编写
[06] mitmproxy使用(三)-读取csv文件(题目和答案)并筛选接口返回的题目中答案
微信扫一扫关注公众号
点击链接加入群聊
https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105