首页 > php开发 > php通用防注入与注入详细说明

php通用防注入与注入详细说明

php通用防注入主要是过滤一些sql命令与php post get传过来的参考我们/要过滤一些非法字符,这样可以防止基本的注入了,那关第于apache 服务器安装设置方法也是必须的,管理员用户名和密码都采取md5加密,这样就能有效地防止了php的注入.

还有服务器和mysql教程也要加强一些安全防范.

对于linux服务器的安全设置:

加密口令,使用"/usr/sbin/authconfig"工具打开密码的shadow功能,对password进行加密,禁止访问重要文件,进入linux命令界面,在提示符下输入:

#chmod 600 /etc/inetd.conf    //改变文件属性为600 
#chattr +i  /etc/inetd.conf     //保证文件属主为root 
#chattr -i  /etc/inetd.conf     // 对该文件的改变做限制

禁止任何用户通过su命令改变为root用户,在su配置文件即/etc/pam.d/目录下的开头添加下面两行:

auth  sufficient  /lib/security/pam_rootok.so debug
auth  required  /lib/security/pam_whell.so group=wheel

删除所有的特殊帐户

#userdel  lp等等 删除用户

#groupdel lp等等  删除组

禁止不使用的suid/sgid程序

<?php
//find / -type f (-perm -04000  - o -perm -02000 ) -execls -lg {};代码如下:
$arrfiltrate = array("'",";","union","select","insert","update","delete","load_file","outfile");
//出错后要跳转的url
$strgourl = "";
function funstringexist($strfiltrate, $arrfiltrate) {
    foreach ($arrfiltrate as $key => $value) {
        if (eregi($value, $strfiltrate)) {
            return true;
        }
    }
    return false;
}
//合并$_post 、 $_get和$_cookie
if (function_exists(array_merge)) {
    $arrpostgetcookiesession = array_merge($http_post_vars, $http_get_vars, $http_cookie_vars);
    $string = implode("", $arrpostgetcookiesession);
}
//验证
if (funstringexist($string, $arrfiltrate)) {
    echo "<script language="javascript">alert("提示,非法字符");</script>";
} else {
    echo "<script language="javascript">window.location="".$strgourl."";</script>";
}
?>

第二款防注入实例,代码如下:

php通用防注入安全代码.

说明:判断传递的变量中是否含有非法字符,如$_post、$_get

功能:防注入.

<?php
//要过滤的非法字符
$arrfiltrate=array("'",";","union");
//出错后要跳转的url,不填则默认前一页
$strgourl = "";
//是否存在数组中的值
function funstringexist($strfiltrate, $arrfiltrate) {
    foreach ($arrfiltrate as $key => $value) {
        if (eregi($value, $strfiltrate)) {
            return true;
        }
    }
    return false;
}
//合并$_post 和 $_get
if (function_exists(array_merge)) {
    $arrpostandget = array_merge($http_post_vars, $http_get_vars);
} else {
    foreach ($http_post_vars as $key => $value) {
        $arrpostandget[] = $value;
    }
    foreach ($http_get_vars as $key => $value) {
        $arrpostandget[] = $value;
    }
}
//验证开始
foreach ($arrpostandget as $key => $value) {
    if (funstringexist($value, $arrfiltrate)) {
        echo "alert(/"neeao提示,非法字符 / ");";
        if (emptyempty($strgourl)) {
            echo "history.go(-1);";
        } else {
            echo "window.location=/"".$strgourl." / ";";
        }
        exit;
    }
}
?>

看一下关于注入细节.

转化成ascii后是char(97,108,112,104,97),转化成16进制是0x616c706861,我们将在光盘中提供16进制和ascii转换工具,好了直接在浏览器里输入:

http://localhost/site/admin/login.php?username=char(97,108,112,104,97)%23 

sql语句变成:

select * from alphaaut hor where username=char(97,108,112,104,97)# and password=

正如我们期望的那样,他顺利执行了,我们得到我们想要的,当然咯,我们也可以这样构造:

http://www.phprm.com/site/admin/login.php?username=0x616c706861%23 

sql语句变成:

select * from alphaauthor where username=0x616c706861%23# and password= 

我们再一次是成功者了,很有成就感吧,或许你会问我们是否可以把#也放在char()里,实际上char(97,108,112,104,97)相当于 alpha,注意是alpha上加引号,表示alpha字符串,我们知道在mysql中如果执行如下代码:

mysql> select * from dl_users where username=alpha;error 1054 (42s22): unknown column alpha in where clause 

看返回错误了,因为他会认为alpha是一个变量,所以我们得在alpha上加引号,代码如下:

mysql> select * from dl_users where username= alpha ;


教程地址:http://www.phprm.com/develop/fs4735.html

欢迎转载!但请带上文章地址^^

标签:php防注入 php通用sql

相关文章

发表留言