Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PATH argument to which function #1738

Merged
merged 4 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
naweiss marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
20 changes: 10 additions & 10 deletions pwnlib/tubes/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
naweiss marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -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'
Expand All @@ -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)):
Expand Down
4 changes: 2 additions & 2 deletions pwnlib/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down