我正在尝试为域名中最左边的通配符编写正则表达式。到目前为止,我有这样的想法:
import re
o = urlparse(argv[1])
host_name = o.netloc
context = SSL.Context(SSL.TLSv1_METHOD) # Use TLS Method
context.set_options(SSL.OP_NO_SSLv2) # Don't accept SSLv2
context.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
callback)
# context.load_verify_locations(ca_file, ca_path)
sock = socket()
ssl_sock = SSL.Connection(context, sock)
ssl_sock.connect((host_name, 443))
ssl_sock.set_connect_state()
ssl_sock.set_tlsext_host_name(host_name)
ssl_sock.do_handshake()
cert = ssl_sock.get_peer_certificate()
common_name = cert.get_subject().commonName.decode()
print "Common Name: ", common_name
print "Cert number: ", cert.get_serial_number()
regex = common_name.replace('.', r'\.').replace('*',r'.*') + '$'
if re.match(regex, host_name):
print "matches"
else:
print "invalid"
# output:
Common Name: *.example.com
Cert number: 63694395280496902491340707875731768741但是,正则表达式不仅匹配*.example.com,还匹配*.*.*或www.*.com。此外,不应允许https://wrong.host.example.com/匹配。如何确保它只与最左边的标签匹配?
发布于 2016-03-06 06:43:32
您可以使用urlparse和split来代替regex。
from urlparse import urlparse
.
.
common_name = cert.get_subject().commonName.decode()
domain = urlparse(common_name).netloc
host = domain.split('.',1)[0]发布于 2016-03-06 09:27:10
你可以试试这个正则表达式:
r'(?:^|\s)(\w+\.)?example\.com(?:$|\s)'完整演示:
sock = socket()
ssl_sock = SSL.Connection(context, sock)
ssl_sock.connect((host_name, 443))
ssl_sock.set_connect_state()
ssl_sock.set_tlsext_host_name(host_name)
ssl_sock.do_handshake()
cert = ssl_sock.get_peer_certificate()
common_name = cert.get_subject().commonName.decode()
print "Common Name: ", common_name
print "Cert number: ", cert.get_serial_number()
rxString = r'(?:^|\s)(\w+\.)?' + common_name.replace('.', '\.')[3:] + '(?:$|\s)'
regex = re.compile(rxString)
if regex.match(host_name):
print "matches"
else:
print "invalid"输入:
url
-------------------
www.example.com
example.com
hello.example.com
foo.bar.example.com
*.*.*
www.*.com 输出:
url | result
------------------- | -----------
www.example.com | matches
example.com | matches
hello.example.com | matches
foo.bar.example.com | invalid
*.*.* | invalid
www.*.com | invalid发布于 2020-11-11 00:18:30
不幸的是,在Saleem的回答中,Regexp是错误的,并且不符合RFC61256.4.3。
我认为,最好的方法是将'*‘字符改为'^.+’(或者'^.*‘--不管f.example.com是否与f*.example.com匹配,RFC都是不干净的):
rxString = '^'+common_name.replace('.','\.').replace('*','[^\.]+')+'$'https://stackoverflow.com/questions/35820618
复制相似问题