【发布时间】: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