22

我知道这个话题已经讨论了很多,但我还有一些具体的问题没有得到解答。例如:

// **PREVENTING SESSION HIJACKING**
// Prevents javascript XSS attacks aimed to steal the session ID
ini_set('session.cookie_httponly', 1);

// Adds entropy into the randomization of the session ID, as PHP's random number
// generator has some known flaws
ini_set('session.entropy_file', '/dev/urandom');

// Uses a strong hash
ini_set('session.hash_function', 'whirlpool');

// **PREVENTING SESSION FIXATION**
// Session ID cannot be passed through URLs
ini_set('session.use_only_cookies', 1);

// Uses a secure connection (HTTPS) if possible
ini_set('session.cookie_secure', 1);

session_start();

// If the user is already logged
if (isset($_SESSION['uid'])) {
    // If the IP or the navigator doesn't match with the one stored in the session
    // there's probably a session hijacking going on

    if ($_SESSION['ip'] !== getIp() || $_SESSION['user_agent_id'] !== getUserAgentId()) {
        // Then it destroys the session
        session_unset();
        session_destroy();

        // Creates a new one
        session_regenerate_id(true); // Prevent's session fixation
        session_id(sha1(uniqid(microtime())); // Sets a random ID for the session
    }
} else {
    session_regenerate_id(true); // Prevent's session fixation
    session_id(sha1(uniqid(microtime())); // Sets a random ID for the session
    // Set the default values for the session
    setSessionDefaults();
    $_SESSION['ip'] = getIp(); // Saves the user's IP
    $_SESSION['user_agent_id'] = getUserAgentId(); // Saves the user's navigator
}

所以,我的问题是

  • 是否ini_set提供足够的安全性?
  • 是否可以保存用户的IP和导航器,然后每次加载页面时检查以检测会话劫持?这会不会有任何问题?
  • 使用session_regenerate_id()正确吗?
  • 使用session_id()正确吗?
4

1 回答 1

17

你的配置很棒。您肯定阅读了如何锁定 php 会话。然而,这行代码否定了您的 php 配置提供的许多保护: session_id(sha1(uniqid(microtime()));

这是一种特别糟糕的生成会话 id 的方法。根据您的配置,您正在生成会话 id,/dev/urandom 这是一个很棒的熵池。这将比 uniqid() 随机得多,后者已经主要是一个时间戳,在这个组合中添加另一个时间戳根本没有帮助。尽快删除这行代码。

检查 IP 地址是有问题的,IP 地址更改是出于正当原因,例如用户在负载均衡器或 TOR 后面。用户代理检查是没有意义的,就像有一个 GET 变量一样?is_hacker=False,如果攻击者有会话 id,他们可能有用户代理,如果他们没有这个值很容易暴力破解。

于 2011-12-07T17:38:50.850 回答