【问题标题】:How Java (Java8) fix WhiteHat "Improper Certificate Validation" (CWE-295) security vulnerabilityJava (Java8) 如何修复 WhiteHat “不正确的证书验证” (CWE-295) 安全漏洞
【发布时间】:2020-02-21 00:21:02
【问题描述】:

我们使用 WhiteHat Source 扫描器来扫描我们的源代码。该工具通过 2 种方法找出“不正确的证书验证”(CWE-295) 安全问题。这是一个真正的安全问题吗?如果是,我们如何在 Java 8 中修复它,我们是否有解决此类问题的解决方案?非常感谢。

  • public void checkClientTrusted(X509Certificate[] certs, String authType) --> 安全漏洞
  • public void checkServerTrusted(X509Certificate[] certs, String authType) --> 安全漏洞
// http://www.nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/
public class JavaCertificationUtils {

    private static final SanitizedLogger LOG = new SanitizedLogger(JavaCertificationUtils.class);

    public static void javaTrustAllCerts() {
        try {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }};

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        } catch (Exception e) {
            LOG.error("Java Certificate All Certs Exception.", e);
        }
    }
}

【问题讨论】:

    标签: security


    【解决方案1】:

    你展示的代码做了两件事:

    1. 它禁用 TLS 证书链验证(使用 trustAllCerts)。
    2. 然后它会禁用主机名验证(使用allHostsValid)。

    如何解决这个问题?首先 - 删除代码。所有的。似乎造成损害的确切行是:

    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    

    您可能会注意到(有 90% 的可能性)代码将停止工作。这可能是由于不同的原因。

    1. 您正在尝试连接到使用自签名证书的服务(可能是有人首先进行此类黑客攻击的原因)。该服务需要解决此问题。
    2. 您在信任库中没有验证信任链所需的所有证书。在这种情况下,您需要扩展您的信任库并添加必要的根 CA 证书。
    3. 您通过 SSH 隧道连接到 TLS 安全服务。将正确的域名转换为 localhost 后:TLS 主机名验证将失败。
    4. 其他事情会失败。

    然后让我们知道错误是什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      相关资源
      最近更新 更多