Beispiel #1
0
        //Tutorial: port forwarding
        private static void ConnectSSH2AndPortforwarding()
        {
            SSHConnectionParameter f = new SSHConnectionParameter();

            f.EventTracer = new Tracer();             //to receive detailed events, set ISSHEventTracer
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            f.Protocol = SSHProtocol.SSH1;                           //this sample works on both SSH1 and SSH2
            string host_ip = "10.10.9.8";                            //<--!!! [TO USERS OF Granados]

            f.UserName = "******";                                     //<--!!! if you try this sample, edit these values for your environment!
            f.Password = "";                                         //<--!!!
            s.Connect(new IPEndPoint(IPAddress.Parse(host_ip), 22)); //22 is the default SSH port

            f.Protocol = SSHProtocol.SSH2;

            f.AuthenticationType = AuthenticationType.Password;
            //NOTE: if you use public-key authentication, follow this sample instead of the line above:
            //  f.AuthenticationType = AuthenticationType.PublicKey;
            //  f.IdentityFile = "privatekey.bin";
            //  f.Password = "******";

            //former algorithm is given priority in the algorithm negotiation
            f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.DSA };
            f.PreferableCipherAlgorithms  = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES };

            f.WindowSize = 0x1000;             //this option is ignored with SSH1

            Reader reader = new Reader();      //simple event receiver

            //Creating a new SSH connection over the underlying socket
            _conn        = SSHConnection.Connect(f, reader, s);
            reader._conn = _conn;

            //Local->Remote port forwarding
            SSHChannel ch = _conn.ForwardPort(reader, "www.google.co.jp", 80, "localhost", 0);

            reader._pf = ch;
            while (!reader._ready)
            {
                System.Threading.Thread.Sleep(100);                                 //wait response
            }
            reader._pf.Transmit(Encoding.ASCII.GetBytes("GET / HTTP/1.0\r\n\r\n")); //get the toppage

            //Remote->Local
            // if you want to listen to a port on the SSH server, follow this line:
            //_conn.ListenForwardedPort("0.0.0.0", 10000);

            //NOTE: if you use SSH2, dynamic key exchange feature is supported.
            //((SSH2Connection)_conn).ReexchangeKeys();
        }
        //Tutorial: port forwarding
        private static void ConnectSSH2AndPortforwarding()
        {
            SSHConnectionParameter f = new SSHConnectionParameter();
            f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            f.Protocol = SSHProtocol.SSH1; //this sample works on both SSH1 and SSH2
            string host_ip = "10.10.9.8"; //<--!!! [TO USERS OF Granados]
            f.UserName = "******";          //<--!!! if you try this sample, edit these values for your environment!
            f.Password = "";              //<--!!!
            s.Connect(new IPEndPoint(IPAddress.Parse(host_ip), 22)); //22 is the default SSH port

            f.Protocol = SSHProtocol.SSH2;

            f.AuthenticationType = AuthenticationType.Password;
            //NOTE: if you use public-key authentication, follow this sample instead of the line above:
            //  f.AuthenticationType = AuthenticationType.PublicKey;
            //  f.IdentityFile = "privatekey.bin";
            //  f.Password = "******";

            //former algorithm is given priority in the algorithm negotiation
            f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.DSA };
            f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES };

            f.WindowSize = 0x1000; //this option is ignored with SSH1

            Reader reader = new Reader(); //simple event receiver

            //Creating a new SSH connection over the underlying socket
            _conn = SSHConnection.Connect(f, reader, s);
            reader._conn = _conn;

            //Local->Remote port forwarding
            SSHChannel ch = _conn.ForwardPort(reader, "www.google.co.jp", 80, "localhost", 0);
            reader._pf = ch;
            while (!reader._ready)
                System.Threading.Thread.Sleep(100); //wait response
            reader._pf.Transmit(Encoding.ASCII.GetBytes("GET / HTTP/1.0\r\n\r\n")); //get the toppage

            //Remote->Local
            // if you want to listen to a port on the SSH server, follow this line:
            //_conn.ListenForwardedPort("0.0.0.0", 10000);

            //NOTE: if you use SSH2, dynamic key exchange feature is supported.
            //((SSH2Connection)_conn).ReexchangeKeys();
        }
