Today I'm talking about Oracle Rootkits and a little bit about Oracle Viruses, the first and the second generation. Short introduction of myself, my name is Alexander Kornprost from Germany and my company Red Database Security is specialized in Oracle Security. Okay, we have only limited time so I start fast. Operating systems and databases are quite similar in the architecture. In both systems you have users, you have processes, you have jobs, executables and symbolic links. And then I made the assumption, okay, if this is the case then a database is a kind of operating system. It's not identical but it's a kind of. And to prove my assumption I made a chart so you can see the PS command in the operating system world. It's a select star from V dollar process in Oracle or select star from sys processes in SQL server. In the database you can kill database jobs or database processes with the alter system command. You have executables like view, packages, procedures, functions. You can execute executables, for example, with select star from view or exec procedure. And in some databases you have even a kind of CD. In Oracle you can switch into different schemas with Oracle. Alter session set current schema equal and then the name of the schema. And then my assumption was, okay, if a database is a kind of operating system then it's possible to migrate all the malware stuff from the operating system world into the database world. Because it's the same, it's only a migration. And my first idea, okay, let's do a proof of concept virus in Oracle. Everybody here in the room knows, okay, viruses replicates itself, yada, yada, yada. And we have different type of viruses. We have append viruses, companion viruses. So if you start in the beginning, 15, 20 years ago. And the classical append virus in the operating system world looked like this. We had our executable code and then we append the virus code at the end and we jump from the beginning to the end, execute the code and then jump back. Okay. Okay. So we want to migrate this concept into the Oracle world or the database world. First, instead of using the operating system, we are using the database. Instead of using a binary file, we are using a view. And instead of using assembler, we are using PL, SQL. And it looks quite similar. So we have a view and we append our view at the end of this view. And now this is a proof of concept virus. This is a proof of concept. So on the left side, we have the views, my employee, select staff from employees for salary bigger than 5,000. And on the right side, we are appending a PL, SQL function. And every time you run a select from the view, from the now infected view, this function is executed. And this function is looking for new, uninfected views. And we can also spread. So virus must spread. So via database links, we can also connect to other databases. We can do HTTP requests from the database in many cases. And then we can go to Google, retrieve data. And I did a little bit research. I was able to implement six, seven different concept of database viruses. I don't think database viruses will become a big problem. For me, it was only to sharp my skills, a proof of concept. Okay. And here's an example how to do the first infection. For example, this is possible via the web. Two and a half years ago, I found a bug in Oracle package. And you can execute your own code. You can also create functions and procedures over the web. So if you do a Google search for PLS and DAD, like portal or something like this, you can call the package via iLoad validate statement. And then you can infect the first infection. Okay. And this is the first function in the database. Because all these commands are executed with DBA privileges. And then we create our first virus. Then we grant privileges. And then we infect the view. So this was the first proof of concept that it's possible to migrate malware from the operating system world to the database world. I think it's not a big problem or will not become a big problem. And then I... I tried the next step, operating system or rootkits. So everybody here in the room knows rootkits, I guess. You can use it to protect your music from being stolen, like Sony. Here without the Sony rootkit, you see this $sys, $rk.exe. And if the Sony rootkit is running, it's no longer visible. And you can also hide operating system users. If you want to migrate this concept into the database world, we must do an informal definition of a rootkit. A rootkit is informal. We are hiding objects like users, jobs, and processes in the operating system. And now we are trying to hide operating system use...database users in the database. And generally, there are two concepts to implement a rootkit. One possibility is we can modify the object itself. For example, in the operating system world, we modify the ls command. Or we change the execution path, like the path. And I presented the database rootkit last year at Black Hat Europe. And today I will give a short introduction from the stuff last year and the second generation. And the second generation does no longer need modifications in the data dictionary. And it's also possible to write a new code. You can write a new code in your database. third generation rootkits because Oracle released with Oracle 10g release to an API and with this API you can directly access the memory of the database and you can also modify the memory in the database the first generation is easy to find easy to implement and it affects all databases all relational databases not only Oracle I'm Oracle expert so I did everything with Oracle and on other databases it's a little bit more complicated and in sequel server 2005 Microsoft already implemented some anti database rootkit technologies so they are signing views and if the view is modified you are getting a warning but Oracle is not so fast to do this before hiding operating database users we must you check our and a maker solution so what happens if you are checking for user name the normal committee selected user name from the users if you are doing this article checks first is still a local object it occurred schema cold users if yes use it normally did nós such an object then we are looking for privates and immune is also normally not to privates in the mean then we are looking for public's new name and normally does the public suggestions synonym DBA users. And after everything, before it's executed, Oracle checks if there's a virtual private database rule, a special concept of Oracle, and then it's possible to modify the statement via virtual private database. So we have here from top to down, if we are user one, Oracle looks first for the object itself, then for private synonym, and then for public synonym. So we can modify the objects, but we can also change the execution path by creating a local object with the same name. We can create a private synonym pointing to a different object. We can also create or modify a public synonym, or we can switch to a different schema with alter session sets, current schema. And the user management is quite simple. It's a very simple process. It's a very simple process. It's a very simple process. In Oracle, we have users and we have roles, and both objects are stored together in the same table, this user dollar. And users have to flag type number one and roles to flag type number zero. And to make everything access easier and simpler, Oracle provides two views, DBA users and all users, and public synonyms. So wherever you are in the database, you can submit a select star from all users. Okay. If you create a user, create user identified by hacker, grant DBA to a hacker. A clever hacker would never use such an obvious username. He would use something more similar to an Oracle account, like mtsys, or, which sounds like an Oracle account. And then if you are submitting the select username from DBA users, we see here the username hacker. But nowadays, in times of graphical user interfaces, most DBAs, they don't have the same username as the user integrators. The DBAs are using for the daily work graphical user interfaces. Quite common is Oracle Enterprise Manager, the Java version, and we see there's a user hacker or the database or grid control or tools like Toad from Quest, third party vendor. Now comes the trick. We are just modifying the view and we add one single line, and uname not equal hacker. This means the view shows everything except the user hacker. doing this simple modification and our user disappears and also from database security scanners and stuff like that most of the tools are using views instead of the base objects only tote in tote the user is still visible now you can say okay Oracle is doing crappy software that's not always the case is tote a better program no tote is just using a different view the view all users so if you modify the view all users and add our end user name not equal hacker then the user is also gone in tote and in most cases if you don't see something then it's invisible you can still look up in the table this user dollar but most DBAs are looking in the DBA users view so you should never trust use for critical objects and in this case we modify to view here in the source object but we could also create a private zoom in pointing to different objects so there are endless possibilities okay now we have an invisible user not 100% invisible but for most cases now we can hide processes it's the same game we have also different views for sessions in Oracle and with select SID serial number program from the dollar session we can get a list of the running sessions at the moment and then by doing for modification in for objects we can hide this information from the typical select statement or user interfaces one of the problems is if you apply as a security update on normal database patch set Oracle quite often recreates the views and then our invisible user is gone that's why it's always useful for an attacker to create a database job and the database job is looking once a day if the change still persists and then if not the job is doing the modification of the object so we can survive database updates so here we can create a view and then we see okay the view is visible and then same game we modify the view and the job is still running but it's not shown if you apply update you will see it but then the DBA you have this short time frame between the job is running and the modification again so this is quite easy everybody in this room can can modify the view and then the job is running and the modification again so this is quite easy everybody in this room can can modify this it's so simple and it's also simple to find even if the views are quite long if you have if you have a deeper look in the views you can find a modification this is a simple first-generation rootkit as a script so first Oracle has a package called tbms metadata and with DBMS metadata you can can create a DDL statement of an object. And then we spool the result in a text file. And then we say, OK, get the DDL statement of the view all users. And then we replace where username not equal hacker. And then this is written in the file RK underscore source. And then we create a user and execute the view. And after doing this, we have at least the user hacker is not visible in the normal tools. But modification in views is always too obvious. So it would be much better if we implement our backdoor in an Oracle database package. Oracle comes with a lot of functions and procedures. In Oracle 10G, there are 8,000 function and procedures granted to public. But all Oracle packages are obfuscated. Oracle 10G is not visible. Oracle calls this wrapping. And a few days ago at a Black Hat, Pete Finnegan gave a presentation how to unwrap this. But there are also working unwrappers there. So you can convert the wrapped system packages from Oracle into text file. Then we can implement our backdoor, wrap the stuff again, and install it in the database. So here's one. I have also such an unwrapper. This is quite useful. It's quite useful if you are looking for security bugs. Because just with simple crap statements, you find vulnerabilities. And here I'm unwrapping the package DBMS output, which is a quite common package. And now there's one possibility. We can say if this package is executed, and if it's Sunday or Saturday, we escalate the privileges of the user's code. This is normally much more intelligent than creating a user with DBA. So if you change the DBA privileges, if you change password and privileges, because quite often this is not audited. And then on the normal weekdays, we revoke the DBA privileges from Scott. So during a normal security audit, during business hours, during the week, you will never find this vulnerability. Because the package is wrapped, and during the week, everything looks fine. And at the weekend, you have DBA privileges. Then we wrap this package again, install it in the database. So the only thing which was changed is modified checksum. Another possible approach is we can call a function. Here we have the same function enable. And if the value of the buffer size is 31337, we can call a function. Then we grant privileges. So whenever we need DBA privileges, we are calling this procedure with a special value. And after that, we add DBA. And we are doing our work. And then we can reset it again. So this is more ancient. You would have DBA privileges on request. Install it again. And so on. This was all stuff. And if you're using checksum, so we add all this stuff, it's easy to find. So checksum about all objects and if there are modifications then you should have a deeper look in the modify objects. But my goal was to to have a rootkit without modification in the data dictionary because then you cannot use checksums to find this stuff. And I found several possibilities how to implement this. One possibility is the modification of binary files. So what happens if you connect to an Oracle database? Oracle looks up in the table this user dollar. The question is how does Oracle know to look up in this table? Because it's somewhere hard coded. And in Oracle 10g for example there are 106 occurrences in the binary file. And all the select statements in the binary files are not obfuscated. So with a simple text editor you can rename this table. So if you log on to the database Oracle is now looking in a different table. The old table still exists. So a security auditor which heard of database rootkit first generation even if you look up in the this user dollar table he will not find the back door. So the guy is looking in the wrong table. So here we create a user with DBA privileges. Then we create a table because we must do this before we change the table name in the binary file. So first we create a hacker. This hacker is created in this user dollar table. Then we create a copy of this user dollar table containing the user hacker. And then we drop the user hacker from this user dollar. So it's not available in this user dollar but in this table. Then shut down the database, replace everything. And then we can log on with this user. Sometimes it depends on the database. If you have a lot of user creation activities you can think about the possibility to create a trigger on the ASAR table. So every time, yes? You can do this with a trigger but it depends on the database. Quite often for example in the SAP database you have only five or six users and there's no change. So there's no need to synchronize this stuff. But for synchronizing you can use a trigger. But to do this a trigger does not work on this object. You must create an object in the different schema. So this is the way how to do this. It's quite simple. And the protection from Oracle. Oracle should sign their binaries. So I think it's not a good idea to allow users to patch SQL statement in the binaries. You should use checksum tools. It depends on your level of paranoia on the binary files. And you should harden your database to avoid hackers. And it's also a good programming style to obfuscate the SQL statements in the binary. Normally if you do a normal strings, something like this, you should never see SQL statements. This was one possibility. Second possibility. Oracle has a feature called PLSQL native. In PLSQL native you can convert a PLSQL statement into C code. And the C code is compiled on the platform and then executed. So instead of executing the PLSQL procedure, you are executing a binary. And Oracle generates a C file. And in Oracle 9i, these C files are, these DLLs libraries are stored in the file system. And in 10g, Oracle copies the file system. And in 10g, Oracle copies the file system from the database, from the file system into the database. This PLSQL native feature is also the fastest way to run operating system commands. Because there's a command alter session set PLSQL native make utility. And I just said, okay, I want to use the make utility calc. And then every time I create or modify a PLSQL procedure, the Windows calculator pops up on the database server. In 10g, Oracle changed this. So in 10g, Oracle is looking in the registry where Microsoft Visual Studio is installed or in the environment variable and is taking the value from there. And in 10g, Oracle is looking up for the compiler statement in this text file. So if you compile a statement, Oracle calls this. And then it generates the file. To switch in this PLSQL native mode, you can say alter session set PLSQL compiler flex equal native. And then alter procedure, my procedure compile. In this case, Oracle generates the C file, compiles the C file, and the result is a DLL. In 10g, this DLL is loaded into the, into a CLOB in the database. This is a part of the generated C file. This and this. Now we implement a backdoor in our procedure. And the result is Oracle generates a C file. Then compiles the C file. And now we have a DLL with a backdoor. From this file we are creating a copy. Now we undo the change. So Oracle again creates the C file and creates a DLL without the backdoor. And Oracle is not monitoring these files on the operating system level, so we are replacing the copy with the backdoor without backdoor. So now every time we execute the my procedure, Oracle is executing the backdoored version in the operating system. So just by doing checksums in the database, you are not able to find this kind of rootkit. So if you don't need PLSQL native, I would recommend not to use it. And you should also check there are some data dictionary tables where you can look up if PLSQL native is used in your environment. The third possibility is pinning. I will show you how. Oracle in older versions had a bigger problem with memory fragmentation of the memory of the database. So they introduced a concept called pinning. With a package DBMS share pool, you are able to pin PLSQL packages into the memory for performance reasons and to avoid fragmentation. The problem is these changed objects are not automatically reloaded into the memory if you change these in the database. So this is the problem. So the concept is identical. We have a procedure. Then we create a backdoor. And then with DBMS shared pool.keep, we are loading this backdoored procedure into the memory. And from now on, Oracle will always execute the my procedure in the memory instead of the package in the database. Then we remove the backdoor. so our data dictionary again is clean not no modification and the problem is if you change the package if you don't do a drop and recreate the package is available in the database until you stop the database or you clean the cache but normally databases are quite stable so it talks sometimes databases are not restarted for months or even years so if you are responsible for the security you should check if DBMS shared pool is installed and you should check from time to time for pin packages if you do some forensics before stopping the database you should always do a dump of the memory so you have a proof of concept that you have some evidence if there's something in the memory an Oracle has a you have some evidence if there's something in the memory an Oracle has a small tool dump SGA to do this Oracle has so many possibilities that I expect much more possibilities to implement rootkits without modifications Oracle has a functionality called virtual private database and this functionality is able to modify a sequel statement afterwards so you run a select staff from table and Oracle at the end select staff from table and Oracle at the end user name not equal something so this is possible and then by using special VPD user name not equal something so this is possible and then by using special VPD rules we can remove table content and you can only find these kind of rules we can remove table content and you can only find these kind of vulnerabilities if you have a special privilege exempt access policy or if vulnerabilities if you have a special privilege exempt access policy or if you're working as so my recommendation if you are doing a security audit you you're working as so my recommendation if you are doing a security audit you should always work with the highest number of should always work with the highest number of privileges. Another possibility is in Oracle feature called query rewrite. For performance reason the Oracle optimizer is able to completely rewrite the select statement. For example you submit a select star from table A, then Oracle is able to say okay it's much faster if I use a materialized view so Oracle changed this in select star from table B. And it's completely transparent so you don't see anything. You just get the result back and even if you look up okay I submitted a select star from table A, Oracle is doing something else. And I haven't worked with this feature but I also think it's possible to do something with this. The third generation I mentioned it already. I think it's quite difficult to implement. You need operating system access and there are already some tools available. The question is why has Oracle implemented such a feature. The reason is simple if you have a problem with your database and you are not able to submit SQL statements to check for performance problems. Then you can directly connect to the memory and do your modifications. You can change values and so on. You can change data blocks. And these rootkits will be much more difficult to find but you need operating system access. So one of the big problems I mentioned it already is to survive updates. For example what happens if you import or if you update the database dictionary or the binaries. Oracle rebuilds everything from scratch. And to protect our rootkit we could create a special database job. I showed this already. Or we can modify the glogin.sql. This is an auto startup file on the database server. And if someone calls sqlplus on the database server the entire content in this file is executed. So we could also implement something in the glogin file. Oracle has also a database startup trigger. And this code is always executed if you start a database. So we can also implement something in the database startup trigger. Or instead of backdooring Oracle packages we can backdoor custom packages because quite often the customer has his own application also written in PL SQL and you can also implement backdoors there. To find rootkits like always in the rootkit world checksums baselines. So you check you should check some database objects. You should check some binary files. And for the advanced one you should check if PL SQL native is active. DBMS shared pool is installed. And it's quite important to harden your database. From my experience in most cases you can break into the database. So because patches are missing, default passwords, the same old game. And in most cases it's not a challenge to become DBA of a database. In 10G Oracle did a quite good job. So 10G is much more difficult to hack and it separates the men from the boys. But in 8i and 9i it's quite easy to become DBA. And Oracle is much more powerful than SQL Server for example. That's why you have a bigger you have more complexity and more complexity means also more security holes and more possibilities. So at the moment you should you should try to get rid of all unneeded features in the database. Ok questions? Ok thank you.