首页 > 编程技术 > php

php sql防注入以及 html 过滤安全函数

发布时间:2016-11-25 15:25

方法一过滤html自定义函数

 代码如下 复制代码
function ihtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = ihtmlspecialchars($val);
}
} else {
$string = preg_replace('/&((#(d{3,5}|x[a-fa-f0-9]{4})|[a-za-z][a-z0-9]{2,5});)/', '&\1',
str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
}
return $string;

方法二

 代码如下 复制代码
// $rptype = 0 表示仅替换 html标记
// $rptype = 1 表示替换 html标记同时去除连续空白字符
// $rptype = 2 表示替换 html标记同时去除所有空白字符
// $rptype = -1 表示仅替换 html危险的标记
function htmlreplace($str,$rptype=0)
{
$str = strips教程lashes($str);
if($rptype==0)
{
$str = htmlspecialchars($str);
}
else if($rptype==1)
{
$str = htmlspecialchars($str);
$str = str_replace(" ",' ',$str);
$str = ereg_replace("[rnt ]{1,}",' ',$str);
}
else if($rptype==2)
{
$str = htmlspecialchars($str);
$str = str_replace(" ",'',$str);
$str = ereg_replace("[rnt ]",'',$str);
}
else
{
$str = ereg_replace("[rnt ]{1,}",' ',$str);
$str = eregi_replace('script','script',$str);
$str = eregi_replace("<[/]{0,1}(link|meta|ifr|fra)[^>]*>",'',$str);
}
return addslashes($str);
}

其它方法

php教程过滤不安全字符函数

 代码如下 复制代码

function uh($str)
{
    $farr = array(
        "/s+/",//过滤多余的空白
        "/<(/?)(script|i?frame|style|html|body|title|link|meta|?|%)([^>]*?)>/isu",//过滤 <script 等可能引入恶意内容或恶意改变显示布局的代码,如果不需要插入flash等,还可以加入<object的过滤
        "/(<[^>]*)on[a-za-z]+s*=([^>]*>)/isu",//过滤网页特效的on事件
    );
   $tarr = array(
        " ",
        "<\1\2\3>", //如果要直接清除不安全的标签,这里可以留空
        "\1\2",
   );

  $str = preg_replace($farr,$tarr,$str);
  return $str;

 代码如下 复制代码

function uc_addslashes($string, $force = 0, $strip = false) {
 !defined('magic_quotes_gpc') && define('magic_quotes_gpc', get_magic_quotes_gpc());
 if(!magic_quotes_gpc || $force) {
  if(is_array($string)) {
   foreach($string as $key => $val) {
    $string[$key] = uc_addslashes($val, $force, $strip);
   }
  } else {
   $string = addslashes($strip ? strips教程lashes($string) : $string);
  }
 }
 return $string;
}

if(!function_exists('daddslashes')) {
 function daddslashes($string, $force = 0) {
  return uc_addslashes($string, $force);
 }
}

//php 过滤函数应用实例111cn.net

$get = $_get;
$g = uc_addslashes($get, $force = 0, $strip = false);

//过滤post提交数据

$post =  $_post;
$p = uc_addslashes($post, $force = 0, $strip = false);

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>几种防御php程序被木马攻击配置详解方法</title>
</head>

<body>
防止跳出web目录
  首先修改httpd.conf,如果你只允许你的php脚本程序在web目录里操作,还可以修改httpd.conf文件限制php的操作路径。比如你的web目录是/usr/local/apache/htdocs,那么在httpd.conf里加上这么几行:

  php_admin_value open_basedir /usr/local/apache

  /htdocs

  这样,如果脚本要读取/usr/local/apache/htdocs以外的文件将不会被允许,如果错误显示打开的话会提示这样的错误:

  warning: open_basedir restriction in effect. file is in wrong directory in

  /usr/local/apache/htdocs/open.php on line 4

  等等。

2、防止php木马执行webshell
  打开safe_mode,

  在,php.ini中设置

  disable_functions= passthru,exec,shell_exec,system

  二者选一即可,也可都选

3、防止php木马读写文件目录
  在php.ini中的

  disable_functions= passthru,exec,shell_exec,system

  后面加上php处理文件的函数

  主要有

  fopen,mkdir,rmdir,chmod,unlink,dir

  fopen,fread,fclose,fwrite,file_exists

  closedir,is_dir,readdir.opendir

  fileperms.copy,unlink,delfile

  即成为

  disable_functions= passthru,exec,shell_exec,system,fopen,mkdir,rmdir,chmod,unlink,dir

  ,fopen,fread,fclose,fwrite,file_exists

  ,closedir,is_dir,readdir.opendir

  ,fileperms.copy,unlink,delfile

  ok,大功告成,php木马拿我们没辙了,遗憾的是这样的话,利用文本数据库教程的那些东西就都不能用了。

  如果是在windos平台下搭建的apache我们还需要注意一点,apache默认运行是system权限,这很恐怖,这让人感觉很不爽.那我们就给apache降降权限吧。

  net user apache fuckmicrosoft /add

  net localgroup users apache /del

  ok.我们建立了一个不属于任何组的用户apche。

  我们打开计算机管理器,选服务,点apache服务的属性,我们选择log on,选择this account,我们填入上面所建立的账户和密码,重启apache服务,ok,apache运行在低权限下了。

  实际上我们还可以通过设置各个文件夹的权限,来让apache用户只能执行我们想让它能干的事情,给每一个目录建立一个单独能读写的用户。这也是当前很多虚拟主机提供商的流行配置方法哦,不过这种方法用于防止这里就显的有点大材小用了
</body>
</html>

 代码如下 复制代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.111cn.net/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
<title>login</title>
<link rel="stylesheet" type="text/css教程" href="/tryit.css教程" />
<script type="text/网页特效">
function getpinimg()
{
 document.getelementbyid("pinimg").src="log_demo.php教程?a=pin&r=" + math.random();
}
</script>
</head>

<body>
<form action="log_demo.php" method="get">
 <input type="hidden" name="a" value="log" />
 user:<input type="text" name="user" size="20" value="test" /><br /><br />
 password:<input type="password" name="pass" size="12" value="123456" /><br /><br />
 pincode:<input type="text" name="pin" size="8" value="" />
 <input type="button" value="getpin" onclick="getpinimg();">
   <img id="pinimg"" width=100% src="log_demo.php?a=pin" onclick="getpinimg();" />
 <input type="submit" value="submit"><br />
</form>
</body>
</html>

log_demo.php代码

 代码如下 复制代码

<?php
require_once("log_aux.php");

if ($_get['a'] == "pin") {
 getpinimg();
 exit;
}

if ($_get['a'] == "log") {
 $ret = checkpincookie($_get['pin'], 300);
 if ($ret < 0) {
  echo "pin error:$ret ";
  exit;
 }
 // check user name and password.
 echo "ok..$ret ";
 if ("123xx" == 123) {
  echo "dddd ";
 }
 echo $_get['u'];
}

log_aux.php代码

 代码如下 复制代码

<?php
$magic_a = "www.111cn.net34589";
$magic_b = "234566***+";

function getpincookie($pin_code)
{
 global $magic_a;
 global $magic_b;
 $now = time();
 $client_ip = $_server['remote_addr'];
 $pin = md5($now . $client_ip . $magic_a . $pin_code . $maigc_b) . $now;
 return $pin;
}

function checkpincookie($pin_code_in, $timeout=300) // 5 miniuteswww.111cn.net
{
 global $magic_a;
 global $magic_b;
 $pin = substr($_cookie['pin'], 0, 32);
 $time = substr($_cookie['pin'], 32);
 $now = time();
 if ($now - $time > $timeout) {
  return -100;  // time_out
 }
 $pin_code_in = strtolower($pin_code_in);
 $client_ip = $_server['remote_addr'];
 $pin_2 = md5($time . $client_ip . $magic_a . $pin_code_in . $maigc_b);
 if ($pin === $pin_2) {
  return 0;
 } else {
  return -200; // pincode is error.
 }
}

function getpinimg()
{
// $rnd = rand(0, 10);
 $rnd = 0;
 $path = "./www.111cn.net/" . substr("00000$rnd", -4);
 for ($i = 0; $i < 4; ++$i) {
  if (__getpinimg($path)) {
   break;
  }
 }
 exit;
}

function __getpinimg($path)
{
 require("$path/pinmap.php");
 $pinv = $pin[rand(0, $pin_max)];
 list ($file, $pin_code) = explode('#', $pinv);
 $filepath = "$path/$file$pin_ext";

 //  output pincode image.
 $fh = fopen($filepath, "rb");
 if (!$fh) {
  return false;
 }
 $data = fread($fh, 10240); // php just read max size: 8k.
 fclose($fh);

 if (strlen($data) < 200) {
  return false;
 }
 // set cookie;
 header("content-type: image/jpeg");
 $pin_code = strtolower($pin_code);
 $pin = getpincookie($pin_code);
 setcookie("pin", $pin);

 echo $data;
 return true;
}

 代码如下 复制代码
function _strips教程lashes($string) {
 if(is_array($string)) {
  foreach($string as $key => $val) {
   $string[$key] = _strips教程lashes($val);
  }
 } else {
  $string = stripslashes($string);
 }
 return $string;
}

//111cn.net提示你:使用方法

 代码如下 复制代码
$post = _stripslashes( $_post );
$get = _stripslashes( $_get );
$ot = _stripslashes( $_post['cc'] );

//这样用户所提交的" ' 增加的了' 或"这样,可以防止一些基本sql注入方法。

标签:[!--infotagslink--]

您可能感兴趣的文章: