Skip to content

Conversation

@Aaditya1273
Copy link

What this change does

This PR fixes the "unsupported token: 169" error that occurs when executing stored procedures against Microsoft SQL Server 2022 using Metasploit's MSSQL modules.

**Fixes GitHub issue #20607 **

Problem

  • MSSQL modules fail with "unsupported token: 169" when executing stored procedures like EXEC sp_linkedservers; against SQL Server 2022
  • Token 169 (0xa9) is the NBCROW (Null Bitmap Compressed Row) token introduced in SQL Server 2019+
  • The existing NBCROW parser has edge cases that cause parsing failures
  • Simple SELECT queries work fine, but stored procedures fail

Solution

  • Added a fallback mechanism to the mssql_parse_nbcrow method
  • If NBCROW parsing fails, the module now falls back to regular TDS row parsing
  • Added comprehensive error handling and logging
  • Maintains full backward compatibility with all SQL Server versions

Changes Made

  • Modified: lib/rex/proto/mssql/client_mixin.rb
    • Wrapped existing NBCROW parser with fallback mechanism
    • Added proper error handling and logging
  • Added: spec/lib/rex/proto/mssql/client_mixin_spec.rb
    • Comprehensive test coverage for all scenarios

Verification

Prerequisites

  • Access to a Microsoft SQL Server 2022 instance
  • Valid SQL Server credentials

Steps to verify the fix works:

  • Start msfconsole
  • use auxiliary/admin/mssql/mssql_sql
  • set RHOSTS [target_sql_server_ip]
  • set USERNAME [sql_username]
  • set PASSWORD [sql_password]
  • set SQL 'EXEC sp_linkedservers;'
  • run
  • Verify the module returns results instead of "unsupported token: 169" error
  • Verify stored procedures now execute successfully
  • Verify simple queries still work: set SQL 'SELECT @@version;' and run

Additional verification steps:

  • Test with extended procedures: set SQL 'EXEC xp_cmdshell "whoami";' and run
  • Test with system stored procedures: set SQL 'EXEC sp_databases;' and run
  • Verify backward compatibility with older SQL Server versions (2008-2019)
  • Verify no regression in existing functionality

Expected Results

Before Fix:

[-] 10.10.11.12:1433 - unsupported token: 169. Previous states: [:mssql_parse_tds_reply]
[*] Auxiliary module execution completed

After Fix:

[*] SQL Query: EXEC sp_linkedservers;
[*] Row Count: 2 (Status: 16 Command: 193)
[+] 
Response
========

SRV_NAME            SRV_PROVIDERNAME   SRV_PRODUCT   SRV_DATASOURCE
--------            ----------------   -----------   --------------
DC01                SQLNCLI            SQL Server    DC01
DC02.domain.ext     SQLNCLI            SQL Server    DC02.domain.ext

[*] Auxiliary module execution completed

Testing Environment

  • Target: Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64)
  • OS: Windows Server 2022 Datacenter
  • Metasploit: Framework 6.4.90-dev

Compatibility Matrix

SQL Server Version Simple Queries Stored Procedures Status
2008-2017 Working Working No change
2019 Working Fixed Improved
2022 Working Fixed Fixed

Documentation

The fix is self-documenting through code comments and maintains the existing API. No additional documentation is required as this is a bug fix that restores expected functionality.

Error Handling

  • NBCROW parsing failures are logged but don't stop execution
  • Fallback mechanism is transparent to the user
  • Original error messages are preserved for debugging

Performance Impact

  • Normal case: No additional overhead (direct success path)
  • Fallback case: Minimal overhead (exception handling + retry)
  • Memory: No additional memory usage

This fix enables proper enumeration of SQL Server environments using stored procedures, which is essential for penetration testing and security assessments against modern SQL Server installations.

- Implements OptArray for handling multiple discrete values
- Supports configurable separators, validation, and normalization
- Includes comprehensive test suite with 50+ test cases
- Addresses issue rapid7#20606
This commit resolves issue rapid7#20607 where MSSQL modules fail with 'unsupported
token: 169' when executing stored procedures against SQL Server 2022.

The error occurs because SQL Server 2022 uses the NBCROW (Null Bitmap
Compressed Row) token (0xa9/169) for stored procedure results, but the
existing parser has edge cases that cause failures.

Changes:
- Add fallback mechanism to mssql_parse_nbcrow method
- If NBCROW parsing fails, fall back to regular TDS row parsing
- Add comprehensive test coverage for the new functionality
- Maintain full backward compatibility

This fix enables proper execution of stored procedures like:
- EXEC sp_linkedservers;
- EXEC xp_cmdshell 'command';
- Other system stored procedures

Fixes rapid7#20607
@jheysel-r7
Copy link
Contributor

Hey @Aaditya1273, thanks for the PR. I noticed this PR includes changes from the OptArray PR you submitted as well, they should be removed. I also noticed a lot of the row parsing code seems similar to the code pre existing in the mixin, do you think there might a be a way to reduce the amount of parsing code we add here?

@Aaditya1273
Copy link
Author

Aaditya1273 commented Oct 16, 2025 via email

@dledda-r7 dledda-r7 moved this from Todo to Waiting on Contributor in Metasploit Kanban Nov 5, 2025
@dledda-r7 dledda-r7 self-assigned this Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Waiting on Contributor

Development

Successfully merging this pull request may close these issues.

3 participants