php - 使用 PHP 和 cURL 防止远程服务器欺骗

标签 php ssl curl spoofing

我正在编写从远程服务器请求 secret 数据的 php 脚本。 我正在使用 cURL 获取远程服务器的证书信息(其输出请见下方)。

我需要检查哪些数组键的证书有效性,以防止有人可以欺骗它们?

例如, key [certinfo][0][Subject][CN]可以被自签名证书欺骗。

我可以只检查我在客户端使用的 ca-bundle 文件的 md5 文件哈希,但是当服务器的证书到期时,我需要相应地替换 ca-bundle 文件,并在 php 脚本中更新 md5 文件哈希 -这对我来说是不能接受的。唯一的默认是在不更新 php 脚本的情况下替换 ca-bundle 文件。为此,我需要验证服务器证书的属性,这些属性在未来证书重新生成时保持不变,并且不会被恶意因素欺骗。

print_r( curl_getinfo($ch) ) :

[url] => https://remoteserver.com
[content_type] => text/html
[http_code] => 200
[header_size] => 148
[request_size] => 79
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.374
[namelookup_time] => 0
[connect_time] => 0.062
[pretransfer_time] => 0.203
[size_upload] => 0
[size_download] => 20618
[speed_download] => 55128
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => 0
[starttransfer_time] => 0.281
[redirect_time] => 0
[certinfo] => Array
    (
        [0] => Array
            (
                [Subject] => Array
                    (
                        [OU] => Globe Standard SSL
                        [CN] => www.remoteserver.com
                    )

                [Issuer] => Array
                    (
                        [C] => US
                        [O] => Globe Hosting, Inc.
                        [OU] => GlobeSSL DV Certification Authority
                        [CN] => GlobeSSL CA
                    )

                [Version] => 2
                [Signature Algorithm] => sha1WithRSAEncryption
                [Start date] => 2011-09-30 00:00:00 GMT
                [Expire date] => 2014-09-30 23:59:59 GMT
                [Public Key Algorithm] => rsaEncryption
                [RSA Public Key] => 2048
                [rsa(n)] => d7:c0:0b:3f:f3:3e:d6:ed:92:56:22:12:64:c1:c4:00:d7:c9:a1:1e:..cut..
                [rsa(e)] => 01:00:01:
                [X509v3 Authority Key Identifier] => keyid:C3:AB:A0:02:F0:9B:F5:66:7F:28:15:92:22:95:DB:B8:4E:D3:93:08
                [X509v3 Subject Key Identifier] => 13:1B:B2:52:14:3C:70:1C:B2:93:F1:C5:04:06:86:60:8A:D4:E5:5C
                [X509v3 Key Usage] => DigitalSignature,KeyEncipherment
                [X509v3 Basic Constraints] => CA:FALSE
                [X509v3 Extended Key Usage] => TLSWebServerAuthentication,TLSWebClientAuthentication
                [X509v3 Certificate Policies] => Policy:1.3.6.1.4.1.6449.1.2.2.27, CPS:http://www.globessl.com/docs/GlobeSSL_CPS.pdf
                [X509v3 CRL Distribution Points] => URI:http://crl.globessl.com/GlobeSSLDVCertificationAuthority.crl
                [Authority Information Access] => CAIssuers-URI:http://crt.globessl.com/GlobeSSLDVCertificationAuthority.crt, OCSP-URI:http://ocsp.globessl.com
                [X509v3 Subject Alternative Name] => DNS:www.remoteserver.com,DNS:remoteserver.com
                [Signature] => 61:38:06:d4:30:9c:14:a4:e5:1e:b2:c8:c4:..cut..
                [Cert] => -----BEGIN CERTIFICATE-----cut-----END CERTIFICATE-----

            )

        [1] => Array
            (
                [Subject] => Array
                    (
                        [C] => US
                        [O] => Globe Hosting, Inc.
                        [OU] => GlobeSSL DV Certification Authority
                        [CN] => GlobeSSL CA
                    )

                [Issuer] => Array
                    (
                        [C] => SE
                        [O] => AddTrust AB
                        [OU] => AddTrust External TTP Network
                        [CN] => AddTrust External CA Root
                    )

                [Version] => 2
                [Signature Algorithm] => sha1WithRSAEncryption
                [Start date] => 2010-06-22 00:00:00 GMT
                [Expire date] => 2020-05-30 10:48:38 GMT
                [Public Key Algorithm] => rsaEncryption
                [RSA Public Key] => 2048
                [rsa(n)] => a0:47:04:ce:a8:33:ab:..cut..
                [rsa(e)] => 01:00:01:
                [X509v3 Authority Key Identifier] => keyid:AD:BD:98:7A:34:B4:26:F7:FA:C4:26:54:EF:03:BD:E0:24:CB:54:1A
                [X509v3 Subject Key Identifier] => C3:AB:A0:02:F0:9B:F5:66:7F:28:15:92:22:95:DB:B8:4E:D3:93:08
                [X509v3 Key Usage] => CertificateSign,CRLSign
                [X509v3 Basic Constraints] => CA:TRUE,pathlen:0
                [X509v3 Certificate Policies] => Policy:1.3.6.1.4.1.6449.1.2.2.27
                [X509v3 CRL Distribution Points] => URI:http://crl.usertrust.com/AddTrustExternalCARoot.crl
                [Authority Information Access] => CAIssuers-URI:http://crt.usertrust.com/AddTrustExternalCARoot.p7c, CAIssuers-URI:http://crt.usertrust.com/AddTrustUTNSGCCA.crt, OCSP-URI:http://ocsp.usertrust.com
                [Signature] => 66:9c:13:6d:d2:7e:2c:..cut..
                [Cert] => -----BEGIN CERTIFICATE-----cut-----END CERTIFICATE-----

            )

    )

谢谢你的帮助。

最佳答案

$ch=curl_init("https://default_cert");
curl_setopt($ch, CURLOPT_CAINFO, getcwd().'mydefault.cert');
curl_setopt ($ch,CURLOPT_CERTINFO,1);
curl_setopt ($ch,CURLOPT_VERBOSE,1);
curl_exec($ch) or die;
print_r( curl_getinfo($ch) );

在这种情况下,如果 mydefault.cert 包含不正确的 CN 或证书将被 中间人 欺骗,curl exec 将失败。

mydefault.cert 应该包含远程主机的证书。您必须使用受信任的 channel 获取它(或者至少使用 CURLOPT_SSL_VERIFYPEER,FALSE 下载一次,当您确定远程主机在此时此刻)。

一些理论:

  • selfsignednot selfsigned 证书的区别如下 - selfsigned 是由个人为自己颁发的,它有 key 和证书部分,虽然不是自签名也是由您信任的人签名的。它可以是证书中心或您最好的 friend ,或您的老板。如果你最好的 friend 颁发了自签名证书并且你通过授权方式获得了证书(他在磁盘上给你,或者使用自己的 pgp key 用它签署一封信,或者他从证书中间告诉它的 20 个字符)你可以使用那个作为 CA 的证书。
  • 证书本身是加密对的公钥 key 私钥一对。因此,站点执行以下操作 - 它使用私钥以电子方式签署拥有的内容,在另一端客户端使用加密内容公钥。与使用 PGP 时相同。
  • 从服务器到客户端的数据使用一种加密算法(例如 ES、3DES、 Blowfish、CAST128 或 Arcfour,在客户端将主机授权为可信后由客户端初始化的算法 key ,该 key 由服务器的公钥加密(如果是 https,则为证书)之后来自客户端的数据到服务器也被该算法加密,作为使用上述方案完成的 key 交换。

更多信息在 google

你能使用这个模式来欺骗服务器发送给客户端的任何数据吗?

关于php - 使用 PHP 和 cURL 防止远程服务器欺骗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103942/

相关文章:

php - 如何使用php创建一个表头在左侧的表格?

php - 与 PHP 的 JDBC 连接 URL

php - 如何解决 PHP 中的 OpenSSL "EVP_DecryptFinal_ex:bad decrypt"错误?

ssl - 尽管启用了 Keep-Alive 和 session 标识符/票证,仍进行多次 SSL/TLS 握手

php - 使用 cURL 和 PHP 处理 1,000 多个 URL 的列表

php - 如何在 Codeigniter 中使用 PHP 基于列类别在行中显示 MySQL 数据

c# - ASP.NET 2.0 从控件强制使用 SSL

postgresql - 致命 : could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied

php - Instagram API。 "Unable to reach callback url"

java - 通过 HTTP 客户端编写curl 等效 java 代码时遇到问题