Skip to content

Commit d7c6952

Browse files
Get-DbaNetworkEncryption - Fix malformed pre-login packet causing bytesRead=0
The pre-login byte array had incorrect data-section offsets. With 5 option headers (5 bytes each) plus a terminator, the headers occupy payload bytes 0-25, so data must start at offset 26 or higher. The original offsets (21-29) caused VERSION data to overlap with the MARS header and terminator bytes, producing a malformed TDS pre-login packet that SQL Server rejected by closing the connection, resulting in NetworkStream.Read() returning 0. Fixed by simplifying to two options (VERSION + ENCRYPTION) with correct offsets: VERSION data at payload offset 11, ENCRYPTION data at offset 17, total packet 26 bytes. (do Get-DbaNetworkEncryption) Co-authored-by: Andreas Jordan <[email protected]>
1 parent 24eb2dd commit d7c6952

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

public/Get-DbaNetworkEncryption.ps1

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,23 @@ function Get-DbaNetworkEncryption {
165165
$networkStream = $tcpClient.GetStream()
166166

167167
# We need to send a SQL Server pre-login packet before doing TLS,
168-
# because SQL Server uses STARTTLS-style negotiation
169-
# Pre-login packet: Type=0x12, Status=0x01, Length=0x002F, SPID=0x0000, PacketID=0x01, Window=0x00
170-
# followed by pre-login options
168+
# because SQL Server uses STARTTLS-style negotiation.
169+
# Pre-login packet layout (26 bytes total):
170+
# TDS header (8 bytes): type=0x12 (PRE_LOGIN), status=0x01 (EOM), length=0x001A (26)
171+
# Payload option headers (11 bytes):
172+
# VERSION (type=0x00): data-offset=11 (0x000B), data-length=6 (0x0006)
173+
# ENCRYPTION (type=0x01): data-offset=17 (0x0011), data-length=1 (0x0001)
174+
# TERMINATOR (0xFF)
175+
# Payload data (7 bytes):
176+
# VERSION data at payload offset 11: major=8, minor=0, build=0, subbuild=0
177+
# ENCRYPTION data at payload offset 17: 0x01 = ENCRYPT_ON
171178
$preLoginBytes = [byte[]](
172-
0x12, 0x01, 0x00, 0x2F, 0x00, 0x00, 0x01, 0x00, # TDS header
173-
0x00, 0x00, 0x15, 0x00, 0x06, 0x01, 0x00, 0x1B, # VERSION option
174-
0x00, 0x01, 0x02, 0x00, 0x1C, 0x00, 0x01, 0x03, # ENCRYPTION option
175-
0x00, 0x1D, 0x00, 0x00, 0x04, 0x00, 0x1D, 0x00, # INSTOPT + THREADID
176-
0x01, 0xFF, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, # options
177-
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 # version + encryption=ENCRYPT_ON
179+
0x12, 0x01, 0x00, 0x1A, 0x00, 0x00, 0x01, 0x00, # TDS header
180+
0x00, 0x00, 0x0B, 0x00, 0x06, # VERSION: type=0, offset=11, length=6
181+
0x01, 0x00, 0x11, 0x00, 0x01, # ENCRYPTION: type=1, offset=17, length=1
182+
0xFF, # TERMINATOR
183+
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, # VERSION data: 8.0.0.0
184+
0x01 # ENCRYPTION: ENCRYPT_ON
178185
)
179186

180187
$networkStream.Write($preLoginBytes, 0, $preLoginBytes.Length)

0 commit comments

Comments
 (0)