簡體   English   中英

安全的LDAP PHP腳本?

[英]Secure LDAP PHP Script?

我想知道我的腳本是否安全。 特別是將序列化的對象存儲在會話變量中。

index.php是受保護站點的登錄名(我省略了html部分)。 受保護的站點從LDAP中讀取並打印信息。 為了從用戶條目讀取LDAP信息,必須將用戶與LDAP綁定,否則ldap_read將返回錯誤。 沒有用戶綁定main.php是我錯誤的第一次嘗試。

如果登錄成功,則另一種可能性是將main.php包含在index.php中。 但是我不想這樣。

所以我以這種方式嘗試了一下,並且可以正常工作,但是我不確定它的安全性。 特別是會話變量中的對象。

# index.php
<?php
    session_start();
    include_once("LDAP.php");
    if (isset($_POST) && isset($_POST['loginName']) && isset($_POST['loginPassword'])){        
        $ActiveDirectoryUser = new ActiveDirectoryUser($_POST['loginName'], $_POST['loginPassword']);
        if ($ActiveDirectoryUser->connect()) {                
            $_SESSION['unique_string'] = true;
            $_SESSION['unique_string_Obj'] = serialize($ActiveDirectoryUser);
            header('Location: main.php');
        }
    }
?> 

# LDAP.php
<?php
/**
 * Always have documentation here
 */
class ActiveDirectoryUser {
    private $connection;
    private $username;
    private $password;
    private $ldap_db = "Server Adress";
    private $ldap_connection;

    /**
     * Always have documentation 
     * @param $username string
     * @param $password string
     */
    public function __construct($username, $password) {
      $this->username = "uid=".$username.",ou=users,dc="company_name",dc=de";
      $this->password = $password;
    }

    /**
     * Always have documentation
     */
    public function __destruct(){
        ldap_close($this->ldap_connection);
    }

    /**
     * Always have documentation 
     * @param $username string
     * @param $password string
     */

    public function connect() {
        $this->ldap_connection = ldap_connect($this->ldap_db);

        if ($bind = ldap_bind($this->ldap_connection, $this->username, $this->password)) {
            return True;
        } else {
            return False;
        }
    }

    /**
     * Always have documentation
     */
    public function getInfos(){
        ldap_set_option($this->ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($this->ldap_connection, LDAP_OPT_REFERRALS, 0);

        $bind = ldap_bind($this->ldap_connection, $this->username, $this->password);

        $filter = "(objectclass=*)";

        $justthese = array("sn", "givenname", "mail");

        $sr = ldap_read($this->ldap_connection, $this->username, $filter, $justthese);

        $info = ldap_get_entries($this->ldap_connection, $sr);

        return $info;
    }
}
?>

# main.php
<?php
    session_start();
    include_once("LDAP.php");
    $ActiveDirectoryUser = unserialize($_SESSION["unique_string_Obj"]);            
    if($ActiveDirectoryUser->connect()) {                
        $entry = $ActiveDirectoryUser->getInfos();
    }
?>

我是否正確地看到在會話中存儲了序列化的用戶對象? 哪個包含用戶明文密碼? 至少在我看來,這是非常不安全的。

我希望能找到一種方法,以免在以后的通話中使用密碼,但是由於我不知道您的確切環境,因此我不願意這樣做。 下一步將是只存儲加密的密碼,並在需要時將其解密。 但是最好不要干!

您看過fe Gosa或PHPLdapAdmin的源代碼嗎? 他們有類似的問題要解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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