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

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - 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