简体   繁体   English

使用安全会话 cookie 在 HTTP 和 HTTPS 页面之间切换

[英]Switching between HTTP and HTTPS pages with secure session-cookie

Update: Note that every website switching between unsecure HTTP and encrypted HTTPS pages, is inevitable prone to SSL-strip .更新:请注意,每个网站在不安全的 HTTP 和加密的 HTTPS 页面之间切换,都不可避免地容易出现SSL-strip Please think about using HTTPS for the whole site, although this neither can prevent SSL-strip, at least this gives the user the possibility to call the site safely, if he cares.请考虑为整个站点使用 HTTPS,虽然这也不能阻止 SSL-strip,但至少这使用户可以安全地调用站点,如果他关心的话。 For sites that need to switch, this method is probably still the best option.对于需要切换的站点,这种方法可能仍然是最佳选择。

It's a common scenario, that a website has pages with sensitive data, which should be accessed only with the HTTPS protocoll, and other ones with noncritical data.这是一个常见的场景,一个网站的页面包含敏感数据,只能使用 HTTPS 协议访问,而其他页面包含非关键数据。

I found a solution which allows switching between secure and non secure pages, while keeping the session and would like to ask you for any hints about flaws in the concept.我找到了一个解决方案,它允许在安全和非安全页面之间切换,同时保留 session,并且想向您询问有关该概念缺陷的任何提示 The whole article you can find here: Secure session cookie with SSL (of course i'm also happy to hear, that it is safe).您可以在此处找到整篇文章:使用 SSL 保护 session cookie (当然我也很高兴听到它是安全的)。

The problem问题

HTTPS makes sure, that nobody between client and server can eavesdrop our communication and prevents a man-in-the-middle attack. HTTPS 确保客户端和服务器之间的任何人都无法窃听我们的通信并防止中间人攻击。 Unfortunately this doesn't apply to the session-cookie, it is sent to unencrypted requests too.不幸的是,这不适用于 session-cookie,它也被发送到未加密的请求。

PHP offers the function session_set_cookie_params(...) with the parameter $secure. PHP 提供带有参数 $secure 的 function session_set_cookie_params(...)。 This is what we need, but it leaves us to the problem that we loose our session, when we switch to an unsecure page.这是我们需要的,但是当我们切换到不安全的页面时,我们会失去 session 的问题。

The authentication cookie身份验证cookie

The idea of the authentication cookie is, that when the user enters his password (increases his access privileges), we create a second cookie additionally to the unsecure session-cookie, and make sure that only encrypted HTTPS pages have access to it.身份验证 cookie 的想法是,当用户输入他的密码(增加他的访问权限)时,我们在不安全的会话 cookie 之外创建第二个 cookie,并确保只有加密的 HTTPS 页面可以访问它。

https://www.example.com/login.php

<?php
  session_start();
  // regenerate session id to make session fixation more difficult
  session_regenerate_id(true);

  // generate random code for the authentication cookie and store it in the session
  $authCode = md5(uniqid(mt_rand(), true));
  $_SESSION['authentication'] = $authCode;

  // create authentication cookie, and restrict it to HTTPS pages
  setcookie('authentication', $authCode, 0, '/', '', true, true);

  print('<h1>login</h1>');
  ...
?>

Now every page (HTTPS and HTTP) can read the unsecure session-cookie, but pages with sensitive information can check for the secure authentication cookie.现在每个页面(HTTPS 和 HTTP)都可以读取不安全的会话 cookie,但包含敏感信息的页面可以检查安全身份验证 cookie。

https://www.example.com/secret.php

<?php
  session_start();

  // check that the authentication cookie exists, and that
  // it contains the same code which is stored in the session.
  $pageIsSecure = (!empty($_COOKIE['authentication']))
    && ($_COOKIE['authentication'] === $_SESSION['authentication']);

  if (!$pageIsSecure)
  {
    // do not display the page, redirect to the login page
  }

  ...
?>

An attacker could manipulate the session cookie, but he never has access to the authentication cookie.攻击者可以操纵 session cookie,但他永远无法访问身份验证 cookie。 Only the person who entered the password, can own the authentication cookie, it's always sent over encrypted HTTPS connections.只有输入密码的人才能拥有身份验证 cookie,它总是通过加密的 HTTPS 连接发送。

Thanks a lot for every answer!非常感谢每一个答案!

A simpler alternative: It is becoming an increasingly accepted alternative to use TLS all the time, rather than switching back and forth between secure and unsecure connections.一个更简单的替代方案:一直使用 TLS,而不是在安全和不安全连接之间来回切换,正在成为一种越来越被接受的替代方案。 The bulk of additional processing time is spent setting up the secure tunnel, but this is only done once and cached (typically).大部分额外处理时间用于设置安全隧道,但这只完成一次并缓存(通常)。 The symmetric encryption of subsequent traffic is very, very fast on modern processors.后续流量的对称加密在现代处理器上非常非常快。 It's somewhat out-of-date thinking to believe that this would cause a server overhead or scalability issue.认为这会导致服务器开销或可伸缩性问题的想法有些过时。

In a recent blog post, a Google engineer reported that when they switched to HTTPS-only for GMail, they found their server overheard increased by only 4%.在最近的一篇博客文章中,一位 Google 工程师报告说,当他们为 GMail 切换到仅 HTTPS 时,他们发现他们的服务器监听率仅增加了 4%。 (Can't find the citation.) (找不到引文。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM