从Basic Authentication认知cookie和session

HTTP Basic Authentication

查看wikipedia的解释,http://en.wikipedia.org/wiki/Basic_authentication_scheme

HTTP Basic Authentication即Basic access authentication

概念:In the context of an HTTP transaction, the basic access authentication is a method designed to allow a web browser, or other client program, to provide credentials – in the form of a user name and password – when making a request.

 

 在执行会话之前,用户名和密码username:password对作为一个字符串经过Base64算法加密编码,之后经Base64编码的字符串被发送到接收端,接收端服务器应用解码得到用户名和密码。

Base64加密算法很简单,只能防止直接查看得到用户名和密码,解码是十分容易的。因此,编码的初衷不是出于安全性的考虑,而是为了将用户名和密码中可能存在的一些不被HTTP兼容的字符(non-HTTP-compatible characters)编码为HTTP兼容的字符!

 

缺点:因为用户名和密码几乎相当于明文传输,所以存在严重的安全性问题。因此Basic Authentication基本用在可信任网络或进行测试时用!

digest access authentication被制定来更加安全的进行认证。

 

用户名和密码对 如guest:guest经Base64编码后得到Z3Vlc3Q6Z3Vlc3Q=,并被放入认证头Authorization中

形如Authorization: Basic Z3Vlc3Q6Z3Vlc3Q=

 

 

Example:typical transaction between an HTTP client and an HTTP server running on the local machine (localhost). 

  • The client asks for a page that requires authentication but does not provide a user name and password. Typically this is because the user simply entered the address or followed a link to the page.
  • The server responds with the 401 response code and provides the authentication realm.
  • At this point, the client will present the authentication realm (typically a description of the computer or system being accessed) to the user and prompt for a user name and password. The user may decide to cancel at this point.
  • Once a user name and password have been supplied, the client adds an authentication header (with value base64encode(username+":"+password)) to the original request and re-sends it.
  • In this example, the server accepts the authentication and the page is returned. If the user name is invalid or the password incorrect, the server might return the 401 response code and the client would prompt the user again.

Note: A client may pre-emptively send the authentication header in its first request, with no user interaction required.

方法:在http://url中间加入用户名和密码对,例如:http://guest:guest@www.baidu.com即可!

PS:要在发送请求的时候添加HTTP Basic Authentication认证信息到请求中,有两种方法:

 

 

攻击过程:

因为是tomcat服务器,因此cookie中为JSESSIONID,初始截获请求没有发现Authorization头,因此修改JSESSIONID发送给服务器,因为是错误的cookie,服务器返回401响应,并要求输入用户名和密码,再截获会发现Authorization头出现了,但是问题是并没有重新生成新的JSESSIONID!!——因为最开始存在的cookie是存在浏览器内存中和服务器端的,当服务器端接收到错误的JSESSIONID(亦即cookie)时,会返回401要求认证,这时我填入用户名和密码,浏览器自动加入内存已有的cookie(未修改前的)。

这时有了Authorization头,在截获请求时改变JSESSIONID,因为用户名密码是正确的,所以服务端接收后返回的是200 ok响应,但是会重新分配一个新的JSESSIONID给用户,此时JSESSIONID才会改变。

从这个过程不难看出,在已登录并正常通信后,客户端服务端的Authorization头是可有可无的,只要JSESSIONID不变!

 

认识到这一点,按部就班就可以达成第一步攻击!第二步只需要篡改请求中的JSESSIONID,并将Authorization头中的用户名和密码改变为需要登录的用户名密码对即可。或者清空浏览器中的cookie(JSESSIONID和Authorization头),再用上面所述在url中添加用户名和密码即可!!

 

 

以下转自:http://www.cnblogs.com/QLeelulu/archive/2009/11/22/1607898.html

 

访问需要HTTP Basic Authentication认证的资源的各种语言的实现

下面来看下对于第一种在请求中添加Authorization头部的各种语言的实现代码。

.NET下

你当然也可以使用HttpWebRequest或者其他的类来发送请求。

然后是Python的:

 

reference:http://en.wikipedia.org/wiki/Basic_authentication_scheme

http://topic.csdn.net/u/20100111/19/3da6591b-5090-4891-af14-c8ad94181767.html

http://www.cnblogs.com/QLeelulu/archive/2009/11/22/1607898.html

http://www.cnblogs.com/cmt/archive/2010/05/13/1733904.html

 

再来是PHP的:

还有flash的AS3的:

还有Ruby On Rails的:

JavaScript的:

PS:HTTP Basic Authentication对于跨域又要发送post请求的用JavaScript是实现不了的(注:对于Chrome插件这类允许通过AJAX访问跨域资源的,是可以的)。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值