Deep scan option

This commit is contained in:
oitsjustjose 2025-05-23 22:25:41 -05:00
commit 9ee9b7a727

View file

@ -6,6 +6,7 @@ import zipfile
from pathlib import Path from pathlib import Path
def scan_jar(jar_path: Path, args) -> None: def scan_jar(jar_path: Path, args) -> None:
"""Opens the jar at jar_path and iterates over all files within, """Opens the jar at jar_path and iterates over all files within,
determining if the file name matches determining if the file name matches
@ -17,27 +18,41 @@ def scan_jar(jar_path: Path, args) -> None:
scan_dir = Path(args.dir).resolve() scan_dir = Path(args.dir).resolve()
def __test(file_name: str) -> bool: def __test(scan_target: str) -> bool:
"""Tests to see if a given filename within the jar matches the provided args """Scans the scan_target to see if it contains the queries from the parsed args.
Uses regex matching if the regex flag is passed to the program If the args instruct us to use regex, compile the regex and attempt to use it
Args: Args:
file_name (str): The name (full path) of the file within the jar scan_target (str): The text to scan for - can be a filename or file contents
Returns:
bool: True if the scan_target contains the query or regex
""" """
if args.regex: if args.regex:
try: try:
return re.compile(args.query).search(file_name) is not None return re.compile(args.query).search(scan_target) is not None
except re.error as err: except re.error as err:
print(f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}") print(
f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}"
)
sys.exit(1) sys.exit(1)
else: else:
return args.query in file_name return args.query in scan_target
with zipfile.ZipFile(jar_path, 'r') as jar: with zipfile.ZipFile(jar_path, "r") as jar:
for file_name in jar.namelist(): for file_name in jar.namelist():
if __test(file_name): if args.deep: # Perform a deep scan of the jar itself
with jar.open(file_name) as file:
try:
if __test(file.read().decode()):
print(f"[{jar_path.relative_to(scan_dir)}] :: {file_name}")
except UnicodeDecodeError:
pass # This just means the file was a binary / non-text file
elif __test(file_name):
print(f"[{jar_path.relative_to(scan_dir)}] :: {file_name}") print(f"[{jar_path.relative_to(scan_dir)}] :: {file_name}")
def main(args) -> None: def main(args) -> None:
scan_dir = Path(args.dir).resolve() scan_dir = Path(args.dir).resolve()
@ -45,10 +60,33 @@ def main(args) -> None:
for file in list(filter(lambda x: x.endswith(".jar"), files)): for file in list(filter(lambda x: x.endswith(".jar"), files)):
scan_jar(Path(f"{root}/{file}").resolve(), args) scan_jar(Path(f"{root}/{file}").resolve(), args)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(prog='Jar Scanner', description='Scans jars for a given file') parser = argparse.ArgumentParser(
parser.add_argument('query', help='The term (or regex) to search for within the filename') prog="Jar Scanner", description="Scans jars for a given file"
parser.add_argument('-d', '--dir', default=os.getcwd(), help='The directory to scan. Defaults to cwd') )
parser.add_argument('-r', '--regex', default=False, action='store_true', help='Parse and use regex any in your query.') parser.add_argument(
"query", help="The term (or regex) to search for within the filename"
)
parser.add_argument(
"-d",
"--dir",
default=os.getcwd(),
help="The directory to scan. Defaults to cwd",
)
parser.add_argument(
"-r",
"--regex",
default=False,
action="store_true",
help="Parse and use regex any in your query.",
)
parser.add_argument(
"-D",
"--deep",
default=False,
action="store_true",
help="Rather than searching the file *name* for the given term (or regex), searches the contents of files instead",
)
args = parser.parse_args() args = parser.parse_args()
main(args) main(args)