| 1 |
to: [email protected] |
| 2 |
Subject: StackGuard with ... Re: [Paper] Format bugs. |
| 3 |
In-reply-to: Your message of "Tue, 18 Jul 2000 17:10:50 +0200." |
| 4 |
<[email protected]> |
| 5 |
Date: Fri, 21 Jul 2000 12:21:20 -0400 |
| 6 |
From: Alan DeKok <[email protected]> |
| 7 |
|
| 8 |
Pascal Bouchareine <[email protected]> wrote: |
| 9 |
> This paper tries to explain how to exploit a printf(userinput) format |
| 10 |
> bug, reported in some recent advisories. The approach is primary, and |
| 11 |
> more precisely does not take into account any existing exploit (wu-ftpd, ...). |
| 12 |
> [ ... ] |
| 13 |
> |
| 14 |
> When sprintf encounters a conversion string, it simply takes the first pushed |
| 15 |
> word (32 bits, 4 bytes on intel) on the stack and in the case of "%x" |
| 16 |
> converter, prints it to screen as hexadecimal. |
| 17 |
|
| 18 |
That is, you easily examine the stack remotely. This allows you to |
| 19 |
gain additional information about the system, such as the status of |
| 20 |
it's protection against stack smashing attacks, e.g. |
| 21 |
|
| 22 |
http://immunix.org/stackguard.html |
| 23 |
|
| 24 |
|
| 25 |
My reading of their pages and papers leads to me to conclude that |
| 26 |
they have an implicit assumption (I don't notice it explicitely |
| 27 |
stated) that the attacker does NOT have read access to the stack. The |
| 28 |
Stack Guard papers seem to assume that "blind" buffer overflows are |
| 29 |
the primary means of attack. |
| 30 |
|
| 31 |
[ Crispin Cowan (author of StackGuard) responds: ] |
| 32 |
|
| 33 |
At the time StackGuard was built, and at present, blind overflows |
| 34 |
are the primary means of attack. This % printf hack has the potential |
| 35 |
to change that. [1] |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
As the "Format bugs" paper pointed out, it is possible to READ the |
| 40 |
stack, as well as to write (nearly) arbitrary data to the stack of the |
| 41 |
target machine. The obvious conclusion is that SOME methods of stack |
| 42 |
"canaries" may be externally discovered, and externally bypassed. I |
| 43 |
will not go into details here, as they should be readily apparent from |
| 44 |
Pascal's paper. |
| 45 |
|
| 46 |
The methods that Stack Guard uses to protect the stack are: |
| 47 |
|
| 48 |
- Random canaries |
| 49 |
- NULL canary (0x00000000) |
| 50 |
- Terminator canary (combination of NULL, CR, LF, -1) |
| 51 |
|
| 52 |
These canaries are all constant. They MAY be discovered by an |
| 53 |
external attacker, and it MAY be possible to re-write the canaries |
| 54 |
with an introspective stack overflow. This will bypass the canary |
| 55 |
protection. |
| 56 |
|
| 57 |
There is no obvious method of using constant canaries to protect the |
| 58 |
stack against an introspective attacker. They do, however, protect |
| 59 |
very well against blind buffer overflows. |
| 60 |
|
| 61 |
|
| 62 |
Stack Guard has another method to protect against attacks which |
| 63 |
do not overflow the stack, but in which: |
| 64 |
|
| 65 |
... the attacker can cause the p pointer to point anywhere in |
| 66 |
memory, but especially at a return address record in an |
| 67 |
activation record. When the program then takes input and stores |
| 68 |
it where p points, the input data is stored where the attacker |
| 69 |
said to store it. [2] |
| 70 |
|
| 71 |
|
| 72 |
The XOR Random Canary protects against this attack by having it |
| 73 |
|
| 74 |
XOR the canary with the return address word, so that the return |
| 75 |
address is bound to the random canary value. [2] |
| 76 |
|
| 77 |
|
| 78 |
If the attacker can examine the stack, this protection may be |
| 79 |
bypassed as well. The return address is usually well known, or at |
| 80 |
least strongly bounded. It is then possible to use the known return |
| 81 |
address to discover the value of the random canary, and to XOR it with |
| 82 |
a return address chosen by the attacker. Any security offered by the |
| 83 |
XOR Random Canary is then bypassed. |
| 84 |
|
| 85 |
|
| 86 |
Stack Guard does use more than one XOR Random Canary, which |
| 87 |
increases the difficulty of the discovering or predicting which of the |
| 88 |
128 random canaries will next be used. |
| 89 |
|
| 90 |
Note, however, that the stack can be observed, and the canaries |
| 91 |
discovered, WITHOUT the attacker writing to the stack. As the stack |
| 92 |
is not written to, the Stack Guard protection mechanism will never |
| 93 |
fire, and the administrator will never notice that an attacker is |
| 94 |
nearing the ability to bypass the Stack Guard protection. |
| 95 |
|
| 96 |
|
| 97 |
A more secure method of using XOR canaries would be to continuously |
| 98 |
generate new canaries from a cryptographically strong pseudo-random |
| 99 |
number generator. By choosing a good CS-PRNG, the Stack Guard |
| 100 |
protection can make it arbitrarily difficult for the attacker to |
| 101 |
discover the next random canary. The attacker thus can never gain |
| 102 |
undetected control of the stack. |
| 103 |
|
| 104 |
[ Crispin Cowan responds: ] |
| 105 |
|
| 106 |
The values are "constant" (statically bound to functions) because |
| 107 |
you need to be able to remember the canaries you left on the stack, in |
| 108 |
stack order. Normally, one would use a stack to remember such things, |
| 109 |
but we're trying to protect the stack, so that won't work so well :-) |
| 110 |
Regardless of your random number generation technique, it must be |
| 111 |
possible for the function cleanup code to decide whether the canary is |
| 112 |
good when the function returns. [1] |
| 113 |
|
| 114 |
|
| 115 |
Even if a CS-PRNG was used, Pascal Bouchareine's paper comes in |
| 116 |
handy. The state of the PRNG may be discovered by the attacker, by |
| 117 |
having him: |
| 118 |
|
| 119 |
> issue the following "pseudo string" to print out the buffer: |
| 120 |
> |
| 121 |
> [4 bytes address of find_me]%s |
| 122 |
> |
| 123 |
> Yes! It is *that* simple: in this case, the input buffer is both the format |
| 124 |
> string AND the format string argument.. :) |
| 125 |
> |
| 126 |
> Let's do it simply : |
| 127 |
> |
| 128 |
> [pb@camel][formats]> printf "\x02\x96\x04\x08%s\n" | ./v |
| 129 |
|
| 130 |
|
| 131 |
The attacker can thus examine arbitrary memory on the machine, which |
| 132 |
allows him to eventually discover the PRNG, and it's state. The |
| 133 |
challenge, then, is to put the state of the PRNG in a location which |
| 134 |
is not easily discoverable by an attacker. It is also not obvious how |
| 135 |
to protect a program against an attacker who can see it's entire |
| 136 |
internal state. |
| 137 |
|
| 138 |
|
| 139 |
We should note that this sort of attack is NOT the classic and |
| 140 |
well-known buffer overflow attack. As Crispin says above, this attack |
| 141 |
appears to be new. |
| 142 |
|
| 143 |
|
| 144 |
In summary, an attacker who is introspective of the stack can bypass |
| 145 |
all predictable methods of protecting the stack with canaries. |
| 146 |
Programmers should carefully audit their code for the sort of % printf |
| 147 |
style vulnerabilities. An automated scanner PScan which *may* help |
| 148 |
(but which isn't perfect) is available.[3] |
| 149 |
|
| 150 |
There is no substitute, however, for a careful line-by-line audit of |
| 151 |
code. |
| 152 |
|
| 153 |
|
| 154 |
Alan DeKok. |
| 155 |
|
| 156 |
References |
| 157 |
- ---------- |
| 158 |
[1] Crispin Cowan <[email protected]>, private communication |
| 159 |
[2] http://immunix.org/StackGuard/emsi_vuln.html |
| 160 |
[3] http://www.striker.ottawa.on.ca/~aland/pscan/ |
| 161 |
[4] http://immunix.org/stackguard.html |