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

Allow to passthru kwargs on ssh.__getattr__ convenience function to fix SSH motd problems #2517

Merged
merged 6 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ The table below shows which release corresponds to each branch, and what date th
- [#2522][2522] Support starting a kitty debugging window with the 'kitten' command
- [#2524][2524] Raise EOFError during `process.recv` when stdout closes on Windows
- [#2526][2526] Properly make use of extra arguments in `packing` utilities. `sign` parameter requires keyword syntax to specify it.
- [#2517][2517] Allow to passthru kwargs on `ssh.__getattr__` convenience function to fix SSH motd problems

[2507]: https://github.com/Gallopsled/pwntools/pull/2507
[2522]: https://github.com/Gallopsled/pwntools/pull/2522
[2524]: https://github.com/Gallopsled/pwntools/pull/2524
[2526]: https://github.com/Gallopsled/pwntools/pull/2526
[2517]: https://github.com/Gallopsled/pwntools/pull/2517

## 4.15.0 (`beta`)
- [#2508][2508] Ignore a warning when compiling with asm on nix
Expand Down
26 changes: 15 additions & 11 deletions pwnlib/tubes/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ def __init__(self, user=None, host=None, port=22, password=None, key=None,

if self.sftp:
with context.quiet:
self.cwd = packing._decode(self.pwd())
self.cwd = packing._decode(self.pwd(tty=False))
else:
self.cwd = '.'

Expand Down Expand Up @@ -1140,7 +1140,7 @@ def run_to_end(self, process, tty = False, cwd = None, env = None, wd = None):
cwd = wd

with context.local(log_level = 'ERROR'):
c = self.run(process, tty, cwd = cwd, env = env, timeout = Timeout.default)
c = self.system(process, tty, cwd = cwd, env = env, timeout = Timeout.default)
data = c.recvall()
retcode = c.wait()
c.close()
Expand Down Expand Up @@ -1203,7 +1203,7 @@ def __getitem__(self, attr):
>>> print(repr(s['echo hello']))
b'hello'
"""
return self.run(attr).recvall().strip()
return self.system(attr).recvall().strip()

def __call__(self, attr):
"""Permits function-style access to run commands over SSH
Expand All @@ -1214,10 +1214,12 @@ def __call__(self, attr):
>>> print(repr(s('echo hello')))
b'hello'
"""
return self.run(attr).recvall().strip()
return self.system(attr).recvall().strip()

def __getattr__(self, attr):
"""Permits member access to run commands over SSH
"""Permits member access to run commands over SSH.

Supports other keyword arguments which are passed to :meth:`.system`.

Examples:

Expand All @@ -1228,6 +1230,8 @@ def __getattr__(self, attr):
b'travis'
>>> s.echo(['huh','yay','args'])
b'huh yay args'
>>> s.echo('value: $MYENV', env={'MYENV':'the env'})
b'value: the env'
"""
bad_attrs = [
'trait_names', # ipython tab-complete
Expand All @@ -1239,7 +1243,7 @@ def __getattr__(self, attr):
raise AttributeError

@LocalContext
def runner(*args):
def runner(*args, **kwargs):
if len(args) == 1 and isinstance(args[0], (list, tuple)):
command = [attr]
command.extend(args[0])
Expand All @@ -1248,7 +1252,7 @@ def runner(*args):
command.extend(args)
command = b' '.join(packing._need_bytes(arg, min_wrong=0x80) for arg in command)

return self.run(command).recvall().strip()
return self.system(command, **kwargs).recvall().strip()
return runner

def connected(self):
Expand Down Expand Up @@ -1348,7 +1352,7 @@ def update(has, total):

with context.local(log_level = 'ERROR'):
cmd = 'cat < ' + sh_string(remote)
c = self.run(cmd)
c = self.system(cmd)
data = b''

while True:
Expand All @@ -1369,7 +1373,7 @@ def update(has, total):
def _download_to_cache(self, remote, p, fingerprint=True):

with context.local(log_level='error'):
remote = self.readlink('-f',remote)
remote = self.readlink('-f', remote, tty=False)
if not hasattr(remote, 'encode'):
remote = remote.decode('utf-8')

Expand Down Expand Up @@ -1534,7 +1538,7 @@ def upload_data(self, data, remote):

with context.local(log_level = 'ERROR'):
cmd = 'cat > ' + sh_string(remote)
s = self.run(cmd, tty=False)
s = self.system(cmd, tty=False)
s.send(data)
s.shutdown('send')
data = s.recvall()
Expand Down Expand Up @@ -1592,7 +1596,7 @@ def upload_dir(self, local, remote=None):
remote_tar = self.mktemp('--suffix=.tar.gz')
self.upload_file(local_tar, remote_tar)

untar = self.run(b'cd %s && tar -xzf %s' % (sh_string(remote), sh_string(remote_tar)))
untar = self.system(b'cd %s && tar -xzf %s' % (sh_string(remote), sh_string(remote_tar)))
message = untar.recvrepeat(2)

if untar.wait() != 0:
Expand Down
Loading