C
C#2mo ago
Marcig

ssh c# implementation exaplanation of some shenanigans

i have a couple of questions regarding this code, the first one being it reads 8 bytes from the socket, which contains the packet_length (4 bytes) and the padding_length (1 byte) why then is required to calculate the bytes to read, if the packetLength itself is the length of the packet in bytes, not including 'mac' or the 'packet_length' field itself., so, if that is the case why is it necessary to remove the blockSize + 4, and even then, why is this necessary, the socket.Receive() shouldn't offset the stream from the last position it was when it was read? `
uint bytesToRead = packetLength - blockSize + 4;
uint bytesToRead = packetLength - blockSize + 4;
and also this, why is needed to substract one also, like if packetLength is 7 and the paddingLength is 2, the payload length should be 5, althought this variable remains unused til later i guess
uint payloadLength = packetLength - paddingLength - 1;
uint payloadLength = packetLength - paddingLength - 1;
4 Replies
Marcig
MarcigOP2mo ago
this all comes from a golang perspective dev, im trying to implement this in golang, and i don't really know if some functions work the same way they do in go (specially the Receive) thanks ^^ src:https://github.com/TyrenDe/SSHServer/wiki/008:-Reading-a-Packet uint32 packet_length byte padding_length byte[n1] payload; n1 = packet_length - padding_length - 1 byte[n2] random padding; n2 = padding_length byte[m] mac (Message Authentication Code - MAC); m = mac_length
GitHub
008: Reading a Packet
This is a tutorial on how to build a basic SSH Server in C#, but you are welcome to try following in any language. - TyrenDe/SSHServer
Jimmacle
Jimmacle2mo ago
i suspect reading the spec directly will be easier in that case: https://datatracker.ietf.org/doc/html/rfc4253#section-6
IETF Datatracker
RFC 4253: The Secure Shell (SSH) Transport Layer Protocol
The Secure Shell (SSH) is a protocol for secure remote login and other secure network services over an insecure network. This document describes the SSH transport layer protocol, which typically runs on top of TCP/IP. The protocol can be used as a basis for a number of secure network services. It provides strong encryption, server authentication...
Jimmacle
Jimmacle2mo ago
then you don't have to worry about whether C# apis are equivalent to go apis or not
the socket.Receive() shouldn't offset the stream from the last position it was when it was read
why wouldn't it? your assumption is incorrect, it consumes the incoming data and transfers it to the buffer
Marcig
MarcigOP5w ago
i think i worded it wrongly (sleep and not eng speaker sry abt that) what i meant is, what i expect from the behavior Receive is this data = [1,2,3,4,5,6] first[2] second[2] third[2] receive(first) receive(second ) receive(third) print(first) -> [1,2] print(second) -> [3,4] print(third) -> [5,6]

Did you find this page helpful?