Enum Databases
Theory
When exploiting SQL injection vulnerabilities, or when you gain access to the database itself, it is often necessary to gather some information about the database itself. This includes the type and version of the database software, and the contents of the database in terms of which tables and columns it contains or even users and permissions informations.
Practice
Database version
Different databases provide different ways of querying their version. You often need to try out different queries to find one that works, allowing you to determine both the type and version of the database software. The queries to determine the database version for some popular database types are as follows:
SELECT @@version SELECT @@version SELECT banner FROM v$versionSELECT version() SELECT sqlite_version();Database Names
When performing SQL injections, it can be useful to know the names of the databases that are present on the targeted server. Enumerating the database names allows you to identify which databases are available and potentially gain insight into the server's configuration and architecture. This information can be used to craft more targeted and effective SQL injection attacks.
We can enum the current database with the following query:
SELECT database();We can list all databases with the following query:
SELECT schema_name FROM information_schema.schemata;We can enum the current database with the following query:
SELECT DB_NAME();We can list all databases with the following queries:
SELECT name FROM master..sysdatabases;
#Or
SELECT DB_NAME(N); — for N = 0, 1, 2, …
#Or in mssqlclient's impacket shell
enum_dbWe can enum the current database with the following queries:
SELECT global_name FROM global_name;
SELECT name FROM V$DATABASE;
SELECT instance_name FROM V$INSTANCE;
SELECT SYS.DATABASE_NAME FROM DUAL;We can list all databases with the following query:
SELECT DISTINCT owner FROM all_tables;We can enum the current database with the following query:
We can list all databases with the following query:
We can extract current database structure with the following query:
We can list all databases with the following query:
Tables Names
The next step in performing SQL injections is to enumerate the tables that are present within each database. Enumerating the table names can provide valuable information about the structure and content of the databases.
Columns Names
Next step is to enumerate columns within tables.It's a crucial step in the process of exploiting a SQL injection vulnerability.
DB Users
Additionally, we may enumerate DB users with following queries.
In MSSQL, logins and users are both types of security principals, but they operate at different scopes.
A login is defined at the server level and is used to authenticate access to the SQL Server instance
An user is defined at the database level and controls access to specific database resources.
A single login can be associated with one user per database, allowing it to access multiple databases under distinct user contexts.
Enumerate Users
Enumerate Logins
Permissions & Privileges
Sometimes it can be useful to enumerate user's permissions or privileges. We can acheive this with the following queries.
Introduction about some MSSQL terms:
Securable: These are the resources to which the SQL Server Database Engine authorization system controls access. There are three broader categories under which a securable can be differentiated:
Server – For example databases, logins, endpoints, availability groups and server roles
Database – For example database role, application roles, schema, certificate, full text catalog, user
Schema – For example table, view, procedure, function, synonym
Permission: Every SQL Server securable has associated permissions like ALTER, CONTROL, CREATE that can be granted to a principal. Permissions are managed at the server level using logins and at the database level using users.
Principal: The entity that receives permission to a securable is called a principal. The most common principals are logins and database users. Access to a securable is controlled by granting or denying permissions or by adding logins and users to roles which have access.
Privileges that are inhereted through other roles will not be readily shown. To resolve this, it is advisable to use this advanced script by David Arthur:
Resources
Last updated
Was this helpful?