Skip to content

feat(mssql): add explicit login impersonation for linked-server execution - #1443

Open
BrunoRochaDev wants to merge 3 commits into
Pennyw0rth:mainfrom
BrunoRochaDev:feat/mssql-linked-login
Open

BrunoRochaDev wants to merge 3 commits into
Pennyw0rth:mainfrom
BrunoRochaDev:feat/mssql-linked-login

Conversation

@BrunoRochaDev

@BrunoRochaDev BrunoRochaDev commented Sep 23, 2026 •

Copy link
Copy Markdown

Description

This change adds explicit MSSQL login impersonation support to the exec_on_link module.

While using NetExec in an MSSQL linked-server lab, I found that exec_on_link could return an empty result when the authenticated login did not have a linked-server login mapping.

For example, I attempted to execute a query through a linked server:

┌──(user㉿kali)-[~]
└─$ nxc mssql 192.xxx.xxx.xxx -u administrator -H $NTLM -M exec_on_link -o LINKED_SERVER=SQL27 COMMAND='SELECT @@SERVERNAME'
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [*] Windows 10 / Server 2019 Build 17763 (name:SQL11) (domain:sql11) (EncryptionReq:False)
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [+] sql11\administrator:<HASH> (Pwn3d!)
EXEC_ON_... 192.xxx.xxx.xxx 1433   SQL11            [*] Command output: []

The module didn't return any errors, but also there was no command output. It just silently failed.

Running the query manually showed the actual MSSQL error:

┌──(user㉿kali)-[~]
└─$ nxc mssql 192.xxx.xxx.xxx -u administrator -H $NTLM --query "EXEC('SELECT @@SERVERNAME') AT [SQL27];"                 
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [*] Windows 10 / Server 2019 Build 17763 (name:SQL11) (domain:sql11) (EncryptionReq:False)
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [+] sql11\administrator:<HASH> (Pwn3d!)
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [-] ERROR(SQL11\SQLEXPRESS): Line 1: Access to the remote server is denied because no login-mapping exists.

The enum_links module showed that the server had a mapping for local login webapp11, rather than the authenticated administrator login.

┌──(user㉿kali)-[~]
└─$ nxc mssql 192.xxx.xxx.xxx -u administrator -H $NTLM -M enum_links                                                       
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [*] Windows 10 / Server 2019 Build 17763 (name:SQL11) (domain:sql11) (EncryptionReq:False)
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [+] sql11\administrator:<HASH> (Pwn3d!)
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [+] Linked servers found:
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*]   - SQL11\SQLEXPRESS
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*]   - SQL27
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*]   - SQL53
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [+] Linked servers found:
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*] Linked server: SQL27
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*]   - Local login: webapp11
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*]   - Remote login: webappGroup
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*] Linked server: SQL53
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*]   - Local login: webapp11
ENUM_LINKS  192.xxx.xxx.xxx 1433   SQL11            [*]   - Remote login: testAccount

Manually impersonating that login allowed execution through the linked server:

┌──(user㉿kali)-[~]
└─$ nxc mssql 192.xxx.xxx.xxx -u administrator -H $NTLM --query "EXECUTE AS LOGIN = 'webapp11'; EXEC('SELECT @@SERVERNAME') AT [SQL27];"
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [*] Windows 10 / Server 2019 Build 17763 (name:SQL11) (domain:sql11) (EncryptionReq:False)
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [+] sql11\administrator:<HASH> (Pwn3d!)
MSSQL       192.xxx.xxx.xxx 1433   SQL11            SQL27\SQLEXPRESS

To address this, I created this PR that adds an explicit AS_LOGIN option to exec_on_link:

┌──(user㉿kali)-[~]
└─$ nxc mssql 192.xxx.xxx.xxx -u administrator -H $NTLM -M exec_on_link -o LINKED_SERVER=SQL27 AS_LOGIN=webapp11 COMMAND='SELECT @@SERVERNAME'
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [*] Windows 10 / Server 2019 Build 17763 (2019 RTM 15.0.2000) (name:SQL11) (domain:sql11) (EncryptionReq:False) 
MSSQL       192.xxx.xxx.xxx 1433   SQL11            [+] sql11\administrator:<HASH> (Pwn3d!)
EXEC_ON_... 192.xxx.xxx.xxx 1433   SQL11            [*] Command output:
EXEC_ON_... 192.xxx.xxx.xxx 1433   SQL11            SQL27\SQLEXPRESS

The implementation uses statement-scoped AS LOGIN syntax:

EXEC (N'<command>') AS LOGIN = N'<login>' AT [<linked_server>];

This keeps impersonation explicit and scoped to the linked-server command, without leaking to further queries or anything of the sort.

The module now also:

  • Checks and reports MSSQL query errors instead of displaying an empty result.
  • Detects the linked-server no-login-mapping error.
  • Enumerates explicit mappings for the requested linked server when privileges permit, then shows possible AS_LOGIN=<login> retry hints without selecting one automatically.
  • Formats linked-server query output like normal --query output instead of displaying raw Python dictionaries.
  • Escapes T-SQL string literals and bracketed identifiers. This is not specific to AS_LOGIN, but prevents quotes in commands/login names and closing brackets in linked-server names from breaking the dynamically constructed wrapper query.

I manually tested this contribution on some of the OSEP challenge labs, and it works fine.

AI assistance disclosure

I discovered this limitation organically while using NetExec in my OSEP studies. I investigated the existing code, repository documentation, git history, and available discussions to confirm that this functionality was not already implemented or being contributed in an existing PR.

I then used Codex GPT-5.6-Terra as an implementation assistant. It helped investigate the relevant code paths, propose the implementation approach, and generate code changes. I reviewed the changes and tested them manually in labs.

I wrote this PR description myself, although I used LLMs for paraphrasing in some places.

Related discussion: Reworking part of the MSSQL protocol

Type of change

Insert an "x" inside the brackets for relevant items (do not delete options)

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Deprecation of feature or functionality
  • This change requires a documentation update
  • This requires a third party update (such as Impacket, Dploot, lsassy, etc)
  • This PR was created with the assistance of AI (list what type of assistance, tool(s)/model(s) in the description)

Setup guide for the review

Testing was performed from a Kali Linux VM against an MSSQL linked-server lab (from the OSEP challenge labs).

The original behavior can be reproduced by authenticating as a login without a mapping and running:

nxc mssql ... -M exec_on_link -o LINKED_SERVER=<SERVER-NAME> COMMAND='SELECT @@SERVERNAME'

Before this change, the module displayed an empty result even though SQL Server reported a missing login mapping (as shown in the following screenshots).

The new behavior was tested with:

nxc mssql ... -M exec_on_link -o LINKED_SERVER=<SERVER_NAME> AS_LOGIN=<LOGIN> COMMAND='SELECT @SERVERNAME'

This successfully executed the query through the linked server and returned formatted output.

Screenshots (if appropriate):

Before (no query execution):
Before
After (explicit errors, guides the user in the right direction, query execution):
After

Checklist:

Insert an "x" inside the brackets for completed and relevant items (do not delete options)

  • I have ran Ruff against my changes (poetry: poetry run ruff check ., use --fix to automatically fix what it can)
  • I have added or updated the tests/e2e_commands.txt file if necessary (new modules or features are required to be added to the e2e tests)
  • If reliant on changes of third party dependencies, such as Impacket, dploot, lsassy, etc, I have linked the relevant PRs in those projects
  • I have linked relevant sources that describes the added technique (blog posts, documentation, etc)
  • I have performed a self-review of my own code (not an AI review)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation: Update mssql-linked-servers.md to match PR #1443 on main repository - #116

@NeffIsBack

Copy link
Copy Markdown
Member

Thanks for the PR! Looks cool :)

Related: #1067

@NeffIsBack NeffIsBack added the enhancement New feature or request label Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants