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.
tunnel-ssh
Advanced tools
The tunnel-ssh npm package allows you to create SSH tunnels in Node.js applications. This can be useful for securely connecting to remote servers, databases, or other services that require SSH tunneling.
Basic SSH Tunnel
This code sample demonstrates how to set up a basic SSH tunnel using the tunnel-ssh package. The configuration object includes details such as the SSH username, host, port, destination host and port, local host and port, and the private key for authentication.
const tunnel = require('tunnel-ssh');
const config = {
username: 'user',
host: 'remote.server.com',
port: 22,
dstHost: '127.0.0.1',
dstPort: 27017,
localHost: '127.0.0.1',
localPort: 27017,
privateKey: require('fs').readFileSync('/path/to/private/key')
};
tunnel(config, function (error, server) {
if (error) {
console.error('SSH connection error: ', error);
} else {
console.log('SSH tunnel established');
}
});
SSH Tunnel with Forwarding
This code sample shows how to set up an SSH tunnel with port forwarding. The 'keepAlive' option is set to true to maintain the connection. This is useful for applications that need to keep a persistent connection to a remote database or service.
const tunnel = require('tunnel-ssh');
const config = {
username: 'user',
host: 'remote.server.com',
port: 22,
dstHost: '127.0.0.1',
dstPort: 3306,
localHost: '127.0.0.1',
localPort: 3306,
privateKey: require('fs').readFileSync('/path/to/private/key'),
keepAlive: true
};
tunnel(config, function (error, server) {
if (error) {
console.error('SSH connection error: ', error);
} else {
console.log('SSH tunnel with forwarding established');
}
});
Dynamic Port Forwarding
This code sample demonstrates how to set up a dynamic SSH tunnel, which can be used as a SOCKS proxy. The 'dynamic' option is set to true, allowing the tunnel to dynamically forward traffic to different destinations.
const tunnel = require('tunnel-ssh');
const config = {
username: 'user',
host: 'remote.server.com',
port: 22,
dstHost: '0.0.0.0',
dstPort: 0,
localHost: '127.0.0.1',
localPort: 1080,
privateKey: require('fs').readFileSync('/path/to/private/key'),
dynamic: true
};
tunnel(config, function (error, server) {
if (error) {
console.error('SSH connection error: ', error);
} else {
console.log('Dynamic SSH tunnel established');
}
});
The ssh2 package is a more general-purpose SSH client for Node.js. It provides a wide range of SSH functionalities, including tunneling, executing commands, and file transfers. Compared to tunnel-ssh, ssh2 offers more flexibility and control over SSH connections but requires more setup and configuration.
The node-ssh package is a high-level SSH client for Node.js that simplifies the process of connecting to SSH servers, executing commands, and transferring files. It is easier to use than ssh2 but offers less control over the SSH connection. It is a good alternative to tunnel-ssh for users who need basic SSH functionalities without the complexity.
The ssh2-sftp-client package is built on top of ssh2 and provides a simplified API for SFTP (SSH File Transfer Protocol) operations. While it focuses on file transfers, it can also be used to create SSH tunnels. It is a good choice for users who primarily need SFTP capabilities with occasional SSH tunneling.
One to connect them all !
Tunnel-ssh is based on the fantastic ssh2 library by Brian White. Trouble ? Please study the ssh2 configuration.
Special thanks to @vweevers and @dickeyxxx
By default tunnel-ssh will close the tunnel after a client disconnects, so your cli tools should work in the same way, they do if you connect directly. If you need the tunnel to stay open, use the "keepAlive:true" option within the configuration.
var config = {
...
keepAlive:true
};
var tnl = tunnel(config, function(error, tnl){
yourClient.connect();
yourClient.disconnect();
setTimeout(function(){
// you only need to close the tunnel by yourself if you set the
// keepAlive:true option in the configuration !
tnl.close();
},2000);
});
// you can also close the tunnel from here...
setTimeout(function(){
tnl.close();
},2000);
A local server listening for connections to forward via ssh Description: This is where you bind your interface. Properties: ** localHost (default is '127.0.0.1') ** localPort (default is dstPort)
The ssh configuration Description: The host you want to use as ssh-tunnel server. Properties: ** host ** port (22) ** username ** ...
The destination host configuration (based on the ssh host) Imagine you just connected to The host you want to connect to. (via host:port) now that server connects requires a target to tunnel to. Properties: ** dstHost (localhost) ** dstPort
var config = {
username:'root',
Password:'secret',
host:sshServer,
port:22,
dstHost:destinationServer,
dstPort:27017,
localHost:'127.0.0.1',
localPort: 27000
};
var tunnel = require('tunnel-ssh');
tunnel(config, function (error, server) {
//....
});
tunnel-ssh assumes that you want to map the same port on a remote machine to your localhost using the ssh-server on the remote machine.
var config = {
username:'root',
dstHost:'remotehost.with.sshserver.com',
dstPort:27017,
privateKey:require(fs.readFileSync('/path/to/key'),
passphrase:'secret'
};
tunnel-ssh pipes the configuration direct into the ssh2 library so every config option provided by ssh2 still works. ssh2 configuration
var tunnel = require('tunnel-ssh');
//map port from remote 3306 to localhost 3306
var server = tunnel({host: '172.16.0.8', dstPort: 3306}, function (error, server) {
if(error){
//catch configuration and startup errors here.
}
});
// Use a listener to handle errors outside the callback
server.on('error', function(err){
console.error('Something bad happened:', err);
});
FAQs
Easy extendable SSH tunnel
The npm package tunnel-ssh receives a total of 123,153 weekly downloads. As such, tunnel-ssh popularity was classified as popular.
We found that tunnel-ssh 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.