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.
Description
Attack Execution Flow
Step 1
ExploreThe 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.
L'avversario identifica un sistema di gestione del database in esecuzione su una macchina che desidera controllare, o su una rete attraverso cui vuole muoversi lateralmente.
Step 2
ExperimentThe adversary goes about the typical steps of an SQL injection and determines if an injection is possible.
L'avversario segue i passaggi tipici di un'iniezione SQL e determina se è possibile un'iniezione.
Step 3
ExperimentOnce 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.
Una volta che l'Adversary determina che è possibile un'iniezione SQL, deve assicurarsi che siano soddisfatti i requisiti per l'attacco. Questi sono un utente con sessione ad alta privilegio e il supporto alle query batch. Ciò viene fatto in modi simili a quelli utilizzati per scoprire se un'iniezione SQL è possibile.
Step 4
ExperimentIf 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
Se i requisiti sono soddisfatti, in base al sistema di gestione del database in esecuzione, l'attaccante troverà o creerà funzioni definite dall'utente (UDF) che possono essere caricate come DLL. Un esempio di DLL può essere trovato su https://github.com/rapid7/metasploit-framework/tree/master/data/exploits/mysql
Step 5
ExperimentIn 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'
Per caricare il DLL, l'attaccante deve prima trovare il percorso della directory dei plugin. Il comando per ottenere questa informazione varia in base al tipo di DBMS, ma per MySQL, ciò può essere fatto eseguendo il comando "select @@plugin_dir"
Step 6
ExploitThe DLL is then moved into the previously found plugin directory so that the contained functions can be loaded. This can be done in a number of ways; loading from a network share, writing the entire hex encoded string to a file in the plugin directory, or loading the DLL into a table and then into a file. An example using MySQL to load the hex string is as follows. select 0x4d5a9000... into dump file '{plugin directory}\\udf.dll';
Il DLL viene quindi spostato nella directory del plugin precedentemente individuata in modo che le funzioni contenute possano essere caricate. Questo può essere fatto in diversi modi; caricando da una condivisione di rete, scrivendo l'intera stringa codificata in esadecimale in un file nella directory del plugin, o caricando il DLL in una tabella e successivamente in un file. Un esempio di utilizzo di MySQL per caricare la stringa esadecimale è il seguente: select 0x4d5a9000... into dump file "{plugin directory}\\udf.dll";
Step 7
ExploitOnce the DLL is in the plugin directory, a command is then run to load the UDFs. An example of this in MySQL is 'create function sys_eval returns string soname 'udf.dll';' The function sys_eval is specific to the example DLL listed above.
Una volta che la DLL si trova nella directory dei plugin, viene eseguito un comando per caricare gli UDF. Un esempio di questo in MySQL è "create function sys_eval returns string soname 'udf.dll';" La funzione sys_eval è specifica per la DLL di esempio elencata sopra.
Step 8
ExploitOnce 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.
Una volta che l'avversario ha caricato la/e funzione/i desiderata/e, le utilizzerà per eseguire comandi arbitrari sul sistema compromesso. Ciò avviene tramite un semplice comando select sulla UDF caricata. Ad esempio: "select sys_eval('dir');". Poiché il prerequisito per questo attacco è che l'utente della sessione del database sia un superuser, ciò significa che l'avversario sarà in grado di eseguire comandi con privilegi elevati.
Mitigations
Configuration: Ensure That The Dbms Is Patched With The Latest Security Patches
Design: Ensure That Dbms Runs With The Lowest Possible Level Of Privileges On The Host Machine And That It Runs As A Separate User
Design: Ensure That The Dbms Login Used By The Application Has The Lowest Possible Level Of Privileges In The Dbms
Design: Follow The Defensive Programming Practices Needed To Protect An Application Accessing The Database From Sql Injection
Implementation: Remove / Disable All Unneeded / Unused Functions Of The Dbms System That May Allow An Attacker To Elevate Privileges If Compromised
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: Do Not Use The Dbms Machine For Anything Else Other Than The Database
Usage: Use An Intrusion Detection System To Monitor Network Connections And Logs On The Database Host.
Consequences
Consequence Information
{'impacts': [], 'impacts_translate': [], 'scopes': [], 'scopes_translate': []}