コード例 #1
0
ファイル: SshBot.cs プロジェクト: dusty3ntity/certpot
        public void AuthenticateWithKey(string username, string key, string password = null)
        {
            if (!_client.IsConnected)
            {
                throw new Exception("Client is not connected");
            }

            var sshKey = new Chilkat.SshKey();

            if (password != null)
            {
                sshKey.Password = password;
            }

            if (!sshKey.FromOpenSshPrivateKey(key))
            {
                throw new SshException(ErrorType.SshKeyParsingError, _client.LastErrorText);
            }

            if (!_client.AuthenticatePk(username, sshKey))
            {
                throw new SshException(ErrorType.SshAuthenticationError, _client.LastErrorText);
            }

            IsAuthenticated = true;
        }
コード例 #2
0
ファイル: Utils.cs プロジェクト: junaid-ashraf/Prb.WinService
        public static Chilkat.SshKey RsaKeyPair()
        {
            Chilkat.SshKey key = new Chilkat.SshKey();

            bool success;
            int  numBits;
            int  exponent;

            numBits  = 2048;
            exponent = 65537;
            success  = key.GenerateRsaKey(numBits, exponent);
            //var sshKeyPair = new SshKeyPair();

            key.ToPuttyPrivateKey(false);
            if (!success)
            {
                RsaKeyPair();
            }
            return(key);
        }
コード例 #3
0
        public static void publickeyAuth()
        {
            Chilkat.Ssh ssh = new Chilkat.Ssh();

            //  Any string automatically begins a fully-functional 30-day trial.
            bool success = ssh.UnlockComponent("Anything for 30-day trial");

            if (success != true)
            {
                Console.WriteLine(ssh.LastErrorText);
                return;
            }

            //  Set some timeouts, in milliseconds:
            ssh.ConnectTimeoutMs = 5000;
            ssh.IdleTimeoutMs    = 15000;

            //  Connect to the SSH server.
            //  The standard SSH port = 22
            //  The hostname may be a hostname or IP address.
            int    port;
            string hostname;

            hostname = "172.24.16.155";
            port     = 22;
            success  = ssh.Connect(hostname, port);
            if (success != true)
            {
                Console.WriteLine(ssh.LastErrorText);
                return;
            }

            Chilkat.SshKey key = new Chilkat.SshKey();

            //  Read the PEM file into a string variable:
            //  (This does not load the PEM file into the key.  The LoadText
            //  method is a convenience method for loading the full contents of ANY text
            //  file into a string variable.)
            string privKey = key.LoadText("private.ppk");

            if (key.LastMethodSuccess != true)
            {
                Console.WriteLine(key.LastErrorText);
                return;
            }

            //  Load a private key from a PEM string:
            //  (Private keys may be loaded from OpenSSH and Putty formats.
            //  Both encrypted and unencrypted private key file formats
            //  are supported.  This example loads an unencrypted private
            //  key in OpenSSH format.  PuTTY keys typically use the .ppk
            //  file extension, while OpenSSH keys use the PEM format.
            //  (For PuTTY keys, call FromPuttyPrivateKey instead.)
            success = key.FromOpenSshPrivateKey(privKey);
            if (success != true)
            {
                Console.WriteLine(key.LastErrorText);
                return;
            }

            //  Authenticate with the SSH server using the login and
            //  private key.  (The corresponding public key should've
            //  been installed on the SSH server beforehand.)
            success = ssh.AuthenticatePk("myLogin", key);
            if (success != true)
            {
                Console.WriteLine(ssh.LastErrorText);
                return;
            }

            Console.WriteLine(ssh.LastErrorText);
            Console.WriteLine("Public-Key Authentication Successful!");
        }
コード例 #4
0
        public static void puttykeyAuth(int serverPort)
        {
            Chilkat.Ssh ssh = new Chilkat.Ssh();

            bool success = ssh.UnlockComponent("Anything for 30-day trial.");

            if (success != true)
            {
                Console.WriteLine(ssh.LastErrorText);
                return;
            }

            //  Load a .ppk PuTTY private key.
            Chilkat.SshKey puttyKey = new Chilkat.SshKey();
            string         ppkText  = puttyKey.LoadText("private_key.ppk");

            //  The ppkText contains this content:

            //      PuTTY-User-Key-File-2: ssh-rsa
            //      Encryption: aes256-cbc
            //      Comment: rsa-key-20170126
            //      Public-Lines: 6
            //      AAAAB3NzaC1yc2EAAAABJQAAAQEAx+52s7vvaZ8rT2UdFZWlSUVDHDQ+5ZRFvgRW
            //      6nm2sO1c9WqNg7u2PQL4jeKSDX2XWcMnpleALz2x8Rr4rMy5E1iZzvWov8VtFd8l
            //      fa9HOkgEeJB3VFuYR3NlnD3eyCoYJYPVpHJHrIeui2WZs5vQ76HDe+th8+z5Ald4
            //      zPw3p2c6ZJpBrkSBM67hWokoBDi23c7NhszDHhJBrv+B98cQxnagI1PUKqN7E8Vg
            //      bNtBI8beIMHyI69up9G1AXSEi0cGIjYNx9RNUPau1mRk/SvfqxgWkAjM005lj7hc
            //      bOsjbdKK3T2NtrKTaYjEiXlEXcj1iGuApsD/m73pYaEJB3Nd7w==
            //      Private-Lines: 14
            //      MoaDbq0owouN/7Z9Pga0favDhM2bSEgMErJBxdDmNUXIVVcUoLiD/Ps1RA+BeBBX
            //      wxqKUt9PqLy/pnafPR/i2xjJiQtQ0CWkPxND16Gi1dqLzmbQYYl1ev4+LzuG0zNX
            //      HDGMvRiwagY7mY+F1tUjBYfOL6z8XHw4m40YcY1QorOO+0MMzAVT5Hkg8YyXW209
            //      B/V/LRADFMVA2BlL39y11cb5ZpFStPH/waYUMY+2w1ZmJZ7dcRoMjuKmY+YE/tUx
            //      n9X3P0qTNSbw6e6sMG3Dhr1vfoJUQWApUliD6GpUiCeIvXBcVqG8Vsfq9XADsPl0
            //      +nFAwjSZflywcB7/FwhGb7q5UmcJK06SzoMl7Og5e3g7NCs3yNNQIv+qCpDjhxrA
            //      hpT03mbipu7OXCZDeUwUhMGJAmYHE5iqm1rPCsSVbaMgpxhCWf01Cx4gLx3aMvn4
            //      MdylA31GuL3wSxcWTslrOI8+449lZN/qZEnGEZkYTrnlu123jTqsAWMMtuHSz2Ig
            //      6GA89oTdlppkNflhNH3OJ85kMUrc3p/ZBMdndz8jTDTljmJjHR5oNMoShFof115A
            //      nWjUHqBwCgcubLYyH3afDvBTOhtl0tJ9Oby0wJlOAGnCXiPSDbF/y7J7xml/PS9t
            //      XlSVNxtAY15NDO6Fp96sBVfKuJsfJ90PzdBom4ikIuf7sMwtElrHHLuYfcXJQYLp
            //      G5jBmqDgnirosVPEBIxlxFzz/HCRmdU+tsYg46gqI4R5UpKUe8WSaJoZkDGsrqhm
            //      e+1SJaBuafR4v2bx/bV414Hg7LGQosK2S3crxH4UgZl+g02vWznZfBH+9CmHvKDR
            //      AxfcKOTzsaILKJtQPV81lmJ45sARYMcB5jMiE4kBg56hiXouChsvKkm55WVcW1E+
            //      Private-MAC: 17512c9f7582c1d9c3ae2b01b4d67a6b1dbd1d0e

            //  "secret" is the actual password for the above PPK.

            puttyKey.Password = "******";
            success           = puttyKey.FromPuttyPrivateKey(ppkText);
            if (success != true)
            {
                Console.WriteLine(puttyKey.LastErrorText);
                return;
            }

            string sshHostname = "172.24.16.155";
            //string sshHostname = "172.24.19.59";
            int sshPort = 22;

            //  Connect to an SSH server
            success = ssh.Connect(sshHostname, sshPort);
            if (success != true)
            {
                Console.WriteLine(ssh.LastErrorText);
                return;
            }

            //  Authenticate with the SSH server using a username + private key.
            //  (The private key serves as the password.  The username identifies
            //  the SSH user account on the server.)
            success = ssh.AuthenticatePk("harinath", puttyKey);
            if (success != true)
            {
                Console.WriteLine(ssh.LastErrorText);
                return;
            }

            Console.WriteLine("OK, the connection and authentication with the SSH server is completed.");
        }
コード例 #5
0
        public void Open()
        {
            try
            {
                if (session != null && IsOpen())
                {
                    return;
                }

                bool success;

                session = new Chilkat.SshTunnel();

                success = session.UnlockComponent("ITAYHESSH_d674QVQunRnj");
                if (success != true)
                {
                    throw new DuradosException(session.LastErrorText);
                }

                //  The destination host/port is the database server.
                //  The DestHostname may be the domain name or
                //  IP address (in dotted decimal notation) of the database
                //  server.
                session.DestPort     = remotePort;
                session.DestHostname = "localhost";

                //  Provide information about the location of the SSH server,
                //  and the authentication to be used with it. This is the
                //  login information for the SSH server (not the database server).
                session.SshHostname = tunnel.RemoteHost;
                session.SshPort     = tunnel.Port;
                session.SshLogin    = tunnel.User;
                if (string.IsNullOrEmpty(tunnel.Password))
                {
                    Chilkat.SshKey sshKey = new Chilkat.SshKey();
                    //string sss = sshKey.LoadText(@"G:\Dev\Relly - new\Duradus.Web.Mvc.App\Modubiz2012.pem");
                    string keyText = tunnel.PrivateKey;
                    success = sshKey.FromOpenSshPrivateKey(keyText);
                    //string fff = sshKey.ToOpenSshPrivateKey(false);
                    success = session.SetSshAuthenticationKey(sshKey);
                    if (success)
                    {
                    }
                }
                else
                {
                    session.SshPassword = tunnel.Password;
                }

                //  Start accepting connections in a background thread.
                //  The SSH tunnels are autonomously run in a background
                //  thread.  There is one background thread for accepting
                //  connections, and another for managing the tunnel pool.
                int listenPort;
                listenPort = localPort;
                success    = session.BeginAccepting(listenPort);
                if (success != true)
                {
                    throw new DuradosException(session.LastErrorText);
                }

                //  At this point you may connect to the database server through
                //  the SSH tunnel.  Your database connection string would
                //  use "localhost" for the hostname and 3316 for the port.
                //  We're not going to show the database coding here,
                //  because it can vary depending on the API you're using
                //  (ADO, ODBC, OLE DB, etc. )
            }
            catch (Exception exception)
            {
                throw new DuradosException("Could not  open SSH tunnel", exception);
            }
        }