public Protocol() { _params = new SSHConnectionParameter(); _params.KeyCheck = delegate(SSHConnectionInfo info) { //byte[] h = info.HostKeyMD5FingerPrint(); //foreach(byte b in h) Debug.Write(String.Format("{0:x2} ", b)); return true; }; }
protected bool _execCmdWaitFlag; // wait response flag for sending exec command to server protected SSHConnection(SSHConnectionParameter param, AbstractGranadosSocket strm, ISSHConnectionEventReceiver receiver) { _param = (SSHConnectionParameter)param.Clone(); _stream = strm; _eventReceiver = receiver; _channel_collection = new ChannelCollection(); _autoDisconnect = true; _execCmd = null; _execCmdWaitFlag = true; }
protected override void Negotiate() { ITerminalParameter term = (ITerminalParameter)_destination.GetAdapter(typeof(ITerminalParameter)); ITCPParameter tcp = (ITCPParameter)_destination.GetAdapter(typeof(ITCPParameter)); SSHConnectionParameter con = new SSHConnectionParameter(); #if DEBUG // con.EventTracer = new SSHDebugTracer(); #endif con.Protocol = _destination.Method; con.CheckMACError = PEnv.Options.SSHCheckMAC; con.UserName = _destination.Account; con.Password = _destination.PasswordOrPassphrase; con.AuthenticationType = _destination.AuthenticationType; con.IdentityFile = _destination.IdentityFileName; con.TerminalWidth = term.InitialWidth; con.TerminalHeight = term.InitialHeight; con.TerminalName = term.TerminalType; con.WindowSize = PEnv.Options.SSHWindowSize; con.PreferableCipherAlgorithms = LocalSSHUtil.ParseCipherAlgorithm(PEnv.Options.CipherAlgorithmOrder); con.PreferableHostKeyAlgorithms = LocalSSHUtil.ParsePublicKeyAlgorithm(PEnv.Options.HostKeyAlgorithmOrder); con.AgentForward = _destination.AgentForward; if (ProtocolsPlugin.Instance.ProtocolOptions.LogSSHEvents) con.EventTracer = new SSHEventTracer(tcp.Destination); if (_keycheck != null) con.KeyCheck += new HostKeyCheckCallback(this.CheckKey); SSHTerminalConnection r = new SSHTerminalConnection(_destination); SSHConnection ssh = SSHConnection.Connect(con, r.ConnectionEventReceiver, _socket); if (ssh != null) { if (PEnv.Options.RetainsPassphrase && _destination.AuthenticationType != AuthenticationType.KeyboardInteractive) ProtocolsPlugin.Instance.PassphraseCache.Add(tcp.Destination, _destination.Account, _destination.PasswordOrPassphrase); //接続成功時のみセット //_destination.PasswordOrPassphrase = ""; 接続の複製のためにここで消さずに残しておく r.AttachTransmissionSide(ssh); r.UsingSocks = _socks != null; _result = r; } else { throw new IOException(PEnv.Strings.GetString("Message.SSHConnector.Cancelled")); } }
protected override void Negotiate() { SSHConnectionParameter con = new SSHConnectionParameter(_host, _port, SSHProtocol.SSH2, _profile.AuthType, _profile.SSHAccount, _password); con.IdentityFile = _profile.PrivateKeyFile; con.PreferableCipherAlgorithms = SSHUtil.ParseCipherAlgorithm(Env.Options.CipherAlgorithmOrder); con.PreferableHostKeyAlgorithms = SSHUtil.ParsePublicKeyAlgorithm(Env.Options.HostKeyAlgorithmOrder); con.WindowSize = Env.Options.SSHWindowSize; con.CheckMACError = Env.Options.SSHCheckMAC; if (_keycheck != null) con.VerifySSHHostKey = this.CheckKey; _result = ChannelFactory.Create(_profile); ISSHConnection c = SSHConnection.Connect( _socket, con, sshconn => _result, null); if (c != null) { /* if(_profile.ProtocolType==ProtocolType.Udp) OpenUdpDestination(c, (UdpChannelFactory)_result); else */ _result.FixConnection(c); if (Env.Options.RetainsPassphrase) _profile.Passphrase = _password; //接続成功時のみセット } else { throw new IOException(Env.Strings.GetString("Message.ConnectionManager.ConnectionCancelled")); } }
private void SendMyVersion(SSHConnectionParameter param) { string cv = SSHUtil.ClientVersionString(param.Protocol); string cv2 = cv + param.VersionEOL; byte[] data = Encoding.ASCII.GetBytes(cv2); _stream.Write(data, 0, data.Length); TraceTransmissionEvent("client version-string", cv); }
private static SSHConnection ConnectMain(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, VersionExchangeHandler pnh, AbstractGranadosSocket s) { DataFragment data = pnh.WaitResponse(); string sv = pnh.ServerVersion; SSHConnection con = null; if (param.Protocol == SSHProtocol.SSH1) con = new SSH1Connection(param, s, receiver, sv, SSHUtil.ClientVersionString(param.Protocol)); else con = new SSH2Connection(param, s, receiver, sv, SSHUtil.ClientVersionString(param.Protocol)); con.TraceReceptionEvent("server version-string", sv.Trim()); pnh.Close(); s.SetHandler(con.PacketBuilder); con.SendMyVersion(param); if (con.Connect() != AuthenticationResult.Failure) { return con; } else { s.Close(); return null; } }
/** * opens another SSH connection via port-forwarded connection */ public SSHConnection OpenPortForwardedAnotherConnection(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, string host, int port) { ChannelSocket s = new ChannelSocket(null); SSHChannel ch = ForwardPort(s, host, port, "localhost", 0); s.SSHChennal = ch; VersionExchangeHandler pnh = new VersionExchangeHandler(param, s); s.SetHandler(pnh); return ConnectMain(param, receiver, pnh, s); }
/** * open a new SSH connection via the .NET socket */ public static SSHConnection Connect(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, Socket underlying_socket) { if (param.UserName == null) throw new InvalidOperationException("UserName property is not set"); if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null) throw new InvalidOperationException("Password property is not set"); PlainSocket s = new PlainSocket(underlying_socket, null); VersionExchangeHandler pnh = new VersionExchangeHandler(param, s); s.SetHandler(pnh); s.RepeatAsyncRead(); return ConnectMain(param, receiver, pnh, s); }
/// <summary> /// Establish a SSH connection /// </summary> /// <param name="socket">TCP socket which is already connected to the server.</param> /// <param name="param">SSH connection parameter</param> /// <param name="connectionEventHandlerCreator">a factory function to create a connection event handler (can be null)</param> /// <param name="protocolEventLoggerCreator">a factory function to create a protocol log event handler (can be null)</param> /// <returns>new connection object</returns> public static ISSHConnection Connect( Socket socket, SSHConnectionParameter param, Func <ISSHConnection, ISSHConnectionEventHandler> connectionEventHandlerCreator = null, Func <ISSHConnection, ISSHProtocolEventLogger> protocolEventLoggerCreator = null) { if (socket == null) { throw new ArgumentNullException("socket"); } if (param == null) { throw new ArgumentNullException("param"); } if (!socket.Connected) { throw new ArgumentException("socket is not connected to the remote host", "socket"); } if (param.UserName == null) { throw new ArgumentException("UserName property is not set", "param"); } if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null) { throw new ArgumentException("Password property is not set", "param"); } string clientVersion = SSHUtil.ClientVersionString(param.Protocol); PlainSocket psocket = new PlainSocket(socket, null); try { // receive protocol version string SSHProtocolVersionReceiver protoVerReceiver = new SSHProtocolVersionReceiver(); protoVerReceiver.Receive(psocket, 5000); // verify the version string protoVerReceiver.Verify(param.Protocol); ISSHConnection sshConnection; if (param.Protocol == SSHProtocol.SSH1) { // create a connection object var con = new SSH1Connection( psocket, param, protoVerReceiver.ServerVersion, clientVersion, connectionEventHandlerCreator, protocolEventLoggerCreator); // start receiving loop psocket.RepeatAsyncRead(); // send client version con.SendMyVersion(); // establish a SSH connection con.Connect(); sshConnection = con; } else { // create a connection object var con = new SSH2Connection( psocket, param, protoVerReceiver.ServerVersion, clientVersion, connectionEventHandlerCreator, protocolEventLoggerCreator); // start receiving loop psocket.RepeatAsyncRead(); // send client version con.SendMyVersion(); // establish a SSH connection con.Connect(); sshConnection = con; } return(sshConnection); } catch (Exception) { psocket.Close(); throw; } }
/** * open a new SSH connection via the .NET socket */ public static SSHConnection Connect(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, Socket underlying_socket) { if (param.UserName == null) throw new InvalidOperationException("UserName property is not set"); if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null) throw new InvalidOperationException("Password property is not set"); PlainSocket s = new PlainSocket(underlying_socket, null); try { SSHProtocolVersionReceiver protoVerReceiver = new SSHProtocolVersionReceiver(); protoVerReceiver.Receive(s, 5000); protoVerReceiver.Verify(param.Protocol); SSHConnection con; if (param.Protocol == SSHProtocol.SSH1) con = new SSH1Connection(param, s, receiver, protoVerReceiver.ServerVersion, SSHUtil.ClientVersionString(param.Protocol)); else con = new SSH2Connection(param, s, receiver, protoVerReceiver.ServerVersion, SSHUtil.ClientVersionString(param.Protocol)); s.SetHandler(con.Packetizer); s.RepeatAsyncRead(); con.SendMyVersion(param); if (con.Connect() == AuthenticationResult.Failure) { s.Close(); return null; } return con; } catch (Exception) { s.Close(); throw; } }
public object Clone() { SSHConnectionParameter n = new SSHConnectionParameter(); n._authtype = _authtype; n._cipherAlgorithms = _cipherAlgorithms; n._height = _height; n._hostkeyAlgorithms = _hostkeyAlgorithms; n._identityFile = _identityFile; n._keycheck = _keycheck; n._maxpacketsize = _maxpacketsize; n._password = _password; n._protocol = _protocol; n._terminalname = _terminalname; n._username = _username; n._width = _width; n._windowsize = _windowsize; n._checkMACError = _checkMACError; n._tracer = _tracer; n._agentForward = _agentForward; return n; }
/// <summary> /// Establish a SSH connection /// </summary> /// <param name="socket">TCP socket which is already connected to the server.</param> /// <param name="param">SSH connection parameter</param> /// <param name="connectionEventHandlerCreator">a factory function to create a connection event handler (can be null)</param> /// <param name="protocolEventLoggerCreator">a factory function to create a protocol log event handler (can be null)</param> /// <returns>new connection object</returns> public static ISSHConnection Connect( Socket socket, SSHConnectionParameter param, Func<ISSHConnection, ISSHConnectionEventHandler> connectionEventHandlerCreator = null, Func<ISSHConnection, ISSHProtocolEventLogger> protocolEventLoggerCreator = null) { if (socket == null) { throw new ArgumentNullException("socket"); } if (param == null) { throw new ArgumentNullException("param"); } if (!socket.Connected) { throw new ArgumentException("socket is not connected to the remote host", "socket"); } if (param.UserName == null) { throw new ArgumentException("UserName property is not set", "param"); } if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null) { throw new ArgumentException("Password property is not set", "param"); } string clientVersion = SSHUtil.ClientVersionString(param.Protocol); PlainSocket psocket = new PlainSocket(socket, null); try { // receive protocol version string SSHProtocolVersionReceiver protoVerReceiver = new SSHProtocolVersionReceiver(); protoVerReceiver.Receive(psocket, 5000); // verify the version string protoVerReceiver.Verify(param.Protocol); ISSHConnection sshConnection; if (param.Protocol == SSHProtocol.SSH1) { // create a connection object var con = new SSH1Connection( psocket, param, protoVerReceiver.ServerVersion, clientVersion, connectionEventHandlerCreator, protocolEventLoggerCreator); // start receiving loop psocket.RepeatAsyncRead(); // send client version con.SendMyVersion(); // establish a SSH connection con.Connect(); sshConnection = con; } else { // create a connection object var con = new SSH2Connection( psocket, param, protoVerReceiver.ServerVersion, clientVersion, connectionEventHandlerCreator, protocolEventLoggerCreator); // start receiving loop psocket.RepeatAsyncRead(); // send client version con.SendMyVersion(); // establish a SSH connection con.Connect(); sshConnection = con; } return sshConnection; } catch (Exception) { psocket.Close(); throw; } }
protected override void Negotiate() { SSHConnectionParameter con = new SSHConnectionParameter(); con.Protocol = SSHProtocol.SSH2; con.UserName = _profile.SSHAccount; con.Password = _password; con.AuthenticationType = _profile.AuthType; con.IdentityFile = _profile.PrivateKeyFile; con.PreferableCipherAlgorithms = SSHUtil.ParseCipherAlgorithm(Env.Options.CipherAlgorithmOrder); con.PreferableHostKeyAlgorithms = SSHUtil.ParsePublicKeyAlgorithm(Env.Options.HostKeyAlgorithmOrder); con.WindowSize = Env.Options.SSHWindowSize; con.CheckMACError = Env.Options.SSHCheckMAC; if (_keycheck != null) con.KeyCheck += new HostKeyCheckCallback(this.CheckKey); _result = ChannelFactory.Create(_profile); SSHConnection c = SSHConnection.Connect(con, _result, _socket); c.AutoDisconnect = false; if (c != null) { /* if(_profile.ProtocolType==ProtocolType.Udp) OpenUdpDestination(c, (UdpChannelFactory)_result); else */ _result.FixConnection(c); if (Env.Options.RetainsPassphrase) _profile.Passphrase = _password; //接続成功時のみセット } else { throw new IOException(Env.Strings.GetString("Message.ConnectionManager.ConnectionCancelled")); } }
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); } }
protected override void Negotiate() { ITerminalParameter term = (ITerminalParameter)_destination.GetAdapter(typeof(ITerminalParameter)); ITCPParameter tcp = (ITCPParameter)_destination.GetAdapter(typeof(ITCPParameter)); SSHTerminalConnection terminalConnection = new SSHTerminalConnection(_destination); SSHConnectionParameter con = new SSHConnectionParameter( tcp.Destination, tcp.Port, _destination.Method, _destination.AuthenticationType, _destination.Account, _destination.PasswordOrPassphrase); #if DEBUG // con.EventTracer = new SSHDebugTracer(); #endif con.Protocol = _destination.Method; con.CheckMACError = PEnv.Options.SSHCheckMAC; con.UserName = _destination.Account; con.Password = _destination.PasswordOrPassphrase; con.AuthenticationType = _destination.AuthenticationType; con.IdentityFile = _destination.IdentityFileName; con.TerminalWidth = term.InitialWidth; con.TerminalHeight = term.InitialHeight; con.TerminalName = term.TerminalType; con.WindowSize = PEnv.Options.SSHWindowSize; con.PreferableCipherAlgorithms = LocalSSHUtil.AppendMissingCipherAlgorithm( LocalSSHUtil.ParseCipherAlgorithm(PEnv.Options.CipherAlgorithmOrder)); con.PreferableHostKeyAlgorithms = LocalSSHUtil.AppendMissingPublicKeyAlgorithm( LocalSSHUtil.ParsePublicKeyAlgorithm(PEnv.Options.HostKeyAlgorithmOrder)); con.AgentForwardingAuthKeyProvider = _destination.AgentForwardingAuthKeyProvider; con.X11ForwardingParams = _destination.X11Forwarding; if (_keycheck != null) { con.VerifySSHHostKey = (info) => { return _keycheck.Vefiry(info); }; } con.KeyboardInteractiveAuthenticationHandlerCreator = sshconn => terminalConnection.GetKeyboardInteractiveAuthenticationHandler(); ISSHProtocolEventLogger protocolEventLogger; if (ProtocolsPlugin.Instance.ProtocolOptions.LogSSHEvents) { protocolEventLogger = new SSHEventTracer(tcp.Destination); } else { protocolEventLogger = null; } var connection = SSHConnection.Connect(_socket, con, sshconn => terminalConnection.ConnectionEventReceiver, sshconn => protocolEventLogger); if (PEnv.Options.RetainsPassphrase && _destination.AuthenticationType != AuthenticationType.KeyboardInteractive) { ProtocolsPlugin.Instance.PassphraseCache.Add(tcp.Destination, _destination.Account, _destination.PasswordOrPassphrase); //接続成功時のみセット } //_destination.PasswordOrPassphrase = ""; 接続の複製のためにここで消さずに残しておく terminalConnection.AttachTransmissionSide(connection, connection.AuthenticationStatus); _result = terminalConnection; }
public void setTerminalParams(string type, int rows, int cols) { _params = new SSHConnectionParameter(); TerminalName = type; TerminalHeight = rows; TerminalWidth = cols; }