PHP安全编程:HTTP请求欺骗

一个比欺骗表单更高级和复杂的攻击方式是HTTP请求欺骗。这给了攻击者彻底的控制权与灵活性,它进一步证实了不能盲目信任用户提交的任何数据。php

为了演示这是如何进行的,请看下面位于http://example.org/form.php的表单:html

1 <form action="process.php" method="POST">
2   <p>Please select a color:
3   <select name="color">
4     <option value="red">Red</option>
5     <option value="green">Green</option>
6     <option value="blue">Blue</option>
7   </select><br />
8   <input type="submit" value="Select" /></p>
9 </form>

若是用户选择了Red并点击了Select按钮后,浏览器会发出下面的HTTP请求:web

1 POST /process.php HTTP/1.1
2 Host: example.org
3 User-Agent: Mozilla/5.0 (X11; U; Linux i686)
4 Referer: http://example.org/form.php
5 Content-Type: application/x-www-form-urlencoded
6 Content-Length: 9
7  
8 color=red

看到大多数浏览器会包含一个来源的URL值,你可能会试图使用$_SERVER['HTTP_REFERER']变量去防止欺骗。确实,这能够用于对付利用标准浏览器发起的攻击,但攻击者是不会被这个小麻烦给挡住的。经过编辑HTTP请求的原始信息,攻击者能够彻底控制HTTP头部的值,GET和POST的数据,以及全部在HTTP请求的内容。浏览器

攻击者如何更改原始的HTTP请求?过程很是简单。经过在大多数系统平台上都提供的Telnet实用程序,你就能够经过链接网站服务器的侦听端口(典型的端口为80)来与Web服务器直接通讯。下面就是使用这个技巧请求http://example.org/页面的例子:安全

01 $ telnet example.org 80
02 Trying 192.0.34.166...
03 Connected to example.org (192.0.34.166).
04 Escape character is '^]'.
05 GET / HTTP/1.1
06 Host: example.org
07  
08 HTTP/1.1 200 OK
09 Date: Sat, 21 May 2005 12:34:56 GMT
10 Server: Apache/1.3.31 (Unix)
11 Accept-Ranges: bytes
12 Content-Length: 410
13 Connection: close
14 Content-Type: text/html
15  
16 <html>
17 <head>
18 <title>Example Web Page</title>
19 </head>
20 <body>
21 <p>You have reached this web page by typing "example.com",
22 "example.net"or "example.org" into your web browser.</p>
23 <p>These domain names are reserved for use in documentation and are not
24 available for registration. See
25 <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 2606</a>, Section
26 3.</p>
27 </body>
28 </html>
29  
30 Connection closed by foreign host.
31 $

上例中所显示的请求是符合HTTP/1.1规范的最简单的请求,这是由于Host信息是头部信息中所必须有的。一旦你输入了表示请求结束的连续两个换行符,整个HTML的回应即显示在屏幕上。服务器

Telnet实用程序不是与Web服务器直接通讯的惟一方法,但它经常是最方便的。但是若是你用PHP编码一样的请求,你能够就能够实现自动操做了。前面的请求能够用下面的PHP代码实现:app

01 <?php
02   
03   $http_response '';
04   
05   $fp fsockopen('example.org', 80);
06   fputs($fp"GET / HTTP/1.1\r\n");
07   fputs($fp"Host: example.org\r\n\r\n");
08   
09   while (!feof($fp))
10   {
11     $http_response .= fgets($fp, 128);
12   }
13   
14   fclose($fp);
15   
16   echo nl2br(htmlentities($http_response, ENT_QUOTES, 'UTF-8'));
17   
18 ?>

固然,还有不少方法去达到上面的目的,但其要点是HTTP是一个广为人知的标准协议,稍有经验的攻击者都会对它很是熟悉,而且对常见的安全漏洞的攻击方法也很熟悉。dom

相对于欺骗表单,欺骗HTTP请求的作法并很少,对它不该该过多关注。我讲述这些技巧的缘由是为了更好的演示一个攻击者在向你的应用输入恶意信息时是如何地方便。这再次强调了过滤输入的重要性和HTTP请求提供的任何信息都是不可信的这个事实。网站

相关文章
相关标签/搜索