使用cookie强制phpMyAdmin登录


Forcing login on phpMyAdmin using cookies

StackExchange用户你们好。今天的问题比平时要复杂一些。我正在开发一个基于网络的托管面板的服务器,我的公司刚刚成立,他们想从互联网管理网页。控制面板的其余部分工作得很好,但这个第三方应用程序真的很困扰我,这个应用程序是phpMyAdmin!

我希望用户能够登录到他们的网站的"cPanel"(注意:我们不使用cPanel或任何以前的软件),只需点击phpMyAdmin链接,并已登录到自己的特定数据库。当他们登录时,我使用与phpMyAdmin相同的加密类型将他们的sql数据库帐户详细信息传递到cookie中。查看cookie显示cookie格式正确,他们应该登录到系统

问题是,当他们点击链接去phpMyAdmin页面,他们仍然得到登录表单,即使cookie已经设置,似乎是正确的。系统中没有错误信息,甚至没有请重新登录的信息。

我已经包含了我的代码为我的CookieAuth类下面,所以你们可以看看它。这真的很令人沮丧,因为cookie似乎是我可以正确登录它们的唯一方法,而且没有很多关于如何在其他地方做到这一点的文档示例。

class CookieAuth {
    private $_cookie_iv = null;
    public function createPMAsession($username, $password, $blowfish_key) {
        setCookie('pmaUser-1', $this->cookieEncrypt($username, $blowfish_key), time()+3600, "/phpmyadmin/");
        setCookie('pmaAuth-1', $this->cookieEncrypt($password, $blowfish_key), null, "/phpmyadmin/");
    }
    private function _useOpenSSL() {
        return (function_exists('openssl_encrypt') && function_exists('openssl_decrypt') && function_exists('openssl_random_pseudo_bytes') && PHP_VERSION_ID >= 50304);
    }
    public function enlargeKey($key) {
        while(strlen($key) < 16) {
            $key .= $key;
        }
        return substr($key, 0, 16);
    }
    public function getMAC($key) {
        $length = strlen($key);
        if($length > 16) {
            return substr($key, 0, 16);
        }
        return $this->enlargeKey($length == 1 ? $key : substr($key, 0, -1));
    }
    public function getAES($key) {
        $length = strlen($key);
        if($length > 16) {
            return substr($key, 0, 16);
        }
        return $this->enlargeKey($length == 1 ? $key : substr($key, 0, -1));
    }
    public function cookieEncrypt($data, $key) {
        $mac = $this->getMAC($key);
        $aes = $this->getAES($key);
        $iv = $this->createIV();
        if($this->_useOpenSSL()) {
        $result = openssl_encrypt(
                $data,
                'AES-128-CBC',
                $key,
                0,
                $iv
            );
        } else {
            $cipher = new Crypt'AES(Crypt'Base::MODE_CBC);
            $cipher->setIV($iv);
            $cipher->setKey($aes);
            $result = base64_encode($cipher->encrypt($data));
        }
        $iv = base64_encode($iv);
        return json_encode(
            array(
                'iv' => $iv,
                'mac' => hash_hmac('sha1', $iv . $result, $mac),
                'payload' => $result,
            )
        );
    }
    public function getIVSize() {
        if($this->_useOpenSSL()) {
            return openssl_cipher_iv_length('AES-128-CBC');
        }
        $cipher = new Crypt'AES(Crypt'Base::MODE_CBC);
        return $cipher->block_size;
    }
    public function createIV() {
        if(!is_null($this->_cookie_iv)) {
            return $this->_cookie_iv;
        }
        if($this->_useOpenSSL()) {
            return openssl_random_pseudo_bytes($this->getIVSize());
        } else {
            return Crypt'Random::string($this->getIVSize());
        }
    }
    public function setIV($vector) {
        $this->_cookie_iv = $vector;
    }
}

感谢您花时间阅读我的问题,我希望有人能给我指出正确的方向。

这听起来正是auth_type登录的设计目的。你不用这个有什么特别的原因吗?

如果做不到这一点,文档显示了如何将用户名和密码作为查询字符串的一部分传递,从安全的角度来看,这可能不是理想的,但可能为您提供了一个进行修改的起点。

你试过直接张贴正确的用户名和密码到index.php吗?由于安全令牌的原因,我不确定这是否会起作用,但值得一试。

我现在真的觉得自己很笨。使用登录会话,你必须在配置中为phpMyAdmin声明会话变量。我显然没有意识到这一点,在更改这个会话变量后,我能够单点登录用户。

如果您想使用登录认证模式使用sign .php,如examples文件夹中所述,不要忘记更改config.inc.php

中的auth_type。

改变
$cfg['Servers'][$i]['auth_type'] = 'cookie';

$cfg['Servers'][$i]['auth_type'] = 'signon';

同时添加以下

$cfg['Servers'][$i]['SignonSession'] = 'SignonSession';
$cfg['Servers'][$i]['SignonURL']     = 'examples/signon.php';