Path Traversal là lỗ hổng cho phép kẻ tấn công truy cập vào file hoặc thư mục nằm ngoại phạm vi được phép trên máy chủ. Dẫn đến việc bị lộ thông tin hoặc ke tấn công có thê thay đổi dữ liệu, thậm chí chiếm quyền điều khiển.
Lỗ hổng này xảy ra khi ứng dụng sử dụng dữ liệu đầu vào từ người dùng để truy cập tệp mà không kiểm tra hoặc lọc hợp lệ.
Payload cơ bản:
../ ..\ (Windows) ..\/
Eg:
app = Flask(__name__)
BASE_DIR = "/var/www/images"
@app.route("/file")
def get_file():
filename = request.args.get("name")
path = BASE_DIR + "/" + filename
return send_file(path)
Payload: /file?name=../../../etc/passwd
filename = filename.replace("../", "")
filename = urllib.parse.unquote(request.args.get("name"))
filename = filename.replace("../", "")
if filename.endswith(".png"):
open("/images/" + filename)
WAF
def check_path(input_str, times=3):
decoded_str = input_str
for _ in range(times):
decoded_str = urllib.parse.unquote(decoded_str)
real_path = os.path.realpath(os.path.join(BASE_DIR, decoded_str))
if not real_path.startswith(BASE_DIR):
raise ValueError("Invalid file path")
return real_path