Skip to content

Instantly share code, notes, and snippets.

@saurabh500
Last active April 16, 2026 02:32
Show Gist options
  • Select an option

  • Save saurabh500/5d32f170dce242dfb682f407be115a79 to your computer and use it in GitHub Desktop.

Select an option

Save saurabh500/5d32f170dce242dfb682f407be115a79 to your computer and use it in GitHub Desktop.

Check TLS Protocol and Driver Version for SQL Server Connections Using Extended Events

This guide shows how to use SQL Server Extended Events to determine the TLS protocol version and driver version used by client connections.

Prerequisites

  • SQL Server 2016 or later
  • Permissions: ALTER ANY EVENT SESSION and VIEW SERVER STATE
  • A query tool such as SSMS or sqlcmd

Note: If using sqlcmd, ensure QUOTED_IDENTIFIER is ON (required for XML methods).
Run SET QUOTED_IDENTIFIER ON; before executing the queries below.


Step 1: Create and Start the Extended Events Session

CREATE EVENT SESSION [TLS_Check] ON SERVER 
ADD EVENT sqlserver.process_login_finish
ADD TARGET package0.ring_buffer
WITH (
    MAX_MEMORY = 4096 KB,
    EVENT_RETENTION_MODE = NO_EVENT_LOSS,
    MAX_DISPATCH_LATENCY = 30 SECONDS,
    MAX_EVENT_SIZE = 0 KB,
    MEMORY_PARTITION_MODE = NONE,
    TRACK_CAUSALITY = OFF,
    STARTUP_STATE = OFF
);

ALTER EVENT SESSION [TLS_Check] ON SERVER STATE = START;

This captures every login event into an in-memory ring buffer.


Step 2: Make a Connection

Connect to SQL Server using the client or driver you want to inspect. For example, using sqlcmd with TCP and encryption:

sqlcmd -S "tcp:YourServer\YourInstance" -E -N -C -Q "SELECT 1"
Flag Purpose
tcp: Forces TCP protocol
-E Windows integrated authentication
-N Encrypt the connection
-C Trust the server certificate (for self-signed certs)

Important: Connections over shared memory will show SNI_UNDEFINED_PROTOCOL because no TLS handshake occurs. Use tcp: to force a TCP connection.

Wait ~30 seconds after connecting (to allow the MAX_DISPATCH_LATENCY to flush events to the ring buffer).


Step 3: Query the TLS Protocol and Driver Version

SET QUOTED_IDENTIFIER ON;

WITH ring_buffer AS (
    SELECT CAST(xst.target_data AS xml) AS TargetData
    FROM sys.dm_xe_session_targets AS xst
    INNER JOIN sys.dm_xe_sessions AS xs 
        ON xst.event_session_address = xs.address
    WHERE xs.name = N'TLS_Check'
),
event_node AS (
    SELECT CAST(node_data.query('.') AS xml) AS event_info
    FROM ring_buffer AS rb
    CROSS APPLY rb.TargetData.nodes('/RingBufferTarget/event') AS n(node_data)
)
SELECT
    event_info.value('(/event/@timestamp)[1]', 'DATETIME2')                                     AS [timestamp],
    event_info.value('(/event/data[@name="spid"]/value)[1]', 'INT')                             AS [spid],
    event_info.value('(/event/data[@name="client_hostname"]/value)[1]', 'NVARCHAR(256)')        AS [client_hostname],
    event_info.value('(/event/data[@name="client_app_name"]/value)[1]', 'NVARCHAR(256)')        AS [client_app],
    event_info.value('(/event/data[@name="driver_name"]/value)[1]', 'NVARCHAR(256)')            AS [driver_name],
    event_info.value('(/event/data[@name="driver_version"]/value)[1]', 'BIGINT')                 AS [driver_version_raw],
    CAST((event_info.value('(/event/data[@name="driver_version"]/value)[1]', 'BIGINT') % 256) AS VARCHAR) + '.' +
    CAST(((event_info.value('(/event/data[@name="driver_version"]/value)[1]', 'BIGINT') / 256) % 256) AS VARCHAR) + '.' +
    CAST(((event_info.value('(/event/data[@name="driver_version"]/value)[1]', 'BIGINT') / 65536) % 256) * 256 +
         ((event_info.value('(/event/data[@name="driver_version"]/value)[1]', 'BIGINT') / 16777216) % 256) AS VARCHAR)
                                                                                                AS [driver_version],
    event_info.value('(/event/data[@name="ssl_protocol"]/text)[1]', 'NVARCHAR(40)')             AS [ssl_protocol],
    event_info.value('(/event/data[@name="ssl_cipher"]/text)[1]', 'NVARCHAR(40)')               AS [ssl_cipher]
FROM event_node
ORDER BY [timestamp] DESC;

Example Output

timestamp spid client_hostname client_app driver_name driver_version_raw driver_version ssl_protocol ssl_cipher
2026-04-16 02:20:... 58 MYPC SSMS Framework Microsoft SqlClient Data Provider 3680308997 5.15.24027 SNI_UNDEFINED_PROTOCOL UNDEFINED_CIPHER
2026-04-16 01:52:... 56 MYPC sqlcmd ODBC 100665873 17.10.6 SNI_TLS1_2_SERVER AES_256
2026-04-16 01:50:... 55 MYPC .Net SqlClient .Net SqlClient Data Provider 86247428 4.8.9221 SNI_UNDEFINED_PROTOCOL UNDEFINED_CIPHER

Interpreting ssl_protocol

Value Meaning
SNI_TLS1_0_SERVER TLS 1.0
SNI_TLS1_1_SERVER TLS 1.1
SNI_TLS1_2_SERVER TLS 1.2 ✅
SNI_TLS1_3_SERVER TLS 1.3 ✅
SNI_UNDEFINED_PROTOCOL No encryption (shared memory, or encryption not enabled)

Decoding driver_version_raw

The driver_version_raw value comes from the TDS prelogin VERSION option. The client sends 4 bytes on the wire in the format [Major][Minor][BuildHi][BuildLo], which SQL Server stores as a little-endian 32-bit integer.

To decode:

Major   = byte 0 =  value & 0xFF
Minor   = byte 1 = (value >> 8)  & 0xFF
BuildHi = byte 2 = (value >> 16) & 0xFF
BuildLo = byte 3 = (value >> 24) & 0xFF
Build   = BuildHi * 256 + BuildLo

Example values:

Driver Raw Value Hex Decoded Version
ODBC Driver 17 for SQL Server 100665873 0x06000A11 17.10.6
Microsoft.Data.SqlClient (SSMS) 3680308997 0xDB5D0F05 5.15.24027
.Net SqlClient (.NET Framework 4.8) 86247428 0x05240804 4.8.9221

Note: Some newer drivers produce values that overflow a 32-bit signed integer. Always use BIGINT when reading driver_version from XEvents.

You can decode this inline in SQL:

SELECT
    driver_version_raw,
    CAST((driver_version_raw % 256) AS VARCHAR) + '.' +
    CAST(((driver_version_raw / 256) % 256) AS VARCHAR) + '.' +
    CAST(((driver_version_raw / 65536) % 256) * 256 +
         ((driver_version_raw / 16777216) % 256) AS VARCHAR) AS driver_version_decoded
FROM (
    -- driver_version_raw must be BIGINT to avoid overflow with newer drivers
) AS results;

Step 4: Clean Up

When finished, stop and drop the session:

ALTER EVENT SESSION [TLS_Check] ON SERVER STATE = STOP;
DROP EVENT SESSION [TLS_Check] ON SERVER;

Troubleshooting

Issue Solution
SET QUOTED_IDENTIFIER error Run SET QUOTED_IDENTIFIER ON; before the query
All rows show SNI_UNDEFINED_PROTOCOL Connection is not encrypted — use tcp: prefix and Encrypt=Yes in connection string
No rows returned Wait 30 seconds for event dispatch, or verify the session is running with SELECT * FROM sys.dm_xe_sessions WHERE name = 'TLS_Check'
Ring buffer is empty after restart The ring buffer is in-memory only; events are lost when the session stops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment