Table of Contents
php 安全过滤
Home php教程 php手册 php 安全过滤

php 安全过滤

Jun 13, 2016 am 08:54 AM
android

php 安全过滤

/*ansic码-Url码表: http://www.w3school.com.cn/tags/html_ref_urlencode.html

-----------------------------------------------------------------------------------------------------------------

1、验证过滤用户的输入

即使是最普通的字母数字输入也可能是危险的,列举几个容易引起安全问题的字符:

!$ ^ & * ( ) ~ [ ] \ | { } ' " ; ? - `

在数据库中可能有特殊意义的字符:

'" ; \

还有一些非打印字符:

字符\x00或者说ASCII 0,NULL或FALSE

字符\x10和\x13,或者说ASCII 10和13,\n \r

字符\x1a或者说ASCII 26,表示文件的结束

输入错误的参数类型,也可能导致程序出现意想不到的错误。

输入过多的参数值,可能导致溢出等错误。

2、对于文件的路径与名称的过滤

文件名中不能包含二进制数据,否则可能引起问题。

一些系统允许Unicode多字节编码的文件名,但是尽量避免,应当使用ASCII的字符。

虽然Unix系统几乎可以在文件名设定中使用任何符号,但是应当尽量使用 - 和 _ 避免使用其他字符。

同时需要限定文件名的长度。

3、防止SQL注入

检查用户输入的类型,当用户输入的为数字时可以使用如下方式:

使用is_int()函数(或is_integer()或is_long()函数)

使用gettype()函数

使用intval()函数

使用settype()函数

检查用户输入字符串的长度使用strlen()函数。

检查日期或时间是否是有效的,可以使用strtotime()函数

4、防止XSS攻击

xss攻击一个常用的方法就是注入HTML元素执行js脚本,php中已经内置了一些防御的函数(如htmlentities或者htmlspecialchars)

5、过滤用户提交的URL

如果允许用户输入一个URL用来调用一个图片或者链接,你需要保证他不传入javascript:或者vbscript:或data:等非http协议。

可以使用php的内置函数parse_url()函数来分割URL,然后做判断。

6、防止远程执行--下表列出了跟Shell相关的一些字符:

远程执行通常是使用了php代码执行如eval()函数,或者是调用了命令执行如exec(),passthru(),proc_open(),shell_exec(),system()或popen()。

注入php代码:php为开发者提供了非常多的方法可以来调用允许php脚本,我们就需要注意对用户可控的数据进行过滤。

7、Shell命令执行

PHP提供了一些可以直接执行系统命令的函数,如exec()函数或者 `(反引号)。

PHP的安全模式会提供一些保护,但是也有一些方式可以绕过安全模式:

1、上传一个Perl脚本,或者Python或Ruby等,服务器支持的环境,来执行其他语言的脚本可绕过PHP的安全模式。

2、利用系统的缓冲溢出漏洞,绕过安全模式。

跟Shell相关的一些字符:

名称 字符 ASCII 16进制 URL编码 HTML编码

换行 10 \x0a %0a

感叹号 ! 33 \x21 %21 !

双引号 " 34 \x22 %22 "或"

美元符号 $ 36 \x24 %24 $

连接符 & 38 \x26 %26 &或amp

单引号 ' 39 \x27 %27 '

左括号 ( 40 \x28 %28 (

右括号 ) 41 \x29 %29 )

星号 * 42 \x2a %2a *

连字符号 - 45 \x2d %2d -

分号 ; 59 \x3b %3b ;

左尖括号

右尖括号 > 62 \x3e %3e >

问号 ? 63 \x3f %3f ?

左方括号 [ 91 \x5b %5b [

反斜线 \ 92 \x5c %5c \

右方括号 ] 93 \x5d %5d ]

插入符 ^ 94 \x5e %5e ^

反引号 ` 96 \x60 %60 `

左花括号 { 123 \x7b %7b {

管道符 | 124 \x7c %7c |

右花括号 } 125 \x7d %7d }

波浪号 ~ 126 \x7e %7e ~

-----------------------------------------------------------------------------------------------------------------

安全过滤函数代码*/

/**

* 安全过滤输入[jb]

*/

function check_str($string, $isurl = false)

{

$string= preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/','',$string); //去掉控制字符

$string= str_replace(array("\0","%00","\r"),'',$string);//\0表示ASCII 0x00的字符,通常作为字符串结束标志;这三个都是可能有害字符

empty($isurl)&& $string =preg_replace("/&(?!(#[0-9]+|[a-z]+);)/si",'&',$string);//HTML里面可以用xx;来对一些字符进行编码,比如 (空格), ? Unicode字符等,A(?!B) 表示的是A后面不是B,所以作者想保留 ?类似的 HTML编码字符,去掉其他的问题字符

$string= str_replace(array("%3C",'

$string= str_replace(array("%3E",'>'),'>',$string);

$string= str_replace(array('"',"'","\t",' '),array('“','‘','',' '),$string);

returntrim($string);

}

/**

* 安全过滤类-过滤javascript,css,iframes,object等不安全参数 过滤级别高

* @param string $value 需要过滤的值

* @return string

*/

function fliter_script($value) {

$value=preg_replace("/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i","&111n\\2",$value);

$value= preg_replace("/(.*?)/si","",$value);

$value= preg_replace("/(.*?)/si","",$value);

$value= preg_replace ("//iesU", '', $value);

return$value;

}

/**

* 安全过滤类-过滤HTML标签

* @param string $value 需要过滤的值

* @return string

*/

function fliter_html($value) {

if(function_exists('htmlspecialchars')) return htmlspecialchars($value);

returnstr_replace(array("&", '"', "'", ""), array("&", "\"", "'",""), $value);

}

/**

* 安全过滤类-对进入的数据加下划线 防止SQL注入

* @param string $value 需要过滤的值

* @return string

*/

function fliter_sql($value) {

$sql= array("select", 'insert', "update", "delete","\'", "\/\*","\.\.\/", "\.\/","union", "into", "load_file","outfile");

$sql_re=array("","","","","","","","","","","","");

returnstr_replace($sql, $sql_re, $value);

}

/**

* 安全过滤类-通用数据过滤

* @param string $value 需要过滤的变量

* @return string|array

*/

function fliter_escape($value) {

if(is_array($value)) {

foreach($value as $k => $v) {

$value[$k]= self::fliter_str($v);

}

}else {

$value= self::fliter_str($value);

}

return$value;

}

/**

* 安全过滤类-字符串过滤 过滤特殊有危害字符

* @param string $value 需要过滤的值

* @return string

*/

function fliter_str($value) {

$badstr= array("\0", "%00", "\r", '&', ' ','"', "'", "", " ", "%3C", "%3E");

$newstr= array('', '', '', '&', ' ', '"', ''', "", " ","");

$value = str_replace($badstr, $newstr, $value);

$value =preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $value);

return$value;

}

/**

* 私有路劲安全转化

* @param string $fileName

* @return string

*/

function filter_dir($fileName) {

$tmpname= strtolower($fileName);

$temp= array(':/',"\0", "..");

if(str_replace($temp, '', $tmpname) !== $tmpname) {

returnfalse;

}

return$fileName;

}

/**

* 过滤目录

* @param string $path

* @return array

*/

public function filter_path($path) {

$path= str_replace(array("'",'#','=','`','$','%','&',';'), '', $path);

returnrtrim(preg_replace('/(\/){2,}|(\\\){1,}/', '/', $path), '/');

}

/**

* 过滤PHP标签

* @param string $string

* @return string

*/

public function filter_phptag($string) {

returnstr_replace(array(''), array('', '?>'), $string);

}

/**

* 安全过滤类-返回函数

* @param string $value 需要过滤的值

* @return string

*/

public function str_out($value) {

$badstr= array("", "%3C", "%3E");

$newstr= array("", "");

$value = str_replace($newstr, $badstr, $value);

returnstripslashes($value); //下划线

}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship Sep 10, 2024 am 06:45 AM

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T

See all articles