protected override void OnLoad(SshDataWorker reader) { ReasonCode = (DisconnectReason)reader.ReadUInt32(); Description = reader.ReadString(Encoding.UTF8); if (reader.DataAvailable >= 4) Language = reader.ReadString(Encoding.UTF8); }
protected override void OnLoad(SshDataWorker reader) { ReasonCode = (DisconnectReason)reader.ReadUInt32(); Description = reader.ReadString(Encoding.UTF8); if (reader.DataAvailable >= 4) { Language = reader.ReadString(Encoding.UTF8); } }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); this.Terminal = reader.ReadString(Encoding.ASCII); this.TerminalWidthCharacters = reader.ReadUInt32(); this.TerminalHeightRows = reader.ReadUInt32(); this.TerminalWidthPixels = reader.ReadUInt32(); this.TerminalHeightPixels = reader.ReadUInt32(); this.EncodedTerminalModes = reader.ReadString(Encoding.ASCII); }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); Terminal = reader.ReadString(Encoding.ASCII); widthChars = reader.ReadUInt32(); heightRows = reader.ReadUInt32(); widthPx = reader.ReadUInt32(); heightPx = reader.ReadUInt32(); modes = reader.ReadString(Encoding.ASCII); }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); if (ChannelType != "forwarded-tcpip") { throw new ArgumentException(string.Format("Channel type {0} is not valid.", ChannelType)); } Address = reader.ReadString(Encoding.ASCII); Port = reader.ReadUInt32(); OriginatorIPAddress = reader.ReadString(Encoding.ASCII); OriginatorPort = reader.ReadUInt32(); }
protected override void OnLoad(SshDataWorker reader) { Cookie = reader.ReadBinary(16); KeyExchangeAlgorithms = reader.ReadString(Encoding.ASCII).Split(','); ServerHostKeyAlgorithms = reader.ReadString(Encoding.ASCII).Split(','); EncryptionAlgorithmsClientToServer = reader.ReadString(Encoding.ASCII).Split(','); EncryptionAlgorithmsServerToClient = reader.ReadString(Encoding.ASCII).Split(','); MacAlgorithmsClientToServer = reader.ReadString(Encoding.ASCII).Split(','); MacAlgorithmsServerToClient = reader.ReadString(Encoding.ASCII).Split(','); CompressionAlgorithmsClientToServer = reader.ReadString(Encoding.ASCII).Split(','); CompressionAlgorithmsServerToClient = reader.ReadString(Encoding.ASCII).Split(','); LanguagesClientToServer = reader.ReadString(Encoding.ASCII).Split(','); LanguagesServerToClient = reader.ReadString(Encoding.ASCII).Split(','); FirstKexPacketFollows = reader.ReadBoolean(); Reserved = reader.ReadUInt32(); }
protected override void OnLoad(SshDataWorker reader) { ChannelType = reader.ReadString(Encoding.ASCII); SenderChannel = reader.ReadUInt32(); InitialWindowSize = reader.ReadUInt32(); MaximumPacketSize = reader.ReadUInt32(); }
public byte[] GetSignature(byte[] signatureData) { Contract.Requires(signatureData != null); using (var worker = new SshDataWorker(signatureData)) { if (worker.ReadString(Encoding.ASCII) != this.Name) throw new CryptographicException("Signature was not created with this algorithm."); var signature = worker.ReadBinary(); return signature; } }
protected override void OnLoad(SshDataWorker reader) { /* * byte SSH_MSG_CHANNEL_REQUEST * uint32 recipient channel * string "subsystem" * boolean want reply * string subsystem name */ base.OnLoad(reader); SubsystemName = reader.ReadString(Encoding.ASCII); }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); if (MethodName != "password") { throw new ArgumentException(string.Format("Method name {0} is not valid.", MethodName)); } var isFalse = reader.ReadBoolean(); Password = reader.ReadString(Encoding.ASCII); }
public override void LoadKeyAndCertificatesData(byte[] data) { using (var worker = new SshDataWorker(data)) { if (worker.ReadString(Encoding.ASCII) != this.Name) throw new CryptographicException("Key and certificates were not created with this algorithm."); var args = new RSAParameters(); args.Exponent = worker.ReadMpint(); args.Modulus = worker.ReadMpint(); _algorithm.ImportParameters(args); } }
public byte[] GetSignature(byte[] signatureData) { Contract.Requires(signatureData != null); using (var worker = new SshDataWorker(signatureData)) { if (worker.ReadString(Encoding.ASCII) != this.Name) { throw new CryptographicException("Signature was not created with this algorithm."); } var signature = worker.ReadBinary(); return(signature); } }
public override void LoadKeyAndCertificatesData(byte[] data) { using (var worker = new SshDataWorker(data)) { if (worker.ReadString(Encoding.ASCII) != this.Name) { throw new CryptographicException("Key and certificates were not created with this algorithm."); } var args = new RSAParameters(); args.Exponent = worker.ReadMpint(); args.Modulus = worker.ReadMpint(); _algorithm.ImportParameters(args); } }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); if (MethodName != "publickey") throw new ArgumentException(string.Format("Method name {0} is not valid.", MethodName)); HasSignature = reader.ReadBoolean(); KeyAlgorithmName = reader.ReadString(Encoding.ASCII); PublicKey = reader.ReadBinary(); if (HasSignature) { Signature = reader.ReadBinary(); PayloadWithoutSignature = RawBytes.Take(RawBytes.Length - Signature.Length - 5).ToArray(); } }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); if (MethodName != "publickey") { throw new ArgumentException(string.Format("Method name {0} is not valid.", MethodName)); } HasSignature = reader.ReadBoolean(); KeyAlgorithmName = reader.ReadString(Encoding.ASCII); PublicKey = reader.ReadBinary(); if (HasSignature) { Signature = reader.ReadBinary(); PayloadWithoutSignature = RawBytes.Take(RawBytes.Length - Signature.Length - 5).ToArray(); } }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); if (this.MethodName != "publickey") { throw new ArgumentException($"Method name {this.MethodName} is not valid."); } this.HasSignature = reader.ReadBoolean(); this.KeyAlgorithmName = reader.ReadString(Encoding.ASCII); this.PublicKey = reader.ReadBinary(); if (!this.HasSignature) { return; } this.Signature = reader.ReadBinary(); this.PayloadWithoutSignature = this.RawBytes.Take(this.RawBytes.Length - this.Signature.Length - 5).ToArray(); }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); this.Address = reader.ReadString(Encoding.UTF8); this.Port = reader.ReadUInt32(); }
protected override void OnLoad(SshDataWorker reader) { ServiceName = reader.ReadString(Encoding.ASCII); }
protected override void OnLoad(SshDataWorker reader) { this.RecipientChannel = reader.ReadUInt32(); this.RequestType = reader.ReadString(Encoding.ASCII); this.WantReply = reader.ReadBoolean(); }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); Name = reader.ReadString(Encoding.ASCII); }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); Command = reader.ReadString(Encoding.ASCII); }
protected override void OnLoad(SshDataWorker reader) { Username = reader.ReadString(Encoding.UTF8); ServiceName = reader.ReadString(Encoding.ASCII); MethodName = reader.ReadString(Encoding.ASCII); }
protected override void OnLoad(SshDataWorker reader) { RecipientChannel = reader.ReadUInt32(); RequestType = reader.ReadString(Encoding.ASCII); WantReply = reader.ReadBoolean(); }
protected override void OnLoad(SshDataWorker reader) { base.OnLoad(reader); reader.ReadBoolean(); this.Password = reader.ReadString(Encoding.UTF8); }
internal void OnInput(byte[] ee) { var input = Encoding.ASCII.GetString(ee); //uint32 length //byte type //byte[length - 1] data payload SshDataWorker reader = new SshDataWorker(ee); SshDataWorker writer = new SshDataWorker(); if (reader.DataAvailable >= 5) { var msglength = reader.ReadUInt32(); var msgtype = (RequestPacketType)(int)reader.ReadByte(); uint requestId = 0; string path = ""; string absolutepath = ""; string handle = Guid.NewGuid().ToString().Replace("-", ""); switch (msgtype) { case RequestPacketType.SSH_FXP_INIT: var sftpclientversion = reader.ReadUInt32(); writer.Write((byte)RequestPacketType.SSH_FXP_VERSION); var version = Math.Max(3, sftpclientversion); writer.Write((uint)version); // SFTP protocol version SendPacket(writer.ToByteArray()); break; case RequestPacketType.SSH_FXP_REALPATH: requestId = reader.ReadUInt32(); path = reader.ReadString(Encoding.UTF8); // List files in root directory for this user // foreach file create a response message if (path == ".") { path = "/"; // replace with current filepath } writer.Write((byte)RequestPacketType.SSH_FXP_NAME); writer.Write((uint)requestId); writer.Write((uint)1); // Dummy file for SSH_FXP_REALPATH request writer.Write(path, Encoding.UTF8); writer.Write(@"-rwxr-xr-x 1 mjos staff 348911 Mar 25 14:29 " + path, Encoding.UTF8); writer.Write((uint)requestId); writer.Write(uint.MaxValue); // flags writer.Write((ulong)0); // size writer.Write(uint.MaxValue); // uid writer.Write(uint.MaxValue); // gid writer.Write(uint.MaxValue); // permissions writer.Write(GetUnixFileTime(DateTime.Now)); //atime writer.Write(GetUnixFileTime(DateTime.Now)); //mtime writer.Write((uint)0); // extended_count //string extended_type blank //string extended_data blank SendPacket(writer.ToByteArray()); break; case RequestPacketType.SSH_FXP_READDIR: requestId = reader.ReadUInt32(); handle = reader.ReadString(Encoding.UTF8); if (HandleToPathDictionary.ContainsKey(handle)) // remove after handle is used first time { var relativepath = HandleToPathDictionary[handle]; absolutepath = UserRootDirectory + relativepath; System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(absolutepath); var allfiles = di.GetFiles(); // returns SSH_FXP_NAME or SSH_FXP_STATUS with SSH_FX_EOF writer.Write((byte)RequestPacketType.SSH_FXP_NAME); writer.Write((uint)requestId); writer.Write((uint)allfiles.Count()); // all files at the same time foreach (var file in allfiles) { writer.Write(GetFileWithAttributes(file)); } SendPacket(writer.ToByteArray()); HandleToPathDictionary.Remove(handle); // remove will return EOF next time } else { // return SSH_FXP_STATUS indicating SSH_FX_EOF when all files have been listed writer.Write((byte)RequestPacketType.SSH_FXP_STATUS); writer.Write((uint)requestId); writer.Write((uint)SftpStatusType.SSH_FX_EOF); // status code //writer.Write("", Encoding.UTF8); //writer.Write("", Encoding.UTF8); SendPacket(writer.ToByteArray()); } break; case RequestPacketType.SSH_FXP_OPENDIR: requestId = reader.ReadUInt32(); path = reader.ReadString(Encoding.UTF8); HandleToPathDictionary.Add(handle, path); writer.Write((byte)RequestPacketType.SSH_FXP_HANDLE); writer.Write((uint)requestId); writer.Write(handle, Encoding.UTF8); // returns SSH_FXP_HANDLE on success or a SSH_FXP_STATUS message on fail SendPacket(writer.ToByteArray()); _logger.LogInformation($"sftp command {msgtype} request Id: \"{requestId}\". path: {path} on channel: {channel}"); break; case RequestPacketType.SSH_FXP_STAT: // follows symbolic links case RequestPacketType.SSH_FXP_LSTAT: // does not follow symbolic links requestId = reader.ReadUInt32(); path = reader.ReadString(Encoding.UTF8); SendAttributes(requestId, path); break; case RequestPacketType.SSH_FXP_FSTAT: // SSH_FXP_FSTAT differs from the others in that it returns status information for an open file(identified by the file handle). requestId = reader.ReadUInt32(); handle = reader.ReadString(Encoding.UTF8); if (HandleToPathDictionary.ContainsKey(handle)) { path = HandleToPathDictionary[handle]; SendAttributes(requestId, path); } else { SendStatus(requestId, SftpStatusType.SSH_FX_NO_SUCH_FILE); } break; case RequestPacketType.SSH_FXP_CLOSE: requestId = reader.ReadUInt32(); handle = reader.ReadString(Encoding.UTF8); if (HandleToPathDictionary.ContainsKey(handle)) { HandleToPathDictionary.Remove(handle); } SendStatus(requestId, SftpStatusType.SSH_FX_OK); break; case RequestPacketType.SSH_FXP_OPEN: requestId = reader.ReadUInt32(); //uint32 request-id path = reader.ReadString(Encoding.UTF8); // //string filename [UTF-8] HandleToPathDictionary.Add(handle, path); var desired_access = reader.ReadUInt32(); var flags = reader.ReadUInt32(); //uint32 desired-access var write = desired_access & (uint)FileSystemOperation.Write; var read = desired_access & (uint)FileSystemOperation.Read; var create = desired_access & (uint)FileSystemOperation.Create; //uint32 flags //ATTRS attrs absolutepath = UserRootDirectory + path; System.IO.FileInfo fi = new System.IO.FileInfo(absolutepath); if (read > 0 && write == 0 && create == 0) { try { var fs = fi.OpenRead(); HandleToFileStreamDictionary.Add(handle, fs); } catch { SendStatus(requestId, SftpStatusType.SSH_FX_PERMISSION_DENIED); } } writer.Write((byte)RequestPacketType.SSH_FXP_HANDLE); writer.Write((uint)requestId); writer.Write(handle, Encoding.UTF8); // returns SSH_FXP_HANDLE on success or a SSH_FXP_STATUS message on fail SendPacket(writer.ToByteArray()); break; case RequestPacketType.SSH_FXP_READ: requestId = reader.ReadUInt32(); //uint32 request-id handle = reader.ReadString(Encoding.UTF8); if (HandleToPathDictionary.ContainsKey(handle)) { var fs = HandleToFileStreamDictionary[handle]; var offset = (long)reader.ReadUInt64(); var length = (int)reader.ReadUInt32(); var buffer = new byte[length]; if (fs.Length - offset < 0) // EOF already reached { SendStatus((uint)requestId, SftpStatusType.SSH_FX_EOF); break; } writer.Write((byte)RequestPacketType.SSH_FXP_DATA); writer.Write((uint)requestId); if (fs.Length - offset < length) { buffer = new byte[fs.Length - offset]; } fs.Seek(offset, SeekOrigin.Begin); fs.Read(buffer); writer.WriteBinary(buffer); SendPacket(writer.ToByteArray()); } else { // send invalid handle: SSH_FX_INVALID_HANDLE } break; default: // unsupported command _logger.LogWarning($"Unsupported sftp command {msgtype.ToString()} input: \"{input}\". on channel: {channel}"); SendStatus(requestId, SftpStatusType.SSH_FX_OP_UNSUPPORTED); break; } } }
protected override void OnLoad(SshDataWorker reader) { this.RequestType = reader.ReadString(Encoding.UTF8); this.WantReply = reader.ReadBoolean(); }