Skip to content

Instantly share code, notes, and snippets.

@malwareslayer
Last active November 7, 2019 19:05
Show Gist options
  • Save malwareslayer/c908a3d1f42aa57d86057854c61ba147 to your computer and use it in GitHub Desktop.
Save malwareslayer/c908a3d1f42aa57d86057854c61ba147 to your computer and use it in GitHub Desktop.
Indoxploit Magazines 0x1: Bagaimana Sebuah Bahasa Pemrograman Bekerja ?

Image

Reverse Engineering Enhanced Edition | Publish: 0x1

Bagaimana Sebuah Bahasa Pemrograman Bekerja ?

Pada dasarnya CPU bisa melakukan membaca dan menulis, dan melakukan operasi matematika yang mereka pegang diregisters tetapi tidak bisa membaca bahasa pemrograman apapun terkecuali Machine Code (Binary) yang ter-interpretasi ke Instructions Set Architecture (Assembly) secara vice versa. Agar mempermudah dibuatlah program yang merepresentasikan ulang Assembly tersebut kedalam bahasa pemrograman yaitu program Compilers.

Tutorial ini menggunakan llvm infrastructure.

Ada 3 proses tahapan bahasa pemrograman mengkonversikan ke Machine Code.

  • Lexical / Lexemes Scanners

  • Syntactic / Abstract Syntax Tree

  • Machine Code

Lexical Process

Lexical adalah tahapan pencarian sebuah lexemes (token / words / "kalimat") atau symbol yang telah teridentifikasi oleh mesin compilers, pada dasarnya Lexical hanya me-"minify" (sequential characters) Source Code menjadi satu baris.

Identifiers :

int main ( ) { \n
\t int x = 0 ; \n
} \n

Sesudah :

int main() {\n\tint x=0;\n}\n

Syntactic Process

Abstract Syntax Tree adalah tahapan mengklasifikasi lexemes menjadi bagian Identifiers ke sebuah pohon

Command :

clang -Xclang -ast-dump main.c

Parse Tree :

TranslationUnitDecl 0x560a61e559d8 <<invalid sloc>> <invalid sloc>
|
 `-FunctionDecl 0x560a61eb4718 <main.c:1:1, line:3:1> line:1:5 main 'int ()'
   `-CompoundStmt 0x560a61eb48b8 <col:12, line:3:1>
     `-DeclStmt 0x560a61eb48a0 <line:2:3, col:12>
       `-VarDecl 0x560a61eb4818 <col:3, col:11> col:7 x 'int' cinit
         `-IntegerLiteral 0x560a61eb4880 <col:11> 'int' 0

Simplify Identifiers:

FunctionDecl ->                int main()
CompoundStmt ->                { }
DeclStmt ->                    int
VarDecl ->                     x
cinit ->                       =
IntegerLiteral ->              0
  • Function Declarations (FunctionDecl)

Image

  • Compound Statements (CompoundStmt)

Image

  • Declarations Statement (DeclStmt)

Image

  • Variable Declarations (VarDecl)

Image

  • C initialization-style (cinit)
enum initializationStyle {
  CInit,
  CallInit,
  ListInit
}
  • Integer Literal (IntegerLiteral)

Image

Machine Code

Machine Code Process adalah tahapan mengtranslasikan Source Code ke Primary Operation Code (machine opcode)

Command Compiling :

clang -c main.c && wait && objcopy -j .text -O binary main.o main


Command View Binary & Hex:

xxd -b main

Binary :

01010101 01001000 10001001 11100101 00110001 11000000
11000111 01000101 11111100 00000000 00000000 00000000
00000000 01011101 11000011

Hex :

xxd main

5548 89e5 31c0 c745 fc00 0000 005d c3

Command Disassembly For Comparing :

objdump -M intel -dj .text main.o

55                      push   rbp
48 89 e5                mov    rbp,rsp
31 c0                   xor    eax,eax
c7 45 fc 00 00 00 00    mov    DWORD PTR [rbp-0x4],0x0
5d                      pop    rbp
c3                      ret
Binary Machine Code Assembly Descriptions
01010101 55 push rbp int main() { // Assumption Frame
01001000 48 mov rbp, rsp
10001001 89
11100101 e5
10001001 31 xor eax, eax // return 0
11100101 c0
11000111 c7 mov dword ptr [rbp-0x4], 0x0 int x = 0;
01000101 45
11111100 fc
00000000 00
00000000 00
00000000 00
00000000 00
01011101 5d pop rbp
11000011 c3 ret } // Assumption

Bisa juga dilihat animasi berikut bila ada kebingungan :

Videos


Practice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment