-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip-slip-code.java
More file actions
49 lines (39 loc) · 2.17 KB
/
zip-slip-code.java
File metadata and controls
49 lines (39 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
취약점 개요
zip 아카이브에 있는 파일 이름은 입력 값 및 유효성 검증이 필요하다.
실제로 파일 이름에 '../' 와 같은 Directory Traversal 공격 페이로드가 포함될 시 사용자가 접근할 수 없는 파일 시스템 경로에 접근할 위험성이 존재한다.
Directory Traversal 공격에 성공할 시, 공격자는 파일 시스템의 중요한 정보를 Read/Update/Delete 등 가능하며 때로는 RCE로 이어질 수 있다.
*/
// Noncompliant Code Example
// zipFile: 사용자로부터 입력받은 ZIP 아카이브 파일
public static List<String> zipSlipNoncompliant(ZipFile zipFile) throws IOException {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
List<String> filesContent = new ArrayList<>();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File file = new File(entry.getName());
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8); // Noncompliant
filesContent.add(content);
}
return filesContent;
}
// Compliant Solution
// zipFile: 사용자로부터 입력받은 ZIP 아카이브 파일
// targetDirectory: 원래 ZIP 아카이브가 풀리기 위한 디렉터리 위치 값
// 정확한 targetDirectory를 입력받은 후, 입력받은 값과 Extract된 파일의 경로 값과 동일한지 검증하는 로직을 추가해야 한다.
public static List<String> zipSlipCompliant(ZipFile zipFile, String targetDirectory) throws IOException {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
List<String> filesContent = new ArrayList<>();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File file = new File(entry.getName());
String canonicalDestinationPath = file.getCanonicalPath();
// Extract된 파일의 경로 값이 입력받은 targetDirectory 값과 다를 경우 예외처리한다.
if (!canonicalDestinationPath.startsWith(targetDirectory)) {
throw new IOException("Entry is outside of the target directory");
}
String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8); // OK
filesContent.add(content);
}
return filesContent;
}