開始閱讀此文之前請安裝好OSSIM v4.15
OpenVAS釆用***測試原理,利用Scanner模塊中的腳本引擎對目標進行安全檢測。Openvas的Scanner的掃描性能依賴於同時進行掃描的併發進程數,不同的硬件環境上可設置的最有效併發掃描數各不相同,Openvas的掃描引擎設備可在保證系統穩定的前提下達到最佳的掃描性能,對於大型網絡使用標準設備進行部署可大大降低安裝和維護成本。
腳本引擎根據用戶提交的配置與要求,首先對腳本進行加載與調度,按照順序依次調用腳本並解析執行,實現掃描功能。
0.什麼是NASL腳本
NASL腳本語言是起初針對網絡安全掃描工具Nessus開發的,該腳本語言具有方便快捷,靈活性強,安全性高的特點,便於用戶根據新出現的漏洞特性,寫出有針對性的檢測腳本。
NASL(Nessus Attack Scripting Language)腳本具有其特定的格式要求,其腳本內容可分析兩個部分,第一個部分是腳本的註冊信息,包含腳本名稱、CVE-ID、漏洞描述信息、腳本類型,依賴的腳本或端口等信息。第二部分是針對相應漏洞的檢測代碼,此部分是腳本內容的核心,包含添加引用的庫,腳本初始化,腳本執行及檢測返回信息等操作。
NASL腳本書寫格式大致如下所示。
if(description)
{
/*註冊信息:包含腳本名稱、CVE-ID、漏洞描述信息、腳本類型,依賴的腳本或端口等*/
Script-name
CVE-ID;
exit(0)
}
/*腳本代碼*/
NASL腳本語言功能強大,用於設計安全檢測腳本足以。該腳本語言的語法類似於C語言,但是比C要簡單許多,在使用NASL腳本語言時,不必考慮對象的類型,內存的分配與釋放,變量聲明等問題,只要認真設計漏洞的檢測方法即可。由於漏洞掃描需在網絡環境中進行,所以NASL語言提供了豐富的網絡相關函數,包括套接字相關函數,用戶可根據檢測需要創建套接字,與目標主機進行通信;原始報文處理函數,用戶可根據掃描需求構建檢測報文,並對返回信息進行分析,提取重要內容;NASL語言還提供了一些工具參數來簡化檢測腳本的書寫,例如對目標主機系統、端口等信息的提取函數。
NASL語言還提供了豐富的字符串處理函數。NASL語言允許使用=、<和>等操作符以及正則表達式對字符串進行模式匹配,並且提供多種實現模式匹配功能的函數。NASL語言包含字符串長度計算,內容複製,數據類型轉換、大小寫轉換等功能函數,方便腳本書寫。
1.插件功能舉例****
在/var/lib/openvas/plugins/目錄下,分析cross_site_scriping.nasl腳本,本文重點對該文件進行講解,請大家在閱讀之前準備好腳本。跨站腳本漏洞是Web應用的常見漏洞,下面將開始對cross_site_scriping.nasl腳本分析,總結此類漏洞檢測過程。
76 script_summary("Determine if the remote host is vulnerable to Cross Site Sc ripting vulnerability");
77 script_category(ACT_GATHER_INFO);
78 script_family("Web Servers");
77行script_category(ACT_GATHER_INFO);設置腳本的類別,這些類別有:
ACT_GATH (信息採集類腳本)
ACT_ATTACK (嘗試獲取遠程主機權限腳本)
ACT_DENIAL(拒絕服務***腳本)
ACT_SCANNER(端口掃描腳本)
Category代表了腳本執行的優先級,不是隨便掃描,這種優先級的設定符合常規掃描過程,我們在掃描初期需要對目標主機的端口信息,系統信息,服務信息進行收集,根據這些已知信息合理的進行後續測試,這種測試也是按照由弱到強的順序執行,爲什麼呢?如果首先就進行最強的漏洞掃描,相當於進行最危險的測試,一旦目標主機存在相應漏洞,立即系統就會崩潰,整個測試就無法全面進行。
圖片描述
NASL腳本的結構與參數在後面內容將會進行詳細介紹,此處先對腳本中category參數進行簡單描述,category在腳本調度的過程中具有非常重要的作用,category代表了腳本執行的優先級,通過宏對各類category值的優先級進行定義,NASL腳本按照下表中category優先級由高到低執行,相同優先級按照腳本id順序執行。
這種優先級的設定符合一般掃描過程的邏輯,我們在掃描初期,需要對目標網絡的端口信息、系統信息、服務信息等進行收集,根據這些已知信息,合理進行後續的測試,而且再測試過程中也要按照由弱到強的順序執行,如果首先進行高危測試,且目標系統存在相應漏洞,則會導致系統崩潰,測試無法繼續進行。NASL腳本在完成加載調度的過程中,需要經過三次處理,每次處理都定義了符合要求的鏈表結構對腳本及相關信息進行保存。
32 include("revisions-lib.inc");
33 tag_summary = "The remote web server seems to be vulnerable to a Cross Site Scripting
32行 include(“revisions-lib.inc”) 添加引用的類庫。
63 script_id(10815);
64 script_version("$Revision: 41 $");
65 script_tag(name:"last_modification", value:"$Date: 2013-11-04 20:00:12 +010 0 (Mon, 04 Nov 2013) $");
63行 script_id(10815); 添加 openvas ID。
但有些腳本會同時出現script_cve_id(“CVE-2010-2963”、”CVE-2010-3067”)這代表添加CVE ID,CVE(Common Vulnerabilities and Exposure)這類例子大家可以參看deb_2126_1.nasl腳本,如果該漏洞有CVE編號,可以再這個字段給出。同樣在freebsd_php5-gd.nasl腳本里還出現了script_bugtraq_id(33002),我們可以把CVE看作是一個詞典,它提供了許多的交叉引用,現在大量公司都宣佈他們的安全產品或數據庫都和CVE相兼容,也就是能夠利用CVE中漏洞名稱同其它CVE兼容的產品進行交叉引用,通過CVE中爲每個漏洞分配一個唯一的名稱,在其它使用了這個名稱的工具、網站、數據庫以及服務中檢索到相關信息,同時自身關於該漏洞的信息也能夠被檢索到。所以在網絡安全評估時,利用CVE可以找出修補漏洞的措施,CVE可以提供很好的指導。大家如果要具體查詢這些ID信息可以用grep命令
grep –ir “script_bugtraq_id” /var/lib/openvas/plugins/
78 script_family("Web Servers");
79 script_copyrigcript_summary("Determine if the remote host is vulnerable to Cross Site Sc ripting vulnerability");
78行 script_family("Web Servers");設置腳本所屬的族(family)。
NASL對此沒有明確規定,插件作者可以自己定義腳本所屬的族。Openvas 使用的族有:
Debian Local Security Checks
Fedora Local Security Checks
Mandrake Local Security Checks
HP-UX Local Security Checks
Ubuntu Local Security Checks
CentOS Local Security Checks
Windows : Microsoft Bulletins
FreeBSD Local Security Checks
Red Hat Local Security Checks
Solaris Local Security Checks
CentOS Local Security Checks
Web application abuses
FreeBSD Local Security Checks
Buffer overflow
Firewalls
Web Servers
RPC
General
CISCO
Databases
Useless services
Default Accounts
Nmap NSE net
Gain a shell remotely
Malware
Port scanners
IT-Grundschutz
SMTP problems
Gentoo Local Security Checks
SuSE Local Security Checks
FTP
Denial of Service
Windows
Service detection
實際上這些族就是主要的***種類。
下面繼續看79行內容:
79行 script_copyright("Copyright (C) 2001 SecuriTeam, modified by Chris Sullo and Andrew Hintz");
設置腳本的版權信息
80行 script_dependendes("find_service.nasl", "httpver.nasl");
第80行腳本說明腳本依賴關係,如果要讓cross_site_scriping.nasl正常運行,必須依賴find_service.nasl"、"httpver.nasl"這兩個腳本。
80 77 script_category(ACT_GATHER_INFO);
81 78 script_family("Web Servers");t("Copyright (C) 2001 SecuriTeam, modified by Chris Sullo and Andrew Hintz");
82 script_dependencies("find_service.nasl", "httpver.nasl");
83 script_require_ports("Services/www", 80);
84 if (revcomp(a: OPENVAS_VERSION, b: "6.0+beta5") >= 0) {
85 script_tag(name : "summary" , value : tag_summary);
86 }
87 exit(0);
88 }
89
90 #
91 # The script code starts here
92 #
93
94 include("http_func.inc");
95 include("http_keepalive.inc");
81行script_require_ports("Services/www", 80);
表明執行此腳本所需的目標服務器的端口信息;
95行 port = get_http_port(default:80);獲取服務器端口
123行if(get_port_state(port))判斷端口是否打開
134行req = http_get(item:url, port:port);發送帶有***性的請求
• 注意:nasl腳本主要是對的描述,只是說明的的步驟。
136 req = http_get(item:url, port:port);
137 r = http_keepalive_send_recv(port:port, data:req, bodyonly: TRUE);
135行 r= http_keepalive_send_recv(port:port, data:req, bodyonly: TRUE); 接收響應
165行 set_Kb_item(name:string("www/", port, "/generic_xss"), value:TRUE);165 set_kb_item(name:string("www/", port, "/generic_xss"), value:TRUE);
查看知識庫中是否已存在此漏洞信息,知識庫中保存了各類掃描所需相關信息,例如:
主機存活信息(Host/dead)
主機提供服務的信息(ftp/no_mkdir)
端口掃描信息(services/ftp)
測試登錄信息(SMB/login SMB/password)
在腳本測試過程中,如果收集到有用的信息,將通過set_Kb_item()這個函數,在知識庫中增加相應條目;當需要對知識庫中的相關信息進行調用時,則利用get_Kb_item()函數進行讀取。
2.NASL常用函數介紹
open_sock_tcp()、open_sock_udp(),分別用於打開一個TCP或者UDP套接字。
close(), 關閉一個端口。
forge_ip_packet(), 函數構造一個新的IP報文。
forge_tcp_packet() , 構造TCP報文。
set_tcp_elements(), 可修改TCP報文的內容。
forge_udp_packet(), 構造UDP報文。
send_packet(), 函數發送報文。
pcap_next() , 函數將從使用的最後一個接口讀取一個報文。
this_host(), 獲取運行腳本的主機IP地址,沒有參數。
get_host_name() , 獲取當前被測試主機的主機名,沒有參數。
get_host_ip() , 獲取當前被測試主機的IP地址,沒有參數。
get_host_open_port() ,獲取遠程主機打開的第一個端口號,沒有參數。
get_port_stat(<portnum>),獲取端口的狀態。
除此之外還有大量的正則表達式函數這裏就不一一舉例。
3.OpenVAS腳本加載過程分析
如果你需要開發插件對於,需要了解的一個重點問題是,當啓動OpenVAS時,由openvassd進程加載所有的腳本(/var/lib/openvas/plugins/下的*.nasl腳本),存儲爲argiist結構(數量可變的參數列表指針);根據客戶端傳遞的配置信息,選取需要的腳本,加載爲scheduler_plugin結構;最後根據選取腳本的category將腳本分組,組織腳本的執行順序,保存到plugins_scheduler-struct結構。
OpenVAS關於腳本管理設計的非常合理,初始的加載腳本是保存的argiist鏈表內容,供後面進步一選取腳本及組織腳本執行順序時調用,可有效減少內存的使用,提高工作效率。在腳本引擎的工作中,將腳本執行收集到的信息保存在知識庫中,可有效避免重複掃描,減少不必要的資源浪費,提高工作效率。知識庫中條目結構定義如下:
Struct kb_item
{
char name; /*知識庫中條目名稱*/
char type; / 條目的類型 int 或 string /
union
{
char *v_str;
int v_int;
} V; /*知識庫中條目的值*/
struct kb item next; /指針指向下一條目 */
};
知識庫中保存的信息包含各類掃描信息例如:
主機存活信息(Host/dead)。
主機提供的服務信息(ftp/no_mkdir)。
端口掃描信息(Services/ftp)。
測試登錄信息(SMB/login; SMB/password)。
用戶檢測時進行的系統設置信息。
在腳本執行過程中,如果收集到有用信息,將通過set_kb_item()函數,在知識庫中增加相應條目;如果需要對知識庫中的相關信息進行調用,則使用get_kb_item()函數進行讀取操作。
腳本在執行時如果有依賴的系統環境、端口狀態等信息,會到知識庫中查詢,並根據查詢結果做出合理的操作。例如:某腳本的執行需要目標主機的80端口打開,通過知識庫查詢,發現目標的80端口是關閉的,那麼此腳本的執行將放棄,且該腳本對應線程關閉;如果發現目標的80端口處於開啓狀態,那麼該腳本將順利執行,對目標進行檢測。通過對知識庫中信息的查詢判斷,可有效提高腳本執行的效率,一些無意義的掃描將被過濾。
4.插件同步過程
在OSSIM命令行中輸入命令“openvas-nvt-sync”,以下爲顯示爲輸出結果。
[i] This script synchronizes an NVT collection with the 'OpenVAS NVT Feed'.
[i] The 'OpenVAS NVT Feed' is provided by 'The OpenVAS Project'.
[i] Online information about this feed: 'http://www.openvas.org/openvas-nvt-feed.html'.
[i] NVT dir: /var/lib/openvas/plugins
[i] Will use rsync
[i] Using rsync: /usr/bin/rsync
[i] Configured NVT rsync feed: rsync://feed.openvas.org:/nvt-feed
OpenVAS feed server - http://www.openvas.org/
This service is hosted by Intevation GmbH - http://intevation.de/
All transactions are logged.
Please report synchronization problems to [email protected].
If you have any other questions, please use the OpenVAS mailing lists
or the OpenVAS IRC chat. See http://www.openvas.org/ for details.
receiving incremental file list
deleting gb_openssl_38562.nasl.asc
deleting gb_openssl_38562.nasl
./
COPYING
588 100% 574.22kB/s 0:00:00 (xfer#1, to-check=13347/13355)
COPYING.GPLv2
18002 100% 17.17MB/s 0:00:00 (xfer#2, to-check=13346/13355)
COPYING.files
1819904 100% 1.77MB/s 0:00:00 (xfer#3, to-check=13345/13355)
DDI_Directory_Scanner.nasl
32957 100% 32.74kB/s 0:00:00 (xfer#4, to-check=13342/13355)
DDI_Directory_Scanner.nasl.asc
198 100% 0.20kB/s 0:00:00 (xfer#5, to-check=13341/13355)
... ...
同步數萬個插件時間比較長,消耗資源不大,可以去喝杯咖啡啦。
5.插件更新過程
Tips:以下命令操作佔用CPU資源比較多
#perl /usr/share/ossim/scripts/vulnmeter/updateplugins.pl migrate / 比較消耗CPU和磁盤I/O /
2018-09-07 07:27:33 Framework profile has been found...
2018-09-07 07:27:33 Deleting all tasks in 192.168.11.150 ...
2018-09-07 07:27:33 updateplugins: configured to not updateplugins
2018-09-07 07:27:33 updateplugins: configured to not repair DB
2018-09-07 07:27:33 BEGIN - DUMP PLUGINS
2018-09-07 07:29:01 FINISH - DUMP PLUGINS [ Process took 88 seconds ]
2018-09-07 07:29:01 BEGIN - IMPORT PLUGINS
2018-09-07 07:30:00 FINISH - IMPORT PLUGINS [ 40473 plugins - Process took 59 seconds ]
2018-09-07 07:30:00 BEGIN - UPDATE CATEGORIES
2018-09-07 07:30:00 FINISH - UPDATE CATEGORIES [ Process took 0 seconds ]
2018-09-07 07:30:00 BEGIN - UPDATE FAMILIES
2018-09-07 07:30:00 FINISH - UPDATE FAMILIES [ Process took 0 seconds ]
2018-09-07 07:30:00 BEGIN - UPDATE OPENVAS_PLUGINS
2018-09-07 07:30:03 FINISH - UPDATE OPENVAS_PLUGINS [ Process took 3 seconds ]
2018-09-07 07:30:03 BEGIN - UPDATE NESSUS_PREFERENCES
2018-09-07 07:30:03 show tables like "vuln_nessus_preferences_defaults"
2018-09-07 07:30:03 updateprefs: Getting plugin preferences
2018-09-07 07:30:05 FINISH - UPDATE NESSUS_PREFERENCES [ Process took 2 seconds ]
2018-09-07 07:30:06 Creating Deep profile...
2018-09-07 07:30:06 Filling categories...............
2018-09-07 07:30:06 Done
2018-09-07 07:30:06 Filling families.............................................................
2018-09-07 07:30:06 Done
2018-09-07 07:30:06 Filling plugins...
2018-09-07 07:30:13 Filling preferences in Alienvault DB...
2018-09-07 07:30:14 Done
2018-09-07 07:30:14 Deep profile inserted
2018-09-07 07:30:15 Creating Default profile...
2018-09-07 07:30:15 Filling categories...............
2018-09-07 07:30:15 Done
2018-09-07 07:30:15 Filling families.............................................................
2018-09-07 07:30:15 Done
2018-09-07 07:30:15 Filling plugins...
2018-09-07 07:30:23 Filling preferences in Alienvault DB...
2018-09-07 07:30:24 Done
2018-09-07 07:30:24 Default profile inserted
2018-09-07 07:30:24 Creating Ultimate profile...
2018-09-07 07:30:24 Filling categories...............
2018-09-07 07:30:24 Done
2018-09-07 07:30:24 Filling families.............................................................
2018-09-07 07:30:24 Done
2018-09-07 07:30:24 Filling plugins...
2018-09-07 07:30:32 Filling preferences in Alienvault DB...
2018-09-07 07:30:33 Done
2018-09-07 07:30:33 Ultimate profile inserted
2018-09-07 07:30:33 BEGIN - UPDATE PORT SCANNER
2018-09-07 07:30:35 FINISH - UPDATE PORT SCANNER [ Process took 2 seconds ]
Updating plugin_sid vulnerabilities scanner ids
plugins fetched
Updating...
Script id:94151, Name:IT-Grundschutz M4.288: Sichere Administration von VoIP-Endger?ten, Priority:0
Script id:703073, Name:Debian Security Advisory DSA 3073-1 (libgcrypt11 - security update), Priority:1
Script id:804624, Name:Adobe Reader Plugin Signature Bypass Vulnerability (Windows), Priority:2
Script id:868149, Name:Fedora Update for kernel FEDORA-2014-9959, Priority:5
Script id:95048, Name:IT-Grundschutz M5.145: Sicherer Einsatz von CUPS, Priority:0
Script id:842216, Name:Ubuntu Update for linux USN-2616-1, Priority:4
Script id:105036, Name:Open××× Detection, Priority:0
Script id:868005, Name:Fedora Update for audacious-plugins FEDORA-2014-8183, Priority:1
Script id:869350, Name:Fedora Update for springframework FEDORA-2018-6862, Priority:5
… …
Script id:105084, Name:Multiple ManageEngine Products Arbitrary File Upload Vulnerability, Priority:3
Script id:867751, Name:Fedora Update for python-keystoneclient FEDORA-2014-5555, Priority:3
Script id:882209, Name:CentOS Update for nss CESA-2018:1185 centos6, Priority:2
Script id:842209, Name:Ubuntu Update for libmodule-signature-perl USN-2607-1, Priority:5
大約經過一刻鐘等待終於更新完成。注意,該過程需要一氣呵成,中途不能強制退出。
6. 測試NASL腳本
將自己寫的插件複製到openvas插件庫目錄:
/var/lib/openvas/plugins
加載插件:
openvassd
7. 重建插件庫
openvasmd –rebuild
注意:參數“rebuild”代表從一個正在運行的掃描器(openvassd)中重建數據庫信息。
假如你在OpenVAS操作過程中遇到各種困惑,可以關注我的2019年最新作品,也可以加入OSSIM技術討論羣大家一起學習,共同進步。