linux防止php 伪造本地文件解决方法

 更新时间:2016年11月25日 15:25  点击:1737
我们举例只讲linux的系统,但是防止方法在任何系统都是有效的,下面我们先来看看等操作


你可以这样使用

 代码如下 复制代码

http://www.xxx.com/index.php?page=../etc/passwd
http://www.xxx.com/index.php?page=../../../etc/passwd
http://www.xxx.com/index.php?page=..../../etc/passwd

获取更多数据:
etc/profile
etc/services
/etc/passwd
/etc/shadow
/etc/group
/etc/security/group
/etc/security/passwd
/etc/security/user
/etc/security/environ
/etc/security/limits
/usr/lib/security/mkuser.default


像上面代码,如果你是

?page=$_GET的话这样就完了,分析原因,因为我们分页只有数字,那么我们这样操作

 代码如下 复制代码

?page=intval($_GET);

这样就无法把字符提交了,我们利用了intval函数进行了过滤,那么对于提交字符怎么处理呢。

在处理字符时我们利用php自带函数的函数 addslashes和htmlspecialchars进行过滤,

 代码如下 复制代码

$body = htmlspecialchars(isset($_GET[$str])?$_GET[$str]:'');

这样就基本过滤了各种安全注入,当然如果你服务器有漏洞在php上是解决不了的。

分享一款比较好的php验证码程序,有需要的朋友参考一下。
 代码如下 复制代码

代码如下:
<?php
   /*   网站验证码程序
    *   运行环境: PHP5.0.18 下调试通过
    *   需要 gd2 图形库支持(PHP.INI中 php_gd2.dll开启)
    *   文件名: showimg.php
    *   作者:  17php.com
    *   Date:   2007.03
    */

   //随机生成一个4位数的数字验证码
    $num="";
    for($i=0;$i<4;$i++){
    $num .= rand(0,9);
    }
   //4位验证码也可以用rand(1000,9999)直接生成
   //将生成的验证码写入session,备验证页面使用
    Session_start();
    $_SESSION["Checknum"] = $num;
   //创建图片,定义颜色值
    Header("Content-type: image/PNG");
    srand((double)microtime()*1000000);
    $im = imagecreate(60,20);
    $black = ImageColorAllocate($im, 0,0,0);
    $gray = ImageColorAllocate($im, 200,200,200);
    imagefill($im,0,0,$gray);

    //随机绘制两条虚线,起干扰作用
    $style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);
    imagesetstyle($im, $style);
    $y1=rand(0,20);
    $y2=rand(0,20);
    $y3=rand(0,20);
    $y4=rand(0,20);
    imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);
    imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED);

    //在画布上随机生成大量黑点,起干扰作用;
    for($i=0;$i<80;$i++)
    {
   imagesetpixel($im, rand(0,60), rand(0,20), $black);
    }
    //将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
    $strx=rand(3,8);
    for($i=0;$i<4;$i++){
    $strpos=rand(1,6);
    imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black);
    $strx+=rand(8,12);
    }
    ImagePNG($im);
    ImageDestroy($im);
   ?>


使用方法:
 本程序可以直接运行,运行之后即可看到一个图形验证码,每次刷新都随机生成新码。
 在某页面中使用此程序时,可以用以下代码:
代码如下:
.....
 请输入验证码:

 代码如下 复制代码
<input type=text name=passcode>
<img src=showimg.php>
.....

这样即可显示出验证码图片。到了验证页面,用以下代码:
代码如下:
...

 代码如下 复制代码
$code=$_POST["passcode"];
if( $code == $_SESSION["Checknum"]){
验证通过
}else{
验证码错误
}
...
本文章利用三个文件来简单的讲了一下关于php中怎么去应用中文验证码,中文因为是汉字可能出现乱码所以我们就定义了一个文件来专门处理,有需要的朋友可以参考下。
 代码如下 复制代码


<?php   
/*   
* 文件:chinesechar.php   
* 作用:汉字数据储存   
*/   
$ChineseChar = array("人","出","来","友","学","孝","仁","义","礼","廉","忠","国","中","易","白","者","火 ","土","金","木","雷","风","龙","虎","天","地",   "生","晕","菜","鸟","田","三","百","钱","福 ","爱","情","兽","虫","鱼","九","网","新","度","哎","唉","啊","哦","仪","老","少","日",   "月 ","星");   
?>   

<?php   
/*   
* 文件:check.php   
* 作用:验证   

*/   
session_start();   
$errorMSG = '';   
//验证用户输入是否和验证码一致   
if(!is_null($_POST['check']))   
{   
        if (strcasecmp($_SESSION['code'],$_POST['code'])==0)   
            $errorMSG = "<p style="font-size:12px;color:#009900">验证成功!</p>";   
        else   
            $errorMSG = "<p style="font-size:12px;color:#FF0000">验证失败!</p>";   
}   
?>   
<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
</head>   
<body>   
<?php   
if($errorMSG){   
echo $errorMSG;   
}   
?>   
<form action=<?php echo $_SERVER['PHP_SELF']?> method=post>   
请输入验证码:<input type="text" name="code" style="width:   
80px"><img src="code.php">   
<br>   
<input type="submit" name="check" value="提交验证码">   
</form>   
</body>   
</html>   

<?php   
/*   
* 文件:code.php   
* 作用:验证码生成   
 
* 特注:由 没牙的草 指导 版权所有转载注明出处!有付出才会有收获! 
*/   
include_once("chinesechar.php");   
session_start();   
// 设置 content-type   
header("Content-type: image/png");   
// 创建图片   
$im = imagecreatetruecolor(120, 30);   

// 创建颜色   
$fontcolor = imagecolorallocate($im, 255, 255, 255);   
$bg = imagecolorallocate($im, 0, 0, 0);   

// 设置文字   
for($i=0;$i<4;$i++) $text .= $ChineseChar[(array_rand($ChineseChar))];   

$_SESSION['code'] = $text;   
// 设置字体   
$font = 'simkai.ttf';   

// 添加文字   
imagettftext($im, 18, 0, 11, 21, $fontcolor, $font, iconv("GB2312","UTF-8",$text));   

// 输出图片   
imagepng($im);   
imagedestroy($im);   
?>

如果想把上面的程序改成英文数字,只要在chinesechar.php 里面的数组中文改成数字或字母就KO了。

一个比较实用的php图形验证码生成类,调用方法也很简单的,有需要的朋友可以参考一下。
 代码如下 复制代码

<?php

class ImageCode{
 private $width;//验证码图片宽度
 private $height;//验证码图片高度
 private $codeNum;//验证码字符个数
 private $checkCode;//验证码字符
 private $image;//验证码画布
 function __construct($width=60,$height=20,$codeNum=4){
  $this->width=$width;
  $this->height=$height;
  $this->codeNum=$codeNum;
  $this->checkCode=$this->createCheckCode();
 }
 
 function getcreateImage(){
  $this->getcreateImage();
  $this->outputText();
  $this->setDisturbColor();
  $this->outputImage();
 }
 function getCheckCode(){
  return $this->checkCode;
 }
 
 private function getCreateImage(){
  $this->image=imagecreatetruecolor($this->width,$this->height);
  $black=imagecolorallocate($this->image,255,255,255,0);
  $border=imagecolorallocate($this->image,255,255,255,255);
  imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
 }

 private function createCheckCode(){
  for($i=0;$i<$this->codeNum;$i){
   $number=rand(0,2);
   switch($number){
    case 0:
     $rand_number=rand(48,57);//数字
     break;
    case 1:
     $rand_number=rand(65,90);//大写字母
     break;
    case 2:
     $rand_number=rand(97,122);
     break;
   }
   $asc=sprintf("%c",$rand_number);
   $asc_number=$asc_number.$asc;
  }
  return $asc_number;
 }

 private function setDisturbColor(){
  for($i=0;$i<=100;$i++){
   $color=imagecolorallocate($this->image,255,255,255);
   imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
  }
 }

 private function outputImage(){
  if(imagetypes()&IMG_GIF){
   header("Content_type:image/gif");
   imagegif($this->image);
  }elseif(imagetypes()&IMG_JGP){
   header("Content_type:image/jpeg");
   imagejpeg($this->image,"",0.5);
  }else{
   die("PHP不支持图像创建");
  }
 }

 function __destruct(){
  imagedestroy($this->image);
 }
}

?>

一款国外网站下载的php生成验证码图片代码,这个比较实用我们还举例说明了,有需要的朋友按上面保存成php就可以用了哦。
 代码如下 复制代码

<?php
session_start();

if( isset($_POST['submit'])) {
   if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
  // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
  echo 'Thank you. Your message said "'.$_POST['message'].'"';
  unset($_SESSION['security_code']);
   } else {
  // Insert your code for showing an error message here
  echo 'Sorry, you have provided an invalid security code';
   }
} else {
?>

 <form action="form.php" method="post">
  <label for="name">Name: </label><input type="text" name="name" id="name" /><br />
  <label for="email">Email: </label><input type="text" name="email" id="email" /><br />
  <label for="message">Message: </label><textarea rows="5" cols="30" name="message" id="message"></textarea><br />
  <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
  <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br />
  <input type="submit" name="submit" value="Submit" />
 </form>

<?php
 }
?>

验证程序 CaptchaSecurityImages.php

 代码如下 复制代码

<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class CaptchaSecurityImages {

 var $font = 'monofont.ttf';

 function generateCode($characters) {
  /* list all possible characters, similar looking characters and vowels have been removed */
  $possible = '23456789bcdfghjkmnpqrstvwxyz';
  $code = '';
  $i = 0;
  while ($i < $characters) {
   $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
   $i++;
  }
  return $code;
 }

 function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
  $code = $this->generateCode($characters);
  /* font size will be 75% of the image height */
  $font_size = $height * 0.75;
  $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  /* set the colours */
  $background_color = imagecolorallocate($image, 255, 255, 255);
  $text_color = imagecolorallocate($image, 20, 40, 100);
  $noise_color = imagecolorallocate($image, 100, 120, 180);
  /* generate random dots in background */
  for( $i=0; $i<($width*$height)/3; $i++ ) {
   imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  }
  /* generate random lines in background */
  for( $i=0; $i<($width*$height)/150; $i++ ) {
   imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  }
  /* create textbox and add text */
  $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
  $x = ($width - $textbox[4])/2;
  $y = ($height - $textbox[5])/2;
  imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
  /* output captcha image to browser */
  header('Content-Type: image/jpeg');
  imagejpeg($image);
  imagedestroy($image);
  $_SESSION['security_code'] = $code;
 }

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>

[!--infotagslink--]

相关文章

  • php读取zip文件(删除文件,提取文件,增加文件)实例

    下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
  • Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06
  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • HTTP 408错误是什么 HTTP 408错误解决方法

    相信很多站长都遇到过这样一个问题,访问页面时出现408错误,下面一聚教程网将为大家介绍408错误出现的原因以及408错误的解决办法。 HTTP 408错误出现原因: HTT...2017-01-22
  • Photoshop打开PSD文件空白怎么解决

    有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • php无刷新利用iframe实现页面无刷新上传文件(1/2)

    利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25
  • 安卓手机wifi打不开修复教程,安卓手机wifi打不开解决方法

    手机wifi打不开?让小编来告诉你如何解决。还不知道的朋友快来看看。 手机wifi是现在生活中最常用的手机功能,但是遇到手机wifi打不开的情况该怎么办呢?如果手机wifi...2016-12-21
  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • 连接MySql速度慢的解决方法(skip-name-resolve)

    最近在Linux服务器上安装MySql5后,本地使用客户端连MySql速度超慢,本地程序连接也超慢。 解决方法:在配置文件my.cnf的[mysqld]下加入skip-name-resolve。原因是默认安装的MySql开启了DNS的反向解析。如果禁用的话就不能...2015-10-21
  • AI源文件转photoshop图像变模糊问题解决教程

    今天小编在这里就来给photoshop的这一款软件的使用者们来说下AI源文件转photoshop图像变模糊问题的解决教程,各位想知道具体解决方法的使用者们,那么下面就快来跟着小编...2016-09-14
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • Zend studio文件注释模板设置方法

    步骤:Window -> PHP -> Editor -> Templates,这里可以设置(增、删、改、导入等)管理你的模板。新建文件注释、函数注释、代码块等模板的实例新建模板,分别输入Name、Description、Patterna)文件注释Name: 3cfileDescriptio...2013-10-04
  • php文件上传你必须知道的几点

    本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
  • ant design中upload组件上传大文件,显示进度条进度的实例

    这篇文章主要介绍了ant design中upload组件上传大文件,显示进度条进度的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-29