mitmproxy 脚本

mitmproxy 脚本

编写一个 py 文件供 mitmproxy 加载,文件定义了【变量 addons】,addons 是个数组,每个元素是一个类实例,这些类有若干方法,这些方法实现了某些 mitmproxy 提供的事件,mitmproxy 会在某个事件发生时调用对应的方法。这些类,称为一个个 addon。

模板:

from mitmproxy import http, ctx
import json

class xxx:
	def xxx:
	def xxx

addons = [
	xxx() //类名的加载,也可以定义多个类,然后以数组的形式添加,进行加载
]


客户端请求修改

修改请求链接

from mitmproxy import ctx, http
import json

class Modify:

 	    def request(self, flow):
 	    	#替换请求链接
        	if flow.request.url.startswith("http://www.baidu.com"):
            #有分享
            flow.request.url = "https://jd.com"
            ctx.log.info("修改链接")
        		
addons = [
	Modify()
]

修改 cookie

from mitmproxy import ctx, http
import json

class Modify:

 	 def request(self, flow):
         #替换cookie,两种匹配请求链接的方式
        # if flow.request.host == "xxx.x.xxx.com.cn":
        if flow.request.url.startswith("https://xxx.x.xxx.com/"):
            print(flow.request.url)
            print(flow.request.cookies)
            flow.request.cookies["_testCookie1"] = "www"
            flow.request.cookies["testCookie2"] = "www"

            req = flow.request.cookies["_testCookie1"]
            ctx.log.info(req)
         
addons = [
	Modify()
]

修改请求参数

from mitmproxy import ctx, http
import json

class Modify:
 	 def request(self, flow):
                if flow.request.url.startswith("http://xxx.x.xxx.com.cn/customActivity/bookcode/doJoin"):
            ctx.log.info("modify request form")
            if flow.request.urlencoded_form:
                flow.request.urlencoded_form["code"] = "11111"
            else:
                flow.request.urlencoded_form = [

                ]
         
addons = [
	Modify()
]


保存抓包数据

from mitmproxy import ctx
import requests

def request(flow): 
    # data = {
    # "url": str(flow.request.url),
    # 'method': str(flow.request.method),
    # 'get_text': str(flow.request.get_text),
    # }

    # json_data = json.dumps(data)
    # fp = open('D:/66666.json', 'a+', encoding='utf-8')
    # fp.write(json_data + '
')
    pass

def response(flow):
    # if flow.request.host == "edith.xiaohongshu.com":
    if "edith.xiaohongshu.com/api/sns/" in flow.request.url :
        data = str(flow.response.text)
        size = len(data)
        if size > 100:
            fp = open('D:/77777.json', 'a+', encoding='utf-8')
            fp.write(data + '
') 

addons = [
	response()
]
原文地址:https://www.cnblogs.com/kai-/p/14148788.html