0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

nginxで、ちょっとしたセキュリティ対策

0
Posted at

前の記事

OCIのOracle LinuxでWebサーバ(nginx)を動かす(HTTPまで)

OCIのOracle Linuxにいれた「nginxを使っている!」主張の強すぎるエラー表示を変える

1.403 Forbiddenエラーをみる

1.1.デフォルトの403エラー画面

デフォルト・・

403-001.jpeg

1.2.403エラーの意味

意味は・・

もともとは?

HTTP標準仕様である RFC 9110 (Semantics) に定義 

エラーの意味は?

サーバーがリクエストを理解し、その実行を拒否したこと。
拒否される理由は、サーバーの設定ミス、ファイル権限の異常、あるいはキュリティ機能の誤作動など。

1.3.403エラーの危険性

実はちょっと危険

簡単な説明

Webサーバ上のあるファイルがファイル権限でアクセスできないようになっています、と伝えることになります。
=ファイル権限で守られているファイルは、アクセス権やいろいろな大切なファイルです。
 ↓
「そのファイルがあるかもよ!」と、攻撃しようとして偵察している人に知らせることになります。

1.4.403エラーへの対応

実は認められた対応があります。

どういうこと?

存在を知られたくない非公開ファイルにアクセスされたとき、403ではなくあえて 404 Not Found を返すこともRFC 9110で認められています。

引用元

https://www.rfc-editor.org/info/rfc9110/#status.403

15.5.4. 403 Forbidden
The 403 (Forbidden) status code indicates that the server understood the request but refuses to fulfill it. A server that wishes to make public why the request has been forbidden can describe that reason in the response content (if any).

If authentication credentials were provided in the request, the server considers them insufficient to grant access. The client SHOULD NOT automatically repeat the request with the same credentials. The client MAY repeat the request with new or different credentials. However, a request might be forbidden for reasons unrelated to the credentials.

An origin server that wishes to "hide" the current existence of a forbidden target resource MAY instead respond with a status code of 404 (Not Found).

2.nginx設定

2.1.エラー動作変更のための設定

2.1.1.エラー動作変更のための設定変更

エラー表示変更設定追加

エラー表示変更設定追加変更前

[opc@test-server ~]$ cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
(省略)
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
            root /usr/share/nginx/htmlerror;
            internal;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/htmlerror;
            internal;
        }   
(省略)

error_page 404 /404.html;
404の時には404.htmlを表示しています。

変更:error_pageのところを変更します。

        error_page 403 404 /404.html;
        location = /404.html {
            root /usr/share/nginx/htmlerror;
            internal;
        }

error_page 403 404 /404.html;
404のまえに、403を追記します。(スペースで区切りましょう)

2.2.2.エラー動作変更設定変更の確認

文法確認コマンドで動作確認

変更後確認

[opc@test-server ~]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2.2.3.エラー動作変更設定変更の反映(再起動)

nginx再起動

nginxの再起動

[opc@test-server www]$ sudo systemctl stop nginx
[opc@test-server www]$ sudo systemctl start nginx

2.3.動作確認

動作確認

403-002.jpeg

※具体的なファイル名などはあえて記載しません・・。

最後に

記事の内容はCC BY-SA 4.0(著作者の情報とCCライセンス継承はお願いします。商用利用・改変・再配布は問題なし)です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?