Skip to content

Instantly share code, notes, and snippets.

@malwareslayer
Last active December 26, 2022 00:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save malwareslayer/4a491a78ea69de43b828f1756495e2de to your computer and use it in GitHub Desktop.
Save malwareslayer/4a491a78ea69de43b828f1756495e2de to your computer and use it in GitHub Desktop.
Indoxploit Magazines 0x0: Cepat Memahami Instruksi Assembly

Image

Reverse Engineering Enhanced Edition | Refixes: 0x1

Apa Itu Assembly ?


Assembly adalah bahasa pemrograman CPU yang dibuat berdasarkan Native Processor Language atau biasa disebut Instructions Set Architecture (ISA).

Ada beberapa tipe Assembly

Tutorial ini menggunakan nasm.

Registers / Mnemonics


General Registers :

Mnemonics Registers
a Accumulator
b Base
c Counter
d Data
  • Accumulator

Accumulator adalah sebuah registers untuk menyimpan data sementara.

Contoh :

mov al, 0x61 ; al menyimpan sebuah characters "a"
  • Base

Base adalah sebuah registers sebagai offset stack ataupun address. registers ini jarang digunakan, lebih banyak menggunakan symbol [], check contoh ke-2.

Contoh:

Array a = [ 1, 2, 3, 4 ]
a[1] // 1 adalah bilangan offset
mov eax, [rel rbp+0x2]
; rbp sebagai Array a (address) dengan tambahan base offset sebesar 2
; eax menyimpan data sebesar 3 diambil dari a[2]
size 0x4000200 0x4000201 0x4000202 0x4000203 descriptions
Array a 3 1 2 3 4 address 0x4000200 sampai 0x4000203
offset 2 0 0 3 -1 address 0x4000200 + 2 = 0x4000202 = 3
  • Counter

Counter adalah sebuah registers sebagai pencacah dan penghitung.

Contoh :

for(cx = 0; ; cx++) {
  cx
}
  • Data

Data adalah sebuah registers sebagai penghitung aritmatika.


Index Registers

Mnemonics Registers
si Source Index
di Destination Index
sp Stack Pointer
bp Base Pointer
  • Source Index & Destination Index

Source Index adalah sebuah registers yang melakukan manipulasi address, pada x86-64 (amd64, elf64) Source Index & Destination Index digunakan sebagai parameters pertama (di) dan kedua (si) pada fungsi yang berasal libc & kernel.

  • Stack Pointer & Base Pointer

Stack Pointer & Base Pointer adalah initialized frame. Seperti halnya Base Pointer adalah kertas kosong dan Stack Pointer adalah garis garis kertas, yang akan dijelaskan pada Local Stack.


Special Registers

Mnemonincs Registers
ip Instructions Pointer
flags flags
  • Instructions Pointer

Instructions Pointer adalah sebuah registers yang menjalankan instruksi assembly berdasarkan address.

Contoh:

Address Machine Code Machine Code Human Format Descriptions
0x0040000 b0 01 mov al, 1 [mov al] = b0, [01] = 1
0x0040002 90 nop (no) o(p)erations

Penjalasan pada table diatas adalah address akan ditambahkan 1 untuk setiap Machine Code [ b0 01 ] = 2 + 0x0040000 = 0x0040002

  • Flags

Flags adalah sebuah registers yang digunakan untuk operasi conditional status dan control instruksi

Mnemonics Flags Category Abbreviation Flagging 1 TRUE Abbreviation Flagging 2 FALSE
CF Carry Flag Status CY CARRY NC NO CARRY
PF Parity Flag Status PE PARITY EVEN PO PARITY ODD
AF Adjust Flag Status AC AUXILIARY CARRY NA NO AUXILIARY CARRY
ZF Zero Flag Status ZR ZERO NZ NOT ZERO
SF Sign Flag Status NG NEGATIVE PL POSITIVE
TF Trap Flag Control - - - -
IF Interrupt Flag Control EI ENABLE INTERRUPT DI DISABLE INTERRUPT
DF Direction Flag Control DN DOWN UP UP
OF Overflow Flag Status OV OVERFLOW NV NOT OVERFLOW

Untuk pemahan cepat adalah dengan mengingat flag sebagai mnemonics odiszapc (liat bagan table Mnemonics dari bawah ke atas) dengan mengecualikan Trap Flag, mengapa mengecualikan ? karena Trap Flag tidak digunakan sebagai conditional status ataupun control dalam. jadi flags registers akan dijelaskan satu persatu pada magazines 0x1.

Cheat Sheets Assembly Bekerja


Basic mov & Max Size :

Sebelum

