点击星标,即时接收最新推文
本文选自《web安全攻防渗透测试实战指南(第2版)》
SQL注入漏洞修复建议
常用的SQL注入漏洞的修复方法有两种。
多数CMS都采用过滤危险字符的方式,例如,用正则表达式匹配union、sleep、load_file等关键字。如果匹配到,则退出程序。例如,80sec的防注入代码如下:
- functionCheckSql($db_string,$querytype='select')
- {
- global$cfg_cookie_encode;
- $clean='';
- $error='';
- $old_pos= 0;
- $pos= -1;
- $log_file= DEDEINC.'/../data/'.md5($cfg_cookie_encode).'_safe.txt';
- $userIP= GetIP();
- $getUrl= GetCurUrl();
- //如果是普通查询语句,则直接过滤一些特殊语法
- if($querytype=='select')
- {
- $notallow1="[^0-9a-z@\._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@\.-]{1,}";
- //$notallow2 = "--|/\*";
- if(preg_match("/".$notallow1."/i",$db_string))
- {
- fputs(fopen($log_file,'a+'),"$userIP||$getUrl||$db_string||SelectBreak\r\n");
- exit("Safe Alert: Request Error step 1 !");
- }
- }
- //完整的SQL检查
- while(TRUE)
- {
- $pos=strpos($db_string,'\'',$pos+ 1);
- if($pos=== FALSE)
- {
- break;
- }
- $clean.=substr($db_string,$old_pos,$pos-$old_pos);
- while(TRUE)
- {
- $pos1=strpos($db_string,'\'',$pos+ 1);
- $pos2=strpos($db_string,'\\',$pos+ 1);
- if($pos1=== FALSE)
- {
- break;
- }
- elseif($pos2== FALSE ||$pos2>$pos1)
- {
- $pos=$pos1;
- break;
- }
- $pos=$pos2+ 1;
- }
- $clean.='$s$';
- $old_pos=$pos+ 1;
- }
- $clean.=substr($db_string,$old_pos);
- $clean= trim(strtolower(preg_replace(array('~\s+~s'),array(' '),$clean)));
- //老版本的MySQL不支持Union,常用的程序里也不使用Union,但是一些黑客使用它,所以要检查它
- if(strpos($clean,'union') !== FALSE && preg_match('~(^|[^a-z])union($|[^[a-z])~s',$clean) != 0)
- {
- $fail= TRUE;
- $error="union detect";
- }
- //发布版本的程序可能不包括“--”“#”这样的注释,但是黑客经常使用它们
- elseif(strpos($clean,'/*') > 2 ||strpos($clean,'--') !== FALSE ||strpos($clean,'#') !== FALSE)
- {
- $fail= TRUE;
- $error="comment detect";
- }
- //这些函数不会被使用,但是黑客会用它来操作文件
- elseif(strpos($clean,'sleep') !== FALSE && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s',$clean) != 0)
- {
- $fail= TRUE;
- $error="slown down detect";
- }
- elseif(strpos($clean,'benchmark') !== FALSE && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s',$clean) != 0)
- {
- $fail= TRUE;
- $error="slown down detect";
- }
- elseif(strpos($clean,'load_file') !== FALSE && preg_match('~(^|[^a-z])load_file($|[^[a-z])~s',$clean) != 0)
- {
- $fail= TRUE;
- $error="file fun detect";
- }
- elseif(strpos($clean,'into outfile') !== FALSE && preg_match('~(^|[^a-z])into\s+outfile($|[^[a-z])~s',$clean) != 0)
- {
- $fail= TRUE;
- $error="file fun detect";
- }
- //老版本的MySQL不支持子查询,程序里可能也用得少,但是黑客可以使用它查询数据库敏感信息
- elseif(preg_match('~\([^)]*?select~s',$clean) != 0)
- {
- $fail= TRUE;
- $error="sub select detect";
- }
- if(!empty($fail))
- {
- fputs(fopen($log_file,'a+'),"$userIP||$getUrl||$db_string||$error\r\n");
- exit("Safe Alert: Request Error step 2!");
- }
- else
- {
- return$db_string;
- }
- }
使用过滤的方式,可以在一定程度上防止出现SQL注入漏洞,但仍然存在被绕过的可能。
使用PDO预编译语句时需要注意的是,不要将变量直接拼接到PDO语句中,而是使用占位符进行数据库中数据的增加、删除、修改、查询。示例代码如下:
- $pdo=new PDO('mysql:host=127.0.0.1;dbname=test','root','root');
- $stmt=$pdo->prepare('select * from user where id=:id');
- $stmt->bindParam(':id',$_GET['id']);
- $stmt->execute();
- $result=$stmt->fetchAll(PDO::FETCH_ASSOC);
- var_dump($result);
- ?>
MS08067安全实验室视频号已上线
欢迎各位同学关注转发~
— 实验室旗下直播培训课程 —
和20000+位同学加入MS08067一起学习