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 + '\n')
    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 + '\n') 

addons = [
	response()
]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM