Reverse Engineering - Lecture 2 , By Ahmed Sherif

24
1 Reverse Engineering Reverse Engineering Course Course IS|Reverse Engineering IS|Reverse Engineering Lec 2 Lec 2

Transcript of Reverse Engineering - Lecture 2 , By Ahmed Sherif

Page 1: Reverse Engineering - Lecture 2 , By Ahmed Sherif

1

Reverse Engineering Reverse Engineering CourseCourse

IS|Reverse EngineeringIS|Reverse Engineering

Lec 2Lec 2

Page 2: Reverse Engineering - Lecture 2 , By Ahmed Sherif

2

Stack MemoryStack Memory

IS|Reverse EngineeringIS|Reverse Engineering

Stacks in computing architectures are regions of memory where data is added or removed in a Stacks in computing architectures are regions of memory where data is added or removed in a last-in-first-out manner.last-in-first-out manner.

every process has at least one thread, and every thread has its own stackevery process has at least one thread, and every thread has its own stack

Page 3: Reverse Engineering - Lecture 2 , By Ahmed Sherif

3

Stack MemoryStack Memory

IS|Reverse EngineeringIS|Reverse Engineering

Page 4: Reverse Engineering - Lecture 2 , By Ahmed Sherif

4

Heap MemoryHeap Memory

IS|Reverse EngineeringIS|Reverse Engineering

The heap is memory space that can be allocated by a process when it needs more memory.The heap is memory space that can be allocated by a process when it needs more memory.

Each process has one heap and it is shared among the different threads. All the threadsEach process has one heap and it is shared among the different threads. All the threads

share the same heap. share the same heap.

Page 5: Reverse Engineering - Lecture 2 , By Ahmed Sherif

5

Pointer RegistersPointer Registers

IS|Reverse EngineeringIS|Reverse Engineering

EBP - base pointer.EBP - base pointer.

ESP Stack Pointer.–ESP Stack Pointer.–

ESP is the stack pointer and always points to the top of the stackESP is the stack pointer and always points to the top of the stack

The base is the beginning of a stack frame . The base is the beginning of a stack frame . EBP points to the beginning of the functions stack frame EBP points to the beginning of the functions stack frame

EIP - Instruction pointerEIP - Instruction pointer

pointer to the next address instruction to be executedpointer to the next address instruction to be executed

Page 6: Reverse Engineering - Lecture 2 , By Ahmed Sherif

6

Segment RegistersSegment RegistersSegmentation involves composing a memory address from two parts, a segment andSegmentation involves composing a memory address from two parts, a segment and

an offset.the segment points to the beginning of a 64 KB group of addresses and thean offset.the segment points to the beginning of a 64 KB group of addresses and the

offset determines how far from this beginning address the desired addressoffset determines how far from this beginning address the desired address

Page 7: Reverse Engineering - Lecture 2 , By Ahmed Sherif

7

Segment RegistersSegment RegistersCS → Code segmentCS → Code segment

DS → Data segmentDS → Data segment

SS → Stack segmentSS → Stack segment

ES → Extra segmentES → Extra segment

FS → Extra segmentFS → Extra segment

Example : Example :

Segment:offsetSegment:offset

Mov eax, DS:offset mystringMov eax, DS:offset mystring

Page 8: Reverse Engineering - Lecture 2 , By Ahmed Sherif

8

FlagsFlags

IS|Reverse EngineeringIS|Reverse Engineering

OF: Overflow Flag : indicates an overflow when setOF: Overflow Flag : indicates an overflow when set

DF: Direction Flag : used for string operations to check directionDF: Direction Flag : used for string operations to check direction

SF: Sign Flag : if set, resulting number of calculation is negativeSF: Sign Flag : if set, resulting number of calculation is negative

ZF: Zero Flag : if set, resulting number of calculation is zeroZF: Zero Flag : if set, resulting number of calculation is zero

CF: Carry Flag : used to indicate when an arithmetic carry or borrowCF: Carry Flag : used to indicate when an arithmetic carry or borrow

