Skip to content

Commit

Permalink
Fix adb.write and adb.listdir operating on the wrong type of object
Browse files Browse the repository at this point in the history
  • Loading branch information
zachriggle committed Dec 14, 2016
1 parent e821635 commit 6cbef83
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions pwnlib/adb/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
https://android.googlesource.com/platform/system/core/+/master/adb/protocol.txt
"""
import functools
import stat
import time

from ..context import context
Expand Down Expand Up @@ -298,8 +299,6 @@ def wrapper(self, *a, **kw):
return rv
return wrapper

@_with_transport
@_sync
def list(self, path):
"""Execute the ``LIST`` command of the ``SYNC`` API.
Expand Down Expand Up @@ -332,6 +331,19 @@ def list(self, path):
>>> adb.Client().list('/does/not/exist')
{}
"""
st = self.stat(path)

if not st:
log.error("Cannot list directory %r: Does not exist" % path)

if not stat.S_ISDIR(st['mode']):
log.error("Cannot list directory %r: Path is not a directory" % path)

return self._list(path)

@_with_transport
@_sync
def _list(self, path):
self.c.flat('LIST', len(path), path)
files = {}
while True:
Expand Down Expand Up @@ -391,9 +403,35 @@ def stat(self, path):

return {'mode': mode, 'size': size, 'time': time}

def write(self, path, data, mode=0o755, timestamp=None, callback=None):
"""Execute the ``WRITE`` command of the ``SYNC`` API.
Arguments:
path(str): Path to the file to write
data(str): Data to write to the file
mode(int): File mode to set (e.g. ``0o755``)
timestamp(int): Unix timestamp to set the file date to
callback(callable): Callback function invoked as data
is written. Arguments provided are:
- File path
- All data
- Expected size of all data
- Current chunk
- Expected size of chunk
"""
# We must ensure that 'path' is not a directory
# Writing to a directory is supported, but creates a temporary file
st = self.stat(path)

if st and stat.S_ISDIR(st['mode']):
log.error("Cannot write to %r: Path is a directory" % path)

return self._write(path, data, mode=0o755, timestamp=None, callback=None)

@_with_transport
@_sync
def write(self, path, data, mode=0o755, timestamp=None, callback=None):
def _write(self, path, data, mode=0o755, timestamp=None, callback=None):
path += ',' + str(mode)
self.c.flat('SEND', len(path), path)

Expand Down

0 comments on commit 6cbef83

Please sign in to comment.