1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| <?php
namespace ctfshow;
class C{
const __REF_OFFSET_1 = 0x41; const __REF_OFFSET_2 = 0x7b; const __REF_OFFSET_3 = 0x5b; const __REF_OFFSET_4 = 0x60; const __REF_OFFSET_5 = 0x30; const __REF_OFFSET_6 = 0x5f;
const __REF_SIZE__= 20; const __REF_VAL_SIZE__= 50;
private $cursor=0; private $cache; private $ref_table=[];
function main(){ $flag = md5(file_get_contents("/flag")); $this->define('ctfshow',self::__REF_VAL_SIZE__); $this->define('flag',strlen($flag)); $this->neaten(); $this->fill('flag',$flag); $this->fill('ctfshow',$_POST['data']); if($this->read('ctfshow')===$this->read('flag')){ echo $flag; } }
private function fill($ref,$val){ rewind($this->cache); fseek($this->cache, $this->ref_table[$ref]+23);
$arr = str_split($val);
foreach ($arr as $s) { fwrite($this->cache, pack("C",ord($s))); }
for ($i=sizeof($arr); $i < self::__REF_VAL_SIZE__; $i++) { fwrite($this->cache, pack("C","\x00")); }
$this->cursor= ftell($this->cache); }
public static function clear($var){ ; }
private function neaten(){ $this->ref_table['_clear_']=$this->cursor; $arr = str_split("_clear_"); foreach ($arr as $s) { $this->write(ord($s),"C"); } for ($i=sizeof($arr); $i < self::__REF_SIZE__; $i++) { $this->write("\x00",'C'); }
$arr = str_split(__NAMESPACE__."\C::clear"); foreach ($arr as $s) { $this->write(ord($s),"C"); }
$this->write(0x36d,'Q'); $this->write(0x30,'C');
for ($i=1; $i < self::__REF_SIZE__; $i++) { $this->write("\x00",'C'); }
}
private function readNeaten(){ rewind($this->cache); fseek($this->cache, $this->ref_table['_clear_']+self::__REF_SIZE__); $f = $this->truncation(fread($this->cache, self::__REF_SIZE__-4)); $t = $this->truncation(fread($this->cache, self::__REF_SIZE__-12)); $p = $this->truncation(fread($this->cache, self::__REF_SIZE__)); call_user_func($f,$p);
}
private function define($ref,$size){ $this->checkRef($ref); $r = str_split($ref); $this->ref_table[$ref]=$this->cursor; foreach ($r as $s) { $this->write(ord($s),"C"); } for ($i=sizeof($r); $i < self::__REF_SIZE__; $i++) { $this->write("\x00",'C'); }
fwrite($this->cache,pack("v",$size)); fwrite($this->cache,pack("C",0x31)); $this->cursor= ftell($this->cache);
for ($i=0; $i < $size; $i++) { $this->write("\x00",'a'); } }
private function read($ref){
if(!array_key_exists($ref,$this->ref_table)){ throw new \Exception("Ref not exists!", 1); }
if($this->ref_table[$ref]!=0){ $this->seekCursor($this->ref_table[$ref]); }else{ rewind($this->cache); } $cref = fread($this->cache, 20); $csize = unpack("v", fread($this->cache, 2)); $usize = fread($this->cache, 1);
$val = fread($this->cache, $csize[1]);
return $this->truncation($val);
}
private function write($val,$fmt){ $this->seek(); fwrite($this->cache,pack($fmt,$val)); $this->cursor= ftell($this->cache); }
private function seek(){ rewind($this->cache); fseek($this->cache, $this->cursor); }
private function truncation($data){
return implode(array_filter(str_split($data),function($var){ return $var!=="\x00"; }));
} private function seekCursor($cursor){ rewind($this->cache); fseek($this->cache, $cursor); } private function checkRef($ref){ $r = str_split($ref);
if(sizeof($r)>self::__REF_SIZE__){ throw new \Exception("Refenerce size too long!", 1); }
if(is_numeric($r[0]) || $this->checkByte($r[0])){ throw new \Exception("Ref invalid!", 1); }
array_shift($r);
foreach ($r as $s) {
if($this->checkByte($s)){ throw new \Exception("Ref invalid!", 1); } } }
private function checkByte($check){ if(ord($check) <=self::__REF_OFFSET_5 || ord($check) >=self::__REF_OFFSET_2 ){ return true; }
if(ord($check) >=self::__REF_OFFSET_3 && ord($check) <= self::__REF_OFFSET_4 && ord($check) !== self::__REF_OFFSET_6){ return true; }
return false;
}
function __construct(){ $this->cache=fopen("php://memory","wb"); }
public function __destruct(){ $this->readNeaten(); fclose($this->cache); }
} highlight_file(__FILE__); error_reporting(0); $c = new C;
$c->main();
|