一、BlueCMS的安裝
1、下載BlueCMS的安裝包並解壓,將解壓後的文件上傳至web伺服器上。
2、使用瀏覽器打開安裝文件install.php,出現歡迎頁面後點擊「下一步」按鈕。
<?php
/**
* BlueCMS v1.6 安裝程序
*
* Author: switer
* Time : 2020.12.22
* Email : switer@qq.com
*/
if (!function_exists('mysqli_connect')):
die('PHP環境中沒有安裝mysqli擴展!');
endif;
if (!function_exists('session_start')):
die('PHP環境中沒有安裝session擴展!');
endif;
if (file_exists('./install.lock')):
die('已經安裝過BlueCMS,如果需要重新安裝,請手動刪除install.lock文件!');
endif;
$errors = array();
if (!empty($_POST)):
$database_host = $_POST['database_host'];
$database_username = $_POST['database_username'];
$database_password = $_POST['database_password'];
$database_name = $_POST['database_name'];
$database_table_prefix = trim($_POST['database_table_prefix']);
if (empty($database_host)):
$errors[] = "請輸入MySQL資料庫伺服器地址!";
endif;
if (empty($database_username)):
$errors[] = "請輸入MySQL資料庫登錄帳號!";
endif;
if (empty($database_name)):
$errors[] = "請輸入MySQL資料庫名稱!";
endif;
if (empty($database_table_prefix)):
$errors[] = "請輸入資料庫表前綴!";
endif;
if (empty($errors)):
$link = mysqli_connect($database_host, $database_username, $database_password);
if (!$link):
$errors[] = "連接MySQL伺服器失敗,請確認資料庫登錄信息是否正確!";
endif;
if (!mysqli_select_db($link, $database_name)):
$errors[] = "選擇資料庫失敗,請確認您輸入的資料庫名稱是否正確!";
endif;
if (empty($errors)):
foreach (glob("sql/*.sql") as $filename):
$sql = file_get_contents($filename);
$sql = str_replace("`cms_", "`" . $database_table_prefix, $sql);
$sqls = preg_split("/;\s*\n/", $sql);
foreach ($sqls as $sql):
if (!empty($sql)):
mysqli_query($link, $sql);
endif;
endforeach;
endforeach;
$config_file = './var/config.php';
$config_content = file_get_contents($config_file);
$config_content = str_replace('{database_host}', addslashes($database_host), $config_content);
$config_content = str_replace('{database_username}', addslashes($database_username), $config_content);
$config_content = str_replace('{database_password}', addslashes($database_password), $config_content);
$config_content = str_replace('{database_name}', addslashes($database_name), $config_content);
$config_content = str_replace('{database_table_prefix}', $database_table_prefix, $config_content);
file_put_contents('./var/config.php', $config_content);
touch('./install.lock');
echo '請手動刪除install.php文件,確保系統安全!
';
endif;
endif;
endif;
?>
';
echo '
3、填寫MySQL資料庫的相關信息,包括資料庫伺服器地址、用戶名、密碼、資料庫名稱以及數據表前綴等,然後點擊「下一步」按鈕。
4、按照頁面提示依次填寫網站相關信息,包括網站名稱、管理員帳號、管理員密碼等,最後點擊「完成安裝」按鈕即可。
二、BlueCMS v1.6審計
BlueCMS v1.6是一款國產CMS建站系統,但是在安全方面存在多個漏洞。
1、信息泄露:BlueCMS的安裝文件install.php中沒有對資料庫連接相關信息進行保護,攻擊者可以輕易地獲取到資料庫的用戶名和密碼等敏感信息。
<label>資料庫伺服器地址:</label>
<input type="text" name="database_host" value="localhost" /><br/>
<label>MySQL登錄帳號:</label>
<input type="text" name="database_username" value="root" /><br/>
<label>MySQL登錄密碼:</label>
<input type="password" name="database_password" value="" /><br/>
<label>MySQL資料庫名稱:</label>
<input type="text" name="database_name" value="bluecms" /><br/>
<label>數據表前綴:</label>
<input type="text" name="database_table_prefix" value="cms_" /><br/>
2、SQL注入漏洞:在搜索功能中沒有對用戶輸入的內容進行充分的過濾,導致攻擊者可以通過構造SQL語句獲取到用戶數據或者進行資料庫的惡意操作。
public function search()
{
$keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
if (strlen($keyword) fetch_row()[0];
$total_pages = ceil($total_posts / $posts_per_page);
$query = $this->db->query("SELECT * FROM `" . $this->db->prefix . "posts` WHERE $conditions ORDER BY $order_by LIMIT $offset, $posts_per_page");
$found_posts = array();
while ($row = $query->fetch_assoc()) {
$row['url'] = $this->permalink($row['slug']);
$row['excerpt'] = $this->excerpt($row['body']);
$found_posts[] = $row;
}
$this->render('search', array(
'found_posts' => $found_posts,
'keyword' => $keyword,
'pagination' => array(
'total_pages' => $total_pages,
'current_page' => $page
)
));
}
三、BlueCMS找漏洞
1、上傳頭像功能存在文件上傳漏洞:上傳頭像功能沒有對文件類型進行校驗,攻擊者可以上傳帶有惡意腳本的文件,從而實現對網站的攻擊。
public function save_avatar()
{
// 初始化
$error_code = 0;
$upload_dir = getcwd() . '/uploads/avatar/';
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true);
chmod($upload_dir, 0777);
}
$user_id = $this->current_user_id();
// 上傳新頭像
$filename = $this->generate_hashed_filename();
$full_path = $upload_dir . $filename;
if (move_uploaded_file($_FILES['file']['tmp_name'], $full_path)) {
$this->db->query("UPDATE `" . $this->db->prefix . "users` SET avatar = '$filename' WHERE id = $user_id");
} else {
$error_code = 1;
}
// 響應請求
$response = array();
if ($error_code == 0) {
$response['status'] = 'success';
$response['avatar_url'] = '/uploads/avatar/' . $filename;
} else {
$response['status'] = 'error';
$response['message'] = '上傳頭像失敗,請重試!';
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($response);
}
2、代碼設計缺陷:BlueCMS的代碼結構存在缺陷,在代碼審計過程中發現缺少對用戶輸入內容的完整校驗及過濾,需要對部分模塊進行重構,以確保脆弱性攻擊的安全性。
四、BlueCMS v1.6安裝失敗
安裝過程中,有一些常見錯誤需要注意:
1、文件許可權問題:請確認您的伺服器是否具有創建文件和目錄的許可權,否則會導致安裝失敗。
2、MySQL版本問題:BlueCMS需要使用 MySQL 5.6 或更高版本。
3、伺服器環境問題:請確保您的伺服器環境符合 BlueCMS 的要求,包括 PHP 5.4.0 或更高版本、MySQL資料庫、Apache 或 Nginx 等 web 伺服器。
五、BlueCMS v1.6代碼審計
1、獲取當前用戶信息的代碼:
public function current_user()
{
if (isset($_SESSION['user_id'])) {
return $this->db->query("SELECT * FROM `" . $this->db->prefix . "users` WHERE id = " . intval($_SESSION['user_id']))->fetch_assoc();
} else {
return null;
}
}
2、登錄驗證的代碼:
public function do_login()
{
$username = isset($_POST['username']) ? trim($_POST['username']) : '';
$password = isset($_POST['password']) ? trim($_POST['password']) : '';
$user = $this->db->query("SELECT * FROM `" . $this->db->prefix . "users` WHERE username = '" . $this->db->escape_string($username) . "' AND password = MD5(CONCAT('" . $this->db->escape_string($password) . "', salt))")->fetch_assoc();
if ($user) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['username'];
header('Location: /admin');
} else {
header('Location: /admin/login.php?msg=登錄失敗,請檢查您的用戶名和密碼!');
}
}
3、對文章進行保存的代碼:
public function save_post()
{
$title = isset($_POST['title']) ? trim($_POST['title']) : '';
$slug = isset($_POST['slug']) ? trim($_POST['slug']) : $title;
$body = isset($_POST['body']) ? $_POST['body'] : '';
$status = isset($_POST['status']) ? intval($_POST['status']) : 0;
$user_id = $this->current_user_id();
$post_id = isset($_POST['id']) ? intval($_POST['id']) : 0; if (empty($title)) {
header('Location: /admin/new.php?msg=文章標題不能為空!');
return;
}
if (empty($body)) {
header('Location: /admin/new.php?msg=文章內容不能為空!');
return;
}
if (!empty($slug) && !$this->is_slug_valid($slug)) {
header('Location: /admin/new.php?msg=文章別名已經被使用,請輸入其他別名!');
return;
}
if ($post_id) {
$this->db->query("UPDATE `" . $this->db->prefix . "posts` SET title = '" . $this->db->escape_string($title) . "', slug = '" . $this->db->escape_string($slug) . "',body = '" . $this->db->escape_string($body) . "', status = " . $status . " WHERE id = " . intval($post_id));
} else {
$this->db->query("INSERT INTO `" . $this->db->prefix . "posts` (title, slug, body, status, user_id) VALUES ('" . $this->db->escape_string($title) . "', '" . $this->db->escape_string($slug) . "', '" . $this->db-&
原創文章,作者:NFBB,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/138454.html