Skip to content

Instantly share code, notes, and snippets.

@MarkBaggett
Last active September 27, 2022 14:52
Show Gist options
  • Select an option

  • Save MarkBaggett/eb9e6c379db3be08c99bce03a67ee3c7 to your computer and use it in GitHub Desktop.

Select an option

Save MarkBaggett/eb9e6c379db3be08c99bce03a67ee3c7 to your computer and use it in GitHub Desktop.

Revisions

  1. MarkBaggett revised this gist Sep 27, 2022. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions dump_process_variables.py
    Original file line number Diff line number Diff line change
    @@ -33,5 +33,9 @@ def get_local_envvars_pid(process_id):
    log.error("You must run this process with root permissions for it to work properly."+str(os.getresuid()))
    sys.exit(1)

    if len(sys.argv) < 2:
    log.error("You must pass a process id number.")
    sys.exit(1)

    result = get_local_envvars_pid(sys.argv[1])
    log.info(str(result))
  2. MarkBaggett revised this gist Aug 2, 2022. No changes.
  3. MarkBaggett created this gist Aug 2, 2022.
    37 changes: 37 additions & 0 deletions dump_process_variables.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import sys
    import os
    import pprint
    import pathlib
    import subprocess
    import logging
    import re

    def get_local_envvars_pid(process_id):
    gdb_script = "set variable $envs = (char **) environ\nset $i = 0\nwhile ($envs[$i] != 0)\nprint $envs[$i++]\nend\nquit\n"
    pathlib.Path("/tmp/getenv.gdb").write_text(gdb_script)
    gdb_command = f"gdb -batch -x /tmp/getenv.gdb -p {process_id}"
    ph = subprocess.Popen(gdb_command.split(),stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out,err = ph.communicate()
    log.debug(f"gdb environment variable output {out} errors {err}")
    try:
    found_items = re.findall(r'\$\d+\s+=\s+[0-9a-fx]+\s+"(\S+)=(.+)"\n', out.decode())
    except:
    log.exception("Unable to decode environment varaibles.")
    return None
    finally:
    pathlib.Path("/tmp/getenv.gdb").unlink()
    return dict(found_items)


    log = logging.getLogger("dump_envs")
    handler = logging.StreamHandler(stream=sys.stdout)
    #handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
    log.setLevel(logging.INFO)
    log.addHandler(handler)

    if not os.getresuid()[0]==0:
    log.error("You must run this process with root permissions for it to work properly."+str(os.getresuid()))
    sys.exit(1)

    result = get_local_envvars_pid(sys.argv[1])
    log.info(str(result))