发布于2小时前2小时 背景这类漏洞常见于 WordPress 插件自定义的 wp_ajax_nopriv_* 接口:接口允许未登录访问,却只校验了请求参数或 nonce,没有校验调用者身份与权限,最终把外部输入写入 wp_options。本文给出一套适合授权测试和代码审计的漏洞记录模板,验证过程仅写入无业务影响的测试项,不修改管理员账号、站点地址或用户注册策略。适用范围:自研或已获授权的 WordPress 测试环境。不要在未授权站点发送验证请求。问题定位先确认插件是否注册了匿名 AJAX action,再沿回调函数追踪写配置的危险调用。重点关注 update_option、add_option、delete_option,以及封装后的批量配置保存函数。cd /var/www/html/wp-content/plugins/target-plugin grep -RInE \ 'wp_ajax_nopriv_|update_option|add_option|delete_option|check_ajax_referer|current_user_can' . # 进一步定位 action 与回调绑定关系 grep -RInE \ 'add_action[[:space:]]*\\([[:space:]]*[\"' 代码审计、调用链与关键函数定位示意 漏洞成因、验证条件与修复闭环示意
1小时前1小时 这类问题修复时不要只补 nonce。WordPress 的 nonce 主要防 CSRF,不等于权限控制;AJAX 回调里至少要同时做“登录态/能力校验 + nonce 校验 + 字段白名单”。如果该接口注册了 wp_ajax_nopriv_*,而业务本身是后台配置修改,应该直接删除匿名 hook。 add_action('wp_ajax_myplugin_save_settings', 'myplugin_save_settings'); // 不要注册:add_action('wp_ajax_nopriv_myplugin_save_settings', ...); function myplugin_save_settings() { if (!is_user_logged_in() || !current_user_can('manage_options')) { wp_send_json_error(array('message' => 'forbidden'), 403); } check_ajax_referer('myplugin_save_settings', 'nonce'); $allowed = array('api_url', 'log_level', 'enabled'); $input = wp_unslash($_POST); $clean = array(); foreach ($allowed as $key) { if (!array_key_exists($key, $input)) { continue; } switch ($key) { case 'api_url': $clean[$key] = esc_url_raw($input[$key]); break; case 'log_level': $levels = array('error', 'warning', 'info'); if (!in_array($input[$key], $levels, true)) { wp_send_json_error(array('message' => 'invalid log_level'), 400); } $clean[$key] = $input[$key]; break; case 'enabled': $clean[$key] = rest_sanitize_boolean($input[$key]); break; } } // 固定 option 名称,禁止从请求中接收 option_name。 update_option('myplugin_settings', $clean, false); wp_send_json_success(array('message' => 'saved')); } 前端生成 nonce 时,action 名要和后端一致: wp_localize_script('myplugin-admin', 'MyPluginAjax', array( 'url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('myplugin_save_settings'), )); 验证建议至少覆盖四组:未登录请求应为 403;普通订阅者即使拿到合法 nonce 也应为 403;管理员使用错误 nonce 应失败;只有管理员加合法 nonce 才能修改。先记录修复前 option 值,再用未登录请求回归,确认数据库没有变化: wp option get myplugin_settings --format=json curl -i -sS ' \ --data 'action=myplugin_save_settings' \ --data 'enabled=1' \ --data 'api_url= wp option get myplugin_settings --format=json 还需要全局搜索同插件中同类入口,避免只修一个 action: grep -RInE "wp_ajax_nopriv_|wp_ajax_|update_option\(|delete_option\(" wp-content/plugins/plugin-name 排查时重点看回调是否允许客户端传入 option_name、是否把整个 $_POST 直接写入、是否仅靠 is_admin() 判断权限。admin-ajax.php 请求中 is_admin() 本来就可能为真,不能作为授权依据。若配置里包含密钥,响应也不要回显完整值;可以额外记录操作者用户 ID、action、来源 IP 和变更字段名,但避免把密钥及 nonce 写入日志。
创建帐户或登录后发表意见