wordpress 修補使用者外洩漏洞

請將下面內容完整寫入外掛檔案。

<?php
/*
Plugin Name: Security Hardening
Description: Block WordPress author enumeration.
Version: 1.1
*/

defined('ABSPATH') || exit;

/**
 * 提前攔截 ?author=1、?author=2 等帳號列舉。
 */
add_action('parse_request', function ($wp) {
    if (is_admin()) {
        return;
    }

    if (
        isset($_GET['author']) &&
        is_scalar($_GET['author']) &&
        preg_match('/^\d+$/', (string) $_GET['author'])
    ) {
        wp_safe_redirect(home_url('/'), 302);
        exit;
    }
}, 1);

/**
 * 阻擋 /author/帳號名稱/ 作者彙整頁。
 */
add_action('template_redirect', function () {
    if (!is_admin() && is_author()) {
        wp_safe_redirect(home_url('/'), 302);
        exit;
    }
}, 1);

/**
 * 防止 WordPress 將 ?author=1 自動轉成 /author/帳號名稱/。
 */
add_filter('redirect_canonical', function ($redirect_url, $requested_url) {
    if (
        isset($_GET['author']) &&
        is_scalar($_GET['author']) &&
        preg_match('/^\d+$/', (string) $_GET['author'])
    ) {
        return false;
    }

    return $redirect_url;
}, 1, 2);

parse_request 會在 WordPress 完成網址與查詢變數解析後執行,適合提前辨識 author 請求;template_redirect 則是在載入前端模板前執行,可利用 is_author() 判斷作者頁。

檔案位置

確認檔案是:

wp-content/plugins/security-hardening/security-hardening.php

目錄結構應為:

wp-content/
└── plugins/
    └── security-hardening/
        └── security-hardening.php

修改後到 WordPress 後台:

外掛 → 已安裝的外掛

先停用 Security Hardening,再重新啟用。

驗證方式

請使用瀏覽器無痕模式測試:

https://你的網址/?author=1

以及:

https://你的網址/author/已知帳號名稱/

兩者都應該回到首頁。

也可以用指令確認:

curl -I "https://你的網址/?author=1"

預期看到:

HTTP/2 302
location: https://你的網址/

若仍無效

很可能是快取造成,請清除:

  • WordPress 快取外掛
  • Nginx FastCGI Cache
  • Cloudflare Cache
  • 主機商頁面快取
  • 瀏覽器快取

測試時也可以加隨機參數:

https://你的網址/?author=1&test=12345

另外要注意,這段程式只處理作者列舉,不會封鎖 REST API users。還要在同一個外掛加入:

add_filter('rest_endpoints', function ($endpoints) {
    unset($endpoints['/wp/v2/users']);
    unset($endpoints['/wp/v2/users/(?P<id>[\d]+)']);

    return $endpoints;
});

再測試:

https://你的網址/wp-json/wp/v2/users

正常應回傳:

{
  "code": "rest_no_route"
}

ubuntu 24.04 限制使用者登入後僅能使用選單中的指令

以下是一套在 Ubuntu 24.04 中,限制使用者 optim 以「密碼登入 SSH 後,只能透過選單執行指定指令」的完整 SOP
此作法適合維運帳號、代管帳號、委外人員,避免任意下指令。


一、設計原則(先說清楚邏輯)

  1. SSH 可用密碼登入
  2. 登入後 不給 Shell
  3. 只執行一個「選單程式(Menu Script)」
  4. 選單內只呼叫 白名單指令
  5. 使用者無法:
    • cd
    • vi / nano
    • bash
    • ssh 到別台
    • Ctrl+C 跳出

二、建立使用者 optim(若已存在可略過)

sudo useradd -m optim
sudo passwd optim

三、準備「受控選單腳本」

1️⃣ 建立目錄

sudo mkdir -p /opt/optim-menu
sudo chown root:root /opt/optim-menu
sudo chmod 755 /opt/optim-menu

2️⃣ 建立選單腳本

sudo vi /opt/optim-menu/menu.sh

內容如下(可依需求調整指令):

#!/bin/bash

while true; do
    clear
    echo "================================="
    echo "   Optim Maintenance Menu"
    echo "================================="
    echo "1) 查看系統時間"
    echo "2) 查看磁碟使用量"
    echo "3) 查看服務狀態 (nginx)"
    echo "4) 查看系統負載"
    echo "0) 登出"
    echo "================================="
    read -p "請選擇操作: " choice

    case "$choice" in
        1)
            /usr/bin/date
            ;;
        2)
            /usr/bin/df -h
            ;;
        3)
            /usr/bin/systemctl status nginx --no-pager
            ;;
        4)
            /usr/bin/uptime
            ;;
        0)
            exit 0
            ;;
        *)
            echo "無效選項"
            ;;
    esac

    echo
    read -p "按 Enter 繼續..."
done

3️⃣ 設定權限(非常重要)

sudo chown root:root /opt/optim-menu/menu.sh
sudo chmod 755 /opt/optim-menu/menu.sh

⚠️ 使用絕對路徑(/usr/bin/df),避免 PATH 被濫用。


四、限制 optim 使用者的 Shell

方法一(最推薦):使用 ForceCommand

編輯 SSH 設定

sudo vi /etc/ssh/sshd_config

在檔案最下方加入:

Match User optim
    ForceCommand /opt/optim-menu/menu.sh
    PasswordAuthentication yes
    AllowTcpForwarding no
    X11Forwarding no
    PermitTTY yes

重新啟動 SSH

sudo systemctl restart ssh

五、(加強)防止逃逸技巧

1️⃣ 禁用 .bashrc / .profile

sudo chown root:root /home/optim/.bashrc /home/optim/.profile
sudo chmod 644 /home/optim/.bashrc /home/optim/.profile

或直接清空:

sudo truncate -s 0 /home/optim/.bashrc
sudo truncate -s 0 /home/optim/.profile

2️⃣ 限制 sudo(若不需要)

sudo visudo

確認 沒有

optim ALL=(ALL) ALL

六、測試驗證清單(必做)

請實際測試以下行為:

測試項目預期結果
SSH 密碼登入✅ 可登入
登入後看到 bash❌ 不可
出現選單
嘗試輸入 bash❌ 無效
Ctrl + C❌ 不可中斷
選 0✅ 正常登出
scp 傳檔❌ 失敗

七、常見錯誤與排查

🔴 登入直接斷線

  • menu.sh 沒有執行權限
  • ForceCommand 路徑錯誤

👉 解法:

chmod +x /opt/optim-menu/menu.sh

🔴 指令顯示 command not found

  • 沒用「絕對路徑」

👉 一律改成:

/usr/bin/xxx

八、進階強化(可選)

  • 搭配 auditd 記錄所有執行行為
  • 將 menu.sh 改為 Python / whiptail UI
  • 搭配 PAM + 時段限制
  • SSH 僅允許指定 IP

九、總結

此 SOP 能達成:

✅ 密碼登入
✅ 僅能選單操作
✅ 指令白名單
✅ 無 Shell、無逃逸
✅ 符合實務維運安全

若你需要:

  • 含審計版(auditd)
  • 多層子選單
  • 一鍵部署 Script
  • 搭配 sudo 精準放權

可直接說明使用情境,我可幫你客製化。


信心指數:96%