OS 명령 삽입 공격
Noncompliant Code Example
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
public void runUnsafe(HttpServletRequest request) throws IOException {
String cmd = request.getParameter("command");
String arg = request.getParameter("arg");
Runtime.getRuntime().exec(cmd+" "+arg); // Noncompliant
}
Compliant Solution
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
public void runUnsafe(HttpServletRequest request) throws IOException {
String cmd = request.getParameter("command");
String arg = request.getParameter("arg");
if(cmd.equals("/usr/bin/ls") || cmd.equals("/usr/bin/cat"))
{
// only ls or cat command are authorized
String cmdarray[] = new String[] { cmd, arg };
Runtime.getRuntime().exec(cmdarray); // Compliant
}
}
Compliant Solution
class MySecurityManager extends SecurityManager {
MySecurityManager() {
super();
}
public void checkExec(String cmd) {
if(!(cmd.equals("/usr/bin/ls") || cmd.equals("/usr/bin/cat"))) {
throw new SecurityException("Unauthorized command: "+cmd);
}
}
}
'Secure Coding' 카테고리의 다른 글
서버측 요청 위조 공격 (0) | 2021.09.17 |
---|---|
HTTP 응답 헤더 인젝션 (0) | 2021.09.17 |
XPath 인젝션 공격 (0) | 2021.09.17 |
데이터베이스에 연결 시 암호 보안 (0) | 2021.09.17 |
데이터베이스 쿼리 인젝션 공격 (0) | 2021.09.17 |
댓글