最新SQL注入漏洞修复建议

编程入门 行业动态 更新时间:2024-10-27 23:19:14

最新SQL注入<a href=https://www.elefans.com/category/jswz/34/1770270.html style=漏洞修复建议"/>

最新SQL注入漏洞修复建议

点击星标,即时接收最新推文

本文选自《web安全攻防渗透测试实战指南(第2版)》

点击图片五折购书

SQL注入漏洞修复建议

常用的SQL注入漏洞的修复方法有两种。

1.过滤危险字符

多数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("<font size='5' color='red'>Safe Alert: Request Error step 1 !</font>");}}//完整的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("<font size='5' color='red'>Safe Alert: Request Error step 2!</font>");}else{return$db_string;}}

使用过滤的方式,可以在一定程度上防止出现SQL注入漏洞,但仍然存在被绕过的可能。

2.使用预编译语句

使用PDO预编译语句时需要注意的是,不要将变量直接拼接到PDO语句中,而是使用占位符进行数据库中数据的增加、删除、修改、查询。示例代码如下:

<?php
$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一起学习

更多推荐

最新SQL注入漏洞修复建议

本文发布于:2023-12-03 12:25:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1655327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:漏洞   建议   最新   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!