.NET SSH port forwarding
Asked Answered
B

5

12

I am trying to build in SSH port forwarding into a .NET application that I am writing.

I have tried using sharpSSH, but it requires the user to input their password every time, and I don't want that. I am going to handle storing the password.

I have downloaded Granados, but there is basically zero documentation for it. How do I accomplish port forwarding with Granados or any other free SSH library for .NET?

Baltazar answered 14/5, 2010 at 16:7 Comment(0)
P
5

If you set up an DSA key on the SSH server remotely, you could save a key for the user (do this as a one-time thing) and then save the key on the server as an authorized user.

Pennywise answered 24/5, 2010 at 21:17 Comment(1)
using keys is a lot dangerous for reverse connecxions and FingerprintProstrate
S
20

The SSH.NET library is a simple way to achieve this:

using (var client = new SshClient("client.net", "user", "password"))
{
    client.Connect();

    var port = new ForwardedPortLocal("localhost", 10000, "remote.net", 80);
    client.AddForwardedPort(port);

    port.Exception += delegate(object sender, ExceptionEventArgs e)
    {
        Console.WriteLine(e.Exception.ToString());
    };
    port.Start();

    // ... hold the port open ... //

    port.Stop();
    client.Disconnect();
}
Stirrup answered 15/6, 2012 at 19:38 Comment(6)
This is old and i might not get an answer but, is client.net the server running the SSH server? What would remote.net be? Any request to localhost:1000 will be sent to client.net which will then forward the request to remote.net:80? Is that right? Or will it go directly to remote.net from my localhost?Stereoscopic
Yes, client.net is the SSH server. remote.net:80 is the computer that you want to connect to via the SSH tunnel. As you state, any request to localhost:10000 will be sent to client.net via SSH which will then forward the request to remote.net:80. Therefore there is no direct connection between localhost and remote.net.Stirrup
actually, that code does not work, for some reason SSH.NET doesn't resolve localhost correctly, 127.0.0.1 seems to work fine though; thought this had to do with my network configuration, anyway using localhost is not robust hereHoneywell
is there any tutorial on how to configure the .net ssh server?Periodontics
I've been trying to get this working, and it looks like you can leave the localhost off the port ForwardedPortLocal call - the disadvantage is that then any IP can connect to the SSH server on port 10000 and it will be forwarded to remote.net:80. If that's not a security problem in your case you're fine - otherwise you'll need to figure out your local hostname or IP and put that instead of localhost.Yorick
Figured out why "localhost" doesn't work, but "127.0.0.1" does. Here is where it's going wrong. In ForwardedLocalPort's private InternalStart method, it calls IPEndPoint ipEndPoint = new IPEndPoint(DnsAbstraction.GetHostAddresses(this.BoundHost)[0]. So it's performing its own DNS resolution on BoundHost (via Dns.GetHostAddresses) and then arbitrarily choosing the one at [0]. The two entries actually returned are ["::1","127.0.0.1"]. So it's binding to an IPv6 loopback address by default. This isn't compatible with, for example, my IPv4 drivers/etc/hosts entries or IPv4 port proxies.Interpret
P
5

If you set up an DSA key on the SSH server remotely, you could save a key for the user (do this as a one-time thing) and then save the key on the server as an authorized user.

Pennywise answered 24/5, 2010 at 21:17 Comment(1)
using keys is a lot dangerous for reverse connecxions and FingerprintProstrate
U
4

These C# alternatives are all derived from JCraft's Java Jsch:

  1. sharpSSH (inactive since Jan 2010) / author's page / article
  2. DotNetSSH (inactive since Jun 2010)
  3. SSH.NET Library (active as of Jan 2012)
  4. Nsch (generated/updated from Jsch Feb 2012)

The Granados product page links to the Poderosa project which includes a PortForwarding plugin. The source code for the channel.cs and connectionmanager.cs files there appear to implement port forwarding. See this answer for a recommendation.

Nsch appears to be a hidden gem within MonoDevelop's NGit; it is mostly-automatically converted (background info) from Jsch.

Further research in Feb 2011 by Krzysztof Kowalczyk of Sumatra PDF.

Unforgettable answered 19/1, 2012 at 19:58 Comment(0)
P
0

Here is a method without promoting any of these parameters: (Fully-automated port forwarding) using SharpSSH

(user,host,Lport,Rhost,Rport,DSA-key-confirmation,Password)

    Dim JJ As Tamir.SharpSsh.jsch.JSch = New Tamir.SharpSsh.jsch.JSch()
    Dim sess As Tamir.SharpSsh.jsch.Session = JJ.getSession("user", "remoteadd.dydns.com")
    Dim conf As Hashtable = New Hashtable()
    conf.Add("StrictHostKeyChecking", "no")
    sess.setConfig(conf)
    sess.setPassword("password")
    sess.connect()
    sess.setPortForwardingR(45, "127.0.0.1", 256)
Prostrate answered 11/3, 2014 at 11:18 Comment(0)
F
0

Although poorly documented - or at least the documentation eludes me - this seems to be able to handle SSH connections including file transfers and port forwarding: https://github.com/sshnet/SSH.NET

Fasten answered 14/8, 2017 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.