has been generated out of the most significant ALU bit positionhas been generated out of the most significant ALU bit position

Page 9: Reverse Engineering - Lecture 2 , By Ahmed Sherif

9

InstructionsInstructions

IS|Reverse EngineeringIS|Reverse Engineering

Arithmetic operationsArithmetic operationsADDADD : :

add dest, src add dest, src

Examples : Examples :

add eax, ebx -> both dest and src are registersadd eax, ebx -> both dest and src are registers

add [esp], eax -> dest is a memory reference to the top of the stackadd [esp], eax -> dest is a memory reference to the top of the stackadd eax, 4 -> source is an immediate valueadd eax, 4 -> source is an immediate value

Page 10: Reverse Engineering - Lecture 2 , By Ahmed Sherif

10

InstructionsInstructions

IS|Reverse EngineeringIS|Reverse Engineering

SubSub : :

sub dest, src sub dest, src

Arithmetic operationsArithmetic operations

DivDiv : :

mov eax, 65 ; move the dividend into eaxmov eax, 65 ; move the dividend into eax

mov ecx, 4; move the divisor into ecxmov ecx, 4; move the divisor into ecx

div ecxdiv ecx

ResultResult::

Eax : 16Eax : 16

Edx : 1 Edx : 1

Page 11: Reverse Engineering - Lecture 2 , By Ahmed Sherif

11

InstructionsInstructions

IS|Reverse EngineeringIS|Reverse Engineering

Arithmetic operationsArithmetic operationsMULMUL : :

MULMUL dest, src dest, src

Page 12: Reverse Engineering - Lecture 2 , By Ahmed Sherif

12

InstructionsInstructions

IS|Reverse EngineeringIS|Reverse Engineering

Bitwise operationsBitwise operationsAND, syntax: AND, syntax: add dest, srcadd dest, src

OR, syntax: OR, syntax: or dest, src or dest, src

XOR, syntax: XOR, syntax: xor dest, srcxor dest, src

NOT, syntax: NOT, syntax: not eaxnot eax

ExampleExample::

Value1 = 10011011Value1 = 10011011

Value2 = 11001001Value2 = 11001001

O/P =O/P =

AND -> 10001001AND -> 10001001

OR -> 11011011OR -> 11011011

XOR -> 01010010 XOR -> 01010010

Page 13: Reverse Engineering - Lecture 2 , By Ahmed Sherif

13

InstructionsInstructions

IS|Reverse EngineeringIS|Reverse Engineering

BranchingBranchingJZ :JZ : jump if result = 0 jump if result = 0

JNZ : JNZ : Jump if result doesn't equal 0 Jump if result doesn't equal 0

JE JE : : Jump if result euqal zero Jump if result euqal zero

JNE : JNE : Jump if result doesn't equal zero Jump if result doesn't equal zero

ExampleExample::

CMP BL,3CMP BL,3

JE label2JE label2

MOV DX,4MOV DX,4

JMP label3JMP label3

label2:label2:

MOV BL ,7MOV BL ,7

Page 14: Reverse Engineering - Lecture 2 , By Ahmed Sherif

14

InstructionsInstructions

IS|Reverse EngineeringIS|Reverse Engineering

Data MovingData MovingMOV syntax; Mov des,srcMOV syntax; Mov des,src

Movzx syntax ; movzx des,srcMovzx syntax ; movzx des,src

Both Source,Destination can be register OR memory reference .Both Source,Destination can be register OR memory reference .

Warning : Both can't be momery referenceWarning : Both can't be momery reference

Lea eax,dword ptr[eax+ecx]Lea eax,dword ptr[eax+ecx]

Page 15: Reverse Engineering - Lecture 2 , By Ahmed Sherif

15

InstructionsInstructions

IS|Reverse EngineeringIS|Reverse Engineering

LOOPSLOOPSmov ecx, 5 ; mov ecx, 5 ; remember ecx stands for extended counter registerremember ecx stands for extended counter register

_proc:_proc:

dec ecx dec ecx ; decrements ecx; decrements ecx

loop _proc loop _proc ; loops back to _procs; loops back to _procs

Page 16: Reverse Engineering - Lecture 2 , By Ahmed Sherif

16

InstructionsInstructions

IS|Reverse EngineeringIS|Reverse Engineering

Stack managementStack managementPOP ; POP destPOP ; POP dest

Push; Push value/regPush; Push value/reg

Page 17: Reverse Engineering - Lecture 2 , By Ahmed Sherif

17

Hands-onHands-on

IS|Reverse EngineeringIS|Reverse Engineering

From C to AssemblyFrom C to Assembly

if (var == 0)if (var == 0)

{{

anyfunction();anyfunction();

}}

// AfterCondition// AfterCondition

Mov eax, [var]Mov eax, [var]

Test eax, eaxTest eax, eax

Jnz AfterConditionJnz AfterCondition

Call anyFunctionCall anyFunction

AfterCondition:AfterCondition:

Page 18: Reverse Engineering - Lecture 2 , By Ahmed Sherif

18

Hands-onHands-on

IS|Reverse EngineeringIS|Reverse Engineering

From C to AssemblyFrom C to Assembly

if (var == 7)if (var == 7)

function();function();

elseelse

anotherFunction();anotherFunction();

Cmp [var], 7Cmp [var], 7

Jz ElseBlockJz ElseBlock

Call functionCall function

Jmp AfterConditionalBlockJmp AfterConditionalBlock

ElseBlock:ElseBlock:

Call anotherFunctionCall anotherFunction

AfterConditionalBlock:AfterConditionalBlock:

Page 19: Reverse Engineering - Lecture 2 , By Ahmed Sherif

19

Hands-onHands-on

IS|Reverse EngineeringIS|Reverse Engineering

From C to AssemblyFrom C to Assembly

if (var1 == 100 && var2 == 50)if (var1 == 100 && var2 == 50)

yes;yes;

Cmp [var1], 100Cmp [var1], 100

Jne AfterConditionJne AfterCondition

Cmp [var2], 50Cmp [var2], 50

Jne AfterConditionJne AfterCondition

yesyes

AfterCondition:AfterCondition:

Page 20: Reverse Engineering - Lecture 2 , By Ahmed Sherif

20

Hands-onHands-on

IS|Reverse EngineeringIS|Reverse Engineering

From C to AssemblyFrom C to Assembly

function (int x, char y );function (int x, char y ); mov eax, ymov eax, y

push eaxpush eax

Mov eax , xMov eax , x

Push eaxPush eax

call functioncall function

Page 21: Reverse Engineering - Lecture 2 , By Ahmed Sherif

21

MS-DOS AssemblyMS-DOS Assembly

IS|Reverse EngineeringIS|Reverse Engineering

Hello World Example Hello World Example

Jmp 0103Jmp 0103

Db "Hello world$"Db "Hello world$"

Mov dx,0102Mov dx,0102

Mov ah,9Mov ah,9

nt 21İnt 21İ

nt 20İnt 20İ

Page 22: Reverse Engineering - Lecture 2 , By Ahmed Sherif

22

MS-DOS AssemblyMS-DOS Assembly

IS|Reverse EngineeringIS|Reverse Engineering

Current System TimeCurrent System Time

Mov ah,2DMov ah,2D

nt 21İnt 21İ

nt 20İnt 20İ

Page 23: Reverse Engineering - Lecture 2 , By Ahmed Sherif

23

MS-DOS AssemblyMS-DOS Assembly

IS|Reverse EngineeringIS|Reverse Engineering

Current DOS VersionCurrent DOS Version

Mov ax,0300Mov ax,0300

nt 21İnt 21İ

nt 20İnt 20İ

Page 24: Reverse Engineering - Lecture 2 , By Ahmed Sherif

24

AssignmentsAssignments

IS|Reverse EngineeringIS|Reverse Engineering

Try With Some Try With Some interrupts with Site interrupts with Site

Below : Below :

http://spike.scu.edu.au/~barry/interruhttp://spike.scu.edu.au/~barry/interrupts.htmlpts.html