<?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>sql注入 &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/sql%E6%B3%A8%E5%85%A5/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Wed, 10 Apr 2013 05:56:03 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>
	<item>
		<title>PHP针对Xss跨域攻击以及sql注入等危险字符串过滤安全模块</title>
		<link>https://www.mudbest.com/php%e8%bf%87%e6%bb%a4xss%e8%b7%a8%e5%9f%9f%e6%94%bb%e5%87%bb%e4%bb%a5%e5%8f%8asql%e7%ad%89%e5%8d%b1%e9%99%a9%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bf%87%e6%bb%a4%e5%ae%89%e5%85%a8%e6%a8%a1%e5%9d%97/</link>
					<comments>https://www.mudbest.com/php%e8%bf%87%e6%bb%a4xss%e8%b7%a8%e5%9f%9f%e6%94%bb%e5%87%bb%e4%bb%a5%e5%8f%8asql%e7%ad%89%e5%8d%b1%e9%99%a9%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bf%87%e6%bb%a4%e5%ae%89%e5%85%a8%e6%a8%a1%e5%9d%97/#comments</comments>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Wed, 10 Apr 2013 03:58:34 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[PHP安全模块]]></category>
		<category><![CDATA[sql注入]]></category>
		<category><![CDATA[Xss跨站攻击]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1855</guid>

					<description><![CDATA[由于该模块在项目中的要求是 不能提示任何信息，也不作断点操作，只作记录并且过滤危险参数。 主要功能：拦截攻击者 ... <a title="PHP针对Xss跨域攻击以及sql注入等危险字符串过滤安全模块" class="read-more" href="https://www.mudbest.com/php%e8%bf%87%e6%bb%a4xss%e8%b7%a8%e5%9f%9f%e6%94%bb%e5%87%bb%e4%bb%a5%e5%8f%8asql%e7%ad%89%e5%8d%b1%e9%99%a9%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bf%87%e6%bb%a4%e5%ae%89%e5%85%a8%e6%a8%a1%e5%9d%97/" aria-label="阅读 PHP针对Xss跨域攻击以及sql注入等危险字符串过滤安全模块">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>由于该模块在项目中的要求是 不能提示任何信息，也不作断点操作，只作记录并且过滤危险参数。</p>
<p>主要功能：拦截攻击者注入恶意代码，可以防御诸如跨站脚本攻击（XSS）、SQL注入攻击等恶意攻击行为。<br />
<span id="more-1855"></span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * 安全模块
 * Email:zhangyuan@tieyou.com
 * 主要针对xss跨站攻击、sql注入等敏感字符串进行过滤
 * @author hkshadow
 */
class safeMode{
	
	/**
	 * 执行过滤
	 * @param 1 linux/2 http/3 Db/ $group
	 * @param 保存路径以及文件名/文件名/null $projectName
	 */
	public function xss($group = 1,$projectName = NULL){
		//正则条件
		$referer = empty ( $_SERVER &#x5B;'HTTP_REFERER'] ) ? array () : array ($_SERVER &#x5B;'HTTP_REFERER'] );
		$getfilter = &quot;'|&lt;&#x5B;^&gt;]*?&gt;|^\\+\/v(8|9)|\\b(and|or)\\b.+?(&gt;|&lt;|=|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|&lt;\\s*script\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)&quot;;
		$postfilter = &quot;^\\+\/v(8|9)|\\b(and|or)\\b.{1,6}?(=|&gt;|&lt;|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|&lt;\\s*script\\b|&lt;\\s*img\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)&quot;;
		$cookiefilter = &quot;\\b(and|or)\\b.{1,6}?(=|&gt;|&lt;|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|&lt;\\s*script\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)&quot;;

		// $ArrPGC=array_merge($_GET,$_POST,$_COOKIE);

		//遍历过滤
		foreach ( $_GET as $key =&gt; $value ) {
			$this-&gt;stopAttack ( $key, $value, $getfilter ,$group , $projectName);
		}
		//遍历过滤
		foreach ( $_POST as $key =&gt; $value ) {
			$this-&gt;stopAttack ( $key, $value, $postfilter ,$group , $projectName);
		}
		//遍历过滤
		foreach ( $_COOKIE as $key =&gt; $value ) {
			$this-&gt;stopAttack ( $key, $value, $cookiefilter ,$group , $projectName);
		}
		//遍历过滤
		foreach ( $referer as $key =&gt; $value ) {
			$this-&gt;stopAttack ( $key, $value, $getfilter ,$group , $projectName);
		}
	}
	
	/**
	 * 匹配敏感字符串，并处理
	 * @param 参数key $strFiltKey
	 * @param 参数value $strFiltValue
	 * @param 正则条件 $arrFiltReq
	 * @param 项目名 $joinName
	 * @param 1 linux/2 http/3 Db/ $group
	 * @param 项目名/文件名/null $projectName
	 */
	public function stopAttack($strFiltKey, $strFiltValue, $arrFiltReq,$group = 1,$projectName = NULL) {
		
			$strFiltValue = $this-&gt;arr_foreach ( $strFiltValue );
			//匹配参数值是否合法
			if (preg_match ( &quot;/&quot; . $arrFiltReq . &quot;/is&quot;, $strFiltValue ) == 1) {
				//记录ip
				$ip = &quot;操作IP: &quot;.$_SERVER&#x5B;&quot;REMOTE_ADDR&quot;];
				//记录操作时间
				$time = &quot; 操作时间: &quot;.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;);
				//记录详细页面带参数
				$thePage = &quot; 操作页面: &quot;.$this-&gt;request_uri();
				//记录提交方式
				$type = &quot; 提交方式: &quot;.$_SERVER&#x5B;&quot;REQUEST_METHOD&quot;];
				//记录提交参数
				$key = &quot; 提交参数: &quot;.$strFiltKey;
				//记录参数
				$value = &quot; 提交数据: &quot;.htmlspecialchars($strFiltValue);
				//写入日志
				$strWord = $ip.$time.$thePage.$type.$key.$value;
				//保存为linux类型
				if($group == 1){
					$this-&gt;log_result_common($strWord,$projectName);
				}
				//保存为可web浏览
				if($group == 2){
					$strWord .= &quot;&lt;br&gt;&quot;;
					$this-&gt;slog($strWord,$projectName);
				}
				//保存至数据库
				if($group == 3){
					$this-&gt;sDb($strWord);	
				}
				//过滤参数
				$_REQUEST&#x5B;$strFiltKey] = '';
				//这里不作退出处理
				//exit;
			}
			
			//匹配参数是否合法
			if (preg_match ( &quot;/&quot; . $arrFiltReq . &quot;/is&quot;, $strFiltKey ) == 1) {
				//记录ip
				$ip = &quot;操作IP: &quot;.$_SERVER&#x5B;&quot;REMOTE_ADDR&quot;];
				//记录操作时间
				$time = &quot; 操作时间: &quot;.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;);
				//记录详细页面带参数
				$thePage = &quot; 操作页面: &quot;.$this-&gt;request_uri();
				//记录提交方式
				$type = &quot; 提交方式: &quot;.$_SERVER&#x5B;&quot;REQUEST_METHOD&quot;];
				//记录提交参数
				$key = &quot; 提交参数: &quot;.$strFiltKey;
				//记录参数
				$value = &quot; 提交数据: &quot;.htmlspecialchars($strFiltValue);
				//写入日志
				$strWord = $ip.$time.$thePage.$type.$key.$value;
				//保存为linux类型
				if($group == 1){
					$this-&gt;log_result_common($strWord,$projectName);
				}
				//保存为可web浏览
				if($group == 2){
					$strWord .= &quot;&lt;br&gt;&quot;;
					$this-&gt;slog($strWord,$projectName);
				}
				//保存至数据库
				if($group == 3){
					$this-&gt;sDb($strWord);	
				}
				//过滤参数
				$_REQUEST&#x5B;$strFiltKey] = '';
				//这里不作退出处理
				//exit;
			}
		}
	
	/**
	 * 获取当前url带具体参数
	 * @return string
	 */
	public function request_uri() {
		if (isset ( $_SERVER &#x5B;'REQUEST_URI'] )) {
			$uri = $_SERVER &#x5B;'REQUEST_URI'];
		} else {
			if (isset ( $_SERVER &#x5B;'argv'] )) {
				$uri = $_SERVER &#x5B;'PHP_SELF'] . '?' . $_SERVER &#x5B;'argv'] &#x5B;0];
			} else {
				$uri = $_SERVER &#x5B;'PHP_SELF'] . '?' . $_SERVER &#x5B;'QUERY_STRING'];
			}
		}
		return $uri;
	}
	

	/**
	 * 日志记录(linux模式)
	 * @param 保存内容 $strWord
	 * @param 保存文件名$strPathName
	 */
	public function log_result_common($strWord, $strPathName = NULL) {
		if($strPathName == NULL){
			$strPath = &quot;/var/tmp/&quot;;
			$strDay = date('Y-m-d');
			$strPathName = $strPath.&quot;common_log_&quot;.$strDay.'.log';
		}
	
		$fp = fopen($strPathName,&quot;a&quot;);
		flock($fp, LOCK_EX) ;
		fwrite($fp,$strWord.&quot; date &quot;.date('Y-m-d H:i:s',time()).&quot;\t\n&quot;);
		flock($fp, LOCK_UN);
		fclose($fp);
	}	
	
	/**
	 * 写入日志(支持http查看)
	 * @param 日志内容 $strWord
	 * @param web页面文件名 $fileName
	 */
	public function slog($strWord,$fileName = NULL) {
		if($fileName == NULL){
			$toppath = $_SERVER &#x5B;&quot;DOCUMENT_ROOT&quot;] . &quot;/log.htm&quot;;
		}else{
			$toppath = $_SERVER &#x5B;&quot;DOCUMENT_ROOT&quot;] .'/'. $fileName;
		}
		$Ts = fopen ( $toppath, &quot;a+&quot; );
		fputs ( $Ts, $strWord . &quot;\r\n&quot; );
		fclose ( $Ts );
	}
	
	/**
	 * 写入日志(数据库)
	 * @param 日志内容 $strWord
	 */
	public function sDb($strWord){
		//....
	}
	
	/**
	 * 递归数组
	 * @param array $arr
	 * @return unknown|string
	 */
	public function arr_foreach($arr) {
		static $str = '';
		if (! is_array ( $arr )) {
			return $arr;
		}
		foreach ( $arr as $key =&gt; $val ) {
			if (is_array ( $val )) {
				$this-&gt;arr_foreach ( $val );
			} else {
				$str &#x5B;] = $val;
			}
		}
		return implode ( $str );
	}
}
?&gt;
</pre>
<p>使用：</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//实例类库
$safeMode = new safeMode();
//这里的参数指的的时 类型，保存的文件名，具体请看类注视。
$safeMode-&gt;xss(2,'test.html');
?&gt;
</pre>
<p>至此基本上，参数带有危险的字符串的时候，将其过滤，并且保存日志到网站根目录的test.html内，可在浏览器预览查看。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mudbest.com/php%e8%bf%87%e6%bb%a4xss%e8%b7%a8%e5%9f%9f%e6%94%bb%e5%87%bb%e4%bb%a5%e5%8f%8asql%e7%ad%89%e5%8d%b1%e9%99%a9%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bf%87%e6%bb%a4%e5%ae%89%e5%85%a8%e6%a8%a1%e5%9d%97/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
