Skip to content

Instantly share code, notes, and snippets.

@limitedeternity
Last active February 20, 2023 11:03
Show Gist options
  • Save limitedeternity/0545a6094ecffc89bf7bcc6b13410bb6 to your computer and use it in GitHub Desktop.
Save limitedeternity/0545a6094ecffc89bf7bcc6b13410bb6 to your computer and use it in GitHub Desktop.
A script for planting debug component DLLs instead of release ones
from argparse import ArgumentParser
import logging
from pathlib import Path
import shutil
DLL_LOCATIONS = (
Path("C:\Program Files (x86)\Common Files\SecurIT"),
Path("C:\Program Files (x86)\Zecurion\Endpoint")
)
def replace(args, logger):
if not args["with"].is_dir():
logger.error("Provided path is not a directory: %s" % args["with"].resolve())
exit(1)
dll_source_map = { dll.name: dll for dll in args["with"].glob("**/*.dll") }
dll_dest_map = { dll.name: dll for loc in DLL_LOCATIONS for dll in loc.glob("**/*.dll") }
dll_source_to_dest = { source_path.resolve(): dll_dest_map[source_name] for source_name, source_path in dll_source_map.items()
if source_name in dll_dest_map }
for source, dest in dll_source_to_dest.items():
logger.debug(f"{source} -> {dest}")
if args["dry_run"]:
continue
shutil.move(dest, dest.with_suffix(".plant_dll_bak"))
shutil.copy(source, dest)
def restore(args, logger):
dll_backups = (dll for loc in DLL_LOCATIONS for dll in loc.glob("**/*.plant_dll_bak"))
for source, dest in map(lambda bak: (bak, bak.with_suffix(".dll")), dll_backups):
logger.debug(f"{source} -> {dest}")
if args["dry_run"]:
continue
dest_exists = dest.is_file()
if dest_exists:
shutil.move(dest, dest.with_suffix(".plant_dll_tmp"))
shutil.move(source, dest)
if dest_exists:
shutil.move(dest.with_suffix(".plant_dll_tmp"), dest.with_suffix(".plant_dll_bak"))
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--dry_run", action="store_true", default=False, help="show pending changes without performing them")
subparsers = parser.add_subparsers(dest="action", required=True, help="action to perform")
parser_replace = subparsers.add_parser("replace", help="replace components with debug ones")
parser_replace.add_argument("--with", type=Path, required=True, help="directory with build artifacts")
parser_restore = subparsers.add_parser("restore", help="restore components from backup")
args = parser.parse_args().__dict__
logging.basicConfig(format="[%(levelname)s] (%(filename)s:%(lineno)d): %(message)s")
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG if args["dry_run"] else logging.WARNING)
globals()[args["action"]](args, logger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment