Python-如何打印每个密码套件请求支持的网站数量



所以我的hosts.txt文件中有20个网站,每个网站都运行一个WeakCipher , KnownCipher, ModernCipher来测试是否可以建立连接。

这是一份学校作业,要求我测试3种密码。

如何打印每个密码套件支持多少个网站

import socket
import ssl
import grequests
import re

WeakCipher = 'NULL-MD5'
KnownCipher = 'DHE-RSA-AES256-GCM-SHA384'
ModernCipher = 'ECDHE-RSA-AES256-SHA384'
#Read from txt file and convert it into a List.
List = open("C:\Users\Farzad\Desktop\hosts.txt").read().splitlines()
#async method to do more than 1 URL at a time
rs = (grequests.get(url) for url in List)
requests = grequests.map(rs)

for response in requests:
urlfix = re.compile(r"https?://(www.)?")
urlre = urlfix.sub('', response.url).strip().strip('/')
context = ssl.create_default_context()
context.set_ciphers(WeakCipher)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = context.wrap_socket(s, server_hostname=urlre)
try:
ssl_sock.connect((urlre, 443))
except Exception as e:
print("ERROR:", response.url, "DOES NOT SUPPORT YOUR WEAK CIPHER")
else:
print(response.url,"CONNECTION ESTABLISHED WITH YOUR WEAK CIPHER")

context = ssl.create_default_context()
context.set_ciphers(KnownCipher)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = context.wrap_socket(s, server_hostname=urlre)
try:
ssl_sock.connect((urlre, 443))
except Exception as e:
print("ERROR:", response.url, "DOES NOT SUPPORT YOUR MODERN CIPHER")
else:
print(response.url,"CONNECTION ESTABLISHED WITH YOUR KNOWN CIPHER" )

context = ssl.create_default_context()
context.set_ciphers(ModernCipher)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = context.wrap_socket(s, server_hostname=urlre)
try:
ssl_sock.connect((urlre, 443))
except Exception as e:
print("ERROR:", response.url, "DOES NOT SUPPORT YOUR MODERN CIPHER" 'n')
else:
print(response.url,"CONNECTION ESTABLISHED WITH YOUR MODERN CIPHER" 'n')

您可以编写一个bash脚本来测试密码套件。它应该从OpenSSL获得一个受支持的密码套件列表,并尝试使用每一个进行连接。如果握手成功,它应该打印YES。如果握手不成功,它会打印NO,然后是OpenSSL错误文本。

nmap有一个可以参考的示例脚本。具有ssl枚举密码的Nmap

WeakCipher = 'NULL-MD5'
t_weak=0
context = ssl.create_default_context()
context.set_ciphers(WeakCipher)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = context.wrap_socket(s, server_hostname=urlre)
try:
ssl_sock.connect((urlre, 443))
except Exception as e:
print("ERROR:", response.url, "DOES NOT SUPPORT YOUR WEAK CIPHER")
else:
t_weak +=1
print(response.url,"CONNECTION ESTABLISHED WITH YOUR WEAK CIPHER")
print("Number of websites with Weak cipher", t_weak)