3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

前段にWAFがあるWebサーバにmod_rewrite設定した後、特定IPだけ許可する設定の問題点と解決方法

Posted at

背景

ある日、ざっくり以下のようなインフラのWebサーバに2つの設定をする必要がありました。
【Internet】【WAFサーバ】【ウェブサーバ】

※ 設定内容
① RemoteIPヘッダーがWAFのIPで上書きされないようにする。
② ApacheプロトコルのアクセスをWAFのIPだけ許可する。

初期対応

最初対応した内容は以下の通りでした。

① RemoteIPヘッダーがWAFのIPで上書きされないようにする。

echo "RemoteIPHeader X-Forwarded-For" > /etc/httpd24/conf.d/mod_remoteip.conf
echo "RemoteIPTrustedProxy <WAFのIPアドレス1> <WAFのIPアドレス2>" >> /etc/httpd24/conf.d/mod_remoteip.conf
service httpd24-httpd restart

※ RemoteIPTrustedProxy
The RemoteIPTrustedProxy directive restricts which peer IP addresses (or address blocks) will be trusted to present a valid RemoteIPHeader value of the useragent IP.

② ApacheプロトコルのアクセスをWAFだけ許可する。

# 2.2系以前のApache
Order deny,allow
Deny from all
Allow from <WAFIPアドレス>
 
# 2.4系以降のApache
Require all denied
Require ip <WAFIPアドレス>

//or

# WAFのIPアドレス 以外からのアクセスを拒否
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^(<WAFIPアドレス1>|<WAFIPアドレス2>)$
RewriteRule ^.*$ - [F,L]

※ 参考
🔗Apache Module mod_remoteip
🔗Apache 2.4系でIP制限の設定方法
🔗mod_rewrite を使ったアクセス拒否いろいろ

問題

しかし、当然の結果として①設定によって②の設定が意図した通り反映されませんでした。
(RemoteIPヘッダーを実際アクセスしたクライアントIPに調整した後、
RemoteIPヘッダーでアクセスを制限しているから)

解決方法

Apache設定だけで解決出来るように頑張って見たんですが、
結局、iptablesで対応しました。
(CentOS7場合firewald)

iptables 設定(WAF以外から80, 443 portアクセスを拒否)


iptables -I INPUT -p tcp -s 0.0.0.0/0 --dport 80 -j DROP
iptables -I INPUT -p tcp -s <WAFのIPアドレス1> --dport 80 -j ACCEPT
iptables -I INPUT -p tcp -s <WAFのIPアドレス2> --dport 80 -j ACCEPT

iptables -I INPUT -p tcp -s 0.0.0.0/0 --dport 443 -j DROP
iptables -I INPUT -p tcp -s <WAFのIPアドレス1> --dport 443 -j ACCEPT
iptables -I INPUT -p tcp -s <WAFのIPアドレス2> --dport 443 -j ACCEPT

service iptables restart

👏

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?