<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>程默的博客 &#187; GBK缺陷</title>
	<atom:link href="http://blog.chacuo.net/tag/gbk%e7%bc%ba%e9%99%b7/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.chacuo.net</link>
	<description>web原理、web架构、web安全、web性能、服务器性能、服务器架构、服务器安全;你不能预知明天，但你可以利用今天。你不能样样顺利，但你可以事事尽力!</description>
	<lastBuildDate>Mon, 31 Aug 2020 15:33:40 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>GBK字符编码（字符集）缺陷攻击（注入）原理</title>
		<link>http://blog.chacuo.net/312.html</link>
		<comments>http://blog.chacuo.net/312.html#comments</comments>
		<pubDate>Mon, 24 Jun 2013 09:38:08 +0000</pubDate>
		<dc:creator>程默</dc:creator>
				<category><![CDATA[web安全]]></category>
		<category><![CDATA[GBK缺陷]]></category>
		<category><![CDATA[字符编码]]></category>

		<guid isPermaLink="false">http://blog.chacuo.net/?p=312</guid>
		<description><![CDATA[上一节，我们分析了。选择不同编码可能会导致程序带来本身潜在的漏洞。这次我们以GBK编码为例，看看怎么样通过该编 [...]]]></description>
				<content:encoded><![CDATA[<p>上一节，我们分析了。选择不同编码可能会导致程序带来本身潜在的漏洞。这次我们以GBK编码为例，看看怎么样通过该编码注入到系统中。目前很多开源系统都存在类似的注入问题。我们先来，从一个Demo开始！</p>
<p><strong>GBK字符集漏洞注入原理</strong></p>
<blockquote>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$u</span><span style="color: #339933;">=</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'u'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'u'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$u</span><span style="color: #339933;">=</span><span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$u</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from user where user='<span style="color: #006699; font-weight: bold;">$u</span>'&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>以上是我们写的一个测试例子（GBK编码），现在很多开源系统，比较少的进行统一参数过滤，有时候为了防止注入，就直接对参数进行转义处理。我们看看，这样一个例子，我们怎么样注入进系统！</p>
</blockquote>
<blockquote>
<table class="table" border="1" rules="all" cellspacing="0" width="820" frame="box">
<thead>
<tr valign="top">
<th width="316">步骤</th>
<th width="617">备注</th>
</tr>
</thead>
<tbody>
<tr>
<td valign="top" width="316">1.传入值%D5%27 or 1=1#</td>
<td valign="top" width="617">u参数参入上面值 (%27 对应是“’”&#160; 单引号字符)</td>
</tr>
<tr>
<td valign="top" width="316">2.GET获取的值</td>
<td valign="top" width="617">0xD50x27 or 1=1# </td>
</tr>
<tr>
<td valign="top" width="316">3.Addslashes后值</td>
<td valign="top" width="617">0xD50x5C0x27 or 1=1 (意思是：誠’ or 1=1#’)&#160; #字符后面被注释掉</td>
</tr>
<tr>
<td valign="top" width="316">4.sql值 将变成</td>
<td valign="top" width="617">select * from user where user=’誠’ or 1=1#’</td>
</tr>
<tr>
<td valign="top" colspan="2">#号是sql注释符号，后面字符将截取掉</td>
</tr>
</tbody>
</table>
<pre>&#160;</pre>
<pre><a href="http://blog.chacuo.net/wp-content/uploads/2013/06/image44.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="GBK编码漏洞注入测试" border="0" alt="GBK编码漏洞注入测试" src="http://blog.chacuo.net/wp-content/uploads/2013/06/image_thumb44.png" width="821" height="280" /></a> </pre>
</blockquote>
<p>GPC转义打开，或者是通过addslashes函数，会自动在字符是单引号（<i>&#8216;</i>）、双引号（<i>&quot;</i>）、反斜线（<i>\</i>）与 NUL（<b><tt>NULL</tt></b> 字符）等字符前面增加“\”字符（0x5c)，例子里面，我们采用一个特殊前面字节0xD5，它将与该字节组合变成：0xD50x5c ，刚好是gbk字符集中字符：”誠“ 了。 后面的0&#215;27这个单引号被保留下来了！</p>
<p><strong>GBK字符集漏洞注入总结</strong></p>
<p>呵呵，这个很有意思吧，好了。我们来总结下，这类注入是2个条件的。<u><font color="#0000ff">第一是：gbk编码，第二是：程序采用了转义方法，转义了输入。</font></u> 这2个条件不苛刻，目前大部分开源系统都有gbk,utf-8编码的源码，剩下的就去看看，源码里面有没有用类似转义方法，过滤字符串了。如果有，那么这个系统某个功能，你可以去渗透下了。这个编码漏洞，网上面提的很多，不过很多时候，没有引起开发人员的足够重视，还是在不断的重现！</p>
<p>那么我们如果要注入一个参数，我们该选择什么样的入参参数呢？其实这种转义字符是单引号（<i>&#8216;</i>）、双引号（<i>&quot;</i>）、反斜线（<i>\</i>）与 NUL（<b><tt>NULL</tt></b> 字符），我们这些字符往往在程序中有特殊作用，我们只需要在前面加<strong><font color="#0000ff"><u>一个在&gt;7F字符，后面接一个%27（<i>&#8216;</i>）、%22（<i>&quot;</i>）、%5C（<i>\</i>）、%00（<tt>NULL</tt> 字符）</u></font></strong>，就可以自己让这4个字符，可以逃脱转义了。</p>
<p>好了，这个漏洞原理及注入过程分析就这些了。我们开发时候，需要注意这个问题，特别是使用GBK编码开发程序，要有这个方面的预备知识，对于自己开发安全的代码会有帮助的。更多的<a title="GBK字符集" href="http://doc.chacuo.net/gbk" rel="GBK字符集" target="_blank">GBK编码</a>，可以看<a href="http://doc.chacuo.net/gbk">http://doc.chacuo.net/gbk</a> ！（这里有很多落在5c中文字符呢）也欢迎讨论！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chacuo.net/312.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GBK字符编码（字符集）缺陷导致web安全漏洞</title>
		<link>http://blog.chacuo.net/309.html</link>
		<comments>http://blog.chacuo.net/309.html#comments</comments>
		<pubDate>Fri, 21 Jun 2013 09:21:34 +0000</pubDate>
		<dc:creator>程默</dc:creator>
				<category><![CDATA[web原理]]></category>
		<category><![CDATA[web安全]]></category>
		<category><![CDATA[GBK缺陷]]></category>
		<category><![CDATA[字符编码]]></category>

		<guid isPermaLink="false">http://blog.chacuo.net/?p=309</guid>
		<description><![CDATA[很多时候，一个web站点，选择什么样的字符编码，我们不会太过在意的。象中文网站，我们一般用gb2312,gbk [...]]]></description>
				<content:encoded><![CDATA[<p>很多时候，一个web站点，选择什么样的字符编码，我们不会太过在意的。象中文网站，我们一般用gb2312,gbk,gb18030,也可以用utf-8。但是，可能我们不知道，选择不同编码，可能因此导致程序本身设计缺陷。</p>
<p><strong>多字节编码由来</strong></p>
<p>我们先来看看最常用的，最小字符集是ascii，对应的二级制可以表示为：00-7F 编码 。它也是我们计算机使用最早通用的字符集。前期几乎可以表示所有英文字符。后来，更多使用计算机国家加入后，我们就想在计算机中表示中文字符。我们知道常见中文就有7000多个字符。ascii码就只有128字符，只有0-127编码位置，远远不够用了。因此，我们就开始制作更大字符集，并且保证兼容ascii编码。要支持更多字符，选择更大字符集。我们只能用多个字节来描述一个字符了。为了很好的与ascii码，区分开来！一般做法是：每个字节值都大于&gt;7F，如果是2个字节，那么就是：[&gt;7F][&gt;7F]。这样编码，保证很好的与ascii区分开，并且扩大了字符集。像gb2312范围在[0xA1-0xF7][0xA1-0xFE](中间很多没有填满)，它完全保证所有字节在A0之上，也就完全满足在7F之上了。</p>
<p><strong>GBK编码漏洞缘由</strong></p>
<p>通过上面的分析，我们知道gb2312编码是很好的跟ascii码分开了。 那么我们看看，GBK编码呢，它是完全兼容gb2312（就是说在gb2312字符集中每个字符位置，与gbk字符集里面位置完全一致，而且包含于gb2312)，但是，它有2万多个字符。从上面看，只能选择往下排序了。 就是从A1A0往下排了，我们发现它编码实际范围是：[0x81-0xFE]([0x40-0x7E|0x80-0xFE] ) （<a title="gbk字符集" href="http://doc.chacuo.net/gbk" rel="gbk字符集" target="_blank">GBK编码</a>），我们发现由2个字节组成，首字节范围在7F之上，而第2个字节，有一部分在0&#215;40-0x7E了。这就是导致bug原因。我们看看下面例子吧！</p>
<blockquote><p><font style="background-color: #ffffff" face="Arial">从ASCII码表中，我们知道0&#215;40-0x7E 包含字符有：“<a href="mailto:&ldquo;A-Za-z@[\">A-Za-z@[\</a>]^_`{|}~”，一共有63字符呢。</font></p>
<pre><a href="http://blog.chacuo.net/wp-content/uploads/2013/06/image43.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="GBK编码漏洞" border="0" alt="GBK编码漏洞" src="http://blog.chacuo.net/wp-content/uploads/2013/06/image_thumb43.png" width="574" height="251" /></a> </pre>
<p>选择gbk编码，运行上面代码，就一条简单的命令导致出现错误，说字符串 赋值 没有结束！ 呵呵，估计很多人看到这个就会认为是php 出Bug了。但是，如果我们变成$a=”誠a”，发现可以正常运行了。是不是觉得很奇葩啦！！</p>
<p>&#160;</p>
</blockquote>
<p>原因分析:我们知道文件存在磁盘都是二级制方式，无论你存什么字符，最终都是以<strong>该字符的在所选字符集中字符编码保存</strong>。php解析时候，最小分析单元是字节。无论你是多字节还是单字节字符。最终都是按照字节来处理的。<font color="#0000ff"><u>“誠”&#160; GBK编码是 D55C，php按字节来解释，5C对应字符是“\” 字符。后面直接跟个‘”’，相当于被转义了。 因为没有闭合，因此出现错误！</u>。</font><font color="#000000">大家看出问题所在了吧，按自己处理的话，会自然把多字节拆成单字节了。这样就会出现很多奇怪问题了。 </font></p>
<p>总结：通过上面讲解，我们知道了多字节编码过程，以及GBK导致简单程序出错的原因。其实，我们很多程序语言里面，都会以单个字节来解析的。这样，当你选择多字节GBK编码中文时，刚好有字节落在特殊位置，将会出现奇怪错误问题。而且，还将给系统带来本身的漏洞，后面我再说说，GBK编码缺陷，导致漏洞、以及专门利用该编码漏洞缺陷进行系统入侵！好了，先到这里了，欢迎交流！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chacuo.net/309.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
