From 7cd0156c8d3dcb1b4dfbaa851c8a3cd5bbb072d3 Mon Sep 17 00:00:00 2001 From: nadav Date: Mon, 7 Dec 2020 19:19:17 +0200 Subject: [PATCH 1/4] Add PATH argument to which function --- pwnlib/tubes/process.py | 20 ++++++++++---------- pwnlib/util/misc.py | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pwnlib/tubes/process.py b/pwnlib/tubes/process.py index a13cc0169..86a08c657 100644 --- a/pwnlib/tubes/process.py +++ b/pwnlib/tubes/process.py @@ -278,14 +278,14 @@ def __init__(self, argv = None, #: Full path to the executable self.executable = executable_val + #: Environment passed on envp + self.env = os.environ if env is None else env_val + if self.executable is None: if shell: self.executable = '/bin/sh' else: - self.executable = which(self.argv[0]) - - #: Environment passed on envp - self.env = os.environ if env is None else env_val + self.executable = which(self.argv[0], path=self.env.get('PATH')) self._cwd = os.path.realpath(cwd or os.path.curdir) @@ -550,6 +550,10 @@ def _validate(self, cwd, executable, argv, env): if not isinstance(executable, str): executable = executable.decode('utf-8') + # Create a duplicate so we can modify it safely + env = os.environ if env is None else env + + path = env.get('PATH') # Do not change absolute paths to binaries if executable.startswith(os.path.sep): pass @@ -558,8 +562,8 @@ def _validate(self, cwd, executable, argv, env): # target directory. # # For example, 'sh' - elif os.path.sep not in executable and which(executable): - executable = which(executable) + elif os.path.sep not in executable and which(executable, path=path): + executable = which(executable, path=path) # Either there is a path component, or the binary is not in $PATH # For example, 'foo/bar' or 'bar' with cwd=='foo' @@ -581,10 +585,6 @@ def _validate(self, cwd, executable, argv, env): # - Must be a dictionary of {string:string} # - No strings may contain '\x00' # - - # Create a duplicate so we can modify it safely - env = os.environ if env is None else env - env2 = {} for k,v in env.items(): if not isinstance(k, (bytes, six.text_type)): diff --git a/pwnlib/util/misc.py b/pwnlib/util/misc.py index 9cc37b20a..68986aa7d 100644 --- a/pwnlib/util/misc.py +++ b/pwnlib/util/misc.py @@ -131,7 +131,7 @@ def write(path, data = b'', create_dir = False, mode = 'w'): with open(path, mode) as f: f.write(data) -def which(name, all = False): +def which(name, all = False, path=None): """which(name, flags = os.X_OK, all = False) -> str or str set Works as the system command ``which``; searches $PATH for ``name`` and @@ -159,7 +159,7 @@ def which(name, all = False): isroot = os.getuid() == 0 out = set() try: - path = os.environ['PATH'] + path = path or os.environ['PATH'] except KeyError: log.exception('Environment variable $PATH is not set') for p in path.split(os.pathsep): From 1d583434ddf2c5433dffdd116224ff560ccc165e Mon Sep 17 00:00:00 2001 From: nadav Date: Mon, 7 Dec 2020 19:32:24 +0200 Subject: [PATCH 2/4] [changelog] Add info on the path argument for which --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5281d12a5..f35d66453 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,7 +61,10 @@ The table below shows which release corresponds to each branch, and what date th - [#1261][1261] Misc `run_in_new_terminal` improvements (notably gdb terminated by default) - [#1695][1695] Allow using GDB Python API - [#1735][1735] Python 3.9 support in safeeval +- [#1738][1738] Which function support custom search path + - process also looks now at `env['PATH']` to find the path for the executable +[1738]: https://github.com/Gallopsled/pwntools/pull/1738 [1261]: https://github.com/Gallopsled/pwntools/pull/1261 [1695]: https://github.com/Gallopsled/pwntools/pull/1695 [1735]: https://github.com/Gallopsled/pwntools/pull/1735 From 38435db8a5da84d9d2f599d2eb51618d8c7d410e Mon Sep 17 00:00:00 2001 From: nadav Date: Mon, 7 Dec 2020 19:42:27 +0200 Subject: [PATCH 3/4] Move comment back to original location --- pwnlib/tubes/process.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pwnlib/tubes/process.py b/pwnlib/tubes/process.py index 86a08c657..79921abda 100644 --- a/pwnlib/tubes/process.py +++ b/pwnlib/tubes/process.py @@ -550,7 +550,6 @@ def _validate(self, cwd, executable, argv, env): if not isinstance(executable, str): executable = executable.decode('utf-8') - # Create a duplicate so we can modify it safely env = os.environ if env is None else env path = env.get('PATH') @@ -585,6 +584,8 @@ def _validate(self, cwd, executable, argv, env): # - Must be a dictionary of {string:string} # - No strings may contain '\x00' # + + # Create a duplicate so we can modify it safely env2 = {} for k,v in env.items(): if not isinstance(k, (bytes, six.text_type)): From 8eba6b0a4576499073eb36bb015fd2ae1741f8e6 Mon Sep 17 00:00:00 2001 From: naweiss Date: Mon, 7 Dec 2020 21:18:50 +0200 Subject: [PATCH 4/4] Sort CHANGELOG.md Co-authored-by: Arusekk --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f35d66453..32774b8be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,10 +64,10 @@ The table below shows which release corresponds to each branch, and what date th - [#1738][1738] Which function support custom search path - process also looks now at `env['PATH']` to find the path for the executable -[1738]: https://github.com/Gallopsled/pwntools/pull/1738 [1261]: https://github.com/Gallopsled/pwntools/pull/1261 [1695]: https://github.com/Gallopsled/pwntools/pull/1695 [1735]: https://github.com/Gallopsled/pwntools/pull/1735 +[1738]: https://github.com/Gallopsled/pwntools/pull/1738 ## 4.4.0 (`beta`)