【问题标题】:How to detect if ssl is enabled by entering url?如何通过输入 url 来检测是否启用了 ssl?
【发布时间】:2015-07-23 06:24:18
【问题描述】:

我正在开发一个可以检查远程服务器是否启用或未启用 SSL
的系统 我的输入是一个简单的 URL,例如 http://www.stackoverflow.com

我该怎么做?

【问题讨论】:

  • 你可以尝试一些东西,做一些研究并生成一些代码。
  • @Styphon 自过去三天以来一直在这样做。 :) 最后带着一些希望在这里问。
  • 我们在什么context中?这是浏览器中的用户吗?使用库的开发人员?现在我们戴着眼罩回答这个问题。

标签: php ssl


【解决方案1】:
  1. 使用 CURL 调用给定的 url

  2. 从 curl 中获取所有标题

  3. 读取Curl响应返回的头信息

    对于 curl 库及其 curl 函数,请通过http://php.net/manual/en/book.curl.php

【讨论】:

    【解决方案2】:

    有多种方法可以做到这一点。第一个是最简单的,只需尝试使用 HTTPS 协议从指定的 URL 检索数据(例如使用file_get_contents())。你有超时吗?这很可能意味着不支持 SSL/TLS。

    另一个是打开一个到端口 443 的套接字。获得超时?同样,这很可能意味着不支持安全连接。

    另一种为您提供更多信息的方法是通过 shell 调用类似 OpenSSL (openssl s_client -connect example.com:443) 的东西。但这需要更多的工作,并不总是允许,更容易失败,您需要自己解析结果。

    第一个解决方案的未经测试(目前没有可用的服务器)并且很可能无法完全正常工作(我几年来没有写过一行 PHP)示例:

    <?php
    error_reporting(E_ALL);
    
    $hasSsl = false;
    if ($_SERVER["REQUEST_METHOD"] == "post")
    {
        // I hate error suppressing, but it's just a quick 'n dirty example!
        $data = @file_get_contents($_POST["url"]); 
    
        $hasSsl = (strlen($data) != 0);
    }
    // Do something with $hasSsl
    ?>
    <!doctype HTML>
    
    <body>
        <form method="post">
            <p>
                <input type="uri" name="url">
                <input type="submit">
            </p>
        </form>
    </body>
    

    【讨论】:

      【解决方案3】:
      $sslEnabled = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? TRUE : FALSE;
      

      编辑 1
      使用 curl:将 http 更改为 https
      HEAD Request 发送到此网址
      阅读 http 响应:如果 200 则启用 ssl

      编辑 2

      输入:http://www.stackoverflow.com

      天真的方法:$url = str_replace ("http", "https", $input);
      预卷曲:使用 HTTP

      $ch = curl_init ($url);
      
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt ($ch, CURLOPT_VERBOSE,        0);
      curl_setopt ($ch, CURLOPT_HEADER,         1);     // i want to read header response from server
      
      curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // my request is HEAD 
      curl_setopt ($ch, CURLOPT_NOBODY, true);          // i don't need to get body response : it is better for me and for the remote server
      
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);  // to follow the location when http response is 301
      
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);    // if ssl is not enabled, my connection can take a lot of time so i will wait only for 10 seconds otherwise SSL is not enabled : you can enhance this value 
      
      curl_exec ( $ch ) ;
      
      $header = curl_getinfo($ch, CURLINFO_HEADER_OUT);
      var_dump ($header_size);
      

      结果是:(我得到了 2 个 http 响应,你应该取最后一个)

      * About to connect() to proxy XXX.YYY.ZZZ.AAA port N (#0)
      *   Trying XXX.YYY.ZZZ.AAA... * connected
      * Connected to XXX.YYY.ZZZ.AAA (XXX.YYY.ZZZ.AAA) port N (#0)
      * ******************************************
      > HEAD http://www.stackoverflow.com HTTP/1.1
      * ******************************************
      Host: www.stackoverflow.com
      Pragma: no-cache
      Accept: */*
      Proxy-Connection: Keep-Alive
      
      < HTTP/1.0 301 Moved Permanently
      < Content-Length: 148
      < Content-Type: text/html; charset=UTF-8
      < Location: http://stackoverflow.com/
      < Date: Wed, 13 May 2015 11:01:07 GMT
      * ******************************************
      * ******************************************
      * ******************************************
      * ******************************************
      < Proxy-Connection: keep-alive
      * Connection #0 to host XXX.YYY.ZZZ.AAA left intact
      * Issue another request to this URL: 'http://stackoverflow.com/'
      * Examining connection #0 for reuse
      * Re-using existing connection! (#0) with host XXX.YYY.ZZZ.AAA
      * Connected to XXX.YYY.ZZZ.AAA (XXX.YYY.ZZZ.AAA) port N (#0)
      * ******************************************
      > HEAD http://stackoverflow.com/ HTTP/1.1
      * ******************************************
      Host: stackoverflow.com
      Pragma: no-cache
      Accept: */*
      Proxy-Connection: Keep-Alive
      
      < HTTP/1.0 200 OK
      < Cache-Control: public, no-cache="Set-Cookie", max-age=52
      < Content-Length: 238748
      < Content-Type: text/html; charset=utf-8
      < Expires: Wed, 13 May 2015 11:02:01 GMT
      < Last-Modified: Wed, 13 May 2015 11:01:01 GMT
      < Vary: *
      < X-Frame-Options: SAMEORIGIN
      * ******************************************
      < Set-Cookie: prov=bb4ad145-d7ed-4d40-8e02-43976c589c10; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
      < Date: Wed, 13 May 2015 11:01:08 GMT
      * ******************************************
      * ******************************************
      * ******************************************
      * ******************************************
      < Proxy-Connection: keep-alive
      * Connection #0 to host XXX.YYY.ZZZ.AAA left intact
      * Closing connection #0
      

      标头响应:使用 HTTPS(stackoverflow 在端口 443 上没有 SLL)

      * About to connect() to www.stackoverflow.com port 443 (#0)
      *   Trying 198.252.206.16... * Timeout
      * connect() timed out!
      * Closing connection #0
      

      注意:您必须在 php.ini 文件中启用 php_curl.dll 并重新启动服务器

      【讨论】:

      • 我正在开发一个可以通过输入 URL 来检查 ssl 的系统 URL 是从哪里获取的?
      【解决方案4】:

      https://stackoverflow.com/a/21848415/524743

      使用扩展加载检查!

      http://php.net/manual/en/function.extension-loaded.php

       if(!extension_loaded('openssl')) {
              throw new Exception('This app needs the Open SSL PHP extension.');
          }
      

      或者那个https://stackoverflow.com/a/7304205/524743

      if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
          // SSL connection
      }
      

      【讨论】:

      • 扩展可以加载,但不能使用,小心!!
      猜你喜欢
      • 1970-01-01
      • 2012-05-11
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 2020-10-20
      • 1970-01-01
      相关资源
      最近更新 更多