[英]sessions and cookies with roles in php
我在php中设计角色的方式如下。 用户登录时,请检查该用户是否为主持人。
如果他是一个简单的用户,那么他会得到一个不需要https的cookie。 否则,如果他是主持人,我将分配一个特殊的会话,如下所示:
ini_set('session.use_only_cookies',true);
session_start();
//generate pass for the database && session
$pass=microtime().Moderator::generate_salt(30);
//Insert pass
Moderator::insert_moderator($pass);
//Encrypt the pass and give it back to the users session ID
$_SESSION["mod"]=$mod->encrypt_pass($pass);
上面的代码花费了主持人登录的登录时间,附加了随机盐以及插入或更新主持人通过的权限,具体取决于会话是开始还是结束(在这种情况下,这是第一次,mod被分配给他登录)。
另请注意,这是我使用的加密和解密算法:
public function encrypt_pass($session_id)
{
$session_id=strip_tags($session_id);
$session_id=trim($session_id);
//inititialization vector is being created.- a seed for random encryption and decryption
srand((double)microtime()*10000);
//opens an ecryption algorithm for use.
$this->td=mcrypt_module_open(MCRYPT_RIJNDAEL_256,'',MCRYPT_MODE_CFB,'');
//creates an IV for out encryption. Two parameters: size of IV ti create and method used to create IV
$this->iv=mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_RAND);
//get maximum size the algorithm will take.
$this->ks=mcrypt_enc_get_key_size($this->td);
//here teh key is created
$this->keys=substr(sha1(Moderator::generate_salt()),0, $this->ks);
//initialize the algorithm engine using , IV, and key we selected
mcrypt_generic_init($this->td,$this->keys,$this->iv);
//Two parameters, the algorithm resource and the data we actually want to encrypt. returns an incrypted value
$this->ciphertext=mcrypt_generic($this->td,$session_id);
//clean up!! parameter is -- algorithm resource
mcrypt_generic_deinit($this->td);
//end clean up!!
mcrypt_module_close($this->td);
//Goes to the moderators session $_SESSION['$ciphertext']
return $this->ciphertext;
}
public function decrypt_pass($session_id)
{
$session_id=strip_tags($session_id);
$session_id=trim($session_id);
mcrypt_generic_init($this->td, $this->keys,$this->iv);
$plaintext=mdecrypt_generic($this->td,$session_id);
mcrypt_generic_deinit($this->td);
mcrypt_module_close($this->td);
}
这应该可以防止我进行会话固定/劫持,对于黑客来说,抓住主持人的会话ID太简单了,即使他做到了,只要主持人再次提出另一个请求,密码也会自行重置并且如果他关闭了浏览器会话结束。 这使黑客有很短的时间框架来获取密码并假装为主持人。
问题:这足以应付会话劫持/修复,还是应该添加其他预防措施?
注意,我使用此算法的方式确实减慢了请求和响应的速度。 但是主持人的数量将有限,不得超过10个。
该代码会根据主持人的每个请求进行更新,
算法的缺点是您无法以任何方式识别特定用户。 您只是在那儿放些时间和一些盐。 为了防止会话/ cookie劫持,您需要将尽可能多的标识因素放入加密的字符串中。 例如,客户端的操作系统,用户代理,浏览器版本,IP,转发的IP等。 在这种情况下,您将解密发送给用户的字符串,假设只有您可以解密它,因为只有您拥有AES密钥,并且将验证插入到加密字符串中的所有参数。 即使只有一个因素验证失败,也可能意味着有人劫持了会话,您需要销毁它。
我建议您在http://www.hardened-php.net/suhosin/上查看Suhosin。 这是一个PHP插件,可以为您完成上述所有操作。 它是透明且轻便的。 使用Suhosin时,请注意通过javascript设置cookie,因为javascript设置了未加密的cookie,尽管服务器无法读取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.