search
Homephp教程php手册php防注入及开发安全详细解析

php防注入及开发安全详细解析

Jun 13, 2016 am 11:42 AM
phpprincipleBasicSafetydeveloplevelinjectionofprogrammerexperienceparsedetailed

1、PHP注入的基本原理
程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对 用户输入数据的合法性进行判断,使应用程序存在安全隐患。用户可以提交一段数据 库查询代码,根据程序返回的结果,获得某些他想得知的数据,这就是所谓的 SQL Injection,即SQL注入。 受影响的系统:对输入的参数不进行检查和过滤的系统.

SQL注入过程
正常来讲,我们通过地址接收一些必要的参数如:
页面中我们会使用 2 写入到SQL语句中
正常情况:Select * From Table where id=2

bkJia.php?id=2
如果我们对SQL语句熟悉,就知道2 我们可以替换成我们需要的SQL语句
如:and exists (select id from admin)

2、防止注入的几种办法
其实原来就是我们需要过滤一些我们常见的关键字和符合如:
Select,insert,update,delete,and,*,等等
例子:

复制代码 代码如下:


function inject_check($sql_str) {
return preg_match('/select|insert|update|delete|/'|///*|/*|/././/|/.//|union|into|load_file|outfile/i', $sql_str);      // 进行过滤
}


或者是通过系统函数间的过滤特殊符号
Addslashes(需要被过滤的内容)

3、PHP其他地方安全设置
register_globals = Off 设置为关闭状态
SQL语句书写时尽量不要省略小引号和单引号

复制代码 代码如下:


Select * From Table Where id=2 (不规范)
Select * From ·Table· Where ·id·='2' (规范)


提高数据库命名技巧,对于一些重要的字段可根据程序特点命名
对于常用方法加以封装,避免直接暴露SQL语句

正确的使用 $_POST $_GET $_SESSION 等接受参数,并加以过滤

Statement
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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor