home..

Assembly_refresher

CPU ARCH AND ASSEMBLY REFRESHER

An assembly language comprises:

Radix suffices:

  1. -d: decimal
  2. -h: hexadecimal
  3. -q or o: octal
  4. -b: binary

Examples of integer constants

Directives

Commands for the assembler

They are case-insensitive

Examples

db is the older directive for assigning 8-bit data to a storage area. i.e

.data

val1 db 255 ; unsigned byte

Addressing Modes in Assembly

1. Register Addressing Mode:

2. Memory Addressing Mode:

3. Immediate Addressing Mode:

4. Implicit Addressing Mode:

The X64 architecture General Purpose registers

1. RAX (EAX in x86):

2. RCX (ECX in x86):

3. RBP (EBP in x86):

4. RSP (ESP in x86):

5. RSI/RSI (ESI/EDI in x86):

Segment Registers

Think of these as signposts in your computer memory, helping to section off and organize different types of data or code.

Pointer and Index Registers

These are like pointers or arrows indicating specific spots in the computer’s memory or giving directions for specific tasks.

Floating Point Unit (FPU) Registers

These are specialized registers for decimal numbers.

MMX Registers

Specialized registers for multimedia tasks like image or video processing.

XMM Registers

Used for SIMD (Single Instruction, Multiple Data) operations which let your computer do many similar tasks at once, like applying a filter to all pixels in an image simultaneously.

Control Registers

The control center of the CPU.

Debug Registers

Helpful tools when you want to monitor or troubleshoot code.

POINTS

A single operand to a PUSH instruction will often be a pointer.

A pointer is a value of an address(an address points to a value stored in that address/memory location). i.e

PUSH offset loc_46780;char softwareKey - points to a reg key stored in this address..double click on the address in ida to access string.

Let’s look at a simple assembly instruction: mov eax, [0x410230].

In this example, the result is that the 4 bytes of data located at 0x410230 will be moved to the EAX register.

Indirect Memory Addressing?

Picture a sprawling city with many streets and addresses. Now, some addresses are straightforward – “123 Main St.” But others might be a bit more complex like, “The third house after the big oak tree on Elm St.” This latter way of describing addresses is how you can think of indirect memory addressing.

In assembly, instead of always accessing a specific memory address directly, we can use combinations of registers and values to dynamically compute the address. It’s a powerful method but can also be a bit tricky to decipher. Let’s break down some examples:

Indirect Addressing Examples

  1. [EAX]: This is like saying, “Access the house (or memory) that the EAX register points to.” In our city analogy, EAX holds the address of a particular house, and we want to go there.
  2. [EBP + 0x10]: This one’s a bit more nuanced. If EBP is a starting point, we’re moving a bit further to access data. In our city, it’s like saying, “Go to the address in EBP and then move 16 houses down.”
  3. [EAX + EBX * 8]: Think of this as accessing an apartment complex. EAX points to the start of the complex, and each building (or memory structure) is 8 units apart. EBX tells us which building to access. So if EBX is 2, we’d skip the first two buildings and head to the third one.
  4. [EAX + EBX + 0xC]: This is like navigating a condo with different wings and floors. EAX might point to the start of a wing, EBX might indicate which floor to go to, and 0xC is a specific condo on that floor.

Jump Instructions: BASICS

Imagine you’re reading a book, and suddenly, a note tells you to skip ahead to another chapter or perhaps go back to a previous page. This sudden shift is precisely how “jump instructions” work in assembly.

A jump instruction gives a directive to the CPU to cease its current operation and start executing code from an entirely new location in memory. It’s like a crossroad that determines the flow of our program. In simpler terms, think of it as a detour sign on a road trip.

Unconditional Jumps

As the name suggests, unconditional jumps are like those detour signs that you must follow, no matter what. They always redirect the execution to a new memory location, with no strings attached.

Examples:

  1. JMP: This is the basic jump instruction. It tells the CPU, “Hey, forget what you’re doing right now, and start from this new address.”
  2. CALL: A bit more sophisticated, this not only jumps to a new location but also remembers where it came from. This is useful for procedures or functions, ensuring we can return once the function is done.
  3. RET: Speaking of returning, RET does just that. After a CALL, the RET instruction ensures we go back to where we were before the jump.

