findSymbols.py: fixed errors, added extension filtering, write to output file.

This commit is contained in:
Mariusz B. / mgeeky 2021-10-23 14:08:13 +02:00
parent eda974768d
commit 4e691a0bcc
1 changed files with 20 additions and 7 deletions

View File

@ -224,7 +224,7 @@ def processFile(args, regexes, path, results, uniqueSymbols, filesProcessed, sym
appendRow = verifyCriterias(args, regexes, infos, uniqueSymbols) appendRow = verifyCriterias(args, regexes, infos, uniqueSymbols)
if appendRow: if appendRow:
results.append(row) results.append(infos)
uniqueSymbols.append(symbolName) uniqueSymbols.append(symbolName)
#verbose(args, 'Processed results:\n' + pprint.pformat(infos)) #verbose(args, 'Processed results:\n' + pprint.pformat(infos))
@ -247,11 +247,13 @@ def processDir(args, regexes, path, results, uniqueSymbols, filesProcessed, symb
for file in glob.glob(os.path.join(path, '**'), recursive=args.recurse): for file in glob.glob(os.path.join(path, '**'), recursive=args.recurse):
try: try:
if len(args.extension) > 0: if len(args.extension) > 0:
skip = False
for ext in args.extension:
if not file.lower().endswith(f'.{ext}'):
skip = True skip = True
for ext in args.extension:
if file.lower().endswith(f'.{ext}'):
skip = False
break
if skip: if skip:
verbose(args, f'[-] Skipping file as it not matched extension ({ext}): {file}')
continue continue
if os.path.isfile(file): if os.path.isfile(file):
@ -297,6 +299,7 @@ def opts(argv):
params.add_argument('-v', '--verbose', action='store_true', help='Verbose mode.') params.add_argument('-v', '--verbose', action='store_true', help='Verbose mode.')
params.add_argument('-f', '--format', choices=['text', 'json'], default='text', help='Output format. Text or JSON.') params.add_argument('-f', '--format', choices=['text', 'json'], default='text', help='Output format. Text or JSON.')
params.add_argument('-E', '--extension', default=[], action='append', help='Extensions of files to scan. By default will scan all files. Can be repeated: -E exe -E dll') params.add_argument('-E', '--extension', default=[], action='append', help='Extensions of files to scan. By default will scan all files. Can be repeated: -E exe -E dll')
params.add_argument('-o', '--output', metavar='PATH', help='Write output to file.')
sorting = params.add_argument_group('Output sorting') sorting = params.add_argument_group('Output sorting')
sorting.add_argument('-u', '--unique', action='store_true', help = 'Return unique symbols only. The first symbol with a name that occurs in results, will be returned.') sorting.add_argument('-u', '--unique', action='store_true', help = 'Return unique symbols only. The first symbol with a name that occurs in results, will be returned.')
@ -341,7 +344,8 @@ def opts(argv):
for i in range(len(args.extension)): for i in range(len(args.extension)):
args.extension[i] = args.extension[i].lower() args.extension[i] = args.extension[i].lower()
if args.extension[i].startswith('.'): args.extension[i] = args.extension[i][1:] if args.extension[i].startswith('.'):
args.extension[i] = args.extension[i][1:]
return args, regexes return args, regexes
@ -385,8 +389,13 @@ def main():
if args.format == 'json': if args.format == 'json':
resultsList = list(results) resultsList = list(results)
print(json.dumps(resultsList, indent=4)) dumped = str(json.dumps(resultsList, indent=4))
if args.output:
with open(args.output, 'w') as f:
f.write(dumped)
else:
print(dumped)
else: else:
resultsList = list(results) resultsList = list(results)
if len(resultsList) > 0: if len(resultsList) > 0:
@ -402,6 +411,10 @@ def main():
table = tabulate.tabulate(resultsList, headers=['#',] + headers, showindex='always', tablefmt='pretty') table = tabulate.tabulate(resultsList, headers=['#',] + headers, showindex='always', tablefmt='pretty')
if args.output:
with open(args.output, 'w', encoding='utf-8') as f:
f.write(str(table))
else:
print(table) print(table)
if args.first > 0: if args.first > 0: