Skip to content

Commit

Permalink
fixes and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
crmullineravsec committed Apr 16, 2019
1 parent 8b82f4b commit 0c62147
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ warning that these files were not created.

The script accepts a file with UBI or UBIFS data in it, so should work with a NAND
dump. It will search for the first occurance of UBI or UBIFS data and treat it as
a UBIFS. To list files supply the path to list (-P, --Path), e.g. "-P /" to list
a UBIFS. To list files supply the path to list (-P, --path), e.g. "-P /" to list
the filesystems root directory. To copy a file from the filesystem to a local directory
supply the source path (-C, --Copy) and the destination path (-D, --CopyDestination),
supply the source path (-C, --copy) and the destination path (-D, --copy-dest),
e.g. -C /etc/passwd -D . (extract /etc/passwd from the UBIFS image and copy it to
local directory).

Expand Down
30 changes: 11 additions & 19 deletions scripts/ubireader_list_files
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ if __name__=='__main__':
parser.add_argument('-i', '--ignore-block-header-errors', action='store_true', dest='ignore_block_header_errors',
help='Forces unused and error containing blocks to be included and also displayed with log/verbose. (default: False)')

parser.add_argument('-P', '--Path', dest='listpath',
parser.add_argument('-P', '--path', dest='listpath',
help='Path to list.')

parser.add_argument('-C', '--Copy', dest='copyfile',
help='File to Copy')
parser.add_argument('-C', '--copy', dest='copyfile',
help='File to Copy.')

parser.add_argument('-D', '--CopyDestination', dest='copyfiledest',
help='Copy Destination')
parser.add_argument('-D', '--copy-dest', dest='copyfiledest',
help='Copy Destination.')

parser.add_argument('filepath', help='File to extract contents of.')
parser.add_argument('filepath', help='UBI/UBIFS image file.')

if len(sys.argv) == 1:
parser.print_help()
Expand Down Expand Up @@ -133,22 +133,12 @@ if __name__=='__main__':
# Loop through found images in file.
for image in ubi_obj.images:

# Create path for specific image
# In case multiple images in data
img_outpath = os.path.join(outpath, '%s' % image.image_seq)

# Loop through volumes in each image.
for volume in image.volumes:

# Get blocks associated with this volume.
vol_blocks = image.volumes[volume].get_blocks(ubi_obj.blocks)

# Create volume data output path.
vol_outpath = os.path.join(img_outpath, volume)

# Create volume output path directory.
create_output_dir(vol_outpath)

# Skip volume if empty.
if not len(vol_blocks):
continue
Expand All @@ -158,11 +148,13 @@ if __name__=='__main__':
# into memory.
lebv_file = leb_virtual_file(ubi_obj, vol_blocks)

# Extract files from UBI image.
# Create UBIFS object.
ubifs_obj = ubifs(lebv_file)
#print('Extracting files to: %s' % vol_outpath)
list_files(ubifs_obj, args.listpath)

if args.listpath:
list_files(ubifs_obj, args.listpath)
if args.copyfile and args.copyfiledest:
copy_file(ubifs_obj, args.copyfile, args.copyfiledest)

elif filetype == UBIFS_NODE_MAGIC:
# Create UBIFS object
Expand Down
8 changes: 6 additions & 2 deletions ubireader/ubifs/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def list_files(ubifs, list_path):
except Exception as e:
error(list_files, 'Error', '%s' % e)


def copy_file(ubifs, filepath, destpath):
pathnames = filepath.split("/")
pnames = []
Expand Down Expand Up @@ -93,10 +94,10 @@ def copy_file(ubifs, filepath, destpath):
destpath = os.path.join(destpath, filename)
with open(destpath, 'wb') as f:
f.write(filedata)
#os.chmod(destpath, inodes[dent.inum]['ino'].mode)
return True
return False


def find_dir(inodes, inum, names, idx):
if len(names) == 0:
return 1
Expand All @@ -113,6 +114,7 @@ def print_dent(ubifs, inodes, dent_node, long=True, longts=False):
inode = inodes[dent_node.inum]
if long:
fl = file_leng(ubifs, inode)

lnk = ""
if dent_node.type == UBIFS_ITYPE_LNK:
lnk = " -> " + inode['ino'].data.decode('utf-8')
Expand All @@ -122,10 +124,11 @@ def print_dent(ubifs, inodes, dent_node, long=True, longts=False):
else:
mtime = time.strftime("%b %d %H:%M", time.gmtime(inode['ino'].mtime_sec))

print('%6o %s %s %7d %s %s%s' % (inode['ino'].mode, inode['ino'].uid, inode['ino'].gid, fl, mtime, dent_node.name, lnk))
print('%6o %2d %s %s %7d %s %s%s' % (inode['ino'].mode, inode['ino'].nlink, inode['ino'].uid, inode['ino'].gid, fl, mtime, dent_node.name, lnk))
else:
print(dent_node.name)


def file_leng(ubifs, inode):
fl = 0
if 'data' in inode:
Expand All @@ -142,6 +145,7 @@ def file_leng(ubifs, inode):
return fl
return 0


def _process_reg_file(ubifs, inode, path):
try:
buf = b''
Expand Down

0 comments on commit 0c62147

Please sign in to comment.