Skip to content

Commit 521c049

Browse files
committed
Merge branch 'add-session-encode-option' of https://github.com/Creastery/phpggc
2 parents 178be5d + a56721e commit 521c049

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

lib/PHPGGC.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ public function serialize($gc, $parameters)
193193
$parameters = $this->process_parameters($gc, $parameters);
194194
$object = $gc->generate($parameters);
195195
$object = $this->process_object($gc, $object);
196-
$serialized = serialize($object);
196+
if(in_array('session-encode', $this->options))
197+
$serialized = $this->session_encode($object);
198+
else
199+
$serialized = serialize($object);
197200
$serialized = $this->process_serialized($gc, $serialized);
198201
return $serialized;
199202
}
@@ -366,6 +369,26 @@ function phar_generate($serialized)
366369
return $phar->generate();
367370
}
368371

372+
#
373+
# Session Encode
374+
#
375+
376+
/**
377+
* Uses session_encode() instead of serialize().
378+
*
379+
* This is useful if you have an existing arbitrary file write primitive, but the
380+
* web root directory is non-writable. In such cases, it is possible to forge a
381+
* session file containing an unserialize() payload and trigger the chain by
382+
* visiting any webpage that invokes session_start().
383+
*/
384+
function session_encode($object)
385+
{
386+
$_SESSION['_'] = $object;
387+
$serialized = session_encode();
388+
session_destroy();
389+
return $serialized;
390+
}
391+
369392
/**
370393
* Applies command line parameters and options to the gadget chain
371394
* parameters.
@@ -554,6 +577,10 @@ protected function help()
554577
$this->o(' -pf, --phar-filename <filename>');
555578
$this->o(' Defines the name of the file contained in the generated PHAR (default: test.txt)');
556579
$this->o('');
580+
$this->o('SESSION ENCODE');
581+
$this->o(' -se, --session-encode');
582+
$this->o(' Uses session_encode() instead of serialize() to generate the payload.');
583+
$this->o('');
557584
$this->o('ENHANCEMENTS');
558585
$this->o(' -f, --fast-destruct');
559586
$this->o(' Applies the fast-destruct technique, so that the object is destroyed');
@@ -648,6 +675,8 @@ function _parse_cmdline_arg(&$i, &$argv, &$parameters, &$options)
648675
'phar-jpeg' => true,
649676
'phar-prefix' => true,
650677
'phar-filename' => true,
678+
# Session Encode
679+
'session-encode' => false,
651680
# Enhancements
652681
'fast-destruct' => false,
653682
'ascii-strings' => false,
@@ -675,7 +704,8 @@ function _parse_cmdline_arg(&$i, &$argv, &$parameters, &$options)
675704
'phar-filename' => 'pf',
676705
'new' => 'N',
677706
'ascii-strings' => 'a',
678-
'armor-strings' => 'A'
707+
'armor-strings' => 'A',
708+
'session-encode' => 'se',
679709
] + $abbreviations;
680710

681711
# If we are in this function, the argument starts with a dash, so we
@@ -782,6 +812,10 @@ protected function parse_cmdline($argv)
782812
$this->o($gc, 2);
783813
$this->o($this->_get_command_line_gc($gc));
784814
return;
815+
case 'session-encode':
816+
session_name('phpggc');
817+
session_start();
818+
break;
785819
}
786820
}
787821

0 commit comments

Comments
 (0)