Giter Site home page Giter Site logo

node-mitmproxy's Introduction

node-mitmproxy 3.x

npm
node-mitmproxy是一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。

1、特性

1、支持https
2、支持配置的方式启动,也支持以模块的方式引入到代码中

2、安装

windows
    npm install node-mitmproxy -g
Mac
    sudo npm install node-mitmproxy -g

3、使用

关于配置文件

简单配置:

simpleConfig.js

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();
    }
};

效果图:

详细配置说明
更多例子

启动方式

node-mitmproxy -c simpleConfig.js

安装node-mitmproxy CA根证书

生成CA根证书的默认路径:%用户名%/node-mitmproxy

PC下安装根证书方式

Mac
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/node-mitmproxy/node-mitmproxy.ca.crt
windows

注: 证书需要安装到 ** 受信任的根证书目录 ** 下
参考 issues#3

start %HOMEPATH%/node-mitmproxy/node-mitmproxy.ca.crt

以nodejs模块的方式引用到代码中

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();
    }
});

4、配置详细说明

port

启动端口(默认:6789)

    port: 6789

sslConnectInterceptor

判断该connnect请求是否需要代理,传入参数参考http connnect

    sslConnectInterceptor: (clientReq, clientSocket, head) => true,

requestInterceptor

拦截客户端请求/响应

参数说明:
1、requestOptions:客户端请求参数
2、clientReq: 客户端请求,参考http.IncomingMessage
3、clientRes: 客户端响应,参考http.ServerResponse
4、ssl: 该请求是否为https
5、next: 回调函数,执行完拦截逻辑后调用该方法

    requestInterceptor: (requestOptions, clientReq, clientRes, ssl, next) => {
        next();
    }

responseInterceptor

拦截服务端请求/响应
参数说明:

1、clientReq: 客户端请求,参考http.IncomingMessage
2、clientRes: 客户端响应,参考http.ServerResponse
3、proxyReq: 服务端请求,参考http.IncomingMessage
4、proxyRes: 服务端响应,参考http.ServerResponse
5、ssl: 该请求是否为https
6、next: 回调函数,执行完拦截逻辑后调用该方法

    responseInterceptor: (clientReq, clientRes, proxyReq, proxyRes, ssl, next) => {
        next();
    }

caCertPath

CA根证书路径(ps: 无特殊情况无需配置)
默认:%HOMEPATH%/node-mitmproxy/node-mitmproxy.ca.crt

caCertPath: 'xxxx/xxxx.crt'

caKeyPath

CA根证书密钥路径(ps: 无特殊情况无需配置)
默认:%HOMEPATH%/node-mitmproxy/node-mitmproxy.ca.key.pem

caKeyPath: 'xxxx/xxxx.pem'

5、更多

关于伪造https证书的逻辑图

node-mitmproxy's People

Contributors

wuchangming avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-mitmproxy's Issues

mac os 下生成ca证书报错

MacBook-Pro:~ chke$ node-mitmproxy createCA
undefined
internal/validators.js:125
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
at validateString (internal/validators.js:125:11)
at Object.resolve (path.js:1080:7)
at Object. (/usr/local/lib/node_modules/node-mitmproxy/lib/bin/index.js:21:23)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
at executeUserCode (internal/bootstrap/node.js:342:17)

iOS10.3.1,新生成的根证书问题

新生成的跟证书无法被信任,Certificate Unknown

Frame 44: 73 bytes on wire (584 bits), 73 bytes captured (584 bits) on interface 0
Ethernet II, Src: Hillston_ff:08:09 (00:1c:54:ff:08:09), Dst: Apple_80:79:5d (ac:bc:32:80:79:5d)
    Destination: Apple_80:79:5d (ac:bc:32:80:79:5d)
    Source: Hillston_ff:08:09 (00:1c:54:ff:08:09)
        Address: Hillston_ff:08:09 (00:1c:54:ff:08:09)
        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
    Type: IPv4 (0x0800)
Internet Protocol Version 4, Src: 192.168.27.55, Dst: 192.168.87.121
Transmission Control Protocol, Src Port: 56239 (56239), Dst Port: 9888 (9888), Seq: 486, Ack: 1974, Len: 7
Hypertext Transfer Protocol
    [Proxy-Connect-Hostname: m.baidu.com]
    [Proxy-Connect-Port: 443]
Secure Sockets Layer
    TLSv1.2 Record Layer: Alert (Level: Fatal, Description: Certificate Unknown)
        Content Type: Alert (21)
        Version: TLS 1.2 (0x0303)
        Length: 2
        Alert Message
            Level: Fatal (2)
            Description: Certificate Unknown (46)

关于node-mitmproxy在Android手机端访问https的问题

http没有问题,部分https返回正常,但部分https会报错,错误如下

(node:19245) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Cannot read public key. OID is not RSA.

//js 配置如下
sslConnectInterceptor: (req, cltSocket, head) => true,
caCertPath: '/Users/mac/node-mitmproxy/node-mitmproxy.ca.crt',
caKeyPath: '/Users/mac/node-mitmproxy/node-mitmproxy.ca.key.pem',

Android手机已经安装了对应的证书crt

windows下无法使用

安装的是0.1.0 版本
启动后,浏览器代理设置到127.0.0.1:6789,不能使用

  1. chrome中提示无效的证书
  2. 打不开cnodejs.org

好你,请问怎么得到https服务器的response中body的内容呢?

作者你好,请问怎么从https服务器响应的response中得到返回的内容呢?我试了很多种方法都不行。
在res.on("data", (chunk)=>{})得到的内容都是乱码。
我的目标服务器返回的是一个JSON字串,使用Fidder能看到返回的内容,但是使用您的模块不知道怎么取得response的body内容,还希望能解惑,谢谢。

how can i use other res as response?

我想请求一个地址例如
https://xxx.abc.com/ckd/
然后在responseInterceptor把我�透明传输本地的一个响应过来

const mitmproxyConfig = {
  sslConnectInterceptor: (req, cltSocket, head) => {
    console.log('head', head)
    return 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) => {
    req.pipe(request('http://localhost:9850/ckd')).pipe(res)
    next()
  },
}

mitmproxy.createProxy(mitmproxyConfig)

但是这样的话一直报错 uncaughtException: Error: write after end

我怎么能做到这一点呢

Error: read ECONNRESET

错误信息:

events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TCP.onread (net.js:656:25)
Emitted 'error' event at:
    at Socket.onerror (_stream_readable.js:687:12)
    at Socket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)

复现场景:
在开启代理后,在百度随便搜索字符,奔溃

版本:3.1.0

how can i use it with request

throught this package , i am able to hook https request . but can i use request package to send a request through an existing proxy server?

based on your demo code:

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();
    }
});

生成的CA证书可否默认放在~/.node-mitmproxy下

目前是默认放在~/node-mitmproxy下,在Mac上类似这种文件一般会放在~/.node-mitmproxy下,这样作为隐藏文件,在ls的时候能保证目录的干净。虽然没有什么大的影响,但强迫症患者会有点难受

证书在哪里呢

install -g 之后到处都没有找到证书
node-mitmproxy createCA 命令也报错,好像并没有这个命令

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.