경로 순회
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
OWASP 카테고리: MASVS-STORAGE: 저장소
개요
경로 순회 취약점은 유효성 검증 없이 파일 시스템 API로 전달되는 경로의 일부를 공격자가 제어할 수 있는 경우에 발생합니다. 이로 인해 승인되지 않은 파일 시스템 작업이 발생할 수 있습니다. 예를 들어, 공격자는 ../
와 같은 특수문자를 사용하여 타겟팅된 디렉터리 외부를 순회하는 방식으로 리소스 타겟을 예기치 않게 변경할 수 있습니다.
영향
이 취약점의 영향은 작업 및 파일 콘텐츠에 따라 달라지지만, 일반적으로 파일 덮어쓰기(파일에 쓸 때), 데이터 유출(파일을 읽을 때) 또는 권한 변경(파일/디렉터리 권한을 변경할 때)으로 이어집니다.
완화 조치
File.getCanonicalPath()
를 사용하여 경로를 표준화하고 접두사를 예상 디렉터리와 비교합니다.
Kotlin
@Throws(IllegalArgumentException::class)
fun saferOpenFile(path: String, expectedDir: String?): File {
val f = File(path)
val canonicalPath = f.canonicalPath
require(canonicalPath.startsWith(expectedDir!!))
return f
}
자바
public File saferOpenFile (String path, String expectedDir) throws IllegalArgumentException {
File f = new File(path);
String canonicalPath = f.getCanonicalPath();
if (!canonicalPath.startsWith(expectedDir)) {
throw new IllegalArgumentException();
}
return f;
}
추가적인 권장사항은 예상되는 결과만 발생하도록 유효성 검증을 사용하는 것입니다. 예를 들면 다음과 같습니다.
- 우발적인 덮어쓰기가 이루어지지 않도록 파일이 이미 있는지 확인합니다.
- 데이터 유출이나 올바르지 않은 권한 변경을 방지하기 위해 타겟팅된 파일이 예상된 타겟인지 확인합니다.
- 작업의 현재 디렉터리가 표준 경로의 반환 값에 예상되는 것과 정확히 일치하는지 확인합니다.
- 작업이 루트 권한으로 실행되고 있지 않은지 확인하는 등 권한 시스템의 범위가 해당 작업으로 명시되었는지 확인하고, 디렉터리 권한의 범위가 지정된 서비스 또는 명령어로 지정되었는지 확인합니다.
추천 서비스
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2023-12-08(UTC)
[null,null,["최종 업데이트: 2023-12-08(UTC)"],[],[],null,["# Path traversal\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-STORAGE: Storage](https://mas.owasp.org/MASVS/05-MASVS-STORAGE)\n\nOverview\n--------\n\nPath traversal vulnerabilities occur when an attacker can control part of the\npath that is then passed to the file system APIs without validation. This can\nlead to unauthorized file system operations. For example, an attacker might use\nspecial characters such as `../` to unexpectedly change the resource target, by\ntraversing outside of the targeted directory.\n\nImpact\n------\n\nThe impact varies depending on the operation and file content, but generally\nleads to a file overwrite (when writing files), data leak (when reading files),\nor permission changes (when changing file or directory permissions).\n\nMitigations\n-----------\n\nCanonicalize the path using [`File.getCanonicalPath()`](/reference/java/io/File#getCanonicalPath()) and compare the\nprefix with the expected directory: \n\n### Kotlin\n\n @Throws(IllegalArgumentException::class)\n fun saferOpenFile(path: String, expectedDir: String?): File {\n val f = File(path)\n val canonicalPath = f.canonicalPath\n require(canonicalPath.startsWith(expectedDir!!))\n return f\n }\n\n### Java\n\n public File saferOpenFile (String path, String expectedDir) throws IllegalArgumentException {\n File f = new File(path);\n String canonicalPath = f.getCanonicalPath();\n if (!canonicalPath.startsWith(expectedDir)) {\n throw new IllegalArgumentException();\n }\n return f;\n }\n\nAn additional best practice is to use validation to ensure only expected\noutcomes occur. Examples include the following:\n\n- Checking if the file already exists to prevent an accidental overwrite.\n- Checking if the targeted file is an expected target to prevent leaking data or incorrectly changing permissions.\n- Checking if the current directory of the operation is exactly as expected in the return value from the canonical path.\n- Ensuring a permissions system is explicitly scoped to the operation, such as checking that it isn't running services as root, and ensuring that the directory permissions are scoped to the service or command specified."]]