#!/bin/bash export DIR=$(pwd) # Source - https://stackoverflow.com/a/7069755 # Posted by Flexo - Save the data dump, modified by community. See post 'Timeline' for change history # Retrieved 2026-06-20, License - CC BY-SA 4.0 while test $# -gt 0; do case "$1" in -h|--help) echo "jarscan - scan jars for a file with a given search string." echo " Supports deep searching (searching all files within the jar) and regex" echo " " echo "Usage:" echo " jarscan your-query-here [-d,--dir=DIR] [-r,--regex] [-D,--deep] " echo " " echo "options:" echo "-h, --help Show this prompt" echo "-d, --dir=DIR The directory to scan. Defaults to $(pwd)" echo "-r, --regex Parse and use RegExp in your expression" echo "-D, --deep Perform a deep search, scanning file contents for your query" exit 0 ;; -d) shift if test $# -gt 0; then export DIR=$1 else echo "No directory specified!" exit 1 fi shift ;; --dir*) export DIR=`echo $1 | sed -e 's/^[^=]*=//g'` shift ;; -r|--regex) export USE_REGEX=1 shift ;; -D|--deep) export USE_DEEP=1 shift ;; *) if test -z "$QUERY"; then export QUERY=$1 shift else echo "Only one query can be provided!" exit 1 fi ;; esac done if test -z "$QUERY"; then echo "Query is required!" exit 1 fi # Set up the python environment if [ ! -f "/tmp/shell-scripts/venv/bin/python" ]; then mkdir -p /tmp/shell-scripts python3 -m venv /tmp/shell-scripts/venv > /dev/null 2>&1 fi # Finally run the script /tmp/shell-scripts/venv/bin/python << EOF import os import re from argparse import ArgumentParser from pathlib import Path from sys import exit from zipfile import ZipFile scan_dir = Path("$DIR").resolve() query = "$QUERY" regex = "$USE_REGEX" == "1" deep = "$USE_DEEP" == "1" def scan_jar(jar_path: Path) -> None: """Opens the jar at jar_path and iterates over all files within, determining if the file name matches Args: jar_path (Path): The absolute path, resolved, to the Jar file """ def __test(scan_target: str) -> bool: """Scans the scan_target to see if it contains the queries from the parsed args. If the args instruct us to use regex, compile the regex and attempt to use it Args: 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 regex: try: return re.compile(query).search(scan_target) is not None except re.error as err: print(f"Regex Error! Invalid regular expression passed:\nFull Trace:\n{err}") exit(1) else: return query in scan_target with ZipFile(jar_path, "r") as jar: for file_name in jar.namelist(): if 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}") for root, _, files in os.walk(Path("$DIR").resolve()): for file in list(filter(lambda x: x.endswith(".jar"), files)): scan_jar(Path(f"{root}/{file}")) EOF