Commit 58d1d92e by Craig Heffner

Updated unpfs extractor to create unique output directories in the event of…

Updated unpfs extractor to create unique output directories in the event of multiple PFS images inside the same file; added directory traversal check.
parent f87bcc64
import os import os
import errno import errno
import struct import struct
import binwalk.core.plugin import binwalk.core.common
import binwalk.core.compat import binwalk.core.compat
from binwalk.core.common import BlockFile as open import binwalk.core.plugin
class PFSCommon(object): class PFSCommon(object):
...@@ -23,7 +23,7 @@ class PFS(PFSCommon): ...@@ -23,7 +23,7 @@ class PFS(PFSCommon):
def __init__(self, fname, endianess='<'): def __init__(self, fname, endianess='<'):
self.endianess = endianess self.endianess = endianess
self.meta = open(fname, 'rb') self.meta = binwalk.core.common.BlockFile(fname, 'rb')
header = self.meta.read(self.HEADER_SIZE) header = self.meta.read(self.HEADER_SIZE)
self.file_list_start = self.meta.tell() self.file_list_start = self.meta.tell()
...@@ -96,14 +96,20 @@ class PFSExtractor(binwalk.core.plugin.Plugin): ...@@ -96,14 +96,20 @@ class PFSExtractor(binwalk.core.plugin.Plugin):
def extractor(self, fname): def extractor(self, fname):
fname = os.path.abspath(fname) fname = os.path.abspath(fname)
out_dir = binwalk.core.common.unique_file_name(os.path.join(os.path.dirname(fname), "pfs-root"))
try: try:
with PFS(fname) as fs: with PFS(fname) as fs:
# The end of PFS meta data is the start of the actual data # The end of PFS meta data is the start of the actual data
data = open(fname, 'rb') data = binwalk.core.common.BlockFile(fname, 'rb')
data.seek(fs.get_end_of_meta_data()) data.seek(fs.get_end_of_meta_data())
for entry in fs.entries(): for entry in fs.entries():
self._create_dir_from_fname(entry.fname) outfile_path = os.path.join(out_dir, entry.fname)
outfile = open(entry.fname, 'wb') if not outfile_path.startswith(out_dir):
binwalk.core.common.warning("Unpfs extractor detected directory traversal attempt for file: '%s'. Refusing to extract." % outfile_path)
else:
self._create_dir_from_fname(outfile_path)
outfile = binwalk.core.common.BlockFile(outfile_path, 'wb')
outfile.write(data.read(entry.fsize)) outfile.write(data.read(entry.fsize))
outfile.close() outfile.close()
data.close() data.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment