-
Notifications
You must be signed in to change notification settings - Fork 0
添加 controller #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package org.joychou.controller; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.joychou.security.SecurityUtil; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.joychou.util.WebUtils; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RestController; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @RestController | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class CommandInject { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 文件名与类名不一致 文件名为 -public class CommandInject {
+public class CommandInject2 {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * http://localhost:8080/codeinject?filepath=/tmp;cat /etc/passwd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param filepath filepath | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GetMapping("/codeinject") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String codeInject(String filepath) throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String[] cmdList = new String[]{"sh", "-c", "ls -la " + filepath}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ProcessBuilder builder = new ProcessBuilder(cmdList); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builder.redirectErrorStream(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Process process = builder.start(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return WebUtils.convertStreamToString(process.getInputStream()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 严重安全漏洞:命令注入 此方法存在严重的命令注入漏洞。用户输入的 建议使用以下方式修复: @GetMapping("/codeinject")
public String codeInject(String filepath) throws IOException {
- String[] cmdList = new String[]{"sh", "-c", "ls -la " + filepath};
+ // 方案1:使用参数化命令,避免 shell 解析
+ String[] cmdList = new String[]{"ls", "-la", filepath};
+
+ // 方案2:如果必须使用 shell,先进行严格过滤
+ // String filterFilePath = SecurityUtil.cmdFilter(filepath);
+ // if (filterFilePath == null) {
+ // throw new IllegalArgumentException("非法的文件路径");
+ // }
+ // String[] cmdList = new String[]{"sh", "-c", "ls -la " + filterFilePath};
+
ProcessBuilder builder = new ProcessBuilder(cmdList);
builder.redirectErrorStream(true);
Process process = builder.start();
return WebUtils.convertStreamToString(process.getInputStream());
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Host Injection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Host: hacked by joychou;cat /etc/passwd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * http://localhost:8080/codeinject/host | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GetMapping("/codeinject/host") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String codeInjectHost(HttpServletRequest request) throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String host = request.getHeader("host"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(host); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 使用白名单或其他防护措施,如仅允许合法域名 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String safeHost = SecurityUtil.hostFilter(host); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (safeHost == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("非法或危险的host"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String[] cmdList = new String[]{"sh", "-c", "curl " + safeHost}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ProcessBuilder builder = new ProcessBuilder(cmdList); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builder.redirectErrorStream(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Process process = builder.start(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return WebUtils.convertStreamToString(process.getInputStream()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GetMapping("/codeinject/sec") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String codeInjectSec(String filepath) throws IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String filterFilePath = SecurityUtil.cmdFilter(filepath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (null == filterFilePath) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "Bad boy. I got u."; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String[] cmdList = new String[]{"sh", "-c", "ls -la " + filterFilePath}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ProcessBuilder builder = new ProcessBuilder(cmdList); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builder.redirectErrorStream(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Process process = builder.start(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return WebUtils.convertStreamToString(process.getInputStream()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+56
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 相对安全但仍需改进 此方法使用了 但仍建议:
改进方案: @GetMapping("/codeinject/sec")
public String codeInjectSec(String filepath) throws IOException {
String filterFilePath = SecurityUtil.cmdFilter(filepath);
if (null == filterFilePath) {
return "Bad boy. I got u.";
}
- String[] cmdList = new String[]{"sh", "-c", "ls -la " + filterFilePath};
+ // 使用参数化命令,避免 shell 注入
+ String[] cmdList = new String[]{"ls", "-la", filterFilePath};
ProcessBuilder builder = new ProcessBuilder(cmdList);
builder.redirectErrorStream(true);
Process process = builder.start();
return WebUtils.convertStreamToString(process.getInputStream());
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HELLO 1