源碼地址:https://github.com/wuchangming/node-mitmproxyhtml
node-mitmproxy是一個基於nodejs,支持http/https的中間人(MITM)代理,便於滲透測試和開發調試。node
一、支持https
二、支持配置的方式啓動,也支持以模塊的方式引入到代碼中git
npm install node-mitmproxy -g
sudo npm install node-mitmproxy -g
關於配置文件github
simpleConfig.jswindows
module.exports = { sslConnectInterceptor: (req, cltSocket, head) => true, requestInterceptor: (rOptions, req, res, ssl, next) => { console.log(`正在訪問:${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}`); console.log('cookie:', rOptions.headers.cookie); res.end('hello node-mitmproxy!'); next(); } };
效果圖:api
啓動方式svg
node-mitmproxy -c simpleConfig.js
生成CA根證書的默認路徑:%用戶名%/node-mitmproxy
函數
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/node-mitmproxy/node-mitmproxy.ca.crt
注: 證書須要安裝到 ** 受信任的根證書目錄 ** 下
參考 issues#3
start %HOMEPATH%/node-mitmproxy/node-mitmproxy.ca.crt
var mitmproxy = require('node-mitmproxy'); mitmproxy.createProxy({ sslConnectInterceptor: (req, cltSocket, head) => true, requestInterceptor: (rOptions, req, res, ssl, next) => { console.log(`正在訪問:${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}`); console.log('cookie:', rOptions.headers.cookie); res.end('Hello node-mitmproxy!'); next(); }, responseInterceptor: (req, res, proxyReq, proxyRes, ssl, next) => { next(); } });
啓動端口(默認:6789)
port: 6789
判斷該connnect請求是否須要代理,傳入參數參考http connnect 。
sslConnectInterceptor: (clientReq, clientSocket, head) => true,
攔截客戶端請求/響應
參數說明: 一、requestOptions:客戶端請求參數 二、clientReq: 參考http.IncomingMessage 三、clientRes: 參考http.ServerResponse 四、ssl: 該請求是否爲https 五、next: 回調函數,執行完攔截邏輯後調用該方法
requestInterceptor: (requestOptions, clientReq, clientRes, ssl, next) => { next(); }
CA根證書路徑(ps: 無特殊狀況無需配置) 默認:%HOMEPATH%/node-mitmproxy/node-mitmproxy.ca.crt
caCertPath: 'xxxx/xxxx.crt'
CA根證書密鑰路徑(ps: 無特殊狀況無需配置) 默認:%HOMEPATH%/node-mitmproxy/node-mitmproxy.ca.key.pem
caKeyPath: 'xxxx/xxxx.pem'