Skip to content

Add oleexec, clrexec, jobexec and assembly execution fileless for MSSQL - #1268

Open
Dfte wants to merge 12 commits into
Pennyw0rth:mainfrom
Dfte:add_mssql_ole_exec_method
Open

Dfte wants to merge 12 commits into
Pennyw0rth:mainfrom
Dfte:add_mssql_ole_exec_method

Conversation

@Dfte

@Dfte Dfte commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds two new exec methods for the MSSQL protocol:

  • Oleexec that relies on the sp_oamethod shell procedure ;
  • clrexec that relies on the CLR component.

As such, I have added the --exec-method {mssqlexec, oleexec, crlexec} mechanism as well.

Concerning the CRL exec method, it relies on an assembly which is a binary file that is sent as hexadecimal to the SQL server and used to run the command. This file is located in NXC_DATA/mssql_clr and the corresponding C# code is the following:

using System;
using System.Data.SqlTypes;
using System.Diagnostics;
using Microsoft.SqlServer.Server;

public class StoredProcedures
{
    [SqlProcedure]
    public static void ExecuteCommand(SqlString command)
    {
        var process = new Process();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = "/c " + command.Value;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.Start();

        string output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();

        SqlContext.Pipe.Send(output);
    }
}

If using the --exec-method crlexec, it is possible to use a custom assembly specifying the --clr-assembly PATH_TO_ASSEMBLY. In that case the -x "COMMAND" is not used at all and the CLR assembly is executed.

Note that this PR is work in progress. Indeed, there are few issues:

  • The first one relies with the crlexec method which requries the following hack
image