Conditional Jumps in Assembly

Conditional jumps in assembly direct the CPU to shift its execution to another part of the program based on specific conditions. These jumps rely on the state of the flags register, which gets updated after arithmetic or Boolean operations.

Types of Conditional Jumps:

  1. CMP and TEST Instructions:
    • CMP: This is like a subtraction (SUB) but doesn’t change the destination value. Instead, it updates the flags as if a subtraction took place.
    • TEST: This is similar to an AND operation but doesn’t alter the destination. It’s essentially a shadow Boolean AND.
    • After a CMP or TEST, it’s typical to see a conditional jump.
  2. Jump Conditions (Jcc):
    • A: Above (unsigned)
    • B: Below (unsigned)
    • E/Z: Equal/Zero
    • G: Greater than (signed)
    • L: Less than (signed)
    • N: Not condition, e.g., JNZ means jump if not zero.
  3. How are conditions evaluated?
    • The evaluation relies on bits in the flags register.
    • For instance, JB (jump if below) is true if the Carry flag is set.
    • JG (jump if greater) is true if the Zero flag is unset, and the Sign flag equals the Overflow flag.

OPERATIONS/INSTRUCTIONS

To better explain this, think of the value 0x20 being stored at the address 0x00830048: ebx points to the address 0x00830040. so the value 0x20 will be at ebx + 8.

To increment and decrement:

Multiplication and division operations often operate on a predefined register. The command is therefore the instruction, plus the value that the predefined register will be mul or div with.

The mul operation will multiply the predefined register(eax) with the value defined.

The return value of the operation will be stored across two registers, EDX and EAX with EDX storing most significant bits and EAX storing least significant bits.

Div will do the same operation as mul but will divide the 64-bits across EDX and EAX by value. The result will be stored in EAX and the remainder in EDX.

REVERSING FUNCTIONS

CALLING A FUNCTION

  1. function format: return = function(arg0,arg1)
  2. specific events occur when calling a function:
  3. Pass in parameters(stack / register)
  4. save return pointer
  5. Transfer control to the function.

Specific events occur when returning from a function:

  1. Set up a return value(typically EAX)
  2. clean up stack and store registers
  3. Transfer control to the saved return pointer

Function Prologue actions

push ebp; save ebp

mov ebp, esp; create function stack

sub esp, 104h ; create space for variables/arguments

push edi; save registers to be used within the function.

push esi;

Function Epilogue actions

pop all pushed registers
retn = pop eip;
leave = mov esp, ebp; pop ebp;
add esp, 4 ; restore esp state.

STACK

The stack is Last In, First Out (LIFO).

• PUSH adds an element, and POP removes one.

• ESP points to the next item on the stack and changes with instructions like PUSH, POP, CALL, LEAVE, and RET.

• EBP (a.k.a. “frame pointer”) is an unchanging reference.

• EBP – value = local variable (registers may also be used)

image_info

Typical function Example

image

Corresponding Stack for the function above

image

ACTIONS FOR CLEAN-UP

pop edx implies esp + 4

RET

retn = pop eip; leave = mov esp, ebp + pop ebp; add esp, value ; restore esp state.

FUNCTIONS CALLING CONVENTIONS

cdecl convention (most common)

EXAMPLE

push        edx ; SubStr
push        eax ; str
call        strstr 
add         esp,     8;   clean up

Stdcall convention

EXAMPLE

push      offset LibFileName ; Kernel32.dll

call      ds:LoadLibraryA

mov       [ebp+hModule], eax; no stack clean up after the call.

Fastcall convention

EXAMPLE

mov        edx, offset  aCmdExe ; cmd.exe

lea        ecx,   [ebp+8]

call       sub_409008 ;    note the use of ecx and edx registers

Thiscall convention

EXAMPLE

mov       ecx,     eax   ; ECX holds the address of self. 

call         sub_10001067
© 2023 ~Mystik   •  Powered by Soopr   •  Theme  Moonwalk