Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
http-mitm-proxy
Advanced tools
HTTP Man In The Middle (MITM) Proxy written in node.js. Supports capturing and modifying the request and response data.
npm install --save http-mitm-proxy
The library should work starting Node.js 8.x, but testing is only expected for currently supported LTS versions of Node.js starting Node.js 12.x . use on your own risk with non LTS Node.js versions.
type definitions are now included in this project, no extra steps required.
This example will modify any search results coming from google and replace all the result titles with "Pwned!".
const Proxy = require('http-mitm-proxy').Proxy;
// or using import/module (package.json -> "type": "module")
// import { Proxy } from "http-mitm-proxy";
const proxy = new Proxy();
proxy.onError(function(ctx, err) {
console.error('proxy error:', err);
});
proxy.onRequest(function(ctx, callback) {
if (ctx.clientToProxyRequest.headers.host == 'www.google.com'
&& ctx.clientToProxyRequest.url.indexOf('/search') == 0) {
ctx.use(Proxy.gunzip);
ctx.onResponseData(function(ctx, chunk, callback) {
chunk = Buffer.from(chunk.toString().replace(/<h3.*?<\/h3>/g, '<h3>Pwned!</h3>'));
return callback(null, chunk);
});
}
return callback();
});
console.log('begin listening on 8081')
proxy.listen({port: 8081});
You can find more examples in the examples directory
Using node-forge allows the automatic generation of SSL certificates within the proxy. After running your app you will find options.sslCaDir + '/certs/ca.pem' which can be imported to your browser, phone, etc.
Context functions only effect the current request/response. For example you may only want to gunzip requests made to a particular host.
The context available in websocket handlers is a bit different
Starts the proxy listening on the given port.
Arguments
Example
proxy.listen({ port: 80 });
Stops the proxy listening.
Example
proxy.close();
Adds a function to the list of functions to get called if an error occures.
Arguments
Example
proxy.onError(function(ctx, err, errorKind) {
// ctx may be null
var url = (ctx && ctx.clientToProxyRequest) ? ctx.clientToProxyRequest.url : "";
console.error(errorKind + ' on ' + url + ':', err);
});
Allows the default certificate name/path computation to be overwritten.
The default behavior expects keys/{hostname}.pem
and certs/{hostname}.pem
files to be at self.sslCaDir
.
Arguments
Example 1
proxy.onCertificateRequired = function(hostname, callback) {
return callback(null, {
keyFile: path.resolve('/ca/certs/', hostname + '.key'),
certFile: path.resolve('/ca/certs/', hostname + '.crt')
});
};
Example 2: Wilcard certificates
proxy.onCertificateRequired = function(hostname, callback) {
return callback(null, {
keyFile: path.resolve('/ca/certs/', hostname + '.key'),
certFile: path.resolve('/ca/certs/', hostname + '.crt'),
hosts: ["*.mydomain.com"]
});
};
Allows you to handle missing certificate files for current request, for example, creating them on the fly.
Arguments
files.keyFile
, files.certFile
and optional files.hosts
)keyFileData
and certFileData
)Example 1
proxy.onCertificateMissing = function(ctx, files, callback) {
console.log('Looking for "%s" certificates', ctx.hostname);
console.log('"%s" missing', ctx.files.keyFile);
console.log('"%s" missing', ctx.files.certFile);
// Here you have the last chance to provide certificate files data
// A tipical use case would be creating them on the fly
//
// return callback(null, {
// keyFileData: keyFileData,
// certFileData: certFileData
// });
};
Example 2: Wilcard certificates
proxy.onCertificateMissing = function(ctx, files, callback) {
return callback(null, {
keyFileData: keyFileData,
certFileData: certFileData,
hosts: ["*.mydomain.com"]
});
};
Adds a function to get called at the beginning of a request.
Arguments
Example
proxy.onRequest(function(ctx, callback) {
console.log('REQUEST:', ctx.clientToProxyRequest.url);
return callback();
});
Adds a function to get called for each request data chunk (the body).
Arguments
Example
proxy.onRequestData(function(ctx, chunk, callback) {
console.log('REQUEST DATA:', chunk.toString());
return callback(null, chunk);
});
Adds a function to get called when all request data (the body) was sent.
Arguments
Example
var chunks = [];
proxy.onRequestData(function(ctx, chunk, callback) {
chunks.push(chunk);
return callback(null, chunk);
});
proxy.onRequestEnd(function(ctx, callback) {
console.log('REQUEST END', (Buffer.concat(chunks)).toString());
return callback();
});
Adds a function to get called at the beginning of the response.
Arguments
Example
proxy.onResponse(function(ctx, callback) {
console.log('BEGIN RESPONSE');
return callback();
});
Adds a function to get called for each response data chunk (the body).
Arguments
Example
proxy.onResponseData(function(ctx, chunk, callback) {
console.log('RESPONSE DATA:', chunk.toString());
return callback(null, chunk);
});
Adds a function to get called when the proxy request to server has ended.
Arguments
Example
proxy.onResponseEnd(function(ctx, callback) {
console.log('RESPONSE END');
return callback();
});
Adds a function to get called at the beginning of websocket connection
Arguments
Example
proxy.onWebSocketConnection(function(ctx, callback) {
console.log('WEBSOCKET CONNECT:', ctx.clientToProxyWebSocket.upgradeReq.url);
return callback();
});
Adds a function to get called for each WebSocket message sent by the client.
Arguments
Example
proxy.onWebSocketSend(function(ctx, message, flags, callback) {
console.log('WEBSOCKET SEND:', ctx.clientToProxyWebSocket.upgradeReq.url, message);
return callback(null, message, flags);
});
Adds a function to get called for each WebSocket message received from the server.
Arguments
Example
proxy.onWebSocketMessage(function(ctx, message, flags, callback) {
console.log('WEBSOCKET MESSAGE:', ctx.clientToProxyWebSocket.upgradeReq.url, message);
return callback(null, message, flags);
});
Adds a function to get called for each WebSocket frame exchanged (message
, ping
or pong
).
Arguments
Example
proxy.onWebSocketFrame(function(ctx, type, fromServer, data, flags, callback) {
console.log('WEBSOCKET FRAME ' + type + ' received from ' + (fromServer ? 'server' : 'client'), ctx.clientToProxyWebSocket.upgradeReq.url, data);
return callback(null, data, flags);
});
Adds a function to the list of functions to get called if an error occures in WebSocket.
Arguments
Example
proxy.onWebSocketError(function(ctx, err) {
console.log('WEBSOCKET ERROR:', ctx.clientToProxyWebSocket.upgradeReq.url, err);
});
Adds a function to get called when a WebSocket connection is closed
Arguments
Example
proxy.onWebSocketClose(function(ctx, code, message, callback) {
console.log('WEBSOCKET CLOSED BY '+(ctx.closedByServer ? 'SERVER' : 'CLIENT'), ctx.clientToProxyWebSocket.upgradeReq.url, code, message);
callback(null, code, message);
});
Adds a module into the proxy. Modules encapsulate multiple life cycle processing functions into one object.
Arguments
Example
proxy.use({
onError: function(ctx, err) { },
onCertificateRequired: function(hostname, callback) { return callback(); },
onCertificateMissing: function(ctx, files, callback) { return callback(); },
onRequest: function(ctx, callback) { return callback(); },
onRequestData: function(ctx, chunk, callback) { return callback(null, chunk); },
onResponse: function(ctx, callback) { return callback(); },
onResponseData: function(ctx, chunk, callback) { return callback(null, chunk); },
onWebSocketConnection: function(ctx, callback) { return callback(); },
onWebSocketSend: function(ctx, message, flags, callback) { return callback(null, message, flags); },
onWebSocketMessage: function(ctx, message, flags, callback) { return callback(null, message, flags); },
onWebSocketError: function(ctx, err) { },
onWebSocketClose: function(ctx, code, message, callback) { },
});
node-http-mitm-proxy provide some ready to use modules:
Proxy.gunzip
Gunzip response filter (uncompress gzipped content before onResponseData and compress back after)Proxy.wildcard
Generates wilcard certificates by default (so less certificates are generated)Adds a stream into the request body stream.
Arguments
Example
ctx.addRequestFilter(zlib.createGunzip());
Adds a stream into the response body stream.
Arguments
Example
ctx.addResponseFilter(zlib.createGunzip());
Copyright (c) 2015 Joe Ferner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
FAQs
HTTP Man In The Middle (MITM) Proxy
We found that http-mitm-proxy demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.