天天看點

資料庫common安全滲透測試與防範 - sqlmap

sqlmap , sql注入 , 自動滲透 , boolean-based blind , time-based blind , error-based , union query-based , stacked queries , out-of-band

1. 支援主流資料庫

mysql, oracle, postgresql, microsoft sql server, microsoft access, ibm db2, sqlite, firebird, sybase, sap maxdb, hsqldb and informix database management systems.

2. 支援6種sql注入手段

boolean-based blind, time-based blind, error-based, union query-based, stacked queries and out-of-band.

3. 支援直連資料庫的方式,攻擊一些暴露在公網的資料庫,如建立udf函數(支援某些資料庫),實施越權操作等(前提是能登陸資料庫,然後利用資料庫的漏洞或功能)

攻擊手段,透過udf以及資料庫的功能,實施越權操作(比如利用資料庫的dblink,通路資料庫内網的其他主機),(比如通過資料庫超級使用者,建立udf,執行作業系統的指令)。

4. 支援通過注入的方式,枚舉或提取資料庫使用者、密碼hash(可以用于破譯明文密碼),表、列等

攻擊手段,通常實施的方式是查詢系統表,前提是提取到資料庫的指紋(如品牌,版本),然後根據對應資料庫版本提取對應的系統表.

enumerate users, password hashes, privileges, roles, databases, tables and columns.

automatic recognition of password hash formats and support for cracking them using a dictionary-based attack.

5. 支援通過注入的方式,導出資料,使用者可以控制導出的列、根據條件過濾資料等。

攻擊手段,資料查詢、導入導出

support to dump database tables entirely, a range of entries or specific columns as per user's choice.

the user can also choose to dump only a range of characters from each column's entry.

6. 支援通過注入的方式,在所有資料庫中搜尋指定表名,或者在所有資料庫的表中搜尋指定列名。

攻擊手段,比如從url裡,可以猜測出列名,但是不知道表名,通過查詢系統表可以得到包含該列名的表名。

support to search for specific database names, specific tables across all databases or specific columns across all databases' tables.

this is useful, for instance, to identify tables containing custom application credentials where relevant columns' names contain string like name and pass.

7. 支援通過sql注入的方式,或者直連資料庫的方式,從資料庫所在的作業系統下載下傳檔案,或者上傳檔案到資料庫所在的作業系統。

攻擊手段,通過資料庫的檔案通路接口,以及資料庫的大對象接口。

support to download and upload any file from the database server underlying file system when the database software is mysql, postgresql or microsoft sql server.

8. 支援通過sql注入,或者直連資料庫的方式,在資料庫所在的作業系統中,執行作業系統的指令

攻擊手段,通過資料庫的udf接口,調用作業系統syscall接口執行作業系統指令。

support to execute arbitrary commands and retrieve their standard output on the database server underlying operating system when the database software is mysql, postgresql or microsoft sql server.

9. 支援通過sql注入的方式,讓攻擊機與資料庫所在的作業系統建立tpc會話

攻擊手段,通過端口代理的服務,類似這樣的方法

<a href="https://github.com/digoal/blog/blob/master/201406/20140614_01.md">《使用 ssh -r 建立反向/遠端tcp端口轉發代理》</a>

support to establish an out-of-band stateful tcp connection between the attacker machine and the database server underlying operating system.

this channel can be an interactive command prompt, a meterpreter session or a graphical user interface (vnc) session as per user's choice.

10. 支援通過sql注入的方式,入侵資料庫所在作業系統,并從普通os使用者提升為超級使用者

攻擊手段,udf

support for database process' user privilege escalation via metasploit's meterpreter getsystem command.

sqlmap是一個比較正常的自動化滲透測試工具,主要針對http協定、或者直連資料庫的方式進行攻擊或漏洞探測。

可以利用的漏洞都是正常的,并沒有針對資料庫指紋(指資料庫的版本)實施特殊的攻擊。

比如postgresql資料庫的安全頁面,列出了所有版本的已知漏洞,在哪個版本存在,在哪個版本修複。

<a href="https://www.postgresql.org/support/security/">https://www.postgresql.org/support/security/</a>

而這些漏洞,sqlmap并沒有整合。除非你自己去擴充sqlmap。

也就是說,通過了sqlmap的滲透測試,并不代表安全。

1. sql注入

防範手段,應用程式使用綁定變量,或者使用過濾規則。

sql注入成功是後面的攻擊基礎。

2. 資料庫使用者密碼hash

指sql注入成功後。

防範手段1,業務使用普通使用者連接配接資料庫,那麼它拿不到其他使用者的密碼hash。

防範手段2,隐藏或加密使用者密碼hash,任何情況下,你都隻能看到加密後的hash。(需修改資料庫核心來實作)

3. 上傳下載下傳資料庫主機檔案

防範手段1,業務使用普通使用者連接配接資料庫,那麼它無法讀取或者寫入檔案。

防範手段2,禁止一切檔案讀寫操作權限的函數。

4. 調用資料庫主機作業系統指令

防範手段,業務使用普通使用者連接配接資料庫,那麼它無法讀取或者寫入檔案。也就無法建立udf。

5. 把資料庫主機變成殭屍電腦,或者跳闆機

6. 通路資料庫主機才能通路的私有網絡

防範手段1,業務使用普通使用者連接配接資料庫,那麼它無法讀取或者寫入檔案。也就無法建立udf。

防範手段2,對有網絡通路權限的插件,實行ip位址過濾,隻能通路白名單内的ip,比如資料庫的dblink插件,需要有白名單過濾機制。(需要修改資料庫核心來實作)

防範手段3,隔離資料庫主機與其他主機的網絡通路。

前面已經提到了sqlmap的資料庫安全檢測并不完善,僅僅針對一些正常的攻擊方法。

畢竟他們的團隊不是專業搞資料庫的,是以如果需要更加完善的資料庫安全加強方法,可以參考如下文檔。

<a href="https://github.com/digoal/blog/blob/master/201410/20141009_01.md">《postgresql 密碼 安全指南》</a>

<a href="https://github.com/digoal/blog/blob/master/201506/20150601_01.md">《postgresql 資料庫 安全指南》</a>

<a href="https://github.com/digoal/blog/blob/master/201509/20150925_01.md">《postgresql cancel 安全漏洞》</a>

<a href="https://github.com/digoal/blog/blob/master/201612/20161224_01.md">《dba專供 岡本003系列 - 資料庫安全第一,過個好年》</a>

<a href="https://github.com/digoal/blog/blob/master/201701/20170112_01.md">《postgresql psql 安全設定資料庫使用者密碼的方法之一》</a>

<a href="https://github.com/digoal/blog/blob/master/201307/20130710_01.md">《postgresql views privilege attack and security with security_barrier(視圖攻擊)》</a>

<a href="https://github.com/digoal/blog/blob/master/201404/20140414_01.md">《postgresql leakproof function in rule rewrite("attack" security_barrier views)》</a>

<a href="https://github.com/digoal/blog/blob/master/201509/20150929_01.md">《postgresql 安全陷阱 - 利用觸發器或規則,結合security invoker函數制造反噬陷阱》</a>

<a href="https://github.com/digoal/blog/blob/master/201510/20151022_01.md">《開放dblink , fdw帶來的安全隐患》</a>

<a href="https://github.com/digoal/blog/blob/master/201511/20151130_01.md">《postgresql 安全警鐘長鳴》</a>

<a href="https://github.com/digoal/blog/blob/master/201307/20130727_01.md">《postgresql 9.4 patch : row-level security》</a>

<a href="https://github.com/digoal/blog/blob/master/201606/20160614_02.md">《enterprisedb (ppas) oracle相容性virtual private database(vpd) 資料隔離以及目前缺陷》</a>

sqlmap支援直連資料庫的方式,是以我們可以使用它來測試資料庫是否存在正常漏洞。

比如獲得作業系統權限,通路資料庫主機的私網主機等。(對于雲廠商來說,這個是比較緻命的問題。)

通常雲資料庫如果提供給使用者的是普通賬号,如果是超級賬号,那麼要做到對雲自身的保護,資料庫核心的改動量會非常龐大。

雲資料庫可能運作在實體機也可能運作在虛拟機。

雲資料庫所在環境可能還有其他的主機,對使用者是隔離的,但是雲資料庫主機也許能通路它們。

注意,sqlmap隻包含了一些正常的安全測試手段,是以通過了sqlmap的安全測試,并不代表這個雲資料庫是安全的。

例子

申請一個雲資料庫廠商的rds,根據對應的rds 資料庫版本,編譯udf二進制。

<a href="https://github.com/sqlmapproject/udfhack">https://github.com/sqlmapproject/udfhack</a>

比如postgresql 9.4

下載下傳udf.c檔案

<a href="https://github.com/sqlmapproject/udfhack/blob/master/linux/64/lib_postgresqludf_sys/lib_postgresqludf_sys.c">https://github.com/sqlmapproject/udfhack/blob/master/linux/64/lib_postgresqludf_sys/lib_postgresqludf_sys.c</a>

使用9.4的postgresql版本編譯

使用sqlmap的直連資料庫方式進行測試,一會要用到這個lib_postgresqludf_sys.so檔案,使用postgresql大對象導出的方式将它導出到資料庫主機的/tmp目錄下。

sqlmap使用方法詳見

<a href="https://github.com/sqlmapproject/sqlmap/wiki/usage">https://github.com/sqlmapproject/sqlmap/wiki/usage</a>

檢測連接配接是否有效

擷取資料庫指紋,擷取使用者密碼hash,擷取有哪些資料庫,擷取表清單,擷取資料等等

基于使用者自定義函數,可以直接攻入作業系統,調用作業系統指令,等等。

目前具備危害的udf包括c語言的udf,或者其他例如postgresql plpythonu之類的untrust語言的udf。

sqlmap_src/sqlmap.conf

<a href="http://sqlmap.org/">http://sqlmap.org/</a>

<a href="http://baike.baidu.com/view/9472.htm">http://baike.baidu.com/view/9472.htm</a>

<a href="https://hit-alibaba.github.io/interview/basic/network/http.html">https://hit-alibaba.github.io/interview/basic/network/http.html</a>