Skip to content

Commit 177cd21

Browse files
authored
Merge pull request #196 from mcdruid/public_attributes
PublicProperties enhancement
2 parents bd9c6e0 + acf13a0 commit 177cd21

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/PHPGGC.php

+9
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ public function setup_enhancements()
174174
$enhancements[] = new Enhancement\Wrapper($this->parameters['wrapper']);
175175
if(in_array('fast-destruct', $this->options))
176176
$enhancements[] = new Enhancement\FastDestruct();
177+
if(in_array('public-properties', $this->options))
178+
$enhancements[] = new Enhancement\PublicProperties();
177179
if(in_array('ascii-strings', $this->options))
178180
$enhancements[] = new Enhancement\ASCIIStrings(false);
179181
if(in_array('armor-strings', $this->options))
@@ -598,6 +600,11 @@ protected function help()
598600
$this->o(' This is experimental and it might not work in some cases.');
599601
$this->o(' Note: Since strings grow by a factor of 3 using this option, the payload can get');
600602
$this->o(' really long.');
603+
$this->o(' --public-properties');
604+
$this->o(' Attempts to convert references to protected or private properties within the serialized');
605+
$this->o(' payload to public. The resulting payload should contain no null bytes and may be a little');
606+
$this->o(' shorter.');
607+
$this->o(' This is experimental and it might not work in some cases.');
601608
$this->o(' -n, --plus-numbers <types>');
602609
$this->o(' Adds a + symbol in front of every number symbol of the given type.');
603610
$this->o(' For instance, -n iO adds a + in front of every int and object name size:');
@@ -679,6 +686,7 @@ function _parse_cmdline_arg(&$i, &$argv, &$parameters, &$options)
679686
'session-encode' => false,
680687
# Enhancements
681688
'fast-destruct' => false,
689+
'public-properties' => false,
682690
'ascii-strings' => false,
683691
'armor-strings' => false,
684692
'plus-numbers' => true,
@@ -702,6 +710,7 @@ function _parse_cmdline_arg(&$i, &$argv, &$parameters, &$options)
702710
'phar-jpeg' => 'pj',
703711
'phar-prefix' => 'pp',
704712
'phar-filename' => 'pf',
713+
'public-properties' => 'pub',
705714
'new' => 'N',
706715
'ascii-strings' => 'a',
707716
'armor-strings' => 'A',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace PHPGGC\Enhancement;
4+
5+
/**
6+
* Public Properties
7+
* Attempts to convert references to protected or private properties within the
8+
* serialized payload to public.
9+
*
10+
* This can be useful because when PHP serializes a non-public property of an
11+
* object it prepends the property name with an asterisk (for protected) or the
12+
* class name (for private) surrounded by null bytes, which are easy to lose if
13+
* the payload is transmitted or stored as plain text without encoding. If that
14+
* happens, the payload will fail to unserialize because the string length of the
15+
* property name (and the name itself) will be incorrect.
16+
*
17+
* As an added bonus, payloads are slightly smaller without the prefixes.
18+
*
19+
* Converting properties to public tends to work in more recent PHP versions but
20+
* can cause problems in older versions (before PHP 7.2).
21+
*
22+
* This functionality may not work properly if a chain includes one or more
23+
* objects that have a custom serialize / unserialize implementation.
24+
*/
25+
class PublicProperties extends Enhancement
26+
{
27+
28+
/**
29+
* Post process step of the public-properties technique: removes prefixes
30+
* denoting protected or private properties, converting them to public.
31+
*/
32+
public function process_serialized($serialized)
33+
{
34+
return preg_replace_callback('/\bs:(\d+):"\x00([\w\\\]+|\*)\x00/', [$this, 'remove_prefix'], $serialized);
35+
}
36+
37+
public function remove_prefix($matches)
38+
{
39+
$length = $matches[1];
40+
$reduction = strlen($matches[2]) + 2; // prefix + 2 null bytes
41+
return 's:' . ($length - $reduction) . ':"';
42+
}
43+
}

0 commit comments

Comments
 (0)