Home php教程 php手册 php中sql注入漏洞示例

php中sql注入漏洞示例

Jun 13, 2016 am 09:44 AM
php sql one main exist develop article injection loopholes Example

 这篇文章主要介绍了php中sql注入漏洞示例,大家在开发中一定要注意

在开发网站的时候,出于安全考虑,需要过滤从页面传递过来的字符。通常,用户可以通过以下接口调用数据库的内容:URL地址栏、登陆界面、留言板、搜索框等。这往往给骇客留下了可乘之机。轻则数据遭到泄露,重则服务器被拿下。    一、SQL注入的步骤   a)  寻找注入点(如:登录界面、留言板等)   b)  用户自己构造SQL语句(如:' or 1=1#,后面会讲解)   c)  将sql语句发送给数据库管理系统(DBMS)   d)  DBMS接收请求,并将该请求解释成机器代码指令,执行必要的存取操作   e)  DBMS接受返回的结果,并处理,返回给用户     因为用户构造了特殊的SQL语句,必定返回特殊的结果(只要你的SQL语句够灵活的话)。   下面,我通过一个实例具体来演示下SQL注入   二、SQL注入实例详解(以上测试均假设服务器未开启magic_quote_gpc)   1)  前期准备工作   先来演示通过SQL注入漏洞,登入后台管理员界面   首先,创建一张试验用的数据表:    代码如下: CREATETABLE `users` (   `id`int(11) NOT NULL AUTO_INCREMENT,   `username`varchar(64) NOT NULL,   `password`varchar(64) NOT NULL,   `email`varchar(64) NOT NULL,   PRIMARYKEY (`id`),   UNIQUEKEY `username` (`username`)   )ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;       添加一条记录用于测试:    代码如下: INSERTINTO users (username,password,email)   VALUES('MarcoFly',md5('test'),'marcofly@test.com');       接下来,贴上登录界面的源代码: 代码如下: Sql注入演示
 
    Sql注入演示                                                                                            
用户名:
密  码:
 
    当用户点击提交按钮的时候,将会把表单数据提交给validate.php页面,validate.php页面用来判断用户输入的用户名和密码有没有都符合要求(这一步至关重要,也往往是SQL漏洞所在)   代码如下:    代码如下: 登录验证   请重新登录!";          }   ?>       注意到了没有,我们直接将用户提交过来的数据(用户名和密码)直接拿去执行,并没有实现进行特殊字符过滤,待会你们将明白,这是致命的。 代码分析:如果,用户名和密码都匹配成功的话,将跳转到管理员操作界面(manager.php),不成功,则给出友好提示信息。 到这里,前期工作已经做好了,接下来将展开我们的重头戏:SQL注入   2) 构造SQL语句   填好正确的用户名(marcofly)和密码(test)后,点击提交,将会返回给我们“欢迎管理员”的界面。   因为根据我们提交的用户名和密码被合成到SQL查询语句当中之后是这样的:     复制代码 代码如下: select * from users where username='marcofly' andpassword=md5('test')     很明显,用户名和密码都和我们之前给出的一样,肯定能够成功登陆。但是,如果我们输入一个错误的用户名或密码呢?很明显,肯定登入不了吧。恩,正常情况下是如此,但是对于有SQL注入漏洞的网站来说,只要构造个特殊的“字符串”,照样能够成功登录。   比如:在用户名输入框中输入:' or 1=1#,密码随便输入,这时候的合成后的SQL查询语句为: 代码如下: select * from users where username='' or 1=1#' and password=md5('')     语义分析:“#”在mysql中是注释符,这样井号后面的内容将被mysql视为注释内容,这样就不会去执行了,换句话说,以下的两句sql语句等价: 代码如下: select * from users where username='' or 1=1#' and password=md5('')   等价于  代码如下: select *from users where username='' or 1=1     因为1=1永远是都是成立的,即where子句总是为真,将该sql进一步简化之后,等价于如下select语句:  代码如下: select * from users   没错,该sql语句的作用是检索users表中的所有字段 小技巧:如果不知道' or 1=1#中的单引号的作用,可以自己echo 下sql语句,就一目了然了。 看到了吧,一个经构造后的sql语句竟有如此可怕的破坏力,相信你看到这后,开始对sql注入有了一个理性的认识了吧~ 没错,SQL注入就是这么容易。但是,要根据实际情况构造灵活的sql语句却不是那么容易的。有了基础之后,自己再去慢慢摸索吧。 有没有想过,如果经由后台登录窗口提交的数据都被管理员过滤掉特殊字符之后呢?这样的话,我们的万能用户名' or 1=1#就无法使用了。但这并不是说我们就毫无对策,要知道用户和数据库打交道的途径不止这一条。  
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles