A C# console utility for interacting with MSSQL servers
RedknifeSQL is a simple menu-based console application to aid with pen-testing, specifically whilst dealing with Microsoft SQL servers in an AD environment.
The application prompts the user for the server hostname and database name to connect to. At present, only Windows authentication is supported.
The following command line options are supported:
--debug
- run RedknifeSQL in debug mode (outputs all SQL queries/statements executed via options)--server
- set the server hostname to connect to on startup (saves having to explicitly connect via option)--db
- set the database name to connect to on startup (saves having to explicitly connect via option)--script
- the path of a text file containing a SQL script to execute against the connected server (requires bothserver
anddb
to be provided as args). This is for non-interactive mode.--output
- the path of a file that output will be redirected to. Useful for non-interactive mode.
RedknifeSQL interactive mode outputs a numeric menu to the user and loops until the user quits. The options are as follows:
Connect to server
- initiates a connection to a specific server and database - the user is prompted for these valuesGet current user information
- Outputs information about the current user database connection - theSYSTEM_USER
andUSER_NAME()
values, as well as whether the user has thepublic
andsysadmin
roles.Get impersonatable roles
- gets the MSSQL server principals that can be impersonated by the current user.Connect to UNC path
- attempts to execute connect to the provided UNC path by executing thexp_dirtree
procedure. Handy for grabbing NetNTLM hashes.Set 'EXECUTE AS LOGIN' value
- sets theEXECUTE AS LOGIN
valueSet 'EXECUTE AS USER' value
- sets theEXECUTE AS USER
valueCheck if xp_cmdshell enabled
- checks whetherxp_cmdshell
is enabled on the serverSet xp_cmdshell status
- attempts to enable/disablexp_cmdshell
on the connected server. Also enablesshow advanced options
. Requires appropriate permissions.Execute xp_cmdshell
- attempts to executexp_cmdshell
on the connected server. Requiresxp_cmdshell
to be enabled, and the appropriate permissions.Execute cmd via sp_OACreate/sp_OAMethod
- enablesOle Automation Procedures
and attempts to usesp_oacreate
to executewscript.shell
. Then callssp_oamethod
with the user provided shell command.List TRUSTWORTHY databases
- lists the system databases with theis_trustworthy_on
flag set to1
and the owner of the databaseCreate assembly stored procedure
- usesCREATE ASSEMBLY
to create a new DLL-based stored procedure for command execution. Enablesshow advanced options
,clr enabled
and disablesclr strict security
.Execute cmd via assembly stored procedure
- attempts to call thecmdExecute
stored procedure (created by option12
) with the user-provided command.Get linked SQL server info
- get information about the configured linked SQL servers, including usernames, roles and which user has the linked server connectionCall xp_cmdshell on linked server
- attempts to callxp_cmdshell
on a linked server. Note that this can be chained via serval linked servers by supplying the hostname as a comma-separated string (i.e. hostA,hostB,hostC). This option generates the appropriateEXEC (...) AT [server]
query depending on the number of hosts supplied. This option also attempts to enableshow advanced options
on the target server.PrivEsc via callback xp_cmdshell from linked server
- callsxp_cmdshell
on a linked server to gain priv esc on current server (in cases where current user does not havexp_cmdshell
permissions on the current server)Execute SQL Command
- execute an arbitrary SQL command provided by the user. Formats the output as a table.Get server principals
- lists theuser
,pricipal_id
,type_desc
anddefault_database_name
fromsys.server_principals
.
Entering option value q
, quit
or exit
(irrespective of case) will exit RedknifeSQL and close the current DB connection (if one exists).
In script mode, RedknifeSQL reads an input file (specified by the --script
argument) containing SQL statements to execute. Note that the --server
and --db
command line args must be supplied in order to specify which server and database to connect to.
Upon connecting to the specified database, RedknifeSQL automatically executes the following options:
Get user information
(option 2)Get impersonatable roles
(option 3)Get server principals
(option 18)Check if xp_cmdshell enabled
(option 7)List TRUSTWORTHY databases
(option 11)Get linked SQL server info
(option 14)
Once these options have been executed, RedknifeSQL attempts to execute the script/statements as specified in the --script
argument.
RedknifeSQL can also be run as an InstallUtil Uninstall process, i.e.:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U C:\path\to\RedknifeSQL.exe
When run as an uninstaller, you can provide the arguments to RedknifeSQL as a text file named RedknifeSQL.txt
(located in the same directory as RedknifeSQL.exe
) - these will be parsed and passed to RedknifeSQL as if they had been specified at the command line directly.
Quick note: when running as an uninstaller, the presence of RedknifeSQL.txt is required. However, if no args are specified in the file, RedknifeSQL will drop into interactive mode, which is quite handy :). I should get round to fixing this i.e. allow it to drop into interactive mode by default if no RedknifeSQL.txt file present.