CAPEC Details
Name Expanding Control over the Operating System from the Database
Likelyhood of attack Typical severity
Medium Very High
Summary An attacker is able to leverage access gained to the database to read / write data to the file system, compromise the operating system, create a tunnel for accessing the host machine, and use this access to potentially attack other machines on the same network as the database machine. Traditionally SQL injections attacks are viewed as a way to gain unauthorized read access to the data stored in the database, modify the data in the database, delete the data, etc. However, almost every data base management system (DBMS) system includes facilities that if compromised allow an attacker complete access to the file system, operating system, and full access to the host running the database. The attacker can then use this privileged access to launch subsequent attacks. These facilities include dropping into a command shell, creating user defined functions that can call system level libraries present on the host machine, stored procedures, etc.
Prerequisites A vulnerable DBMS is usedA SQL injection exists that gives an attacker access to the database or an attacker has access to the DBMS via other means
Execution Flow
Step Phase Description Techniques
1 Explore The adversary identifies a database management system running on a machine they would like to gain control over, or on a network they want to move laterally through.
2 Experiment The adversary goes about the typical steps of an SQL injection and determines if an injection is possible.
3 Experiment Once the Adversary determines that an SQL injection is possible, they must ensure that the requirements for the attack are met. These are a high privileged session user and batched query support. This is done in similar ways to discovering if an SQL injection is possible.
4 Experiment If the requirements are met, based on the database management system that is running, the adversary will find or create user defined functions (UDFs) that can be loaded as DLLs. An example of a DLL can be found at https://github.com/rapid7/metasploit-framework/tree/master/data/exploits/mysql
5 Experiment In order to load the DLL, the adversary must first find the path to the plugin directory. The command to achieve this is different based on the type of DBMS, but for MySQL, this can be achieved by running the command "select @@plugin_dir"
6 Exploit Once the adversary has loaded the desired function(s), they will use these to execute arbitrary commands on the compromised system. This is done through a simple select command to the loaded UDF. For example: "select sys_eval('dir');". Because the prerequisite to this attack is that the database session user is a super user, this means that the adversary will be able to execute commands with elevated privileges.
Solutions Design: Follow the defensive programming practices needed to protect an application accessing the database from SQL injection Configuration: Ensure that the DBMS is patched with the latest security patches Design: Ensure that the DBMS login used by the application has the lowest possible level of privileges in the DBMS Design: Ensure that DBMS runs with the lowest possible level of privileges on the host machine and that it runs as a separate user Usage: Do not use the DBMS machine for anything else other than the database Usage: Do not place any trust in the database host on the internal network. Authenticate and validate all network activity originating from the database host. Usage: Use an intrusion detection system to monitor network connections and logs on the database host. Implementation: Remove / disable all unneeded / unused functions of the DBMS system that may allow an attacker to elevate privileges if compromised
Related Weaknesses
CWE ID Description
CWE-89 Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
CWE-250 Execution with Unnecessary Privileges
Related CAPECS
CAPEC ID Description
CAPEC-66 This attack exploits target software that constructs SQL statements based on user input. An attacker crafts input strings so that when the target software constructs SQL statements based on the input, the resulting SQL statement performs actions other than those the application intended. SQL Injection results from failure of the application to appropriately validate input. When specially crafted user-controlled input consisting of SQL syntax is used without proper validation as part of SQL queries, it is possible to glean information from the database in ways not envisaged during application design. Depending upon the database and the design of the application, it may also be possible to leverage injection to have the database execute system-related commands of the attackers' choice. SQL Injection enables an attacker to talk directly to the database, thus bypassing the application completely. Successful injection can cause information disclosure as well as ability to add or modify data in the database. In order to successfully inject SQL and retrieve information from a database, an attacker: