Last active
October 31, 2024 17:10
-
-
Save Beercow/5e13282541f79580405f96364baca8d5 to your computer and use it in GitHub Desktop.
Python script to dump all fields in KAPE targets and modules, including documentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import yaml | |
import argparse | |
import os | |
import sys | |
filenames = [] | |
def main(): | |
if args.target: | |
fieldnames = ['Disabled', 'Local', 'TargetPath', 'Target', 'Description', 'Author', | |
'Version', 'Id', 'RecreateDirectories', 'Name', | |
'Category', 'Path', 'Recursive', 'FileMask', | |
'SaveAsFileName', 'AlwaysAddToQueue', 'Comment', | |
'MinSize', 'MaxSize', 'Documentation' | |
] | |
output = 'Targets.csv' | |
else: | |
fieldnames = ['Disabled', 'Local', 'ModulePath', 'Module', 'Description', 'Category', | |
'Author', 'Version', 'Id', 'BinaryUrl', 'ExportFormat', | |
'WaitTimeout', 'FileMask', 'Executable', 'CommandLine', | |
'ExportFormat', 'ExportFile', 'Append', 'Documentation' | |
] | |
output = 'Modules.csv' | |
with open(output, 'w', newline='', encoding='UTF-8') as f_output: | |
csv_output = csv.DictWriter(f_output, fieldnames=fieldnames) | |
csv_output.writeheader() | |
for filename in filenames: | |
print(f'Parsing {filename}') | |
with open(filename, encoding='UTF-8') as f_input: | |
try: | |
data = yaml.safe_load(f_input) | |
except Exception as e: | |
print(f'Unable to parse {f_input}: {e}') | |
continue | |
doc = False | |
f_input.seek(0) | |
Documentation = [] | |
while True: | |
line = f_input.readline() | |
if not line: | |
break | |
if doc: | |
Documentation.append(line) | |
if '# Documentation' in line: | |
doc = True | |
if args.target: | |
Disabled = 'False' | |
Local = 'False' | |
TargetPath = os.path.dirname(f_input.name) | |
Target = os.path.basename(f_input.name).split('.')[0] | |
if '!Disabled' in f_input.name: | |
Disabled = 'True' | |
if '!Local' in f_input.name: | |
Local = 'True' | |
Description = data['Description'] | |
Author = data['Author'] | |
Version = data['Version'] | |
Id = data['Id'] | |
RecreateDirectories = data['RecreateDirectories'] | |
for dic in data['Targets']: | |
row = {'Disabled': Disabled, 'Local': Local, | |
'TargetPath': TargetPath, 'Target': Target, | |
'Description': Description, 'Author': Author, | |
'Version': Version, 'Id': Id, | |
'RecreateDirectories': RecreateDirectories | |
} | |
for get in ['Name', 'Category', 'Path', 'Recursive', | |
'FileMask', 'SaveAsFileName', | |
'AlwaysAddToQueue', 'Comment', 'MinSize', | |
'MaxSize' | |
]: | |
try: | |
row[get] = dic[get] | |
except KeyError: | |
pass | |
row['Documentation'] = ''.join(Documentation).replace('# ', '') | |
csv_output.writerow(row) | |
else: | |
Disabled = 'False' | |
Local = 'False' | |
ModulePath = os.path.dirname(f_input.name) | |
Module = os.path.basename(f_input.name).split('.')[0] | |
if '!Disabled' in f_input.name: | |
Disabled = 'True' | |
if '!Local' in f_input.name: | |
Local = 'True' | |
Description = data['Description'] | |
Category = data['Category'] | |
Author = data['Author'] | |
Version = data['Version'] | |
Id = data['Id'] | |
try: | |
BinaryUrl = data['BinaryUrl'] | |
except KeyError: | |
BinaryUrl = '' | |
ExportFormat = data['ExportFormat'] | |
try: | |
WaitTimeout = data['WaitTimeout'] | |
except KeyError: | |
WaitTimeout = '' | |
try: | |
FileMask = data['FileMask'] | |
except KeyError: | |
FileMask = '' | |
for dic in data['Processors']: | |
row = {'Disabled': Disabled, 'Local': Local, | |
'ModulePath': ModulePath, 'Module': Module, | |
'Description': Description, 'Category': Category, | |
'Author': Author, 'Version': Version, 'Id': Id, | |
'BinaryUrl': BinaryUrl, 'ExportFormat': ExportFormat, | |
'WaitTimeout': WaitTimeout, 'FileMask': FileMask | |
} | |
for get in ['Executable', 'CommandLine', 'ExportFormat', | |
'ExportFile', 'Append' | |
]: | |
try: | |
row[get] = dic[get] | |
except KeyError: | |
pass | |
row['Documentation'] = ''.join(Documentation).replace('# ', '') | |
csv_output.writerow(row) | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-d", "--dir", help="Directory to be parsed") | |
parser.add_argument("--target", help="Directory to be parsed", action='store_true') | |
parser.add_argument("--module", help="Directory to be parsed", action='store_true') | |
if len(sys.argv) == 1: | |
parser.print_help() | |
parser.exit() | |
args = parser.parse_args() | |
for path, subdirs, files in os.walk(args.dir): | |
if 'bin' not in path: | |
for name in files: | |
filenames.append(os.path.join(path, name)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires PyYAML
pip install PyYAML