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 symlink option for ssh.set_working_directory to simplify exploits #862

Merged
merged 2 commits into from
Jan 20, 2017
Merged
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
41 changes: 40 additions & 1 deletion pwnlib/tubes/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ def interactive(self, shell=None):
s.interactive()
s.close()

def set_working_directory(self, wd = None):
def set_working_directory(self, wd = None, symlink = False):
"""Sets the working directory in which future commands will
be run (via ssh.run) and to which files will be uploaded/downloaded
from if no path is provided
Expand All @@ -1602,6 +1602,17 @@ def set_working_directory(self, wd = None):
Arguments:
wd(string): Working directory. Default is to auto-generate a directory
based on the result of running 'mktemp -d' on the remote machine.
symlink(bool,str): Create symlinks in the new directory.

The default value, ``False``, implies that no symlinks should be
created.

A string value is treated as a path that should be symlinked.
It is passed directly to the shell on the remote end for expansion,
so wildcards work.

Any other value is treated as a boolean, where ``True`` indicates
that all files in the "old" working directory should be symlinked.

Examples:
>>> s = ssh(host='example.pwnme',
Expand All @@ -1612,9 +1623,33 @@ def set_working_directory(self, wd = None):
''
>>> s.pwd() == cwd
True

>>> s = ssh(host='example.pwnme',
... user='travis',
... password='demopass')
>>> homedir = s.pwd()
>>> _=s.touch('foo')

>>> _=s.set_working_directory()
>>> assert s.ls() == ''

>>> _=s.set_working_directory(homedir)
>>> assert 'foo' in s.ls().split()

>>> _=s.set_working_directory(symlink=True)
>>> assert 'foo' in s.ls().split()
>>> assert homedir != s.pwd()

>>> symlink=os.path.join(homedir,'*')
>>> _=s.set_working_directory(symlink=symlink)
>>> assert 'foo' in s.ls().split()
>>> assert homedir != s.pwd()
"""
status = 0

if symlink and not isinstance(symlink, str):
symlink = os.path.join(self.pwd(), '*')

if not wd:
wd, status = self.run_to_end('x=$(mktemp -d) && cd $x && chmod +x . && echo $PWD', wd='.')
wd = wd.strip()
Expand All @@ -1631,6 +1666,10 @@ def set_working_directory(self, wd = None):

self.info("Working directory: %r" % wd)
self.cwd = wd

if symlink:
self.ln('-s', symlink, '.')

return self.cwd

def write(self, path, data):
Expand Down