繁体   English   中英

PHP Apache NTLM身份验证替代

[英]PHP Apache NTLM Authentication Alternate

我需要NTLM身份验证才能获取Windows用户名,该用户名与我当前的功能配合正常。

我所面对的唯一问题是,它在同一页面上命中三次 ,这使我的访问日志难以管理(在流量图中弹出),因此在向他们解释相同内容之前,我想确定是否还有其他方法可以获取Windows会话数据(用户名)。

以下是当前代码。

function getSysId() {
    $headers = apache_request_headers();
    if (!isset($headers['Authorization'])) {
        header('HTTP/1.1 401 Unauthorized');
        header('WWW-Authenticate: NTLM');
        exit;
    }
    $auth = $headers['Authorization'];
    if (substr($auth, 0, 5) == 'NTLM ') {
        $msg = base64_decode(substr($auth, 5));
        if (substr($msg, 0, 8) != "NTLMSSP\x00")
            return '';

        if ($msg[8] == "\x01") {
            $msg2 = "NTLMSSP\x00\x02\x00\x00\x00" .
                    "\x00\x00\x00\x00" . // target name len/alloc
                    "\x00\x00\x00\x00" . // target name offset
                    "\x01\x02\x81\x00" . // flags
                    "\x00\x00\x00\x00\x00\x00\x00\x00" . // challenge
                    "\x00\x00\x00\x00\x00\x00\x00\x00" . // context
                    "\x00\x00\x00\x00\x00\x00\x00\x00"; // target info len/alloc/offset

            header('HTTP/1.1 401 Unauthorized');
            header('WWW-Authenticate: NTLM ' . trim(base64_encode($msg2)));
            exit;
        } else if ($msg[8] == "\x03") {

            function get_msg_str1($msg, $start, $unicode = true) {
                $len = (ord($msg[$start + 1]) * 256) + ord($msg[$start]);
                $off = (ord($msg[$start + 5]) * 256) + ord($msg[$start + 4]);
                if ($unicode)
                    return str_replace("\0", '', substr($msg, $off, $len));
                else
                    return substr($msg, $off, $len);
            }

            $user = get_msg_str1($msg, 36);

            return $user;
        }
    }
    return false;
}

NTLM协议需要两个请求来认证HTTP客户端 这意味着您将至少获得2个请求。 如果还有一个请求,那是因为客户端首先请求了没有任何身份验证标头的资源,这是可以接受的行为。

如果客户端请求多个资源,则可以使用HTTP Keep-Alive来保持连接打开状态,并且所有后续请求都应该已通过身份验证。

与使用Apache%u字段一样,您可以不使用整个访问日志,而是将用户名记录在日志中,而仅使用用户名字段不为空的日志进行报告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM