Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Canyoupwn.me ~

TR | x86 Assembly Cheat Sheet

cypher-matrix-caption

A scene from matrix

“There’s way too much information to decode the Matrix. You get used to it, though. Your brain does the translating. I don’t even see the code. All I see is blonde, brunette, redhead.” – Cypher

Matrix filminde geçen bu söz bana göre tersine mühendisliğin temelinde yatan mantığı anlatır. RE’in bir ayağı assembly ve OS mimarisini bilmek ise diğer ayağı da disassemble edilmiş bir koddaki pattern’ları görüp bunları programlama dillerindeki soyut yapılarla bağdaştırabilmektir. Bu amaçla Linux/x86 mimarisindeki uygulamalarda karşılaştığım birkaç yapıyı sizlerle paylaşma gereği duydum.

Fonksiyon Argümanları

/* ------------------------------------------------------------------ */
// argN = [ebp+0x8 + 0x4*(N-1)]
function(
.    arg1    // [ebp+0x8]
.    arg2    // [ebp+0xc]
.    arg3    // [ebp+0x10]
)
/* ------------------------------------------------------------------ */

 

Fonksiyon Çağırma

/* ------------------------------------------------------------------ */
func(arg1, arg2, arg3);        // push arg3
.                              // push arg2
.                              // push arg1
.                              // call func
/* ------------------------------------------------------------------ */

 

Fonksiyon Return

/* ------------------------------------------------------------------ */
return val;            // mov eax, val
.                      // leave
.                      // ret
/* ------------------------------------------------------------------ */

 

If – Else

/* ------------------------------------------------------------------ */
if (a == b)               // mov eax, DWORD PTR [ebp-0xc]
.    /* if stuff */       // cmp eax, DWORD PTR [ebp-0x10]
else                      // jne addr
.    /* else stuff */         // /* if stuff */
.                             // ...
.                         //  <addr >:
.                             // /* else stuff */
/* ------------------------------------------------------------------ */

 

Döngüler

/* ------------------------------------------------------------------ */
for (int i=0; i <=15; i++)   // mov DWORD PTR [ebp-0xc], 0x0
.    /* do stuff */          // jmp addr2
.
.                            //  <addr1 >:
.                                //    /* do stuff */
.                                //    ...
.                                //    add DWORD PTR [ebp-0xc], 0x1

.                            //  <addr2 >:
.                                 //     mov ecx, DWORD PTR [ebp-0xc]
.                                 //     cmp ecx, 0xf
.                                 //     jle addr1
/* ------------------------------------------------------------------ */

 

Dizi Elemanlarına Erişim

/* ------------------------------------------------------------------ */
// arr[N] = [addr + sizeof(type)*N]
char arr[10];            // mov eax, DWORD PTR [ebp-0xc]
if (arr[4] == 'c')       // add eax, 0x4
.                        // movzx eax, BYTE PTR [eax]
.                        // movsx eax, al
.                        // cmp eax, 0x63
/* ------------------------------------------------------------------ */

 

Değer Atama

/* ------------------------------------------------------------------ */
int a = 0;            // mov DWORD PTR [ebp-0xc], 0x0
a++;                  // mov eax, DWORD PTR [ebp-0xc]
.                     // inc eax
.                     // mov DWORD PTR [ebp-0xc], eax
/* ------------------------------------------------------------------ */

 

Örnek Fonksiyon

/* ------------------------------------------------------------------ */
int myfunction( int arg1,            // [ebp+0x8]
.        char * arg2                 // [ebp+0xc]
){

.    int a = 0;                      // mov DWORD PTR [ebp-0xc], 0x0
.    int len = strlen(arg2);         // push DWORD PTR [ebp+0xc]
.                                    // call  <strlen >
.                                    // mov DWORD PTR [ebp-0x14], eax ; strlen'den dönen değeri len değişkenine at
.
.    for(int i=0; i <=len; i++) {    // mov DWORD PTR[ebp-0x10], 0x0
.                                    // jmp addr2

.                                    //  <addr1 >:
.        if (arg2[i] == arg1) {          //     mov eax, DWORD PTR [ebp+0xc]
.                                        //     mov edx, DWORD PTR [ebp-0x10]
.                                        //     add eax, edx
.                                        //     movzx eax, BYTE PTR [eax]
.                                        //     movsx eax, al
.                                        //     cmp eax, DWORD PTR [ebp+0x8]
.                                        //     jne addr2

.            a++;   }                 // add DWORD PTR [ebp-0xc], 0x1
.    }                                // add DWORD PTR [ebp-0x10], 0x1

.                                     //  <addr2 >:
.                                         //     mov eax, DWORD PTR [ebp-0x10]
.                                         //     cmp eax, [ebp-0x14]
.                                         //     jne addr1

.    return a;                        // mov eax, DWORD PTR [ebp-0xc]
.                                     // leave
}                                     // ret

/* ------------------------------------------------------------------ */

About CanYouPwnMe

Hero!

Follow Me