我正在创建一个使用两个 session 的登录系统(对于那些不允许使用 cookie 的人(以同意 cookie 法。我正在使用网站 http://www.cookielaw.org/the-cookie-law.aspx 作为引用)
现在,我有这个系统用于我的 cookie 身份验证
function GenerateString(){
$length = mt_rand(0,25);
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(5, strlen($characters) -1)];
}
return $string;
}
$RandomString = GenerateString();
$CookieAuth = $DB->prepare("INSERT INTO cookieauth (Username,RandomString) VALUES (?,?)");
$CookieAuth->bind_param('ss',$_POST['Username'],$RandomString);
$CookieAuth->execute(); // Insert the Authentication Methods into the database
$CookieAuth->close(); // Allow another query/statement
$GetInsertID = $DB->prepare("SELECT ID FROM CookieAuth WHERE RandomString=?");
$GetInsertID->bind_param('s',$Randomstring);
$GetInsertID->execute();
$GetInsertID->bind_result($RowID);
$GetInsertID->fetch();
$GetInsertID->close();
setcookie("Auth[ID]",$RowID);
setcookie("Auth[UName],$_POST['Username']);
setcookie("Auth[RandomString]",$RandomString);
然后处理cookie:
if(isset($_COOKIE['Auth'])){
$Authenticate = $DB->prepare("SELECT Username,RandomString FROM cookieauth WHERE ID=?");
$Authenticate->bind_param('i',$_COOKIE['Auth']['ID']);
$Authenticate->execute();
$Authenticate->bind_result($RowUsername,$RowString);
$Authenticate->fetch();
$Authenticate->close();
if ($_Cookie['Auth']['UName'] == $RowUsername){
if ($_COOKIE['Auth']['RandomString'] == $RowString){
header("Location: LoggedIn.php");
}else{
die("Possible Cookie Manipulation, Autologin Cannot Continue");
}
}else{
die("Possible Cookie Manupulation, Autologin Cannot Continue!");
}
我的总体目标是通过使用 cookie 提供自动登录功能。正如人们应该知道的那样,它们基本上以纯文本形式存储在硬盘驱动器上。因此,如果我包含一个随机生成的字符串,每次进一步处理时都会更改该字符串(然后更新 cookie 以匹配数据库),这是一种相当安全的方式吗完成任务?我的意思是,我知道这不是 100% 安全的,因为有些用户可能会尝试操纵随机字符串,所以我可以求助于盐、随 secret 钥,然后使用 hash_hmac
来 sha512 盐+ key 并将其保存为 cookie...
我的总体问题是,我提供的 block 是否提供了一种半安全的方法来通过 cookie 处理自动登录,并且可以最大限度地减少一些坏人操纵 key 以获得所需数据的可能性?
最佳答案
简介
当 cookie 正是 session 进行时,为什么要对 cookie 进行身份验证?如果您想更改 ID
,您可以使用 session_regenerate_id
轻松实现,正如@MarcB 所指出的。
我的假设
我想假设我没有清楚地理解这个问题,可能这就是你想要实现的目标
- 将值存储到 Cookie
- 了解这些值是否已被修改
你已经解决了
I could resort to a salt, random key then use hash_hmac to sha512 the salt+key and save that as the cookie...
这正是解决方案,但您需要注意
- session 是否可以被劫持
- PHP 有更好的方法生成随机字符串
- 想象一下每次 session 都可以轻松为您做的事情而必须更新您的 mysql 表的开销
- 使用
hash_hmac 512
会生成十六进制格式的126
您需要了解有Browser Cookie Limits所以我建议你将它减少到256
您的解决方案已修改
如果我们要使用您的解决方案,我们需要做一些小的修改
session_start();
// Strong private key stored Securly stored
// Used SESSION For demo
$privateKey = isset($_SESSION['key']) ? $_SESSION['key'] : mcrypt_create_iv(128, MCRYPT_DEV_URANDOM);
$my = new SignedCookie($privateKey);
$my->setCookie("test", "hello world", time() + 3600);
echo $my->getCookie("test");
输出
hello world
但是数据是这样存储的:
这仅使用 hash_hmac
来签署和验证您的值,还使用随机变量来确保坏人无法构建可能值的表,因为实际上他们不必破坏哈希 .. 可以只研究它也可以使用以前使用过的有效哈希,例如。
10 Cookies = AAAA
1 Cookie = BBBB
他可以使用有效 session 登录并将 cookie 从 BBBB
更改为 AAAA
所以即使您没有存储到数据库也始终包含一个随机参数
您仍然可以像这样删除 cookie:
$my->setCookie("test", null, time() - 3600);
使用的简单类
class SignedCookie {
private $prifix = '$x$';
private $privateKey;
function __construct($privateKey) {
$this->privateKey = $privateKey;
}
function setCookie($name, $value, $expire, $path = null, $domain = null, $secure = null, $httponly = null) {
$value = $value === null ? $value : $this->hash($value, mcrypt_create_iv(2, MCRYPT_DEV_URANDOM));
return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
}
function getCookie($name, $ignore = false) {
if (! isset($_COOKIE[$name]) || empty($_COOKIE[$name]))
return null; // does not exist
if ($ignore === false) {
if (substr($_COOKIE[$name], 0, 3) !== $this->prifix)
return - 1; // modified
$data = pack("H*", substr($_COOKIE[$name], 3)); // Unpack hex
$value = substr($data, 32, - 2); // Get Value
$rand = substr($data, - 2, 2); // Get Random prifix
if ($this->hash($value, $rand) !== $_COOKIE[$name])
return - 1; // modified
return $value;
}
return $_COOKIE[$name];
}
function hash($value, $suffix) {
// Added random suffix to help the hash keep changing
return $this->prifix . bin2hex(hash_hmac('sha256', $value . $suffix, $this->privateKey, true) . $value . $suffix);
}
}
结论
您不是安全专家 只需使用即可,因此只需使用 SSL ( SSL also has its issues but far better ) 或寻找现有的安全身份验证服务。 @ircmaxell 让我想起了 Schneier's Law最近:
@Baba: "surprise" is the enemy of security. The ONLY thing that should be secret is the private key. Remember Schneier's Law: Anyone can invent an encryption scheme that they themselves can't break. My answer is based on tried and true cryptographic principles.
嗯,我认为你也应该采纳这个建议。
关于php - 如何在没有 SSL 的情况下保护身份验证 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16258376/