服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

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

时间:2016-04-11 00:43来源: 作者: 点击:
php通用防注入主要是过滤一些sql命令与php post get传过来的参考我们/要过滤一些非法字符,这样可以防止基本的注入了,那关第于apache 服务器安装设置方法也是必须的,管理员用户名和密码

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

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

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

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

  1. #chmod 600 /etc/inetd.conf    //改变文件属性为600 
  2. #chattr +i  /etc/inetd.conf     //保证文件属主为root 
  3. #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程序

#find / -type f (-perm -04000  - o –perm -02000 ) -execls –lg {};代码如下:

  1. $arrfiltrate=array("'",";","union","select","insert","update","delete","load_file","outfile");   
  2.  //出错后要跳转的url 
  3. $strgourl="";   
  4. //开源代码最模板zuimoban.com 
  5. function funstringexist($strfiltrate,$arrfiltrate
  6.   foreach ($arrfiltrate as $key=>$value
  7.   { 
  8.     if (eregi($value,$strfiltrate)) 
  9.     { 
  10.       return true; 
  11.     } 
  12.   } 
  13.   return false; 
  14.  
  15. //合并$_post 、 $_get和$_cookie 
  16.  
  17. if(function_exists(array_merge))     
  18.   $arrpostgetcookiesession=array_merge($http_post_vars,$http_get_vars,$http_cookie_vars);   
  19.   $string = implode("",$arrpostgetcookiesession); 
  20. }  
  21.  
  22. //验证 
  23.  
  24. if(funstringexist($string,$arrfiltrate)) 
  25.   echo "<script language="javascript">alert("提示,非法字符");</script>"
  26. else 
  27.   echo "<script language="javascript">window.location="".$strgourl."";</script>"
  28. }  
第二款防注入实例,代码如下:

php通用防注入安全代码.

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

功能:防注入.

  1. //要过滤的非法字符 
  2. $arrfiltrate=array("'",";","union"); 
  3. //出错后要跳转的url,不填则默认前一页 
  4. $strgourl=""
  5. //是否存在数组中的值 
  6. function funstringexist($strfiltrate,$arrfiltrate){ 
  7. foreach ($arrfiltrate as $key=>$value){ 
  8. if (eregi($value,$strfiltrate)){ 
  9. return true; 
  10. return false; 
  11. //合并$_post 和 $_get 
  12. if(function_exists(array_merge)){ 
  13. $arrpostandget=array_merge($http_post_vars,$http_get_vars); 
  14. }else
  15. foreach($http_post_vars as $key=>$value){ 
  16. $arrpostandget[]=$value
  17. foreach($http_get_vars as $key=>$value){ 
  18. $arrpostandget[]=$value
  19. //验证开始 
  20. foreach($arrpostandget as $key=>$value){ 
  21. if (funstringexist($value,$arrfiltrate)){ 
  22. echo "alert(/"neeao提示,非法字符/");"
  23. if (emptyempty($strgourl)){ 
  24. echo "history.go(-1);"
  25. }else
  26. echo "window.location=/"".$strgourl."/";"
  27. 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.最模板zuimoban.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 ;

 
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容