使用mitmproxy实现请求拦截

在这里插入图片描述

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文件(题目和答案)并筛选接口返回的题目中答案

微信扫一扫关注公众号
image.png
点击链接加入群聊

https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值