Docsity

Unconventional Calling - Program and Data Representation | CS 2150, Study notes of Computer Science

Toggle sidebar
bg1
1
CS216: Program and Data Represe ntation
University of Virginia Computer Science
Spring 2006 David Evans
http://www.cs.virginia.edu/cs216
Lecture 22:
Unconventional
Calling
2
UVa CS216 Spring 2006 -Le cture 22: Unconventional Calling
Menu
• Stack Smashing Attacks and
Defenses
• ISR De-Randomizing MicroVM
3
UVa CS216 Spring 2006 -Le cture 22: Unconventional Calling
parameter 3
parameter 2
parameter 1
return address
saved EBP
local variable 1
local variable 2
local variable 3
saved EDI
saved ESI
Higher AddressesStack Growth
EBP
ESP
[ebp]+8
[ebp]+12
[ebp]+16
[ebp]-4
4
UVa CS216 Spring 2006 -Le cture 22: Unconventional Calling
Pathological C Program
void pathological ()
{
char a[4] = "abcd";
char *adr;
int val;
val = 8;
/* Saved EBP */
/* return address <- location of a[16] */
a[val+4] = 0xe4;
a[val+5] = 0xfe;
a[val+6] = 0x12;
a[val+7] = 0x00;
/* a[16-17] <- JMP -2 */
a[val+8] = 0xeb;
a[val+9] = 0xfe;
a[val+10] = 0x00;
a[val+11] = 0x00;
}
int main (int argc, char **argv)
{
pathological ();
printf ("Hello\n");
return EXIT_SUCCESS;
}
5
UVa CS216 Spring 2006 -Le cture 22: Unconventional Calling 6
UVa CS216 Spring 2006 -Le cture 22: Unconventional Calling
a[0]
return address
pf3
pf4

Partial preview of the text

Download Unconventional Calling - Program and Data Representation | CS 2150 and more Study notes Computer Science in PDF only on Docsity!

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans

http://www.cs.virginia.edu/cs

Lecture 22:

Unconventional

Calling

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 2

Menu

• Stack Smashing Attacks and

Defenses

• ISR De-Randomizing MicroVM

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 3

parameter 3

parameter 2

parameter 1

return address

saved EBP

local variable 1

local variable 2

local variable 3

saved EDI

saved ESI

Higher Addresses

Stack Growth

EBP

ESP

[ebp]+

[ebp]+

[ebp]+

[ebp]-

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 4

Pathological C Program

void pathological () { char a[4] = "abcd"; char adr; int val; val = 8; / Saved EBP / / return address <- location of a[16] / a[val+4] = 0xe4; a[val+5] = 0xfe; a[val+6] = 0x12; a[val+7] = 0x00; / a[16-17] <- JMP -2 */ a[val+8] = 0xeb; a[val+9] = 0xfe; a[val+10] = 0x00; a[val+11] = 0x00; } int main (int argc, char **argv) { pathological (); printf ("Hello\n"); return EXIT_SUCCESS; }

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 5 UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 6

a[0]

return address

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 7

a[0]

void pathological ()

char a[4] = "abcd";

char *adr;

int val;

val = 8;

a[val+4] = 0xe4;

a[val+5] = 0xfe;

a[val+6] = 0x12;

a[val+7] = 0x00;

/* a[16-17] <- JMP -2 */

a[val+8] = 0xeb;

a[val+9] = 0xfe;

a[val+10] = 0x00;

a[val+11] = 0x00;

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 8

0040107D mov edx,dword ptr [val]

00401080 mov byte ptr [ebp+edx+3],

00401085 mov esp,ebp

00401087 pop ebp

00401088 ret

0012FEE4 jmp 0012FEE

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 9

/GS

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 10

/GS Option

The compiler injects checks in

functions with local string buffers or,

on x86, functions with exception

handling. A string buffer is defined as

an array whose element size is one

or two bytes, and where the size of

the whole array is at least five bytes,

or, any buffer allocated with _alloca.

http://msdn2.microsoft.com/en-US/library/8dbf701c(VS.80).aspx

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 11

With /GS

void pathological ()

char a[ 5 ] = "abcd";

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 12

Security Cookies

void pathological () { 00401030 push ebp 00401031 mov ebp,esp 00401033 sub esp,14h 00401036 mov eax,dword ptr [___security_cookie (408060h)] 0040103B mov dword ptr [ebp-8],eax ... 0040109B mov eax,dword ptr [val] 0040109E mov byte ptr [ebp+eax-5], } 004010A3 mov ecx,dword ptr [ebp-8] 004010A6 call __security_check_cookie (40111Eh) 004010AB mov esp,ebp 004010AD pop ebp 004010AE ret

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 19

MicroVM Code

push dword ebp mov ebp, WORM_ADDRESS + WORM_REG_OFFSET pop dword [ebp + WORM_DATA_OFFSET] xor eax, eax ; WormIP = 0 (load from ebp + eax) read_more_worm: ; read NUM_BYTES at a time until worm is done cld xor ecx, ecx mov byte cl, NUM_BYTES mov dword esi, WORM_ADDRESS ; get saved WormIP add dword esi, eax mov edi, begin_worm_exec rep movsb ; copies next Worm block into execution buffer add eax, NUM_BYTES ; change WormIP pushad ; save register vals mov edi, dword [ebp] ; restore worm registers mov esi, dword [ebp + ESI_OFFSET] mov ebx, dword [ebp + EBX_OFFSET] mov edx, dword [ebp + EDX_OFFSET] mov ecx, dword [ebp + ECX_OFFSET] mov eax, dword [ebp + EAX_OFFSET] begin_worm_exec: ; this is the worm execution buffer nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop mov [ebp], edi ; save worm registers mov [ebp + ESI_OFFSET], esi mov [ebp + EBX_OFFSET], ebx mov [ebp + EDX_OFFSET], edx mov [ ebp + ECX_OFFSET], ecx mov [ebp + EAX_OFFSET], eax popad ; restore microVM register vals jmp read_more_worm

Note: this is nasm x assembly code, not masm, so some directives are slightly different

UVa CS216 Spring 2006 - Lecture 22: Unconventional Calling 20

Charge

• PS7 Due Wednesday

– Lots of interesting things to explore

• Exam 2 will be posted Thursday

• Next class: review (if you send

questions)

• “x86 Guru” title(s) may be awarded

for especially clever answers to

Question 10