Skip to content

Commit 560789e

Browse files
committed
support preexec_args in process
Previously, preexec_args where supported by ssh.process only. Add support in process too, to allow easier ssh.process/process workflows
1 parent 49964f6 commit 560789e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

pwnlib/tubes/process.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class process(tube):
8686
By default, :const:`True` is used.
8787
preexec_fn(callable):
8888
Callable to invoke immediately before calling ``execve``.
89+
preexec_args(iterable):
90+
Arguments passed to ``preexec_fn``.
8991
raw(bool):
9092
Set the created pty to raw mode (i.e. disable echo and control
9193
characters). :const:`True` by default. If no pty is created, this
@@ -216,6 +218,12 @@ class process(tube):
216218
>>> p = process(binary.path, cwd=binary_dir)
217219
>>> p = process('./{}'.format(binary_name), cwd=os.path.relpath(binary_dir))
218220
>>> p = process(binary.path, cwd=os.path.relpath(binary_dir))
221+
222+
>>> def write(s):
223+
... import os
224+
... os.write(1, s)
225+
>>> print(process('false', preexec_fn=write, preexec_args=(b"Hello World!", )).recvline().strip().decode())
226+
Hello World!
219227
"""
220228

221229
STDOUT = STDOUT
@@ -238,6 +246,7 @@ def __init__(self, argv = None,
238246
stderr = STDOUT,
239247
close_fds = True,
240248
preexec_fn = lambda: None,
249+
preexec_args = (),
241250
raw = True,
242251
aslr = None,
243252
setuid = None,
@@ -330,6 +339,7 @@ def __init__(self, argv = None,
330339
self.alarm = alarm
331340

332341
self.preexec_fn = preexec_fn
342+
self.preexec_args = preexec_args
333343
self.display = display or self.program
334344
self._qemu = False
335345
self._corefile = None
@@ -471,7 +481,9 @@ def __preexec_fn(self):
471481
if self.alarm is not None:
472482
signal.alarm(self.alarm)
473483

474-
self.preexec_fn()
484+
open("test%d" % os.getpid(), "a").write(f"{self.preexec_fn}, {self.preexec_args}")
485+
486+
self.preexec_fn(*self.preexec_args)
475487

476488
def __on_enoexec(self, exception):
477489
"""We received an 'exec format' error (ENOEXEC)

0 commit comments

Comments
 (0)