Mal*_*ist 62 php cryptography actionscript-3 nonce
我正在运行一个网站,并且有一个评分系统可以为您提供玩游戏次数的积分.
它使用散列来证明http请求的完整性,因此用户无法改变任何东西,但是我担心可能发生,有人发现他们不需要改变它,他们只需要获得高分,并复制http请求,标题和所有.
以前我被禁止防止这种攻击,因为它被认为是不可能的.但是,既然已经发生了,我可以.http请求源自Flash游戏,然后由php验证并且php将其输入数据库.
我很确定nonce会解决这个问题,但我不确定如何实现它们.设置nonce系统的常用且安全的方法是什么?
irc*_*ell 61
它实际上很容易......有一些库可以帮到你:
或者如果你想自己编写,那很简单.使用WikiPedia页面作为跳出点,在伪代码中:
在服务器端,您需要两个客户端可调用函数
getNonce() {
$id = Identify Request //(either by username, session, or something)
$nonce = hash('sha512', makeRandomString());
storeNonce($id, $nonce);
return $nonce to client;
}
verifyNonce($data, $cnonce, $hash) {
$id = Identify Request
$nonce = getNonce($id); // Fetch the nonce from the last request
removeNonce($id, $nonce); //Remove the nonce from being used again!
$testHash = hash('sha512',$nonce . $cnonce . $data);
return $testHash == $hash;
}
Run Code Online (Sandbox Code Playgroud)
在客户端:
sendData($data) {
$nonce = getNonceFromServer();
$cnonce = hash('sha512', makeRandomString());
$hash = hash('sha512', $nonce . $cnonce . $data);
$args = array('data' => $data, 'cnonce' => $cnonce, 'hash' => $hash);
sendDataToClient($args);
}
Run Code Online (Sandbox Code Playgroud)
该函数makeRandomString实际上只需要返回一个随机数或字符串.随机性越好,安全性越好......还要注意,由于它被直接输入到散列函数中,因此实现细节与请求请求无关.客户端的版本和服务器的版本不需要匹配.实际上,需要匹配100%的唯一位是用于hash('sha512', $nonce . $cnonce . $data);... 的哈希函数.这是一个合理安全makeRandomString函数的例子......
function makeRandomString($bits = 256) {
$bytes = ceil($bits / 8);
$return = '';
for ($i = 0; $i < $bytes; $i++) {
$return .= chr(mt_rand(0, 255));
}
return $return;
}
Run Code Online (Sandbox Code Playgroud)
Sco*_*ski 22
不,实际上,几个CAESAR条目的动机之一是设计一个经过验证的加密方案,最好是基于流密码,它可以抵抗随机数重用.(例如,重复使用带有AES-CTR的随机数,会破坏您的信息的机密性,使第一年编程学生可以解密它.)
有六种主要的思想流派:
1).因此,考虑到这一点,要问的主要问题是:
对任何随机随机数的问题2的答案是使用CSPRNG.对于PHP项目,这意味着以下之一:
random_bytes() 适用于PHP 7+项目random_bytes()这两个在道德上是等价的:
$factory = new RandomLib\Factory;
$generator = $factory->getMediumStrengthGenerator();
$_SESSION['nonce'] [] = $generator->generate(32);
Run Code Online (Sandbox Code Playgroud)
和
$_SESSION['nonce'] []= random_bytes(32);
Run Code Online (Sandbox Code Playgroud)
有状态的nonce很容易推荐:
$found = array_search($nonce, $_SESSION['nonces']);
if (!$found) {
throw new Exception("Nonce not found! Handle this or the app crashes");
}
// Yay, now delete it.
unset($_SESSION['nonce'][$found]);
Run Code Online (Sandbox Code Playgroud)
随意替换array_search()数据库或memcached查找等.
这是一个难以解决的问题:您需要一些方法来防止重放攻击,但您的服务器在每个HTTP请求后都有完全失忆.
唯一合理的解决方案是验证到期日期/时间,以最大限度地减少重放攻击的有用性.例如:
// Generating a message bearing a nonce
$nonce = random_bytes(32);
$expires = new DateTime('now')
->add(new DateInterval('PT01H'));
$message = json_encode([
'nonce' => base64_encode($nonce),
'expires' => $expires->format('Y-m-d\TH:i:s')
]);
$publishThis = base64_encode(
hash_hmac('sha256', $message, $authenticationKey, true) . $message
);
// Validating a message and retrieving the nonce
$decoded = base64_decode($input);
if ($decoded === false) {
throw new Exception("Encoding error");
}
$mac = mb_substr($decoded, 0, 32, '8bit'); // stored
$message = mb_substr($decoded, 32, null, '8bit');
$calc = hash_hmac('sha256', $message, $authenticationKey, true); // calcuated
if (!hash_equals($calc, $mac)) {
throw new Exception("Invalid MAC");
}
$message = json_decode($message);
$currTime = new DateTime('NOW');
$expireTime = new DateTime($message->expires);
if ($currTime > $expireTime) {
throw new Exception("Expired token");
}
$nonce = $message->nonce; // Valid (for one hour)
Run Code Online (Sandbox Code Playgroud)
细心的观察者会注意到这基本上是JSON Web令牌的非标准兼容变体.