From ccb867db950a697be6e1c4dffe01b84e5073af1d Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 12 Oct 2022 19:54:59 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- binja/analyze.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/binja/analyze.py b/binja/analyze.py index 768f604..27605bd 100644 --- a/binja/analyze.py +++ b/binja/analyze.py @@ -27,7 +27,26 @@ def analyze_crash(core): print("Usage:", sys.argv[0], "") sys.exit(1) with tarfile.open(sys.argv[1], "r:gz") as tar: - tar.extractall() + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tar) dirname = tar.getnames()[0] binaries = {}