검증되지 않은 외부 입력값으로 파일 및 서버 등 시스템 자원에 대한 접근 혹은 식별을 허용할 경우, 입력값 조작으로 시스템이 보호하는 자원에 임의로 접근할 수 있는 보안약점이다.
경로조작 및 자원삽입 약점을 이용하여 공격자는 자원의 수정․삭제, 시스템 정보누출, 시스템 자원 간 충돌로 인한 서비스 장애 등을 유발시킬 수 있다.
즉, 경로 조작 및 자원 삽입으로 공격자가 허용되지 않은 권한을 획득하여, 설정에 관계된 파일을 변경하거나 실행시킬 수 있다.
외부의 입력을 자원(파일, 소켓의 포트 등)의 식별자로 사용하는 경우, 적절한 검증을 거치도록 하거나,
사전에 정의된 적합한 리스트에서 선택되도록 한다. 특히, 외부의 입력이 파일명인 경우에는 경로순회(directory traversal)3) 공격의 위험이 있는 문자( “ / ₩ .. 등 )를 제거할 수 있는 필터를 이용한다.
//외부로부터 입력받은 값을 검증 없이 사용할 경우 안전하지 않다.
String fileName = request.getParameter("P");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
try {
외부 입력값(P)이 버퍼로 내용을 옮길 파일의 경로설정에 사용되고 있다.
만일 공격자에 의해 P의 값으로 ../../../rootFile.txt와 같은 값을 전달하면 의도하지 않았던 파일의 내용이 버퍼에 쓰여 시스템에 악영향을 준다.
response.setHeader("Content-Disposition", "attachment;filename="+fileName+";");
...
//외부로부터 입력받은 값이 검증 또는 처리 없이 파일처리에 수행되었다.
fis = new FileInputStream("C:/datas/" + fileName);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(response.getOutputStream());
String fileName = request.getParameter("P");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
try {
response.setHeader("Content-Disposition", "attachment;filename="+fileName+";");
...
// 외부 입력받은 값을 경로순회 문자열(./₩)을 제거하고 사용해야한다.
filename = filename.replaceAll("₩₩.", "").replaceAll("/", "").replaceAll("₩₩₩₩", "");
fis = new FileInputStream("C:/datas/" + fileName);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(response.getOutputStream());
int read;
while((read = bis.read(buffer, 0, 1024)) != -1) {
bos.write(buffer,0,read);
}
}
외부 입력값에 대하여 상대경로를 설정할 수 없도록 경로순회 문자열( / ₩ & .. 등 )을 제거하고 파일의 경로설정에 사용한다.
public class ShowHelp {
private final static String safeDir = "c:₩₩help_files₩₩";
public static void main(String[] args) throws IOException {
String helpFile = args[0];
try (BufferedReader br = new BufferedReader(new FileReader(safeDir + helpFile))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
...
}
인자값이 파일 이름인 경우에는 애플리케이션에서 정의(제한)한 디렉터리 c:₩help_files₩에서 파일을 읽어서 출력
하지만, args[0]의 값으로 “..₩..₩..₩windows₩system32₩drivers₩etc₩hosts”와 같이 경로조작 문자열을 포함한 입력이 들어오는 경우 접근이 제한된 경로의 파일을 열람할 수 있다
public class ShowHelpSolution {
private final static String safeDir = "c:₩₩help_files₩₩";
//경로조작 문자열 포함 여부를 확인하고 조치 후 사용하도록 한다.
public static void main(String[] args) throws IOException {
String helpFile = args[0];
if (helpFile != null) {
helpFile = helpFile.replaceAll("₩₩. {2, }[/₩₩₩₩]", "");
}
try (BufferedReader br = new BufferedReader(new FileReader(safeDir + helpFile))) {
...
//외부 입력 값이 검증 없이 파일처리에 사용 되었다.
string file = Request.QueryString["path"];
if (file != null)
{
File.Delete(file); 6:
}
다음 C# 코드는 외부 입력값을 파일명에 바로 사용하고 있다.
이는 의도치 않은 파일의 손상을 가져올 수 있다.
string file = Request.QueryString["path"];
if (file != null)
{
//경로조작 문자열이 있는지 확인하고 파일 처리를 하도록 한다.
if (file.IndexOf('₩₩') > -1 || file.IndexOf('/') > -1)
{
Response.Write("Path Traversal Attack");
}
else
{
File.Delete(file);
}
}
char* filename = getenv(“reportfile”);
FILE *fin = NULL;
// 외부 설정 값에서 받은 파일 이름을 그대로 사용한다.
fin = fopen(filename, “r”);
while (fgets(buf, BUF_LEN, fin)) {
// 파일 내용 출력
}
아래 C 코드는 외부 입력 값을 파일 경로로 바로 사용하고 있다.
이는 공격자가 환경 변수 reportfile을 조작하여 디렉토리 경로를 조작할 수 있다.
FILE *fin = NULL;
regex_t regex;
Int ret;
char* filename = getenv(“reportfile”);
ret = regcomp(®ex, “.*₩₩.₩₩..*”, 0);
// 경로 조작 가능성 있는 문자열 탐지
ret = regexec(®ex, filename, 0, NULL, 0);
If (!ret) {
// 경로 조작 문자열 발견, 오류 처리
}
// 필터링된 파일 이름으로 사용
fin = fopen(filename, “r”);
while (fgets(buf, BUF_LEN, fin)) {
// 파일 내용 출력
}
아래 C 코드는 외부에서 불러온 파일 이름을 그대로 사용하지 않고 경로 조작 가능성이 있는 문자열을 검증하고 사용한다