Commit 1646ef51 by devttys0

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

parent 57bdf346
......@@ -36,9 +36,9 @@ uninstall:
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 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:
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):
'''
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})
Returns None.
......@@ -104,43 +104,54 @@ class Library(object):
f = FunctionHandler(self.library, function)
setattr(self, function.name, f.run)
def find_library(self, library):
def find_library(self, libraries):
'''
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.
'''
lib_path = None
system_paths = {
'linux' : ['/usr/local/lib/lib%s.so' % library],
'linux2' : ['/usr/local/lib/lib%s.so' % library],
'linux3' : ['/usr/local/lib/lib%s.so' % library],
'darwin' : ['/opt/local/lib/lib%s.dylib' % library,
'/usr/local/lib/lib%s.dylib' % library,
] + glob.glob('/usr/local/Cellar/lib%s/*/lib/lib%s.dylib' % (library, library)),
'cygwin' : ['/usr/local/lib/lib%s.so' % library],
'win32' : ['%s.dll' % library]
}
# Search the common install directories first; these are usually not in the library search path
# Search these *first*, since a) they are the most likely locations and b) there may be a
# discrepency between where ctypes.util.find_library and ctypes.cdll.LoadLibrary search for libs.
for path in system_paths[sys.platform]:
if os.path.exists(path):
lib_path = path
break
# If we failed to find the library, check the standard library search paths
if not lib_path:
lib_path = ctypes.util.find_library(library)
if isinstance(libraries, str):
libraries = [libraries]
for library in libraries:
system_paths = {
'linux' : ['/usr/local/lib/lib%s.so' % library],
'linux2' : ['/usr/local/lib/lib%s.so' % library],
'linux3' : ['/usr/local/lib/lib%s.so' % library],
'darwin' : ['/opt/local/lib/lib%s.dylib' % library,
'/usr/local/lib/lib%s.dylib' % library,
] + glob.glob('/usr/local/Cellar/lib%s/*/lib/lib%s.dylib' % (library, library)),
'cygwin' : ['/usr/local/lib/lib%s.so' % library],
'win32' : ['%s.dll' % library]
}
# Search the common install directories first; these are usually not in the library search path
# Search these *first*, since a) they are the most likely locations and b) there may be a
# discrepency between where ctypes.util.find_library and ctypes.cdll.LoadLibrary search for libs.
for path in system_paths[sys.platform]:
if os.path.exists(path):
lib_path = path
break
# If we failed to find the library, check the standard library search paths
if not lib_path:
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 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
......@@ -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
# Look for libinmagic first, fall back on libmagic
LIBRARIES = ["inmagic", "magic"]
def __init__(self, magic_file=None, flags=0):
if magic_file:
self.magic_file = str2bytes(magic_file)
else:
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))
self.magic_cookie = self.libmagic.magic_open(self.MAGIC_FLAGS | flags)
......
......@@ -78,8 +78,8 @@ class HashMatch(Module):
Kwarg(name='enabled', default=False),
]
# Requires libfuzzybinwalk.so
LIBRARY_NAME = "infuzzy"
# Look for libinfuzzy first, fall back on libfuzzy
LIBRARY_NAMES = ["infuzzy", "fuzzy"]
LIBRARY_FUNCTIONS = [
binwalk.core.C.Function(name="fuzzy_hash_buf", type=int),
binwalk.core.C.Function(name="fuzzy_hash_filename", type=int),
......@@ -101,7 +101,7 @@ class HashMatch(Module):
self.last_file1 = 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):
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