Commit 1646ef51 by devttys0

Fall back to system libmagic/libfuzzy libraries if custom ones are not found.

parent 57bdf346
...@@ -36,9 +36,9 @@ uninstall: ...@@ -36,9 +36,9 @@ uninstall:
file_make_prep: file_make_prep:
if [ "$(BUILD_MAGIC)" -eq "1" ]; then sed -e s/libmagic/libinmagic/g < ./file-$(FILE_VERSION)/src/Makefile >./file-$(FILE_VERSION)/src/mktmp; fi if [ "$(BUILD_MAGIC)" -eq "1" ]; then sed -e s/libmagic/libinmagic/g < ./file-$(FILE_VERSION)/src/Makefile >./file-$(FILE_VERSION)/src/mktmp; fi
if [ "$(BUILD_MAGIC)" -eq "1" ]; then mv ./file-$(FILE_VERSION)/src/mktmp ./file-$(FILE_VERSION)/src/Makefile; fi if [ "$(BUILD_MAGIC)" -eq "1" ]; then mv -f ./file-$(FILE_VERSION)/src/mktmp ./file-$(FILE_VERSION)/src/Makefile; fi
ssdeep_make_prep: ssdeep_make_prep:
if [ "$(BUILD_FUZZY)" -eq "1" ]; then sed -e s/libfuzzy/libinfuzzy/g < ./ssdeep-$(SSDEEP_VERSION)/Makefile >./ssdeep-$(SSDEEP_VERSION)/mktmp; fi if [ "$(BUILD_FUZZY)" -eq "1" ]; then sed -e s/libfuzzy/libinfuzzy/g < ./ssdeep-$(SSDEEP_VERSION)/Makefile >./ssdeep-$(SSDEEP_VERSION)/mktmp; fi
if [ "$(BUILD_FUZZY)" -eq "1" ]; then mv ./ssdeep-$(SSDEEP_VERSION)/mktmp ./ssdeep-$(SSDEEP_VERSION)/Makefile; fi if [ "$(BUILD_FUZZY)" -eq "1" ]; then mv -f ./ssdeep-$(SSDEEP_VERSION)/mktmp ./ssdeep-$(SSDEEP_VERSION)/Makefile; fi
...@@ -91,7 +91,7 @@ class Library(object): ...@@ -91,7 +91,7 @@ class Library(object):
''' '''
Class constructor. Class constructor.
@library - Library name (e.g., 'magic' for libmagic). @library - Library name (e.g., 'magic' for libmagic), or a list of names.
@functions - A dictionary of function names and their return types (e.g., {'magic_buffer' : str}) @functions - A dictionary of function names and their return types (e.g., {'magic_buffer' : str})
Returns None. Returns None.
...@@ -104,15 +104,20 @@ class Library(object): ...@@ -104,15 +104,20 @@ class Library(object):
f = FunctionHandler(self.library, function) f = FunctionHandler(self.library, function)
setattr(self, function.name, f.run) setattr(self, function.name, f.run)
def find_library(self, library): def find_library(self, libraries):
''' '''
Locates the specified library. Locates the specified library.
@library - Library name (e.g., 'magic' for libmagic). @libraries - Library name (e.g., 'magic' for libmagic), or a list of names.
Returns a string to be passed to ctypes.cdll.LoadLibrary. Returns a string to be passed to ctypes.cdll.LoadLibrary.
''' '''
lib_path = None lib_path = None
if isinstance(libraries, str):
libraries = [libraries]
for library in libraries:
system_paths = { system_paths = {
'linux' : ['/usr/local/lib/lib%s.so' % library], 'linux' : ['/usr/local/lib/lib%s.so' % library],
'linux2' : ['/usr/local/lib/lib%s.so' % library], 'linux2' : ['/usr/local/lib/lib%s.so' % library],
...@@ -137,10 +142,16 @@ class Library(object): ...@@ -137,10 +142,16 @@ class Library(object):
if not lib_path: if not lib_path:
lib_path = ctypes.util.find_library(library) lib_path = ctypes.util.find_library(library)
# Use the first library that we can find
if lib_path:
binwalk.core.common.debug("Found library '%s' at: %s" % (library, lib_path))
break
else:
binwalk.core.common.debug("Could not find library '%s'" % library)
# If we still couldn't find the library, error out # If we still couldn't find the library, error out
if not lib_path: if not lib_path:
raise Exception("Failed to locate library '%s'" % library) raise Exception("Failed to locate libraries '%s'" % str(libraries))
binwalk.core.common.debug("Found library: " + lib_path)
return lib_path return lib_path
...@@ -32,13 +32,16 @@ class Magic(object): ...@@ -32,13 +32,16 @@ class Magic(object):
MAGIC_FLAGS = MAGIC_NO_CHECK_TEXT | MAGIC_NO_CHECK_ENCODING | MAGIC_NO_CHECK_APPTYPE | MAGIC_NO_CHECK_TOKENS MAGIC_FLAGS = MAGIC_NO_CHECK_TEXT | MAGIC_NO_CHECK_ENCODING | MAGIC_NO_CHECK_APPTYPE | MAGIC_NO_CHECK_TOKENS
# Look for libinmagic first, fall back on libmagic
LIBRARIES = ["inmagic", "magic"]
def __init__(self, magic_file=None, flags=0): def __init__(self, magic_file=None, flags=0):
if magic_file: if magic_file:
self.magic_file = str2bytes(magic_file) self.magic_file = str2bytes(magic_file)
else: else:
self.magic_file = None self.magic_file = None
self.libmagic = binwalk.core.C.Library("inmagic", self.LIBMAGIC_FUNCTIONS) self.libmagic = binwalk.core.C.Library(self.LIBRARIES, self.LIBMAGIC_FUNCTIONS)
binwalk.core.common.debug("libmagic.magic_open(0x%X)" % (self.MAGIC_FLAGS | flags)) binwalk.core.common.debug("libmagic.magic_open(0x%X)" % (self.MAGIC_FLAGS | flags))
self.magic_cookie = self.libmagic.magic_open(self.MAGIC_FLAGS | flags) self.magic_cookie = self.libmagic.magic_open(self.MAGIC_FLAGS | flags)
......
...@@ -78,8 +78,8 @@ class HashMatch(Module): ...@@ -78,8 +78,8 @@ class HashMatch(Module):
Kwarg(name='enabled', default=False), Kwarg(name='enabled', default=False),
] ]
# Requires libfuzzybinwalk.so # Look for libinfuzzy first, fall back on libfuzzy
LIBRARY_NAME = "infuzzy" LIBRARY_NAMES = ["infuzzy", "fuzzy"]
LIBRARY_FUNCTIONS = [ LIBRARY_FUNCTIONS = [
binwalk.core.C.Function(name="fuzzy_hash_buf", type=int), binwalk.core.C.Function(name="fuzzy_hash_buf", type=int),
binwalk.core.C.Function(name="fuzzy_hash_filename", type=int), binwalk.core.C.Function(name="fuzzy_hash_filename", type=int),
...@@ -101,7 +101,7 @@ class HashMatch(Module): ...@@ -101,7 +101,7 @@ class HashMatch(Module):
self.last_file1 = HashResult(None) self.last_file1 = HashResult(None)
self.last_file2 = HashResult(None) self.last_file2 = HashResult(None)
self.lib = binwalk.core.C.Library(self.LIBRARY_NAME, self.LIBRARY_FUNCTIONS) self.lib = binwalk.core.C.Library(self.LIBRARY_NAMES, self.LIBRARY_FUNCTIONS)
def _get_strings(self, fname): def _get_strings(self, fname):
return ''.join(list(binwalk.core.common.strings(fname, minimum=10))) return ''.join(list(binwalk.core.common.strings(fname, minimum=10)))
......
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