Registers Value
al 0
bx 0
ecx 0
rdx 0
mov al,   0xff
mov bx,   0xff
mov ecx,  0xff
mov rdx,  0xff

; mov al, bl    No Error
; mov cx, ebx   Error

; untuk perpindahan registers ke registers adalah dengan tipe registers yang sama
; al  dengan al | atau al dengan ah
; ax  dengan ax
; eax dengan eax
; rax dengan rax

Sesudah

Registers Value Alias
al 0xff byte
bx 0x00ff word
ecx 0x000000ff dword
rdx 0x00000000000000ff qword

Basic Label Sections Segments (Memory Layouts) :

Mnemonics Registers Contoh Values Penjalasan
define bytes db 0x55 mendefinisikan bytes 0x55
db 0x55, 0x56, 0x57 mendefinisikan 3 bytes
db 'a', 0x55 character constants literal
db 'hello', 13, 10, '$' string constants literal
define words dw 0x1234 0x34 0x12
dw 'a' ascii a = 0x61 0x00
dw 'ab' character constant literal 0x61 0x62
dw 'abc' string constant literal 0x61 0x62 0x63 0x00
define double-words dd 0x12345678 0x78 0x56 0x34 0x12
dd 1.234567e20 floating-point constant literal
define quartal-words dq 0x123456789abcdef0 eight byte constant
dq 1.234567e20 double-precision float constant literal
Mnemonics Registers Values Penjelasan
reserve bytes resb 64 reserves 64 bytes
reserve a word resw 1 reserve 1 word
reserve a double words resq 10 reserve array size 10

https://nasm.us/doc/nasmdoc3.html

Instructions Assembly

section .rodata                           ; read-only initialized data
  static_buffer: db "Hello", 0xA, 0x0     ; db (define byte) "Hello\n\0"
section .data                             ; read-write initialized data
  dynamic_buffer: db "Worlds", 0xA, 0x0   ; db (define byte) "Hello\n\0"
section .bss                              ; read-write uninitialized data
  stdin_buffer: resb 0xA                  ; resb (reserve byte) 10 bytes

Equivalent C

char stdin_buffer[0xA];
char dynamic_buffer[0x8] = "Worlds\n\0";

int main(void) {
  char * static_buffer = "Hello\n\0";
  // static char dynamic_buffer[7] = "Worlds\n\0"; .data
  // char dynamic_buffer[7]; .data
}

Memory View Segments

Tools to view : objdump -dj "section" "nama program / file objek"

Example: objdump -dj .rodata a.out

sections name of relocations 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F output
.rodata
static_buffer 0x48 0x65 0x6c 0x6c 0x6f 0xA 0x00 Hello .
.data
dynamic_buffer 0x57 0X6f 0x72 0x6c 0x64 0x73 0x0A 0x00 Worlds .
.bss
stdin_buffer 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ..........

Basic Pointer Assembly lea :

Sebelum

Registers Value
eax 0x0000000
section .rodata
  buffer: db "Hello"    ; address 0x4000200
section .text
  lea eax, [rel buffer]

Sesudah

Registers Value
eax 0x4000200

Basic Extern Call libc :

  global main
section .rodata
  buffer: db "Hello, Worlds!", 0xA
section .text
  extern puts
main:
  lea rdi, [rel buffer]
  call puts

Basic Calling Convention / System Call Kernel:

/usr/include/asm/unistd_32.h :

name headers eax ebx ecx edx esi edi
sys_restart_syscall __NR_restart_syscall 0
sys_exit __NR_exit 1 int error_code
sys_fork __NR_fork 2 struct pt_regs *
sys_read __NR_read 3 unsigned int fd char __user * buf size_t count
sys_write __NR_write 4 unsigned int fd const char __user * buf size_t count

Sebelum :

Registers Value Descriptions
eax 0x00000004 write
ebx 0x00000001 file desriptor
ecx 0x40000200 buffer location
edx 0x0000000f length of buffer
  global _start
section .rodata
  hello_worlds: db "Hello, Worlds!", 0xA  ; Address: 0x4000200
  _len_hello: equ $-hello_worlds
section .text
_start:
  mov eax, 4              ;        write(
  mov ebx, 1              ;               1,
  mov ecx, hello_worlds   ;                  hello_worlds,
  mov edx, _len_hello     ;                                 _len_hello );
  int 0x80                ; syscall (executes)

Sesudah :

Registers Value Descriptions
eax 0x00000004 write
ebx 0x00000001 file desriptor
ecx 0x40000200 buffer location
edx 0x0000000f length of buffer

Equivalent C

#include <string.h> // Assumption
#include <unistd.h>

int main(void) {
  write(1, "Hello, Worlds!\n", strlen("Hello, Worlds!\n"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment