Optimize analysis by not running strings for analyzers with no checks

This commit is contained in:
RichardG867
2022-01-27 19:52:07 -03:00
parent 2e3361b0e7
commit 6fd9075f0a
2 changed files with 23 additions and 18 deletions

View File

@@ -316,25 +316,26 @@ def analyze_dir(formatter, scan_base, file_analyzers, scan_dir_path, scan_file_n
bonus_analyzer_oroms = analyzer.oroms
continue
# Run strings on the file data if required (only once).
if not strings:
try:
strings = subprocess.run(['strings', '-n8'], input=file_data, stdout=subprocess.PIPE).stdout.decode('ascii', 'ignore').split('\n')
except:
util.log_traceback('running strings on', os.path.join(scan_dir_path, scan_file_name))
continue
# Run strings on the file data if required (only once if requested by analyzer).
if analyzer.can_analyze():
if not strings:
try:
strings = subprocess.run(['strings', '-n8'], input=file_data, stdout=subprocess.PIPE).stdout.decode('ascii', 'ignore').split('\n')
except:
util.log_traceback('running strings on', os.path.join(scan_dir_path, scan_file_name))
continue
# Analyze each string.
try:
for string in strings:
analyzer.analyze_line(string)
except analyzers.AbortAnalysisError:
# Analysis aborted.
pass
except:
# Log an error.
util.log_traceback('analyzing', os.path.join(scan_dir_path, scan_file_name))
continue
# Analyze each string.
try:
for string in strings:
analyzer.analyze_line(string)
except analyzers.AbortAnalysisError:
# Analysis aborted.
pass
except:
# Log an error.
util.log_traceback('analyzing', os.path.join(scan_dir_path, scan_file_name))
continue
# Take this analyzer if it produced a version.
if analyzer.version:

View File

@@ -127,6 +127,10 @@ class Analyzer:
self.debug_print(callback_func.__name__, line)
return callback_result
def can_analyze(self):
"""Returns True if the given file's strings should be analyzed."""
return len(self._check_list) > 0
def can_handle(self, file_data, header_data):
"""Returns True if this analyzer can handle the given file data.
header_data contains data from the :header: flag file, or