一. Basic 认证php
客户端以“ : ”链接用户名和密码后,再经BASE64编码( Base64 Content-Transfer-Encoding )经过Authorization请求头发送该密文至服务端进行验证,每次请求都须要重复发送该密文。可见Basic认证过程简单,安全性也低,存在泄露我的帐号信息以及其余诸多安全问题,最好在实现了Transport Layer Security (TLS)的状况下去使用。如下仅为原理演示,不表明真实状况:html
GET / HTTP/1.1
Host: www.myrealm.com算法
HTTP/1.1 401 Unauthorised
Server: bfe/1.0.8.18
WWW-Authenticate: Basic realm="myrealm.com"
Content-Type: text/html; charset=utf-8数据库
GET / HTTP/1.1
Host: www.myrealm.com
Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxx编程
二. Digest 认证缓存
Digest认证试图解决Basic认证的诸多缺陷而设计,用户密码在整个认证过程当中是个关键性要素。安全
下为服务端发送的Digest认证响应头部实例及各指令含义说明:(若是你作PHP开发,其官方文档中发送WWW-Authenticate头部时可能各指令之间用了空格,在Chrome下是不会弹出认证对话框的,应该换成”, “或”,“)服务器
WWW-Authenticate: Digest realm="Restricted area", qop="auth,auth-int", nonce="58e8e52922398", opaque="cdce8a5c95a1427d74df7acbf41c9ce0", algorithm="MD5"
nonce:服务端产生的随机数,用于增长摘要生成的复杂性,从而增长破解密码的难度,防范“中间人”与“恶意服务器”等攻击类型,这是相对于不使用该指令而言的;另外,nonce自己可用于防止重放攻击,用于实现服务端对客户端的认证。RFC 2617 建议采用这个随机数计算公式:nonce = BASE64(time-stamp MD5(time-stamp “:” ETag “:” private-key)),服务端能够决定这种nonce时间有效性,ETag(URL对应的资源Entity Tag,在CGI编程中一般须要自行生成ETag和鉴别,可用于鉴别URL对应的资源是否改变,区分不一样语言、Session、Cookie等)能够防止对已更新资源版本(未更新无效,故须要设定nonce有效期)的重放请求,private-key为服务端私有keydom
下为客户端发送的Digest认证头请求部实例及各指令含义说明:ui
Authorization: Digest username="somename", realm="Restricted area", nonce="58e8e52922398", uri="/t.php", response="9c839dde909d270bc5b901c7f80f77d5", opaque="cdce8a5c95a1427d74df7acbf41c9ce0", qop="auth", nc=00000001, cnonce="9c30405c3a67a259"
The countermeasure against this attack(选择明文攻击) is for clients to be configured to require the use of the optional "cnonce" directive;this allows the client to vary the input to the hash in a way not chosen by the attacker.
下面将就摘要计算方法进行说明:
H(data) = MD5(data) KD(secret, data) = H(concat(secret, ":", data))
a) 采用MD5算法: A1=(user):(realm):(password) b) 采用MD5-sess算法: A1=H((user):(realm):(password)):nonce:cnonce
a) QoP为auth或未定义: A2=(request-method):(uri-directive-value) b) QoP为auth-int: A2=(request-method):(uri-directive-value):H((entity-body))
a) 若qop没有定义: response = KD(H(A1),<nonce>:H(A2)) = H(H(A1),<nonce>:H(A2)) b) 若qop为auth或auth-int: response = KD(H(A1),<nonce>:<nc>:<cnonce>:<qop>:H(A2)) = H(H(A1),<nonce>:<nc>:<cnonce>:<qop>:H(A2))
三. 安全风险
假冒服务器欺骗(Spoofing by Counterfeit Servers):对于Basic认证,这种攻击方式更容易奏效,对于Digest认证则更难,但前提是客户端必须知道将要使用的是Digest认证。用户在所使用的认证机制中如何发现这一潜在攻击样式应该获得可见的帮助