Skip to content

Commit

Permalink
Enhance tubes.ssh bytes handling (#1602)
Browse files Browse the repository at this point in the history
* Enhance tubes.ssh bytes handling

Closes #1437

* Add tests

* grep is actually nasty

* Update CHANGELOG.md

Co-authored-by: Heap Crash <66139157+heapcrash@users.noreply.github.com>
  • Loading branch information
Arusekk and heapcrash authored Jul 10, 2020
1 parent 08ab1b6 commit 48a482d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ The table below shows which release corresponds to each branch, and what date th

## 4.4.0 (`dev`)

- [#1602][1602] Fix bytes handling in ssh tubes
- [#1606][1606] Fix `asm()` and `disasm()` for MSP430, S390
- [#1616][1616] Fix `cyclic` cli for 64 bit integers

[1602]: https://github.com/Gallopsled/pwntools/pull/1602
[1606]: https://github.com/Gallopsled/pwntools/pull/1606
[1616]: https://github.com/Gallopsled/pwntools/pull/1616

Expand Down
19 changes: 10 additions & 9 deletions pwnlib/tubes/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,25 @@ def __init__(self, parent, process = None, tty = False, wd = None, env = None, r
self.process = process
self.cwd = wd or '.'
if isinstance(wd, six.text_type):
wd = wd.encode('utf-8')
wd = context._encode(wd)

env = env or {}
msg = 'Opening new channel: %r' % (process or 'shell')

if isinstance(process, (list, tuple)):
process = b' '.join((lambda x:x.encode('utf-8') if isinstance(x, six.text_type) else x)(sh_string(s)) for s in process)
process = b' '.join(context._encode(sh_string(s)) for s in process)
if isinstance(process, six.text_type):
process = process.encode('utf-8')
process = context._encode(process)

if process and wd:
process = b'cd ' + sh_string(wd) + b' >/dev/null 2>&1;' + process

if process and env:
for name, value in env.items():
if not re.match('^[a-zA-Z_][a-zA-Z0-9_]*$', name):
nameb = context._encode(name)
if not re.match(b'^[a-zA-Z_][a-zA-Z0-9_]*$', nameb):
self.error('run(): Invalid environment key %r' % name)
export = 'export %s=%s;' % (name, sh_string(value))
if isinstance(export, six.text_type):
export = export.encode('utf-8')
export = b'export %s=%s;' % (nameb, sh_string(context._encode(value)))
process = export + process

if process and tty:
Expand Down Expand Up @@ -264,11 +263,11 @@ def recv_thread(event):
if not data:
event.set()
else:
data = [six.byte2int(data)]
data = bytearray(data)

if data:
try:
self.send(b''.join(six.int2byte(c) for c in data))
self.send(bytes(bytearray(data)))
except EOFError:
event.set()
self.info('Got EOF while sending in interactive')
Expand Down Expand Up @@ -1154,6 +1153,8 @@ def system(self, process, tty = True, wd = None, env = None, timeout = None, raw
>>> py.sendline(b'exit')
>>> print(repr(py.recvline()))
b'4\n'
>>> s.system('env | grep -a AAAA', env={'AAAA': b'\x90'}).recvall()
b'AAAA=\x90\n'
"""

if wd is None:
Expand Down

0 comments on commit 48a482d

Please sign in to comment.