Commit ef67b406 by Craig Heffner

Updated CPIO extrctor to generate unique output file names

parent 007012ef
import os import os
import subprocess import subprocess
import binwalk.core.common
import binwalk.core.plugin import binwalk.core.plugin
class CPIOPlugin(binwalk.core.plugin.Plugin): class CPIOPlugin(binwalk.core.plugin.Plugin):
''' '''
...@@ -27,20 +27,21 @@ class CPIOPlugin(binwalk.core.plugin.Plugin): ...@@ -27,20 +27,21 @@ class CPIOPlugin(binwalk.core.plugin.Plugin):
def extractor(self, fname): def extractor(self, fname):
result = None result = None
fname = os.path.abspath(fname) fname = os.path.abspath(fname)
out_dir = os.path.join(os.path.dirname(fname), self.CPIO_OUT_DIR) out_dir_base_name = os.path.join(os.path.dirname(fname), self.CPIO_OUT_DIR)
out_dir = binwalk.core.common.unique_file_name(out_dir_base_name)
try: try:
fpin = open(fname, "rb") fpin = open(fname, "rb")
fperr = open(os.devnull, "rb") fperr = open(os.devnull, "rb")
os.mkdir(out_dir) os.mkdir(out_dir)
except OSError: except OSError:
return return False
try: try:
curdir = os.getcwd() curdir = os.getcwd()
os.chdir(out_dir) os.chdir(out_dir)
except OSError: except OSError:
return return False
try: try:
result = subprocess.call( result = subprocess.call(
...@@ -93,16 +94,20 @@ class CPIOPlugin(binwalk.core.plugin.Plugin): ...@@ -93,16 +94,20 @@ class CPIOPlugin(binwalk.core.plugin.Plugin):
# found. # found.
if result.description.startswith('ASCII cpio archive'): if result.description.startswith('ASCII cpio archive'):
# Validate the reported name length # Parse the reported name length and file size
file_size = self._get_file_size(result.description) file_size = self._get_file_size(result.description)
file_name = self._get_file_name(result.description) file_name = self._get_file_name(result.description)
file_name_length = self._get_file_name_length(result.description) file_name_length = self._get_file_name_length(result.description)
# The +1 is to include the terminating NULL byte # The +1 is to include the terminating NULL byte
if file_name_length != len(file_name)+1: if file_name_length != len(file_name)+1:
# If the reported length of the file name doesn't match the actual
# file name length, treat this as a false positive result.
result.valid = False result.valid = False
return return
# Instruct binwalk to skip the rest of this CPIO entry.
# We don't want/need to scan what's inside it.
result.jump = self.CPIO_HEADER_SIZE + file_size + file_name_length result.jump = self.CPIO_HEADER_SIZE + file_size + file_name_length
self.consecutive_hits += 1 self.consecutive_hits += 1
...@@ -133,3 +138,4 @@ class CPIOPlugin(binwalk.core.plugin.Plugin): ...@@ -133,3 +138,4 @@ class CPIOPlugin(binwalk.core.plugin.Plugin):
# TODO: It would be better to jump to the end of this CPIO # TODO: It would be better to jump to the end of this CPIO
# entry rather than make this assumption... # entry rather than make this assumption...
result.valid = False result.valid = False
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