# SQLI 注入原理 講師:陳煜仁、侯智晟 [TOC] --- ## 要先懂得知識 XAMPP PHP or Javascript(node.js/deno) or ..... MYSQL(推薦) --- ## 甚麼是SQL * SQL (Structured Query Language),SQL的全名是結構化查詢語言。 * SQL使我們有能力訪問數據庫 ![](https://i.imgur.com/Ea8lxsC.png) --- ## 實作資料庫操作 --- ### 下載XAMPP ![](https://i.imgur.com/NlGz4Ze.png) --- ### 資料庫基本語法 * 登入 > mysql -u root * 列出所有資料庫 > show databases; * 使用資料庫 > use 資料庫名稱; --- ![](https://i.imgur.com/MSQUWOF.png) --- * 列出資料表 > show tables; ![](https://i.imgur.com/6f3cz8F.png) --- * 建立資料表 > create table 資料表名稱(number int , name char(50), age int); --- ![](https://i.imgur.com/ZU8VYou.png) --- * 查詢資料(select) > select * from 資料表名稱; * 插入欄位資料 > insert into 資料表名稱(欄位1,欄位2,...欄位N)values('值1','值2',... '值N'); * 刪除條件值資料 > delete from 資料表名稱 where 條件式 (例如 sn='5' 或id='91001' ); --- ![](https://i.imgur.com/6JLTpyw.png) --- ![](https://i.imgur.com/3E0b7ku.png) --- ### 練習 建立2個student的資料表(分別為student1,student2),裡面輸入3個學生資料欄位是 number,name,age。value 自行新增 ![](https://i.imgur.com/CtWWGm8.png) --- ![](https://i.imgur.com/24doGGb.png) --- * UNION用法(聯集) > SELECT 欄位名稱, 欄位名稱, 欄位名稱 FROM 資料表名稱 -> UNION -> SELECT 欄位名稱, 欄位名稱, 欄位名稱 FROM 資料表名稱 -> UNION -> SELECT 欄位名稱," ", 欄位名稱 FROM 資料表名稱; --- ![](https://i.imgur.com/Pbq3U81.png) --- * ORDER BY 用法 > SELECT * FROM 資料表名稱 ORDER BY 欄位名稱; ![](https://i.imgur.com/oEG93Zg.png) --- ## 什麼是SQL Injection ![](https://i.imgur.com/Yxnaj5i.png) 圖檔來源:**顏郁茨** 大神 [SQL injection vulnerability allowing login bypass](https://portswigger.net/web-security/sql-injection/lab-login-bypass) --- ## 注入類型 ![](https://i.imgur.com/CA9zNoU.jpg) 圖檔來源:**顏郁茨** 大神 --- ### Error-Based(報錯) 第一步 Proof 輸入 https://meowhecker.php?search=' \&apos; \<html encode> -> ' \&quot; \<html encode> -> " - \-\- - \# --- 輸出 Status code = 500 Internal Server Error LAB https://portswigger.net/web-security/sql-injection/lab-retrieve-hidden-data --- ### Union-Based(聯合) 輸入 https://meowhecker.php?search='+union+select+null,null+from+user-- 如果有成功 會回傳東西 代表我們已經猜到 欄位數目 ``` '+union+select+'1' -- ``` LAB 1 (基礎) https://portswigger.net/web-security/sql-injection/union-attacks/lab-determine-number-of-columns --- LAB 2(進階) [SQL injection attack, listing the database contents on non-Oracle databases](https://portswigger.net/web-security/sql-injection/examining-the-database/lab-listing-database-contents-non-oracle) 示範影片: https://www.youtube.com/watch?v=Dhn9H11c1Hg --- ### Bolean-Based 他是盲注類別(比較複雜攻擊技巧) #### 猜帳號密碼orData(網頁不回傳 錯誤訊息 或 無法撈資料在頁面上) 我們需要自己去構成 true and false 判斷句 並且豆果網頁特定回傳 來判別我們猜得是不是對的 --- True https://meowhecker.php?id = "not exists id " or 1=1; False https://meowhecker.php?id = "not exists id " or 1=2; 這裡比較複雜 有興趣可以看一下 影片 Blind Injection 示範 https://youtu.be/aQO4BSjNaio --- ### Time-based (時間) 透過 sleep() 函式 來測試 當bolean 測試無效時 Time-based 類似 delay 概念 LAB https://portswigger.net/web-security/sql-injection/blind/lab-time-delays (需要 Burp suite 或 用curl 修改Header cookie) --- ### Out-of-band 這個比較不常見也比較難理解 (先跳過) 但簡單來說XD 就是DB 可能有開一些遠端服務 我們可以拼接一些東西 讓DB 以http/Dns 方式 來把DB資料洩漏給我們 不再同一個Session上 所以才叫out of band.(簡易版本解說) --- ## 注入流程 - 收集資訊 - 測試可能弱點 ### Step 1 (收集資訊) - 瀏覽所有網頁 找出會跟後端互動的input (Burp suite->site map) --- Step 2 (測試可能弱點) - 測error-based 配合 union 做測試 - 測bolean-based 配合 union 做測試 - 測time-based 配合 union 做測試 - 測Out of band --- ## 收集資訊(Mysql) version() ![](https://i.imgur.com/8qWMPrr.png) @@basedir ![](https://i.imgur.com/IKbJqkl.png) --- @@datadir ![](https://i.imgur.com/20hLOyR.png) @@hostname ![](https://i.imgur.com/tufzu2y.png) --- ## 例子 ``` select user,password from mysql.user ``` ![](https://i.imgur.com/CSdNv9n.png) --- ### 查DB ``` select databases() ``` ### 查tables ``` select table_name from information_schema.tables; ``` ``` select table_name from information_schema.tables where table_schema='sqlitest'; ``` ![](https://i.imgur.com/Sqy2BEI.png) --- ### 查Columns ``` select column_name from information_schema.columns; ``` ``` select column_name from information_schema.columns where table_schema='sqlitest' and table_name='user1'; ``` ![](https://i.imgur.com/MmHqc3e.png) --- <!-- ## UNION (LABs) [SQL injection UNION attack, finding a column containing text](https://portswigger.net/web-security/sql-injection/union-attacks/lab-find-column-containing-text) [SQL injection UNION attack, retrieving data from other tables](https://portswigger.net/web-security/sql-injection/union-attacks/lab-retrieve-data-from-other-tables) [SQL injection UNION attack, retrieving multiple values in a single column](https://portswigger.net/web-security/sql-injection/union-attacks/lab-retrieve-multiple-values-in-single-column) [SQL injection attack, querying the database type and version on MySQL and Microsoft](https://portswigger.net/web-security/sql-injection/examining-the-database/lab-querying-database-version-mysql-microsoft) --> <!-- ``` select * from customer where id =1 and updatexml(1,concat(0x7e,database(),0x7e,user(),@@datadir),1);# ``` ![](https://i.imgur.com/TFwYoyv.png) --> ## Reference http://note.drx.tw/2012/12/mysql-syntax.html https://zh.wikipedia.org/zh-tw/SQL https://portswigger.net/ --- 課程結束
{"metaMigratedAt":"2023-06-17T14:53:13.305Z","metaMigratedFrom":"Content","title":"SQLI 注入原理","breaks":true,"description":"講師:陳煜仁、侯智晟","contributors":"[{\"id\":\"332e2d0e-4428-4f35-89bd-685b5efb50da\",\"add\":6557,\"del\":2666},{\"id\":\"41013aa4-33a6-4a84-b067-22c95f0900e1\",\"add\":101,\"del\":100},{\"id\":\"dbd5ae00-e2fa-46dd-acbe-00c3a35126e6\",\"add\":1627,\"del\":323}]"}
    674 views