os.path.join 사용 시 LFI로 이루어 질 수 있어 주의점을 알아봅시다
What is os.path.join?
Join one or more path segments intelligently. The return value is the concatenation of path and all members of *paths, with exactly one directory separator following each non-empty part, except the last. That is, the result will only end in a separator if the last part is either empty or ends in a separator. If a segment is an absolute path (which on Windows requires both a drive and a root), then all previous segments are ignored and joining continues from the absolute path segment
.
On Windows, the drive is not reset when a rooted path segment (e.g., r’\foo’) is encountered. If a segment is on a different drive or is an absolute path, all previous segments are ignored and the drive is reset. Note that since there is a current directory for each drive, os.path.join(“c:”, “foo”) represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
os.path.join
함수는 파이썬에서 경로를 나타내는 함수입니다.
1
os.path.join(path, *paths)
Vuln Code
아래와 같이 현재 디렉토리 경로에, 사용자의 입력값으로 경로를 리턴합니다.
해당 값을 기준으로 파일 다운로드 로직을 만들면 LFI
가 발생할 수 있어 주의가 필요합니다.
설명에서도 언급이 되어 있는데 두번째 인자가 절대 경로
, window의 경우 드라이브(ex C:/, D:/등)
일 경우 첫 번째 인자의 값이 무시가 됩니다.
1
2
3
4
5
6
7
import os
filename = input("Enter the filename: ")
current_dir = os.path.dirname(os.path.abspath(__file__))
result = os.path.join(current_dir, filename)
print(result)
Result
윈도우 환경에서 테스트 결과입니다.
1
2
3
4
5
Enter the filename: test
C:\Users\glasses96\Desktop\pentest\test
Enter the filename: C:/
C:/ #LFI
Conclusion
오랜만에 워게임 문제를 풀면서 새로운 지식을 하나 더 얻어 갈 수 있었네요.
요즘 바쁘다는 핑계로 놀고 있는데, 항상 공부를 해야…..