簡體   English   中英

會話和具有php角色的cookie

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM