PHP Apache NTLM身份验证备用

时间:2018-01-31 06:38:17

标签: php apache authentication ntlm

我需要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;
}

1 个答案:

答案 0 :(得分:1)

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

如果客户端请求多个资源,您可以使用HTTP Keep-Alive来保持连接打开,并且所有进一步的请求都应该已经过身份验证。

Apache %u field一样,您可以在日志中记录用户名,而只使用具有非空用户名字段的日志进行报告,而不是使用整个访问日志。