Я получаю данные типа msgpack от API, и когда я использую msgpack.unpackb
в ответе, я получаю dict, который содержит поддикты или подмассивы, в которых некоторые ключи и значения имеют байты типа, int и str, дикт, список. Я хочу преобразовать их все в str, но с метаинформацией, чтобы я мог преобразовать их обратно в то, что они были точно после того, как я отправил их через веб-сокет клиенту.
Вот пример данных:
{b'type': b'exploit', b'name': b'ibstat $PATH Privilege Escalation', b'fullname': b'exploit/aix/local/ibstat_path', b'rank': b'excellent', b'disclosuredate': b'2013-09-24', b'description': b'This module exploits the trusted $PATH environment variable of the SUID binary "ibstat".', b'license': b'Metasploit Framework License (BSD)', b'filepath': '/opt/metasploit-framework/embedded/framework/modules/exploits/aix/local/ibstat_path.rb', b'arch': [b'cmd'], b'platform': ['Msf::Module::Platform::Unix'], b'authors': [b'Kristian Erik Hermansen', b'Sagi Shahar <sagi.shahar@mwrinfosecurity.com>', b'Kostas Lintovois <kostas.lintovois@mwrinfosecurity.com>'], b'privileged': False, b'references': [[b'CVE', b'2013-4011'], [b'OSVDB', b'95420'], [b'BID', b'61287'], [b'URL', b'http://www-01.ibm.com/support/docview.wss?uid=isg1IV43827'], [b'URL', b'http://www-01.ibm.com/support/docview.wss?uid=isg1IV43756']], b'targets': {0: b'IBM AIX Version 6.1', 1: b'IBM AIX Version 7.1'}, b'default_target': 1, b'stance': b'aggressive', b'options': {b'WORKSPACE': {b'type': b'string', b'required': False, b'advanced': True, b'desc': b'Specify the workspace for this module'}, b'VERBOSE': {b'type': b'bool', b'required': False, b'advanced': True, b'desc': b'Enable detailed status messages', b'default': False}, b'WfsDelay': {b'type': b'integer', b'required': False, b'advanced': True, b'desc': b'Additional delay when waiting for a session', b'default': 0}, b'EnableContextEncoding': {b'type': b'bool', b'required': False, b'advanced': True, b'desc': b'Use transient context when encoding payloads', b'default': False}, b'ContextInformationFile': {b'type': b'path', b'required': False, b'advanced': True, b'desc': b'The information file that contains context information'}, b'DisablePayloadHandler': {b'type': b'bool', b'required': False, b'advanced': True, b'desc': b'Disable the handler code for the selected payload', b'default': False}, b'SESSION': {b'type': b'integer', b'required': True, b'advanced': False, b'desc': b'The session to run this module on.'}, b'FileDropperDelay': {b'type': b'integer', b'required': False, b'advanced': True, b'desc': b'Delay in seconds before attempting cleanup'}, b'WritableDir': {b'type': b'string', b'required': True, b'advanced': False, b'desc': b'A directory where we can write files', b'default': b'/tmp'}}}
Ближайший возможный результат, которого я достиг, это то, что я могу преобразовать его в то, что я хочу, если только нет подмассивов или поддиктов. Вот вывод, который я достиг:
[[{'type': 'bytes', 'value': 'type'}, {'type': 'bytes', 'value': 'exploit'}], [{'type': 'bytes', 'value': 'name'}, {'type': 'bytes', 'value': 'ibstat $PATH Privilege Escalation'}], [{'type': 'bytes', 'value': 'fullname'}, {'type': 'bytes', 'value': 'exploit/aix/local/ibstat_path'}], [{'type': 'bytes', 'value': 'rank'}, {'type': 'bytes', 'value': 'excellent'}], [{'type': 'bytes', 'value': 'disclosuredate'}, {'type': 'bytes', 'value': '2013-09-24'}], [{'type': 'bytes', 'value': 'description'}, {'type': 'bytes', 'value': 'This module exploits the trusted $PATH environment variable of the SUID binary "ibstat".'}], [{'type': 'bytes', 'value': 'license'}, {'type': 'bytes', 'value': 'Metasploit Framework License (BSD)'}], [{'type': 'bytes', 'value': 'filepath'}, {'type': 'str', 'value': '/opt/metasploit-framework/embedded/framework/modules/exploits/aix/local/ibstat_path.rb'}], [{'type': 'bytes', 'value': 'arch'}, {'type': 'list', 'value': [b'cmd']}], [{'type': 'bytes', 'value': 'platform'}, {'type': 'list', 'value': ['Msf::Module::Platform::Unix']}], [{'type': 'bytes', 'value': 'authors'}, {'type': 'list', 'value': [b'Kristian Erik Hermansen', b'Sagi Shahar <sagi.shahar@mwrinfosecurity.com>', b'Kostas Lintovois <kostas.lintovois@mwrinfosecurity.com>']}], [{'type': 'bytes', 'value': 'privileged'}, {'type': 'bool', 'value': False}], [{'type': 'bytes', 'value': 'references'}, {'type': 'list', 'value': [[b'CVE', b'2013-4011'], [b'OSVDB', b'95420'], [b'BID', b'61287'], [b'URL', b'http://www-01.ibm.com/support/docview.wss?uid=isg1IV43827'], [b'URL', b'http://www-01.ibm.com/support/docview.wss?uid=isg1IV43756']]}], [{'type': 'bytes', 'value': 'targets'}, {'type': 'dict', 'value': {0: b'IBM AIX Version 6.1', 1: b'IBM AIX Version 7.1'}}], [{'type': 'bytes', 'value': 'default_target'}, {'type': 'int', 'value': 1}], [{'type': 'bytes', 'value': 'stance'}, {'type': 'bytes', 'value': 'aggressive'}], [{'type': 'bytes', 'value': 'options'}, {'type': 'dict', 'value': {b'WORKSPACE': {b'type': b'string', b'required': False, b'advanced': True, b'desc': b'Specify the workspace for this module'}, b'VERBOSE': {b'type': b'bool', b'required': False, b'advanced': True, b'desc': b'Enable detailed status messages', b'default': False}, b'WfsDelay': {b'type': b'integer', b'required': False, b'advanced': True, b'desc': b'Additional delay when waiting for a session', b'default': 0}, b'EnableContextEncoding': {b'type': b'bool', b'required': False, b'advanced': True, b'desc': b'Use transient context when encoding payloads', b'default': False}, b'ContextInformationFile': {b'type': b'path', b'required': False, b'advanced': True, b'desc': b'The information file that contains context information'}, b'DisablePayloadHandler': {b'type': b'bool', b'required': False, b'advanced': True, b'desc': b'Disable the handler code for the selected payload', b'default': False}, b'SESSION': {b'type': b'integer', b'required': True, b'advanced': False, b'desc': b'The session to run this module on.'}, b'FileDropperDelay': {b'type': b'integer', b'required': False, b'advanced': True, b'desc': b'Delay in seconds before attempting cleanup'}, b'WritableDir': {b'type': b'string', b'required': True, b'advanced': False, b'desc': b'A directory where we can write files', b'default': b'/tmp'}}}]]
А вот код, который производит вышеуказанный вывод:
def encode_dict(res):
for key, value in res.items():
op = []
if type(key).__name__ == "bytes":
op.append({'type': type(key).__name__, 'value': key.decode('utf-8')})
else:
op.append({'type': type(key).__name__, 'value': key})
if type(value).__name__ == "bytes":
op.append({'type': type(value).__name__, 'value': value.decode('utf-8')})
else:
op.append({'type': type(value).__name__, 'value': value})
decoded_res.append(op)
return decoded_res
Я хочу вывод для вложенных dicts и nested-lists, так что есть нет единственного значения типа "байты" или ключа внутри dict.