【发布时间】:2020-07-20 22:48:11
【问题描述】:
我从 API 接收 msgpack 类型数据,当我在响应中使用 msgpack.unpackb 时,我得到一个包含子字典或子数组的字典,其中一些键和值的类型为字节、int 和 str,字典,列表。我想将它们全部转换为 str 但带有元信息,这样我就可以在通过 Web 套接字将它们发送到客户端后将它们转换回原来的样子。
这是一个示例数据:
{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
我想要嵌套字典和嵌套列表的输出,因此字典内没有单个“字节”类型的值或键。
【问题讨论】:
-
这个已经解决了。它被称为 JSON。
-
这能回答你的问题吗? How to write a nested dictionary to json
-
@mkrieger1 JSON 无法序列化“字节”类型的对象,所以我需要消除所有“字节”类型的键/值
-
为什么使用字节而不是普通字符串?
-
@mkrieger1 我从 API 接收 msgpack 类型的数据,当我在响应中使用 msgpack.unpackb 时,我得到这些“字节”类型的字典和列表。该 API 不在我的控制范围内,所以我别无选择。
标签: python python-3.x list dictionary