LoginSignup
5
6

More than 3 years have passed since last update.

【WordPress】セキュリティ

Last updated at Posted at 2019-05-30

意図しないリダイレクトをさせない

概要

WordPressで、パーマリンクをデフォルトから変更している場合、URL/loginからURL/wp-login.phpへリダイレクトする仕組みになっている。またURL/adminとURL/dashboardはURL/wp-admin/へリダイレクトされる。

施策

//意図しないリダイレクトを行わなくする
remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );

投稿者アーカイブページへのアクセスを404ページへリダイレクト

概要

パラメータ「?author=1」の指定で投稿者ページにリダイレクトされるが、
その際のURLがログインIDになってしまうため、
投稿者アーカイブページへのアクセスを404ページへリダイレクトさせる。

例)
http://www.example.com/?author=1
 ↓
http://www.example.com/author/admin/
※「admin」がログインID

施策

/*--------------------------------------------------------
// 投稿者アーカイブページへのアクセスを404ページへリダイレクト
--------------------------------------------------------*/
function author_archive_redirect() {
    if($_GET['author'] !== null || $_POST['author'] !== null) {
        // 404ページを表示する
        wp_redirect( home_url('/404') );
        exit;
    }
}
add_action('init', 'author_archive_redirect');

トップ画像、バナー画像のページはホームにリダイレクト

概要

トップ画像、バナー画像のページはホームにリダイレクト

施策

/*--------------------------------------------------------
トップ画像、バナー画像のページはホームにリダイレクト
--------------------------------------------------------*/
function redirect_post_type() {
    global $wp_query;

    if (!is_admin()):
        ///投稿タイプ調べる
        $post_type_name = $wp_query->get_queried_object()->post_type;

        if(
            $post_type_name=='topimg'||
            $post_type_name=='bannerimg'
        ){
            wp_redirect( home_url() );
        }
    endif;

}
add_action( 'template_redirect', 'redirect_post_type' );

インストール後、install.phpを削除

概要

セキュリティのためインストール後、install.phpを削除

施策

wp-adminフォルダのinstall.phpを削除

※但し、アップグレードすると自動的に作成されてしまう。


ログイン画面にアクセス制限をかける

施策

wp-login.phpと同じフォルダに「.htaccess」を作成

IP制限の場合

.htaccess

# 特定のIPのみ ログイン
<Files wp-login.php>
Order deny,allow
Deny from all
Allow from 000.000.000.000 #許可するIPアドレス
</Files>

ベーシック認証の場合

.htaccess

<Files wp-login.php>
AuthUserFile /【アップロードする場所】/.htpasswd
AuthName "Please enter your ID and password"
AuthType Basic
require valid-user
</Files>

.htpasswd

test:SKjdpfPffdsw

”/index.html”から”/”にリダイレクトさせる

概要

“/index.html”から”/”にリダイレクトさせる

施策

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ /$1 [R=301,L]
</IfModule>

パーミッションは644か444
「# BEGIN WordPress 〜 # END WordPress内」に記述すると、管理画面>パーマリンク設定の更新で上書きされてしまうので、「# BEGIN WordPress 〜 # END WordPress」外に記入


httpからのアクセスをhttpsに変更

概要

httpからのアクセスをhttpsに変更

施策

.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>

パスワード認証

概要

パスワード認証

スクリプト

function my_password_form() {
    $htm = '';

    $htm .= '<form action="' . get_option('siteurl') . '/wppp.php" class="post-password-form" method="post">';
    $htm .= '<p>このコンテンツはパスワードで保護されています。閲覧するには以下にパスワードを入力してください。</p>';
    $htm .= '<p>';
    $htm .= '<label for="pwbox-8164">パスワード';
    $htm .= ' <input name="post_password" id="pwbox-8164" type="password" size="20">';
    $htm .= '</label>';
    $htm .= ' <input type="submit" name="Submit" value="' . esc_attr__('送信') . '">';
    $htm .= ' <input type="hidden" name="post_orkey" value="nrola7fznx9zib1epdkck2bci788twhg">';
    $htm .= '</p>';
    $htm .= '</form>';

    return $htm;

}
add_filter('the_password_form', 'my_password_form');

wppp.php
require __DIR__ . '/wp-load.php';

if ( get_magic_quotes_gpc() )
    $_POST['post_password'] = esc_attr($_POST['post_password']);
    $_POST['post_orkey'] = esc_attr($_POST['post_orkey']);

if($_POST['post_orkey']==='nrola7fznx9zib1epdkck2bci788twhg'){
    require_once ABSPATH . WPINC . '/class-phpass.php';
    $hasher = new PasswordHash( 8, true );
// 10 days
    setcookie('wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $_POST['post_password'] ) ), time() + 864000, COOKIEPATH);
}
wp_safe_redirect(wp_get_referer());

exit;
5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6