Beispiel #3
0
        public void CheckFresh()
        {
            //if (ThreadManager.IsRunning == false) return;

            try
            {
                string[] cols = row.Split('|');
                Host = cols[0].Trim();
                User = cols[1].Trim();
                Pass = cols[2].Trim();
                if (cols.Count() == 3)
                {
                    Country = "UNKNOWN";
                }
                else
                {
                    Country = cols[3].Trim();
                }
                _f                    = new SSHConnectionParameter();
                _f.UserName           = User;
                _f.Password           = Pass;
                _f.Protocol           = SSHProtocol.SSH2;
                _f.AuthenticationType = AuthenticationType.Password;
                _f.WindowSize         = 0x1000;

                _reader = new Reader();
                _reader.TimeoutSeconds = TimeoutSeconds;
                _reader.passControl    = new Reader.PassControl(passControl);
                _reader._country       = Country;
                _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //s.Blocking = false;
                _socket.SendTimeout    = TimeoutSeconds * 1000;
                _socket.ReceiveTimeout = TimeoutSeconds * 1000;
                _socket.Connect(new IPEndPoint(IPAddress.Parse(Host), DefaultPort));

                new Thread(new ThreadStart(SetSSHConnectionTimeout)).Start();
                _conn = SSHConnection.Connect(_f, _reader, _socket);
                if (SSHConnectTimeout)
                {
                    throw new Exception("SSH Connect Timeout");
                }
                _reader._conn = _conn;

                SSHChannel ch = _conn.ForwardPort(_reader, "ipinfo.io", 80, "localhost", 80);
                _reader._pf = ch;

                int seconds = TimeoutSeconds;
                while (_reader._ready == false && seconds > 0)
                {
                    seconds--; Thread.Sleep(1000);
                }
                if (_reader._ready == false && seconds <= 0)
                {
                    throw new Exception("Reader._ready timeout");
                }

                _reader.LineIndex = LineIndex;
                _reader.Host      = Host;
                _reader.User      = User;
                _reader.Pass      = Pass;

                new Thread(new ThreadStart(_reader.SetHTTPRequestTimeout)).Start();
                _reader._pf.Transmit(Encoding.ASCII.GetBytes("GET /json HTTP/1.1\r\nHost:ipinfo.io\r\n\r\n")); //http://ipinfo.io/json
            }
            catch (Exception ex)
            {
                passControl(LineIndex, false, "", ex.Message);
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            /*
             * string cn = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
             * string t1 = Routrek.SSHC.Strings.GetString("NotSSHServer");
             * System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ja");
             * Routrek.SSHC.Strings.Reload();
             * string t2 = Routrek.SSHC.Strings.GetString("NotSSHServer");
             */

#if false //RSA keygen
            //RSA KEY GENERATION TEST
            byte[]     testdata = Encoding.ASCII.GetBytes("CHRISTIAN VIERI");
            RSAKeyPair kp       = RSAKeyPair.GenerateNew(2048, new Random());
            byte[]     sig      = kp.Sign(testdata);
            kp.Verify(sig, testdata);

            new SSH2UserAuthKey(kp).WritePublicPartInOpenSSHStyle(new FileStream("C:\\IOPort\\newrsakey", FileMode.Create));
            //SSH2UserAuthKey newpk = SSH2PrivateKey.FromSECSHStyleFile("C:\\IOPort\\newrsakey", "nedved");
#endif

#if false //DSA keygen
            //DSA KEY GENERATION TEST
            byte[]     testdata = Encoding.ASCII.GetBytes("CHRISTIAN VIERI 0000");
            DSAKeyPair kp       = DSAKeyPair.GenerateNew(2048, new Random());
            byte[]     sig      = kp.Sign(testdata);
            kp.Verify(sig, testdata);
            new SSH2UserAuthKey(kp).WritePublicPartInOpenSSHStyle(new FileStream("C:\\IOPort\\newdsakey", FileMode.Create));
            //SSH2PrivateKey newpk = SSH2PrivateKey.FromSECSHStyleFile("C:\\IOPort\\newdsakey", "nedved");
#endif

            SSHConnectionParameter f = new SSHConnectionParameter();
            f.UserName = "******";
#if false //SSH1
            //SSH1
            f.Password                   = "";
            f.Protocol                   = SSHProtocol.SSH2;
            f.AuthenticationType         = AuthenticationType.Password;
            f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES };
#else //SSH2
            f.Password           = "";
            f.Protocol           = SSHProtocol.SSH2;
            f.AuthenticationType = AuthenticationType.Password;
            f.WindowSize         = 0x1000;
#endif
            Reader reader = new Reader();
            Socket s      = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //s.Blocking = false;
            s.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.1"), 22));
            _conn        = SSHConnection.Connect(f, reader, s);
            reader._conn = _conn;
#if false   //Remote->Local
            _conn.ListenForwardedPort("0.0.0.0", 29472);
#elif false //Local->Remote
            SSHChannel ch = _conn.ForwardPort(reader, "www.yahoo.co.jp", 80, "localhost", 0);
            reader._pf = ch;
            while (!reader._ready)
            {
                System.Threading.Thread.Sleep(100);
            }
            reader._pf.Transmit(Encoding.ASCII.GetBytes("GET / HTTP/1.0\r\n\r\n"));
#elif false //SSH over SSH
            f.Password = "******";
            SSHConnection con2 = _conn.OpenPortForwardedAnotherConnection(f, reader, "kuromatsu", 22);
            reader._conn = con2;
            SSHChannel ch = con2.OpenShell(reader);
            reader._pf = ch;
#else //normal shell
            SSHChannel ch = _conn.OpenShell(reader);
            reader._pf = ch;
#endif

            //Debug.WriteLine(_conn.ConnectionInfo.DumpHostKeyInKnownHostsStyle());
            SSHConnectionInfo ci = _conn.ConnectionInfo;

            Thread.Sleep(1000);
            //((SSH2Connection)_conn).ReexchangeKeys();

            byte[] b = new byte[1];
            while (true)
            {
                int input = System.Console.Read();

                b[0] = (byte)input;
                //Debug.WriteLine(input);
                reader._pf.Transmit(b);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Connect to remote host:port over SSH tunnel
        /// </summary>
        public void Connect(string host, int port)
        {
            try
            {
                //remember remote target
                m_RemoteTarget = new IPEndPoint(ResolveHost(host), port);

                //connect to SSH server
                m_Client.Connect(new IPEndPoint(m_RemoteTarget.Address, SSHServerPort));

                //get password from user
                var pass = ErlTransportPasswordSource.GetPassword(this, NodeName, SSHUserName);

                //set params
                var param = new SSHConnectionParameter();
                param.EventTracer        = this; //to receive detailed events
                param.UserName           = SSHUserName;
                param.Password           = pass;
                param.Protocol           = SSHProtocol.SSH2;
                param.AuthenticationType = (AuthenticationType)Enum.Parse(typeof(SSH.AuthenticationType), SSHAuthenticationType);

                if (param.AuthenticationType == AuthenticationType.PublicKey)
                {
                    param.IdentityFile = SSHPrivateKeyFilePath;
                }

                //former algorithm is given priority in the algorithm negotiation
                param.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.RSA, PublicKeyAlgorithm.DSA };
                param.PreferableCipherAlgorithms  = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES, CipherAlgorithm.AES192CTR, CipherAlgorithm.AES256CTR, CipherAlgorithm.AES128CTR };

                param.WindowSize = 0x1000; //this option is ignored with SSH1

                //Creating a new SSH connection over the underlying socket
                m_Connection = SSHConnection.Connect(param, this, m_Client);
                m_Connection.AutoDisconnect = true;
                m_IsChannelReady            = false;

                //Local->Remote port forwarding (we use localhost:0 as local port, because local port is not required for us, we will use this tunnel directly)
                m_Channel = m_Connection.ForwardPort(this, host, port, "localhost", 0);
                var deadLine = DateTime.Now.AddMilliseconds(SSHTunnelCreationTimeout);
                while (!m_IsChannelReady && deadLine > DateTime.Now)
                {
                    System.Threading.Thread.Sleep(50); //wait response
                }
                //if timeouted - throw exception
                if (!m_IsChannelReady && deadLine < DateTime.Now)
                {
                    throw new ErlException(ERL_CREATE_SSH_TUNNEL_ERROR);
                }

                //create network stream
                m_Stream = new SshTunnelStream(m_Channel);

                //Remote->Local
                // if you want to listen to a port on the SSH server, follow this line:
                //_conn.ListenForwardedPort("0.0.0.0", 10000);

                //NOTE: if you use SSH2, dynamic key exchange feature is supported.
                //((SSH2Connection)_conn).ReexchangeKeys();
            }
            catch (Exception ex)
            {
                OnTrace(ErlTraceLevel.Ctrl, Direction.Inbound, ex.Message);
                throw;
            }
        }