你们中有没有人知道,我是否可以通过我的应用程序代码检查服务器是否启用了ssl以及如何检查?
你们中有没有人知道,我是否可以通过我的应用程序代码检查服务器是否启用了ssl以及如何检查?
例如,若想通过 SSL 读取 stackoverflow.com
,不必询问是否支持 SSL,直接操作即可。在 Python 中:
>>> import urllib2
>>> urllib2.urlopen('https://stackoverflow.com')
Traceback (most recent call last):
...
urllib2.URLError: <urlopen error (10060, 'Operation timed out')>
>>> html = urllib2.urlopen('http://stackoverflow.com').read()
>>> len(html)
146271
>>>
这表明 stackoverflow.com
不支持 SSL (2008)。
更新: 现在 stackoverflow.com
支持 https。
2>&1 | grep -q '^depth'
添加到上述命令行中。我很乐意学习更优雅的方法。 - András Kornbash-3.2$ echo ^D | telnet www.google.com https
Trying 66.102.11.104...
Connected to www.l.google.com.
Escape character is '^]'.
Connection closed by foreign host.
bash-3.2$ echo ^D | telnet www.stackoverflow.com https
Trying 69.59.196.211...
telnet: connect to address 69.59.196.211: Connection refused
telnet: Unable to connect to remote host
看这里……Google可以,StackOverflow不行。
这是一个C#单元测试,可以在不需要正确的HTTPContext的情况下执行检测:
[TestMethod]
public void DetectSslSupport()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.someinsecuresite.com");
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
//some sites like stackoverflow will perform a service side redirect to the http site before the browser/request can throw an errror.
Assert.IsTrue(response.ResponseUri.Scheme == "https");
}
}
catch (WebException)//"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."}
{
Assert.IsTrue(false);
}
}
我不确定您偏好的语言是什么,但以下是用C#编写的代码:
public bool IsSecureConnection()
{
return HttpContext.Current.Request.IsSecureConnection ||
HttpContext.Current.Request.Headers["HTTP_X_SSL_REQUEST"].Equals("1");
}
批量 SSL/TLS 测试给定输入文件 http.parsed
的形式为
10.31.11.5:443
10.31.11.25:443
10.31.11.37:55000
10.31.11.116:80
使用GNU Parallel
parallel -j10 'curl -k https://{} 1> /dev/null 2> /dev/null && echo https://{}' :::: http.parsed
我们获得输出
https://10.31.11.5:443
https://10.31.11.25:443
https://10.31.11.37:55000
如果您在服务器上运行PHP或ASP代码,简短的答案是您无法确定是否有SSL。您可以尝试与非SSL IP地址建立套接字连接,并查看是否获得了SSL证书,并枚举其通用名称和主题备用名称,但总体而言,简单的答案是您无法确定是否有SSL。Apache的常见(误)配置是在没有SSL证书的情况下监听端口443,因此能够建立连接并不能保证存在SSL。无法建立连接可能意味着您的应用程序没有网络权限。因为设置SSL很麻烦,所以您应该知道是否有SSL,这是一项配置决策。这就像想知道您有多少个孩子-您应该知道。
您需要指定您正在使用哪种协议 -- HTTP、IMAP、POP 等都有 SSL 版本。
假设您感兴趣的是 HTTPS,您可以检查服务器上是否有端口 443 上正在监听的东西,然后从那里开始...