Documentation
¶
Overview ¶
Package sec is RDP security: the encryption of the session, and the licensing exchange that runs on top of it.
With TLS or NLA this is thin. With standard RDP security it is the whole protection of the session, and that means RC4 with a key derived from the client's random values and the server's certificate, plus a rule that is easy to miss: the encryption keys are replaced after every 4096 packets, and both sides must do it in step. A client that does not will work for a while and then produce garbage, which looks like anything but a key rotation.
The licensing PDUs travel encrypted even though the handshake that sets the encryption up is not finished when they are sent, so the flags on that first packet are their own small trap.
Index ¶
- Constants
- type Client
- func (c *Client) ClientCoreData() *gcc.ClientCoreData
- func (c *Client) ClientNetworkData() *gcc.ClientNetworkData
- func (c *Client) ClientSecurityData() *gcc.ClientSecurityData
- func (c *Client) RecvFastPath(secFlag byte, s []byte)
- func (c *Client) SendToChannel(channel string, b []byte) (int, error)
- func (c *Client) ServerSecurityData() *gcc.ServerSecurityData
- func (c *Client) SetAlternateShell(shell string)
- func (c *Client) SetChannelSender(f core.ChannelSender)
- func (c *Client) SetClientAutoReconnect(id uint32, random []byte)
- func (c *Client) SetDomain(domain string)
- func (c *Client) SetFastPathListener(f core.FastPathListener)
- func (c *Client) SetPwd(pwd string)
- func (c *Client) SetRequireVerifiedCertificate(require bool)
- func (c *Client) SetUser(user string)
- type ClientAutoReconnect
- type ClientSecurityExchangePDU
- type RDPExtendedInfo
- type RDPInfo
- type SEC
- type SecurityHeader
Constants ¶
const ( EXCHANGE_PKT uint16 = 0x0001 TRANSPORT_REQ = 0x0002 TRANSPORT_RSP = 0x0004 ENCRYPT = 0x0008 RESET_SEQNO = 0x0010 IGNORE_SEQNO = 0x0020 INFO_PKT = 0x0040 LICENSE_PKT = 0x0080 LICENSE_ENCRYPT_CS = 0x0200 LICENSE_ENCRYPT_SC = 0x0200 REDIRECTION_PKT = 0x0400 SECURE_CHECKSUM = 0x0800 AUTODETECT_REQ = 0x1000 AUTODETECT_RSP = 0x2000 HEARTBEAT = 0x4000 FLAGSHI_VALID = 0x8000 )
*
- SecurityFlag
- @see http://msdn.microsoft.com/en-us/library/cc240579.aspx
const ( INFO_MOUSE uint32 = 0x00000001 INFO_DISABLECTRLALTDEL = 0x00000002 INFO_AUTOLOGON = 0x00000008 INFO_UNICODE = 0x00000010 INFO_MAXIMIZESHELL = 0x00000020 INFO_LOGONNOTIFY = 0x00000040 INFO_COMPRESSION = 0x00000080 INFO_ENABLEWINDOWSKEY = 0x00000100 INFO_REMOTECONSOLEAUDIO = 0x00002000 INFO_FORCE_ENCRYPTED_CS_PDU = 0x00004000 INFO_RAIL = 0x00008000 INFO_LOGONERRORS = 0x00010000 INFO_MOUSE_HAS_WHEEL = 0x00020000 INFO_PASSWORD_IS_SC_PIN = 0x00040000 INFO_NOAUDIOPLAYBACK = 0x00080000 INFO_USING_SAVED_CREDS = 0x00100000 INFO_AUDIOCAPTURE = 0x00200000 INFO_VIDEO_DISABLE = 0x00400000 INFO_CompressionTypeMask = 0x00001E00 )
The client info flags, which say what the client can do and what the server should tell it about. They travel in the info PDU that follows the credentials.
INFO_UNICODE is the one that decides the character set for everything else in the PDU, including the credentials. INFO_MOUSE_HAS_WHEEL and INFO_FORCE_ENCRYPTED_CS_PDU are what a client asks for rather than what it reports, and INFO_CompressionTypeMask is a mask over three bits rather than a flag of its own.
const ( AF_INET uint16 = 0x00002 AF_INET6 = 0x0017 )
The address families for the client address in the extended info. They are the Windows values rather than the Unix ones, which is why INET is 2 here and not the 2 that happens to be the same on both.
const ( PERF_DISABLE_WALLPAPER uint32 = 0x00000001 PERF_DISABLE_FULLWINDOWDRAG = 0x00000002 PERF_DISABLE_MENUANIMATIONS = 0x00000004 PERF_DISABLE_THEMING = 0x00000008 PERF_DISABLE_CURSOR_SHADOW = 0x00000020 PERF_DISABLE_CURSORSETTINGS = 0x00000040 PERF_ENABLE_FONT_SMOOTHING = 0x00000080 PERF_ENABLE_DESKTOP_COMPOSITION = 0x00000100 )
The performance flags, which ask the server to leave out the things that make a session slow over a thin connection. Each one removes something from what the server draws, so they trade appearance for bandwidth, and all of them being clear is what a local connection wants.
const ( FASTPATH_OUTPUT_SECURE_CHECKSUM = 0x1 FASTPATH_OUTPUT_ENCRYPTED = 0x2 )
The security flags in a fast-path PDU's action byte, which are read from the top two bits and not from a separate field. SECURE_CHECKSUM is what changes the MAC computation, and ENCRYPTED is what decides whether the payload has to be decrypted: the two are independent, so a PDU can be encrypted without the secure checksum.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
*SEC
// contains filtered or unexported fields
}
Client is the security layer's client end, which runs the client half of the connection: it sends the info PDU, answers the licence request, and holds the channel sender and fast-path listener that the layers above write through.
func NewClient ¶
NewClient returns a client over a transport. The connection sequence starts when the transport emits "connect", not here, so the credentials can be set afterwards.
func (*Client) ClientCoreData ¶
func (c *Client) ClientCoreData() *gcc.ClientCoreData
ClientCoreData returns the core data that was sent in the Connect-Initial.
It type asserts what it finds, so it panics if the connection has not reached the point where the data was sent rather than returning nil.
func (*Client) ClientNetworkData ¶
func (c *Client) ClientNetworkData() *gcc.ClientNetworkData
ClientNetworkData returns the network data that was sent, and panics if the connection has not got that far.
func (*Client) ClientSecurityData ¶
func (c *Client) ClientSecurityData() *gcc.ClientSecurityData
ClientSecurityData returns the security data that was sent, and panics if the connection has not got that far.
func (*Client) RecvFastPath ¶
RecvFastPath decrypts a fast-path PDU if its flags say it was encrypted, and hands it to the listener.
The two flags decide independently: ENCRYPTED says whether to decrypt, and SECURE_CHECKSUM changes how the MAC is checked, so a PDU can be one without the other. The listener is called without a nil check, so it must be set before a fast-path PDU can arrive.
func (*Client) SendToChannel ¶
SendToChannel sends a message on a static virtual channel, encrypted when encryption is on and in the clear when it is not.
With encryption the payload is protected and a security header is written in front of it; without it the message goes to the channel unchanged, since over TLS the transport has already protected it. The two paths therefore produce different bytes for the same call, and which one runs depends on the session and not on the caller.
func (*Client) ServerSecurityData ¶
func (c *Client) ServerSecurityData() *gcc.ServerSecurityData
ServerSecurityData returns the server's security data, which is what says which encryption the session settled on.
It panics rather than returning nil if the connect sequence has not run, and it reads the second element of the server's data, so a server that sent its blocks in another order would make this a wrong type rather than an error.
func (*Client) SetAlternateShell ¶
SetAlternateShell sets the program to run instead of Explorer when the session starts, and sets the flag that tells the server the field is there.
The string is UTF-16 with a NUL terminator, which is the form every string in this PDU takes.
func (*Client) SetChannelSender ¶
func (c *Client) SetChannelSender(f core.ChannelSender)
SetChannelSender sets what SendToChannel writes through. It is called by the channel layer once the channels are joined.
func (*Client) SetClientAutoReconnect ¶
SetClientAutoReconnect gives the client an auto reconnect cookie, built from the logon id and the random value the server sent.
The cookie is what lets a session be picked up again after a dropped connection without a second logon, and the verifier inside it is derived from the server's random value so that it cannot be replayed against another session.
func (*Client) SetDomain ¶
SetDomain sets the domain in the info PDU, in UTF-16 with a terminator.
An empty domain is sent as a single terminator rather than as nothing, since the length field counts from it.
func (*Client) SetFastPathListener ¶
func (c *Client) SetFastPathListener(f core.FastPathListener)
SetFastPathListener sets the handler for fast-path PDUs, which are delivered separately from the T.125 data path because their framing and their security flags are their own.
func (*Client) SetPwd ¶
SetPwd sets the password in the info PDU, in UTF-16 with a terminator.
The password is stored because the PDU has to carry it. It is not logged, and a test in this repository fails the build if a log call is handed a password field.
func (*Client) SetRequireVerifiedCertificate ¶ added in v0.9.0
SetRequireVerifiedCertificate decides whether an unverified server certificate ends the connection.
It applies to Standard RDP Security, where the server's certificate is what the client random is encrypted to and there is no other trust model. There the default is to report the result and continue, because a self-signed certificate is what an RDP server normally presents and refusing those by default would refuse most servers.
With it set, a certificate that does not verify is an error and the connection is abandoned. A version 1 certificate never verifies, whatever it contains; see gcc.ProprietaryServerCertificate.Verify.
It has no effect over TLS or CredSSP, where the certificate belongs to the TLS layer and is verified there.
func (*Client) SetUser ¶
SetUser sets the user name in the info PDU, in UTF-16 with a terminator.
Nothing checks it: a name that is wrong is not rejected here, and the server is what decides, which is why the failure a caller sees for a bad account comes from the security exchange rather than from this.
type ClientAutoReconnect ¶
type ClientAutoReconnect struct {
CbAutoReconnectLen uint16
CbLen uint32
Version uint32
LogonId uint32
SecVerifier []byte
}
ClientAutoReconnect is the auto reconnect cookie, which lets a session be picked up again after a connection drops without asking for credentials a second time.
SecVerifier is derived from the random value the server sent, so the cookie cannot be replayed against another session.
func NewClientAutoReconnect ¶
func NewClientAutoReconnect(id uint32, random []byte) *ClientAutoReconnect
NewClientAutoReconnect returns a cookie for the given logon id, with the verifier derived from the server's random value.
type ClientSecurityExchangePDU ¶
type ClientSecurityExchangePDU struct {
Length uint32 `struc:"little"`
EncryptedClientRandom []byte `struc:"little"`
Padding []byte `struc:"[8]byte"`
}
ClientSecurityExchangePDU is the message that carries the client random value at the start of the security exchange, which is what the session keys are derived from along with the server's.
The random value is encrypted with the server's public key, so what travels is the encrypted form and the padding that brings it up to the key's block size.
type RDPExtendedInfo ¶
type RDPExtendedInfo struct {
ClientAddressFamily uint16 `struc:"little"`
CbClientAddress uint16 `struc:"little,sizeof=ClientAddress"`
ClientAddress []byte `struc:"[]byte"`
CbClientDir uint16 `struc:"little,sizeof=ClientDir"`
ClientDir []byte `struc:"[]byte"`
ClientTimeZone []byte `struc:"[172]byte"`
ClientSessionId uint32 `struc:"litttle"`
PerformanceFlags uint32 `struc:"little"`
AutoReconnect *ClientAutoReconnect
}
RDPExtendedInfo is the part of the info PDU that a client sends when it can send one, which is everything after the credentials: the client's address and directory, its time zone, and the auto reconnect cookie.
ClientTimeZone is the Windows time zone structure, which is a fixed 172 bytes whose contents this client leaves at zero. That is allowed: the field is for display in the session, not for the connection.
func NewExtendedInfo ¶
func NewExtendedInfo(auto *ClientAutoReconnect) *RDPExtendedInfo
NewExtendedInfo returns extended info with empty address and directory strings and a zero time zone, which is what this client sends. The auto reconnect cookie may be nil, in which case the cookie is left out of the serialized form.
func (*RDPExtendedInfo) Serialize ¶
func (o *RDPExtendedInfo) Serialize() []byte
Serialize returns the extended info as it goes on the wire. It is written by hand rather than by the struct tags because the two strings are length prefixed with a length that counts the NUL terminator, which the tags cannot express.
The auto reconnect block is written only when there is one, and the field before it is what says so on the wire.
type RDPInfo ¶
type RDPInfo struct {
CodePage uint32
Flag uint32
CbDomain uint16
CbUserName uint16
CbPassword uint16
CbAlternateShell uint16
CbWorkingDir uint16
Domain []byte
UserName []byte
Password []byte
AlternateShell []byte
WorkingDir []byte
ExtendedInfo *RDPExtendedInfo
}
RDPInfo is the info PDU, which is the client's first message after the connection is up: the credentials in UTF-16, the shell and working directory, and the flags that describe the client.
Every string is stored with the NUL terminator that the wire format requires and the length fields count from it, so the serialized lengths are two less than the slices: the terminator is not part of the count.
func NewRDPInfo ¶
func NewRDPInfo() *RDPInfo
NewRDPInfo returns an info PDU with the flags a client normally sends and empty credentials.
The strings are set to a single NUL rather than to nothing, because the length fields are computed by subtracting two from the slice length and a nil slice would underflow. The credentials are filled in by the setters.
func (*RDPInfo) Serialize ¶
Serialize returns the info PDU: the code page, the flags, five lengths, the five strings, and the extended info when the caller says to include it.
The lengths are the slice lengths less two, because each string carries a NUL terminator that the count does not include. hasExtended is the caller's decision rather than something derived here, since the extended info is what a client sends when it negotiated the ability to.
func (*RDPInfo) SetClientAutoReconnect ¶
func (o *RDPInfo) SetClientAutoReconnect(auto *ClientAutoReconnect)
SetClientAutoReconnect attaches an auto reconnect cookie to the info PDU.
func (*RDPInfo) SetClientInfo ¶
func (o *RDPInfo) SetClientInfo()
SetClientInfo adds the flags that ask the server to report logon results, which is what makes a wrong password arrive as a message rather than as a silent disconnect during the later exchange.
type SEC ¶
SEC is the security layer: it encrypts what goes out, decrypts what comes in, and owns the keys and the info PDU.
It works with no encryption at all when the session runs over TLS or CredSSP, which is the usual case now, and with the Standard RDP Security keys otherwise. Which of the two it is decides whether the flags in a PDU header mean anything: over TLS they are written but the payload is already protected by the transport.
The licensing keys are deliberately separate from the session keys, because the MAC over a licensing blob uses a different salt from the MAC over an ordinary PDU, and confusing the two produces a session that connects and then fails at the licence.
func NewSEC ¶
NewSEC returns a security layer over a transport, with an empty info PDU and encryption off until the server's security data says otherwise.
The transport's close and error events are forwarded.
func (*SEC) Close ¶
Close closes the transport. It does not stop the receive path, which notices and emits "close".
func (*SEC) Read ¶
Read reads from the transport with nothing decrypted. Reading is handled by the receive path, which decides from the header whether a PDU is encrypted, so this is only for a caller that wants the raw stream.
type SecurityHeader ¶
type SecurityHeader struct {
// contains filtered or unexported fields
}
SecurityHeader is the two flag words in front of an encrypted PDU. The high word is only meaningful when the low one has FLAGSHI_VALID set, which is what makes the second word optional on the wire.