【问题标题】:Converting dictionary value to string将字典值转换为字符串
【发布时间】:2017-03-15 18:24:39
【问题描述】:

我正在处理 Nmap 脚本引擎的输出,但在字符串转换方面遇到了问题。

nmap 的结果将其放入字典中,我将其转换为字符串:

nm = nmap.PortScanner()
sslNmapOutput = nm.scan(hosts='192.168.2.50', arguments='--script ssl-enum-ciphers -p443')

sslOutput = ', '.join("%s=%s" % (key,val) for (key,val) in sslNmapOutput.iteritems())

resultString = re.search("warnings:(.*)SHA1\n", sslOutput)
resultString = result.group(1)
print sslOutput.strip()

这将打印以下内容:

nmap={'scanstats': {'uphosts': '1', 'timestr': 'Wed Mar 15 17:59:42 2017', 'downhosts': '0', 'totalhosts': '1', 'elapsed': '18.85'}, 'scaninfo': {'tcp': {'services': '443', 'method': 'syn'}}, 'command_line': 'nmap -oX - --script ssl-enum-ciphers -p443
192.168.2.50'}, scan={'192.168.2.50': {'status': {'state': 'up', 'reason': 'reset'}, 'hostnames': [{'type': '', 'name': ''}], 'vendor': {}, 'addresses': {'ipv4': '192.168.2.50'}, 'tcp': {443: {'product': '', 'state': 'open', 'version': '', 'name': 'https', 'conf': '3', 'script': {'ssl-enum-ciphers': '\n  SSLv3: \n    ciphers: \n      TLS_ECDHE_RSA_WITH_RC4_128_SHA (secp256r1) - C\n      TLS_RSA_WITH_RC4_128_SHA (rsa 2048) - C\n      TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA (secp256r1) - A\n      TLS_DHE_RSA_WITH_AES_256_CBC_SHA (dh 2048) - A\n      TLS_RSA_WITH_AES_256_CBC_SHA (rsa 2048) - A\n      TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA (secp256r1) - A\n      TLS_DHE_RSA_WITH_AES_128_CBC_SHA (dh 2048) - A\n      TLS_RSA_WITH_AES_128_CBC_SHA (rsa 2048) - A\n      TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA (secp256r1) - C\n      TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA (dh 2048) - C\n      TLS_RSA_WITH_3DES_EDE_CBC_SHA (rsa 2048) - C\n    compressors: \n      NULL\n    cipher preference: server\n    warnings: \n      64-bit block cipher 3DES vulnerable to SWEET32 attack\n      Broken cipher RC4 is deprecated by RFC 7465\n      CBC-mode cipher in SSLv3 (CVE-2014-3566)\n      Weak certificate signature: SHA1\n }, 'extrainfo': '', 'reason': 'syn-ack', 'cpe': ''}}}}

我想获取warnings: 和SHA1\n 之间的所有信息。但是,当我打印正则表达式的结果时,我得到以下信息:

\n      64-bit block cipher 3DES vulnerable to SWEET32 attack\n      Broken cipher RC4 is deprecated by RFC 7465\n      CBC-mode cipher in SSLv3 (CVE-2014-3566)\n      Weak certificate signature:

我想要打印的值与正确的空格和换行符缩进一样,如下所示:

test =  "\n      64-bit block cipher 3DES vulnerable to SWEET32 attack\n      Broken cipher RC4 is deprecated by RFC 7465\n      CBC-mode cipher in SSLv3 (CVE-2014-3566)\n      Weak certificate signature: "
print test.strip()

输出:

64-bit block cipher 3DES vulnerable to SWEET32 attack
Broken cipher RC4 is deprecated by RFC 7465
CBC-mode cipher in SSLv3 (CVE-2014-3566)
Weak certificate signature:

【问题讨论】:

  • 查看pprint模块。
  • 使用json.dumps将字典转换为字符串,然后使用pprint。

标签: python regex string dictionary nmap


【解决方案1】:

这是发生了什么的演示:

>>> s = 'a\nb'
>>> print s
a
b
>>> print {'key': s}
{'key': 'a\nb'}

sslNmapOutput 是一个字典,其值位于树的深处,包含实际的换行符。在本身是字典的立即值上调用str(通过%s)会创建一个字符串表示,其中内部值中的换行符被两个字符\n替换。不要将正则表达式应用于sslOutput。而是将其应用于 sslNmapOutput 中包含的原始字符串,在这种情况下可用作:

sslNmapOutput['scan']['192.168.2.50']['tcp'][443]['script']['ssl-enum-ciphers']

【讨论】:

  • 我已经尝试使用result = re.search("warnings:(.*)SHA1\n", str(sslNmapOutput)) 将正则表达式应用于sslNmapOutput。但是,打印时换行符仍然会返回。我将如何指定它的 `['ssl-enum-ciphers'] 部分?
  • @Papantonia re.search("warnings:(.*)SHA1\n", sslNmapOutput['scan']['192.168.2.50']['tcp'][443]['script']['ssl-enum-ciphers']).
猜你喜欢
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多