Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 259b345

Browse files
committed
catch ImportError exception if libmagic is not installed
1 parent 128597e commit 259b345

2 files changed

Lines changed: 95 additions & 98 deletions

File tree

lib/techniques/union/use.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,6 @@ def unionUse(expression, unpack=True, dump=False):
159159

160160
_, _, _, _, _, expressionFieldsList, expressionFields, _ = agent.getFields(origExpr)
161161

162-
if expressionFieldsList and len(expressionFieldsList) > 1 and " ORDER BY " in expression.upper():
163-
# No need for it in multicolumn dumps (one row is retrieved per request) and just slowing down on large table dumps
164-
expression = expression[:expression.upper().rindex(" ORDER BY ")]
165-
166162
# We have to check if the SQL query might return multiple entries
167163
# if the technique is partial UNION query and in such case forge the
168164
# SQL limiting the query output one entry at a time
@@ -305,7 +301,7 @@ def unionThread():
305301
kb.suppressResumeInfo = False
306302

307303
if not value and not abortedFlag:
308-
expression = re.sub("\s*ORDER BY\s+[\w,]+", "", expression, re.I) # full union doesn't play well with ORDER BY
304+
expression = re.sub("\s*ORDER BY\s+[\w,]+", "", expression, re.I) # full union does not play well with ORDER BY
309305
value = _oneShotUnionUse(expression, unpack)
310306

311307
duration = calculateDeltaSeconds(start)

thirdparty/magic/magic.py

Lines changed: 94 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -106,99 +106,100 @@ def from_buffer(buffer, mime=False):
106106

107107

108108

109-
110-
libmagic = None
111-
# Let's try to find magic or magic1
112-
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')
113-
114-
# This is necessary because find_library returns None if it doesn't find the library
115-
if dll:
116-
libmagic = ctypes.CDLL(dll)
117-
118-
if not libmagic or not libmagic._name:
119-
import sys
120-
platform_to_lib = {'darwin': ['/opt/local/lib/libmagic.dylib',
121-
'/usr/local/lib/libmagic.dylib',
122-
'/usr/local/Cellar/libmagic/5.10/lib/libmagic.dylib'],
123-
'win32': ['magic1.dll']}
124-
for dll in platform_to_lib.get(sys.platform, []):
125-
try:
126-
libmagic = ctypes.CDLL(dll)
127-
except OSError:
128-
pass
129-
130-
if not libmagic or not libmagic._name:
131-
# It is better to raise an ImportError since we are importing magic module
132-
raise ImportError('failed to find libmagic. Check your installation')
133-
134-
magic_t = ctypes.c_void_p
135-
136-
def errorcheck(result, func, args):
137-
err = magic_error(args[0])
138-
if err is not None:
139-
raise MagicException(err)
140-
else:
141-
return result
142-
143-
def coerce_filename(filename):
144-
if filename is None:
145-
return None
146-
return filename.encode(sys.getfilesystemencoding())
147-
148-
magic_open = libmagic.magic_open
149-
magic_open.restype = magic_t
150-
magic_open.argtypes = [c_int]
151-
152-
magic_close = libmagic.magic_close
153-
magic_close.restype = None
154-
magic_close.argtypes = [magic_t]
155-
156-
magic_error = libmagic.magic_error
157-
magic_error.restype = c_char_p
158-
magic_error.argtypes = [magic_t]
159-
160-
magic_errno = libmagic.magic_errno
161-
magic_errno.restype = c_int
162-
magic_errno.argtypes = [magic_t]
163-
164-
_magic_file = libmagic.magic_file
165-
_magic_file.restype = c_char_p
166-
_magic_file.argtypes = [magic_t, c_char_p]
167-
_magic_file.errcheck = errorcheck
168-
169-
def magic_file(cookie, filename):
170-
return _magic_file(cookie, coerce_filename(filename))
171-
172-
_magic_buffer = libmagic.magic_buffer
173-
_magic_buffer.restype = c_char_p
174-
_magic_buffer.argtypes = [magic_t, c_void_p, c_size_t]
175-
_magic_buffer.errcheck = errorcheck
176-
177-
178-
def magic_buffer(cookie, buf):
179-
return _magic_buffer(cookie, buf, len(buf))
180-
181-
182-
_magic_load = libmagic.magic_load
183-
_magic_load.restype = c_int
184-
_magic_load.argtypes = [magic_t, c_char_p]
185-
_magic_load.errcheck = errorcheck
186-
187-
def magic_load(cookie, filename):
188-
return _magic_load(cookie, coerce_filename(filename))
189-
190-
magic_setflags = libmagic.magic_setflags
191-
magic_setflags.restype = c_int
192-
magic_setflags.argtypes = [magic_t, c_int]
193-
194-
magic_check = libmagic.magic_check
195-
magic_check.restype = c_int
196-
magic_check.argtypes = [magic_t, c_char_p]
197-
198-
magic_compile = libmagic.magic_compile
199-
magic_compile.restype = c_int
200-
magic_compile.argtypes = [magic_t, c_char_p]
201-
109+
try:
110+
libmagic = None
111+
# Let's try to find magic or magic1
112+
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')
113+
114+
# This is necessary because find_library returns None if it doesn't find the library
115+
if dll:
116+
libmagic = ctypes.CDLL(dll)
117+
118+
if not libmagic or not libmagic._name:
119+
import sys
120+
platform_to_lib = {'darwin': ['/opt/local/lib/libmagic.dylib',
121+
'/usr/local/lib/libmagic.dylib',
122+
'/usr/local/Cellar/libmagic/5.10/lib/libmagic.dylib'],
123+
'win32': ['magic1.dll']}
124+
for dll in platform_to_lib.get(sys.platform, []):
125+
try:
126+
libmagic = ctypes.CDLL(dll)
127+
except OSError:
128+
pass
129+
130+
if not libmagic or not libmagic._name:
131+
# It is better to raise an ImportError since we are importing magic module
132+
raise ImportError('failed to find libmagic. Check your installation')
133+
134+
magic_t = ctypes.c_void_p
135+
136+
def errorcheck(result, func, args):
137+
err = magic_error(args[0])
138+
if err is not None:
139+
raise MagicException(err)
140+
else:
141+
return result
142+
143+
def coerce_filename(filename):
144+
if filename is None:
145+
return None
146+
return filename.encode(sys.getfilesystemencoding())
147+
148+
magic_open = libmagic.magic_open
149+
magic_open.restype = magic_t
150+
magic_open.argtypes = [c_int]
151+
152+
magic_close = libmagic.magic_close
153+
magic_close.restype = None
154+
magic_close.argtypes = [magic_t]
155+
156+
magic_error = libmagic.magic_error
157+
magic_error.restype = c_char_p
158+
magic_error.argtypes = [magic_t]
159+
160+
magic_errno = libmagic.magic_errno
161+
magic_errno.restype = c_int
162+
magic_errno.argtypes = [magic_t]
163+
164+
_magic_file = libmagic.magic_file
165+
_magic_file.restype = c_char_p
166+
_magic_file.argtypes = [magic_t, c_char_p]
167+
_magic_file.errcheck = errorcheck
168+
169+
def magic_file(cookie, filename):
170+
return _magic_file(cookie, coerce_filename(filename))
171+
172+
_magic_buffer = libmagic.magic_buffer
173+
_magic_buffer.restype = c_char_p
174+
_magic_buffer.argtypes = [magic_t, c_void_p, c_size_t]
175+
_magic_buffer.errcheck = errorcheck
176+
177+
178+
def magic_buffer(cookie, buf):
179+
return _magic_buffer(cookie, buf, len(buf))
180+
181+
182+
_magic_load = libmagic.magic_load
183+
_magic_load.restype = c_int
184+
_magic_load.argtypes = [magic_t, c_char_p]
185+
_magic_load.errcheck = errorcheck
186+
187+
def magic_load(cookie, filename):
188+
return _magic_load(cookie, coerce_filename(filename))
189+
190+
magic_setflags = libmagic.magic_setflags
191+
magic_setflags.restype = c_int
192+
magic_setflags.argtypes = [magic_t, c_int]
193+
194+
magic_check = libmagic.magic_check
195+
magic_check.restype = c_int
196+
magic_check.argtypes = [magic_t, c_char_p]
197+
198+
magic_compile = libmagic.magic_compile
199+
magic_compile.restype = c_int
200+
magic_compile.argtypes = [magic_t, c_char_p]
201+
except ImportError:
202+
pass
202203

203204

204205
MAGIC_NONE = 0x000000 # No flags

0 commit comments

Comments
 (0)