I know it sucks but for the moment, and considering how -x and -X are designed, it's not really possible to do much better.

  • Each exec-method uses the same is_option_enabled, backup_and_enable, backup_and_disable functions which should be factorized in a single file (nxc/modules/mssql/procudes.py I'd says) so that each exec-method can use them.
  • The MSSQLexec.py file contains functions used to get_file and put_file. We can extract these from mssqlexec to mssql.py the moment we have handled the factorize of the three previously mentionned functions.

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

Nothing to setup, it works on default MSSQL server.

Screenshots (if appropriate):

  • olexec:
image
  • clrexec (with embedded assembly):
image
  • clrexec (with custom assembly):
using System;
using System.Data.SqlTypes;
using System.Diagnostics;
using Microsoft.SqlServer.Server;

public class StoredProcedures
{
    [SqlProcedure]
    public static void ExecuteCommand(SqlString command)
    {
        var process = new Process();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = "/c net localgroup";      
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.Start();

        string output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();

        SqlContext.Pipe.Send(output);
    }
}
image

Notice that -x doesn't have any COMMAND, that's because I had to modify proto_args.

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 (PR here: https://github.com/Pennyw0rth/NetExec-Wiki)

IMPORTANT

I honestly believe we can simplify the way we execute commands. Knowing that most -X command exec rely on cmd /c powershell COMMAND, is absurd to me. Question is whether we are keen modifying that part (which is a huge change) or hack protocols so that we can use more advanced protocol capabilities like the CRL on MSSQL for example. Let me know what you guys think :)

@Dfte

Dfte commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor Author

The latests commits changes a few things. First it centralizes 4 functions used accross all exec methods that are used to check whether a procedure is enabled, disabled/enable it, restore it's original value:

  • is_option_enable
  • backup_and_enable
  • backup_and_disable
  • restore

All configuration options are stored in the following object at mssql.ini:

self.backuped_options = {}

Looking at the the execute function, I changed the arguments given to the exec methods to:

image

Doing so we can keep important and redundant functions in mssql.py and share them to exec methods. Doing so, I unloaded the exec methods from these functions and moved the get_file/put_file functions from mssqlexec.py to mssql.py itself:

  • get_file:
@requires_admin
    def get_file(self):
        remote_path = self.args.get_file[0]
        download_path = self.args.get_file[1]
        self.logger.display(f'Copying "{remote_path}" to "{download_path}"')

        try:
            query = f"SELECT * FROM OPENROWSET(BULK N'{remote_path}', SINGLE_BLOB) rs"
            self.logger.debug(f"Executing query: {query}")
            self.conn.sql_query(query)
            data = self.conn.rows
            self.logger.debug(f"Get file returned: {data}")
            with open(download_path, "wb+") as f:
                f.write(binascii.unhexlify(data[0]["BulkColumn"]))
            self.logger.success(f'File "{remote_path}" was downloaded to "{download_path}"')
        except Exception as e:
            self.logger.fail(f'Error reading file "{remote_path}": {e}')
            if os.path.getsize(download_path) == 0:
                os.remove(download_path)
  • put_file:
@requires_admin
    def put_file(self):
        download_path = self.args.put_file[0]
        remote_path = self.args.put_file[1]
        self.logger.display(f"Copy {download_path} to {remote_path}")
        with open(download_path, "rb") as f:
            data = f.read()
            self.logger.display(f"Size is {len(data)} bytes")
            try:
                self.backup_and_enable("advanced options")
                self.backup_and_enable("Ole Automation Procedures")
                hexdata = data.hex()
                self.logger.debug(f"Hex data to write to file: {hexdata}")
                query = f"DECLARE @ob INT;EXEC sp_OACreate 'ADODB.Stream', @ob OUTPUT;EXEC sp_OASetProperty @ob, 'Type', 1;EXEC sp_OAMethod @ob, 'Open';EXEC sp_OAMethod @ob, 'Write', NULL, 0x{hexdata};EXEC sp_OAMethod @ob, 'SaveToFile', NULL, '{remote_path}', 2;EXEC sp_OAMethod @ob, 'Close';EXEC sp_OADestroy @ob;"
                self.logger.debug(f"Executing query: {query}")
                self.conn.sql_query(query)
                self.restore("Ole Automation Procedures")
                self.restore("advanced options")
            except Exception as e:
                self.logger.debug(f"Error uploading via mssqlexec: {e}")

There are still a few things to modify because for the moment we rely on the @required_admin to execute commands. That's not true with mssql as it under certain circumstances, it is possible to execute specific procedures without being sysadmin. I'll have to look at these and how we can hack nxc. Probably I'll write a couple of checks in the is_admin function. Will see later :)!

@Dfte

Dfte commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Last commit patches the --lsa and --sam functions so that they work as intended with the modifications I applied. Both functions look like:

@requires_admin
    def put_file(self, download_path=None, remote_path=None):
        if remote_path is None and download_path is None:
            download_path = self.args.put_file[0]
            remote_path = self.args.put_file[1]

Which allows calling the function as a method filling remote and local path. Or via the command line from self.args.

@Dfte Dfte mentioned this pull request Jun 5, 2026
1 of 14 tasks
@Dfte

Dfte commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Latests PR adds the jobexec execution method allowing running jobs via the SQL Server agent if it is running:

image

Not entirely finished but :D

@Dfte Dfte changed the title [WIP] Add oleexec and crlexec exec methods allowing running custom assemblies. [WIP] Add oleexec, clrexec, jobexec and assembly execution fileless Jun 5, 2026
@Dfte
Dfte marked this pull request as draft June 8, 2026 09:29
@Dfte
Dfte marked this pull request as ready for review June 8, 2026 10:55
@Dfte Dfte mentioned this pull request Jul 7, 2026
2 of 14 tasks
@Dfte

Dfte commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Last commit fixes a stacktrace displayed in --exec-method jobexec if the sql server agent is not running.
It also adds a --sql-agent option that will check whether the SQL agent server is running and with which service account:

image

@Dfte Dfte changed the title [WIP] Add oleexec, clrexec, jobexec and assembly execution fileless Add oleexec, clrexec, jobexec and assembly execution fileless for MSSQL Jul 16, 2026
Comment thread nxc/protocols/mssql.py
self.logger.fail("Command execution blocked by AMSI")
return ""

if (self.args.execute or self.args.ps_execute or self.args.clr_assembly_explicitly_set):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clr_assembly_explicitly_set only exists on the namespace when --clr-assembly was passed, so every caller that goes through execute() without -x/-X crashes here. nxc mssql <ip> -u <user> -p <pass> -M test_connection -o HOST=localhost gives:

image

Same for met_inject, web_delivery and any module calling connection.execute(). I think the simplest fix is to drop the DefaultTrackingAction on both options, use default=None, and test if self.args.clr_assembly: / if self.args.exec_method:. That also removes the three getattr(self.args, ...) at lines 354, 406 and 417.

Comment thread nxc/protocols/mssql.py
if getattr(self.args, "exec_method_explicitly_set", False):
methods = [self.args.exec_method]
if not methods:
methods = ["mssqlexec", "oleexec", "clrexec"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback list is dead code: the loop breaks as soon as the class is instantiated, and none of the constructors can fail (only JOBEXEC does a query, and it is not in the list). So when xp_cmdshell fails we never try oleexec or clrexec. Was the fallback supposed to happen on execution failure? If not, please drop the loop and use a dict:

exec_methods = {"mssqlexec": MSSQLEXEC, "oleexec": OLEEXEC, "clrexec": CLREXEC, "jobexec": JOBEXEC}
exec_method = exec_methods[self.args.exec_method](self)

That also removes the four copy-pasted try/except Exception blocks and the if "exec_method" in locals() at line 402.

Comment thread nxc/protocols/mssql.py
# This part is hacky because we need to be able to determine whether the user used -x/-X with a command
# Or with a custom assembly to execute.
clr_assembly_explicitly_set = getattr(self.args, "clr_assembly_explicitly_set", False)
if (self.args.execute is True or self.args.ps_execute is True) and not clr_assembly_explicitly_set:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of -x / -X with nargs="?" and the is True checks here. -x alone used to be an argparse error, now it logs in on every target before telling the user the command is missing, and -x --clr-assembly a.dll runs the procedure with @cmd = N'None'. Since the custom assembly does not use the command at all, could --clr-assembly PATH be its own action (a clr_assembly() method called by call_cmd_args, like --put-file)? Then -x keeps its required value and the whole block goes away.

Comment thread nxc/protocols/mssql.py
self.lmhash = ""
self.nthash = ""
self.no_ntlm = False
self.args = args

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connection.__init__ already sets self.args, please remove it.

Comment thread nxc/protocols/mssql.py
LSA.dumpCachedHashes()
LSA.dumpSecrets()

def _is_option_enabled(self, option):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No _ prefix on methods please, is_option_enabled() as before. Same for _cleanup_stale(), _cleanup() and _restore_all() in clrexec.py.

result_rows = self.mssql_conn.sql_query(get_output_query)
if self.mssql_conn.lastError or not result_rows:
self.logger.fail(f"Fail running command execution: {self.mssql_conn.lastError}")
return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This early return leaves the job on the server, sp_delete_job is only reached on the happy path. On my lab the job runs fine (history shows run_status 1) but the history row is not written yet when we read it, so --exec-method jobexec -x whoami always ends here and I had two nxc_mssql_* jobs left in msdb.dbo.sysjobs after two runs. Then execute() in mssql.py calls .decode() on the None we return:

image

Please put the cleanup in a finally and return a str on every path.

self.logger.debug(f"Running {wait_job_finished_query}")
for _ in range(10):
rows = self.mssql_conn.sql_query(wait_job_finished_query)
if rows and rows[0].get("stop_execution_date") != "NULL":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impacket returns None for a SQL NULL now, so != "NULL" is always true and the loop exits on the first iteration without waiting. That is why the history is empty above. Please use is not None, and I think the loop should wait on the history row rather than on stop_execution_date, since the row is written a bit after the job stops. 5 seconds is also short for a real command, the timeout should follow --timeout.

self.logger.fail(f"Fail running command execution: {self.mssql_conn.lastError}")
return

if match := search(r"\.(.*?)\.", result_rows[0]["message"]):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking what is between the first two dots of the message does not hold: with Executed as user: RD.OCD\svc_sql. <output>. Process Exit Code 0. it returns OCD\svc_sql, any output containing a dot (ipconfig, an IP, a file name) is cut at that dot, and a multi-line output does not match at all, in which case output is never assigned and line 100 raises UnboundLocalError. The message is also localised (my lab returns Exécuté en tant qu'utilisateur : ...). Writing the output to a file with @output_file_name in sp_add_jobstep and reading it back with the OPENROWSET from get_file() would be more reliable

SELECT *
FROM sys.dm_server_services
WHERE servicename LIKE '%SQL Server Agent%'
AND status = 4;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query already filters on status = 4, so rows[0]["status"] != 4 at line 27 can never be true, if not rows: is enough. This is also the same query as sql_agent() in mssql.py, please drop one and call the other. Doing a query and logging a failure in __init__ is what forces the is_sql_server_agent_running is False special case in mssql.py, I would move the check into execute().

cgroup.add_argument("--db-hash", action="store_true", help="dump local database hashes")

cgroup = mssql_parser.add_argument_group("Command Execution")
cgroup.add_argument("--exec-method", choices={"mssqlexec", "oleexec", "clrexec", "jobexec"}, default="mssqlexec", help="method to execute the command. Ignored if in MSSQL mode", action=DefaultTrackingAction)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignored if in MSSQL mode is copied from the smb parser and does not mean anything here, please describe the four methods instead. choices should be a list, with a set the order in --help changes on every run. At line 28 the f-string around path.join() is not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants