惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - timseng

ai辅助升级前端安全更新(贼快!hh openclaw的token又耗完了,先不冲了,记录下折腾踩坑过程 (如果需要协助安装500,帮忙卸载250[doge]) VS Code我的配置(持续更新) 推广:flowable工作流引擎就是高级版本的数据库而已(一) springboot升级3x导致邮件发送失败:错误1 jakarta.mail.NoSuchProviderException: smtp nice du更好的du分析文件占用工具ncdu 使用MITMProxy转发https请求到本地、保存鉴权给本地请求(二) 前端发布shell脚本 virtualBox环境Ubuntu升级后太卡,转debian很丝滑 收藏:加不加「/」?Nginx location 路径与 proxy_pass 的规律 使用visjs分析flowable流程数据 浏览器标签页多行显示:使用Floorp浏览器 最先进的跨平台 Firefox 衍生品 开源之光 开源WAF:ModSecurity探究与部署 动态条件实现java mysql备份恢复云端之核心:更新扫描MySQL库里的所有表的UPDATE_TIME,若发生变动就mysqldump 这款Ubuntu的默认壁纸水母是真漂亮啊 笑死!'m not going to tell you who, but I know someone who recently locked himself out of the system he was using for an article covering iptables by forgetting the SSH rule. itext的PdfPCell中设置行间距失败的问题 Java递归函数计算递归次数出错
使用MITMProxy转发请求到本地、保存鉴权给本地请求
timseng · 2024-06-27 · via 博客园 - timseng

背景:

1.公司项目,没有前端项目,无法起前端,无法验证本地接口

2.接口鉴权OATH2,使用postman调试每次都要取浏览器复制Authorization头,还会频繁过期,影响效率

方案:使用MITMProxy自定义流量处理

MITMProxy是一种用于中间人攻击(Man-in-the-middle attack)的代理工具。它的作用是在正常的代理功能基础上,截获、记录或篡改数据,并自定义特定的行为。与其他抓包工具(如Fiddler或Wireshark)不同的是,MITMProxy不仅可以查看和分析截获的请求,还可以通过自定义脚本进行二次开发。例如,可以截获浏览器对特定URL的请求,将返回内容置空并存储真实的返回内容到数据库,并在出现异常时发送邮件通知。与Fiddler类似的需求无法实现高度定制化,而MITMProxy可通过载入自定义Python脚本轻松实现。

安装:brew install mitmproxy

脚本:

# mitmproxy script to intercept and modify requests
from mitmproxy import http
import os

TOKEN_FILE_PATH = "/Users/xy/workspace/python/mitmproxy/token_file.txt"
LOG_FILE_PATH = "/Users/xy/workspace/python/mitmproxy/log_file.txt"


def save_log_to_file(log):
    with open(LOG_FILE_PATH, "a") as file:
        file.writelines(log+"\n")

def save_token_to_file(token):
    with open(TOKEN_FILE_PATH, "w") as file:
        file.write(token)

def load_token_from_file():
    if os.path.exists(TOKEN_FILE_PATH):
        with open(TOKEN_FILE_PATH, "r") as file:
            token = file.read().strip()
            return token
    return None

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

    #保存dev的鉴权
    # Check if the host is a.com and save the token
    if "dev-oa.xx.com" in flow.request.pretty_host:
        save_log_to_file('---->dev-oa.xx.com')
        token = flow.request.headers.get("Authorization")
        if token:
            save_log_to_file('save token')
            save_log_to_file(token)
            save_token_to_file(token)
        # Modify the request URL to dev_a.com
        # flow.request.host = "dev_a.com"
    # Check if the host is b.com and inject the token
            
    #使用保存的鉴权覆盖本地的请求
    elif "127.0.0.1" in flow.request.pretty_host:
        save_log_to_file('---->127.0.0.1')
        token = load_token_from_file()
        if token:
            save_log_to_file('token')
            save_log_to_file(token)
            flow.request.headers["Authorization"] = token

    #将dev的前端请求转发给本地
    # Replace dev-oa.xx.com/flow with 127.0.0.1:9099
    if "dev-oa.xx.com/flow/h5" in flow.request.pretty_url or "dev-oa.xx.com/flow/admin" in flow.request.pretty_url:
    # if False:
        flow.request.host = "127.0.0.1"
        flow.request.port = 9099
        flow.request.scheme = "http"
        flow.request.path = flow.request.path.replace("/flow", "",1)

 启动:mitmproxy -s modify_request.py -p 8089

注意那两个文件的权限

然后在系统设置代理 127.0.0.1 8089