php防注入代码方法 过滤所有GET POST
/* 过滤所有GET过来变量 */
foreach ($_GET as $get_key=>$get_var)
{
if (is_numeric($get_var)) {
$get[strtolower($get_key)] = get_int($get_var);
} else {
$get[strtolower($get_key)] = get_str($get_var);
}
}
/* 过滤所有POST过来的变量 */
foreach ($_POST as $post_key=>$post_var)
{
if (is_numeric($post_var)) {
$post[strtolower($post_key)] = get_int($post_var);
} else {
$post[strtolower($post_key)] = get_str($post_var);
}
}
/* 过滤函数 */
//整型过滤函数
function get_int($number)
{
return intval($number);
}
//字符串型过滤函数
function get_str($string)
{
if (!get_magic_quotes_gpc()) {
return addslashes($string);
}
return $string;
}
这里我们是告诉各位朋友关于php防注入代码方法 过滤所有GET POST哦,因为大多数据都是能过get post 方法注入,当然还有files
<?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 “<script language=”javascript教程”>alert(”非法字符”);</script>”;
if (emptyempty($StrGoUrl)){
echo “<script language=”javascript”>history.go(-1);</script>”;
}else{
echo “<script language=”javascript”>window.location=””.$StrGoUrl.””;</script>”;
}
exit;
}
}
?>
(1) 打开php教程的安全模式
php的安全模式是个非常重要的内嵌的安全机制,能够控制一些php中的函数,比如system(),
同时把很多文件操作函数进行了权限控制,也不允许对某些关键文件的文件,比如/etc/passwd,
但是默认的php.ini是没有打开安全模式的,我们把它打开:
safe_mode = on
(2) 用户组安全
当safe_mode打开时,safe_mode_gid被关闭,那么php脚本能够对文件进行访问,而且相同
组的用户也能够对文件进行访问。
建议设置为:
safe_mode_gid = off
如果不进行设置,可能我们无法对我们服务器网站目录下的文件进行操作了,比如我们需要
对文件进行操作的时候。
(3) 安全模式下执行程序主目录
如果安全模式打开了,但是却是要执行某些程序的时候,可以指定要执行程序的主目录:
safe_mode_exec_dir = D:/usr/bin
一般情况下是不需要执行什么程序的,所以推荐不要执行系统程序目录,可以指向一个目录,
然后把需要执行的程序拷贝过去,比如:
safe_mode_exec_dir = D:/tmp/cmd
但是,我更推荐不要执行任何程序,那么就可以指向我们网页目录:
safe_mode_exec_dir = D:/usr/www
(4) 安全模式下包含文件
如果要在安全模式下包含某些公共文件,那么就修改一下选项:
safe_mode_include_dir = D:/usr/www/include/
其实一般php脚本中包含文件都是在程序自己已经写好了,这个可以根据具体需要设置。
(5) 控制php脚本能访问的目录
使用open_basedir选项能够控制PHP脚本只能访问指定的目录,这样能够避免PHP脚本访问
不应该访问的文件,一定程度上限制了phps教程hell的危害,我们一般可以设置为只能访问网站目录:
open_basedir = D:/usr/www
(6) 关闭危险函数
如果打开了安全模式,那么函数禁止是可以不需要的,但是我们为了安全还是考虑进去。比如,
我们觉得不希望执行包括system()等在那的能够执行命令的php函数,或者能够查看php信息的
phpinfo()等函数,那么我们就可以禁止它们:
disable_functions = system,passthru,exec,shell_exec,popen,phpinfo
如果你要禁止任何文件和目录的操作,那么可以关闭很多文件操作
disable_functions = chdir,chroot,dir,getcwd,opendir,readdir,scandir,fopen,unlink,delete,copy,mkdir, rmdir,rename,file,file_get_contents,fputs,fwrite,chgrp,chmod,chown
以上只是列了部分不叫常用的文件处理函数,你也可以把上面执行命令函数和这个函数结合,
就能够抵制大部分的phpshell了。
(7) 关闭PHP版本信息在http头中的泄漏
我们为了防止黑客获取服务器中php版本的信息,可以关闭该信息斜路在http头中:
expose_php = Off
比如黑客在 te.net教程 www.111cn.net 80 的时候,那么将无法看到PHP的信息。
(8) 关闭注册全局变量
在PHP中提交的变量,包括使用POST或者GET提交的变量,都将自动注册为全局变量,能够直接访问,
这是对服务器非常不安全的,所以我们不能让它注册为全局变量,就把注册全局变量选项关闭:
register_globals = Off
当然,如果这样设置了,那么获取对应变量的时候就要采用合理方式,比如获取GET提交的变量var,
那么就要用$_GET['var']来进行获取,这个php程序员要注意。
(9) 打开magic_quotes_gpc来防止SQL注入
SQL注入是非常危险的问题,小则网站后台被入侵,重则整个服务器沦陷,
所以一定要小心。php.ini中有一个设置:
magic_quotes_gpc = Off
这个默认是关闭的,如果它打开后将自动把用户提交对sql的查询进行转换,
比如把 ' 转为 '等,这对防止sql注射有重大作用。所以我们推荐设置为:
magic_quotes_gpc = On
(10) 错误信息控制
一般php在没有连接到数据库教程或者其他情况下会有提示错误,一般错误信息中会包含php脚本当
前的路径信息或者查询的SQL语句等信息,这类信息提供给黑客后,是不安全的,所以一般服务器建议禁止错误提示:
display_errors = Off
如果你却是是要显示错误信息,一定要设置显示错误的级别,比如只显示警告以上的信息:
error_reporting = E_WARNING & E_ERROR
当然,我还是建议关闭错误提示。
(11) 错误日志
建议在关闭display_errors后能够把错误信息记录下来,便于查找服务器运行的原因:
log_errors = On
同时也要设置错误日志存放的目录,建议根apache的日志存在一起:
error_log = D:/usr/local/apache2/logs/php_error.log
注意:给文件必须允许apache用户的和组具有写的权限。
MYSQL的降权运行
新建立一个用户比如mysql教程start
net user mysqlstart ****microsoft /add
net localgroup users mysqlstart /del
不属于任何组
如果MYSQL装在d:mysql ,那么,给 mysqlstart 完全控制 的权限
然后在系统服务中设置,MYSQL的服务属性,在登录属性当中,选择此用户 mysqlstart 然后输入密码,确定。
重新启动 MYSQL服务,然后MYSQL就运行在低权限下了。
如果是在windos平台下搭建的apache我们还需要注意一点,apache默认运行是system权限,
这很恐怖,这让人感觉很不爽.那我们就给apache降降权限吧。
net user apache ****microsoft /add
net localgroup users apache /del
ok.我们建立了一个不属于任何组的用户apche。
我们打开计算机管理器,选服务,点apache服务的属性,我们选择log on,选择this account,我们填入上面所建立的账户和密码,
重启apache服务,ok,apache运行在低权限下了。
实际上我们还可以通过设置各个文件夹的权限,来让apache用户只能执行我们想让它能干的事情,给每一个目录建立一个单独能读写的用户。
<?php
002 class Captcha
003 {
004 private $_about = array(
005 'imageLine'=>8, // 线条干扰,为0正常
006 'imagePixel'=>300, // 像素干扰,为0正常
007 'location'=>true, // 字体随机位置,false正常
008 'bgColor'=>'#ffffff', // 背景
009 'textColor'=>'', // 文本
010 'borColor'=>'#e1e1e1', // 边框
011 'lineColor'=>'#dedede', // 线条
012 'pixelColor'=>'#646464' // 像素
013 );
014
015 /**
016 * 构造<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%BA%AF%CA%FD">函数</SPAN>
017 */
018 public function __construct(){}
019
020 /**
021 * setCookie
022 *
023 * @param string $data 写入<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%CA%FD%BE%DD">数据</SPAN>
024 */
025 public function setCookie($data)
026 {
027 if (is_array($data)) {
028 $string = '';
029 foreach ($data as $v) $string .= $v;
030 $data = $string;
031 }
032 setcookie('captcha',$data,time()+60,'/');
033 }
034 /**
035 * setCaptcha
036 *
037 * 设置输出<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%D1%E9%D6%A4">验证</SPAN>码
038 *
039 * @param integer $width 宽
040 * @param integer $height 高
041 * @param integer $fontSize 字体大小
042 * @param string $font 字体<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%CE%C4%BC%FE">文件</SPAN>位置
043 * @param integer $length 验证<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%D7%D6%B7%FB">字符</SPAN>长度
044 * @param integer $type 验证字符类型
045 * 1:只数字,2:只小写字母,3:只大写字母,
046 * 4:小写字母数字混合,5:大写字母数字混合,6:大小写字母数字混合,7:汉字
047 * @param array $about 验证码配置
048 */
049 public function setCaptcha($width=80,$height=30,$fontSize=13,$font='code.ttf',$length=4,$type=3,$about=array())
050 {
051 is_array($about) || exit('参数出错!');
052 foreach ($about as $key=>$value) {
053 if (array_key_exists($key,$this->_about)) {
054 $this->_about[$key] = $value;
055 }
056 }
057
058 extract($this->_about,EXTR_OVERWRITE);
059 $im = imagecreatetruecolor($width,$height);
060 // 背景颜色
061 $bgC_Arr = $this->getColor($bgColor);
062 $imgBgColor = imagecolorallocate($im,$bgC_Arr[0],$bgC_Arr[1],$bgC_Arr[2]);
063 // 边框颜色
064 $borC_Arr = $this->getColor($borColor);
065 $imgBorderColor = imagecolorallocate($im,$borC_Arr[0],$borC_Arr[1],$borC_Arr[2]);
066 // 填充
067 imagefill($im,0,0,$imgBgColor);
068 imagerectangle($im,0,0,$width-1,$height-1,$imgBorderColor);
069
070 // 画线
071 if ($imageLine) {
072 // 线条颜色
073 $lineC_Arr = $this->getColor($lineColor);
074 $imgLineColor = imagecolorallocate($im,$lineC_Arr[0],$lineC_Arr[1],$lineC_Arr[2]);
075 for ($i=0;$i<$imageLine;$i++) {
076 imageline($im,0,mt_rand(0,$height),$width,mt_rand(0,$height),$imgLineColor);
077 }
078 }
079 // 像素干扰
080 if ($imagePixel) {
081 // 像素颜色
082 $pixelC_Arr = $this->getColor($pixelColor);
083 $imgPixelColor = imagecolorallocate($im,$pixelC_Arr[0],$pixelC_Arr[1],$pixelC_Arr[2]);
084 for ($i=0;$i<$imagePixel;$i++) {
085 imagesetpixel($im,mt_rand(2,$width-2),mt_rand(2,$height-2),$imgPixelColor);
086 }
087 }
088
089 // 获取字串
090 if (!$text = $this->getText($type,$length)) exit('验证码没有查找到指定字符类型!');
091 // 检查字体文件
092 is_file($font) || exit('验证码未查找到指定字体文件!');
093 $y = ceil($height / 2)+ceil($fontSize/2)*0.9;
094 $pitch = ceil($width / $length);
095 $text = ctype_alnum($text)?$text:str_split($text,3);
096 $pad = 0;
097 for ($i=0;$i<$length;$i++) {
098 if (!empty($textColor)) {
099 // 文本颜色
100 $txtC_Arr = $this->getColor($textColor);
101 $imgTextColor = imagecolorallocate($im,$txtC_Arr[0],$txtC_Arr[1],$txtC_Arr[2]);
102 } else {
103 $imgTextColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
104 }
105 if (false === $location) {
106 $fontSizePad = ctype_alnum($text[$i]) ? $fontSize*1.4 : $fontSize*1.7;
107 $x = $pad + $fontSize + (ceil($pitch/2) - $fontSizePad);
108 imagettftext($im,$fontSize,0,$x,$y,$imgTextColor,$font,$text[$i]);
109 $pad += $pitch;
110 } else {
111 $d = $pad += $pitch;
112 $d += $fontSize*0.5;
113 imagettftext($im,$fontSize,mt_rand(-20,20),mt_rand($d-=$pitch,$pad-$fontSize*1.4),$y,$imgTextColor,$font,$text[$i]);
114 }
115 }
116 // 写入<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=session">session</SPAN>
117 $this->setCookie($text);
118 $this->getCaptcha($im);
119 }
120
121 /**
122 * getText
123 *
124 * 获取字符串
125 *
126 * @param integer $type 字串类型
127 * @param integer $length 长度
128 * @return string $result 字串结果
129 */
130 public function getText($type,$length)
131 {
132 $result = '';
133 $range = array();
134 switch ($type) {
135 case 1 :
136 for ($i=0;$i<$length;$i++) $result .= mt_rand(0,9);
137 break;
138 case 2 :
139 $range = range('a','z');
140 break;
141 case 3 :
142 $range = range('A','Z');
143 break;
144 case 4 :
145 $range = array_merge(range(0,9),range('a','z'));
146 break;
147 case 5 :
148 $range = array_merge(range(0,9),range('A','Z'));
149 break;
150 case 6 :
151 $range = array_merge(range(0,9),range('a','z'),range('A','Z'));
152 break;
153 case 7 :
154 // for ($i=0;$i<$length;$i++) $result .= iconv('gb2312','utf-8',chr(rand(0xB0,0xF7)).chr(rand(0xA1,0xFE)));
155 $range = array(
156 '佟','爱','菲','可','天','一','新','他','策','韵','年','明','礼','杰','雅','诗','忍','子',
157 '好','快','乐','半','美','拜','亮','臣','香','中','国','开','心','阿','笑','图','成','虎'
158 );
159 break;
160 default : return false;
161 }
162 if (!empty($range)) {
163 shuffle($range);
164 $result = mb_substr(implode('',$range),0,$length,'utf-8');
165 }
166 return $result;
167 }
168
169 /**
170 * getColor
171 *
172 * 十六进制颜色转为十进制
173 */
174 public function getColor($string)
175 {
176 $string = str_replace('#','',$string);
177 if (!ctype_alnum($string)) {
178 return false;
179 }
180 $rgb_Arr = array();
181 if (strlen($string) == 3) {
182 for ($i=0;$i<3;$i++) {
183 $rgb_Arr[$i] = hexdec(str_repeat($string{$i},'2'));
184 }
185 } else if (strlen($string) == 6) {
186 $colorSp = str_split($string,2);
187 foreach ($colorSp as $v) {
188 $rgb_Arr[] = hexdec($v);
189 }
190 } else {
191 return false;
192 }
193 return $rgb_Arr;
194 }
195
196 public function getCaptcha($im)
197 {
198 header('Content-type: image/gif');
199 imagegif($im);
200 imagedestroy($im);
201 }
202 }
203
204 $about = array(
205 'imageLine'=>0, // 线条干扰,为0正常
206 'imagePixel'=>300, // 像素干扰,为0正常
207 'location'=>false, // 字体随机位置,false正常
208 'bgColor'=>'#ffffff', // 背景颜色
209 'textColor'=>'', // 文本颜色,默认随机颜色
210 'borColor'=>'#e1e1e1', // 边框颜色
211 'lineColor'=>'#ff0000', // 线条颜色
212 'pixelColor'=>'#a13e3e' // 像素颜色
213 );
214 error_reporting(0);
215 $a = new Captcha();
216 $a->setCaptcha(100,30,13,'font.ttf',4,1,$about);
在该脚本的所有需要,我们会计算生成图表变量第一的第二部分。然后,我们准备的
主要形象,实际输出数据。每个类型的结果是一个从数据库教程中挑选一个,得票率计算
,然后酒吧和解释性说明的输出。
在此脚本中,我们使用的是TrueType字体获得反锯齿文本。功能与TrueType字体的工
作有自己的特点。首先,你需要的字体点所在。为此,系统变量GDFONTPATH设置。然
后,我们需要设置字体的名称。 TrueType字体渲染使用ImageTTFText()。
我们已经讨论了上面的绘图中使用的所有功能,代码有意见,是很清楚的,所以你不
应该有任何麻烦。
//Tell gd2, where your fonts reside
putenv('GDFONTPATH=C:WINDOWSFonts');
$font = 'arial';
//Set starting point for drawing
$y = 50;
//Specify constant values
$width = 700; //Image width in pixels
$bar_height = 20; //Bars height
$height = $num_poller * $bar_height * 1.5 + 70; //Calculating image height
$bar_unit = ($width - 400) / 100; //Distance on the bar chart standing for
1 unit
//Create the image resource
$image = ImageCreate($width, $height);
//We are making four colors, white, black, blue and red
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$red = ImageColorAllocate($image, 255, 0, 0);
$blue = imagecolorallocate($image,0,0,255);
//Create image background
ImageFill($image,$width,$height,$white);
//Draw background shape
ImageRectangle($image, 0, 0, $width-1, $height-1, $black);
//Output header
ImageTTFText($image, 16, 0, $width/3 + 50, $y - 20, $black, $font, 'Poll
Results');
while ($row = mysql教程_fetch_object($result)) {
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;
//Output header for a particular value
ImageTTFText($image,12,0,10, $y+($bar_height/2), $black, $font, $row-
>book_type);
//Output percentage for a particular value
ImageTTFText($image, 12, 0, 170, $y +
($bar_height/2),$red,$font,$percent.'%');
$bar_length = $percent * $bar_unit;
//Draw a shape that corresponds to 100%
ImageRectangle($image, $bar_length+221, $y-2, (220+(100*$bar_unit)),
$y+$bar_height, $black);
//Output a bar for a particular value
ImageFilledRectangle($image,220,$y-2,220+$bar_length, $y+$bar_height,
$blue);
//Output the number of votes
ImageTTFText($image, 12, 0, 250+100*$bar_unit, $y+($bar_height/2), $black,
$font, $row->num_votes.' votes cast.');
//Going down to the next bar
$y = $y + ($bar_height * 1.5);
}
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
?>
相关文章
php 中file_get_contents超时问题的解决方法
file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
- 相信很多站长都遇到过这样一个问题,访问页面时出现408错误,下面一聚教程网将为大家介绍408错误出现的原因以及408错误的解决办法。 HTTP 408错误出现原因: HTT...2017-01-22
- 下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02
- 有一种方法,可以不打开网站而直接查看到这个网站的源代码.. 这样可以有效地防止误入恶意网站... 在浏览器地址栏输入: view-source:http://...2016-09-20
- ps软件是现在非常受大家喜欢的一款软件,有着非常不错的使用功能。这次文章就给大家介绍下ps把文字背景变透明的操作方法,喜欢的一起来看看。 1、使用Photoshop软件...2017-07-06
- <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
intellij idea快速查看当前类中的所有方法(推荐)
这篇文章主要介绍了intellij idea快速查看当前类中的所有方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-09-02- 1.在没有设置默认值的情况下: 复制代码 代码如下:SELECT userinfo.id, user_name, role, adm_regionid, region_name , create_timeFROM userinfoLEFT JOIN region ON userinfo.adm_regionid = region.id 结果:...2014-05-31
- 本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
js导出table数据到excel即导出为EXCEL文档的方法
复制代码 代码如下: <!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 ht...2013-10-13- 批量更新mysql更新语句很简单,更新一条数据的某个字段,一般这样写:复制代码 代码如下:UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_value';如果更新同一字段为同一个值,mysql也很简单,修改下where即...2013-10-04
- 本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
- 本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
- ps软件是一款非常不错的图片处理软件,有着非常不错的使用效果。这次文章要给大家介绍的是ps怎么制作倒影,一起来看看设计倒影的方法。 用ps怎么做倒影最终效果̳...2017-07-06
- php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
- 本文涉及的主题虽然很基础,在许多人看来属于小伎俩,但在JavaScript基础知识中属于一个综合性的话题。这里会涉及到对象属性的封装、原型、构造函数、闭包以及立即执行表达式等知识。公有方法 公有方法就是能被外部访问...2015-11-08
安卓手机wifi打不开修复教程,安卓手机wifi打不开解决方法
手机wifi打不开?让小编来告诉你如何解决。还不知道的朋友快来看看。 手机wifi是现在生活中最常用的手机功能,但是遇到手机wifi打不开的情况该怎么办呢?如果手机wifi...2016-12-21- 最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
- php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p> </p> <p> </p> <form name="form1...2016-11-25