-->
Bayu_Ajie
Bayu_Ajie knowledge is free you can learn anything

Belajar ASM untuk basic reverse engineering

 Belajar ASM untuk basic reverse engineering


Hallo, kembali lagi bersama saya Bayu Aji. Kali ini saya akan memabgikan tutorial ASM atau Assembly, okok saya akan bercerita sebentar. ASM menurut saya susah karena ini adalah bahasa low level language bahasa tingkat rendah, yap bahasa ini ibarat nya seperti mesin sangat susah dimenegrti kaya hati kamu ea..ea..ea hahaha canda. Okok serius sedikit, nah sebelum masuk ke dalam materi saya akan memberikan sebuah peringatan tentang ASM, apa saja? Cekidot 
 
 
1. Bahasa Assembly adalah low level language
 
Bahasa yang sulit seperti wanita, tidak seperti bahasa lain atau high level language 
 
2. Bahasa Assembly tidak portable 
 
Bahasa ini tergantung pada CPU dan architecture x86 dan arm memiliki architecture yang berbeda (ASM berbeda)     
 
3. Bahasa Assembly memakan banyak waktu
 
Karena ketika kita membuat software dengan bahasa ini kita menginstrusikan cpu secara langsung dan ini susah 
 
 
Terus kok bisa ada orang yang memakai bahasa ini? Kan ribet tapi masih ada yang pakai? Kenapa? Okok simak lagi, ada beberapa fungsi dan manfaat bahasa Assembly diantaranya 
 
 
# Fungsi bahasa Assembly  

Bahasa Assembly sebagai symbolic form dari machine code, repersentasi dari machine code adalah bahasa Assembly
 
Manfaat 
 
 
1. Implemntasi micro-optimization
 
Berfungsi untuk software engineering, dalam software engineering micro-optimization adalah optimisasi yang dilakukan di paling akhir, yang pertama kita optimisasi adalah algoritma, data struktur, menghilangkan hidden cost, jika sudah melakukan semua ini baru kita memasuki ke micro-optimization. Jangan lakukan micro-optimization di awal akan mengakibatkan fail optimization
      
 
2. Mendapat pengetahuan yang mendalam tentang arsitektur komputer 
 
Ketika kita mendalami bahasa ASM, kita akan memahami bagaimana hardware nya bekera, memori bekerja, branch bekerja dan lainya 
 
 
3. Meningkatkan kemampuan debugging 
  
4. Dasar reverse engineering 
 
 
 
# Bagaimana cara menggunakan bahasa Assembly 
 
 
Software yang diperlukan diantaranya 
 
1. Assembler (NASM)
2. GCC
3. Build Essential 
 
 
Install 
 
sudo apt-get install nasm gcc build-essential  


# Implentasi ASM 
 
Okok setelah menginstall selanjutnya kita belajar menulis hello world di asm
 
 

 
[section .data]
my_str:
        db "hello world", 10 ;code ASCII untuk karater enter


[section .text]
global _start

_start:

  mov rax, 1
  mov rdi, 1
  mov rsi, my_str
  mov rdx, 12
  syscall

  mov rax, 60
  mov rdi, 0
  syscall

 
 
cara compile 
 
nasm -felf64 test.asm -o test.o



test.o adalah file relocatedable kita perlu link untuk build menjadi excutable
 
ld test.o -o test  


 
kemudian akan ada 3 file baru     

 

test.asm adalah source code
test.o adalah file relocatable object
test excutable
 

run or excute 

 

./nama file 

 
Penjelasan

RDI = argument 1-st
RSI = argument 2-nd
RDX = argument 3-rd
RCX = argument 4-th
R8  = argument 5-th
R9  = argument 6-th
RAX = return value

mov rax

rax adalah register yang digunakan sebagai syscall number

contoh 1. 1 adalah syscall write atau menulis

mov rdi

rdi adalah untuk standar output atau stdout dari syscall number

mov rsi

rsi adalah buffer berisi pointer yang akan dituliskan isi memorinya

mov rdx

rdx adalah panjang buffernya

syscall memanggil system call masuk ke kernel space kemudian akan return ke rax


pelajari lengkap syscall : https://github.com/torvalds/linux/blob/master/arch/x86/entry/syscalls/syscall_64.tbl

contoh


 
ssize_t write(int fd, const void *buf, size_t count);

ada 3 variabel, diantara variabel tersebut memiliki argumen contoh

argumen 1 fd, ini adalah argumen pertama fd adalah (file descriptor) 

argumen 2 buf, ini adalah argumen ke dua  

argumen 3 count, ini adalah argumen ke tiga count adalah jumlah atau panjang buffer yang ditulis di fd

jadi ketika di ASM dari contoh di atas adalah

fd merupakan rdi 

buf merupakan rsi
 
count merupakan rdx

syscall adalah kernel space yang kita jalankan dari fungsi tersebut dan  returnya  ssize_t. "S" yang didepan menunjukan bahwa ada kemungkinan return value nya adalah negatif  


perhatikan di bawah ini


ERRORS
       EAGAIN The file descriptor fd refers to a file other than a socket and has been marked  nonblocking  (O_NONBLOCK),  and  the
              write would block.  See open(2) for further details on the O_NONBLOCK flag.

       EAGAIN or EWOULDBLOCK
              The  file  descriptor  fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the write would block.
              POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the  same
              value, so a portable application should check for both possibilities.

       EBADF  fd is not a valid file descriptor or is not open for writing.

       EDESTADDRREQ
              fd refers to a datagram socket for which a peer address has not been set using connect(2).

       EDQUOT The user's quota of disk blocks on the filesystem containing the file referred to by fd has been exhausted.

       EFAULT buf is outside your accessible address space.

       EFBIG  An  attempt  was made to write a file that exceeds the implementation-defined maximum file size or the process's file
              size limit, or to write at a position past the maximum allowed offset.

       EINTR  The call was interrupted by a signal before any data was written; see signal(7).

       EINVAL fd is attached to an object which is unsuitable for writing; or the file was opened with the O_DIRECT flag,  and  ei‐
              ther the address specified in buf, the value specified in count, or the file offset is not suitably aligned.

       EIO    A low-level I/O error occurred while modifying the inode.  This error may relate to the write-back of data written by
              an earlier write(), which may have been issued to a different file descriptor on the same file.   Since  Linux  4.13,
              errors  from  write-back  come with a promise that they may be reported by subsequent.  write() requests, and will be
              reported by a subsequent fsync(2) (whether or not they were also reported by write()).  An alternate cause of EIO  on
              networked filesystems is when an advisory lock had been taken out on the file descriptor and this lock has been lost.
              See the Lost locks section of fcntl(2) for further details.

       ENOSPC The device containing the file referred to by fd has no room for the data.

       EPERM  The operation was prevented by a file seal; see fcntl(2).

       EPIPE  fd is connected to a pipe or socket whose reading end is closed.  When this happens the writing process will also re‐
              ceive  a  SIGPIPE  signal.  (Thus, the write return value is seen only if the program catches, blocks or ignores this
              signal.)

       Other errors may occur, depending on the object connected to fd.

 
pesan eror ini akan disimbolkan dengan angka negatif masuk ke register rax setelah syscall

mov rax, 60

angka 60 adalah exit atau sys_exit

kita bisa melihat user manual lagi dengan perintah

man 2 exit 


 

exit hanya membutuhkan 1 argumen, berarti hanya membutuhkan rdi dan rax

mov rax, 0

angka 0 adalah status nya atau exit code nya adalah 0

bagiamana cara membuktikanya? Kita bisa menggunakan bash dengan perintah di bawah ini 

echo $?


 
jika Anda masih bingung bisa melihat tutorial di bawah ini



 


Bayu_Ajie
Bayu_Ajie  knowledge is free you can learn anything

Komentar