16
6

More than 1 year has passed since last update.

アクセスログにクライアントIPが表示されていなかったので調査してみた。

Last updated at Posted at 2022-04-03

はじめに

アクセス数が不定期に増加しているサーバーがあり、アクセスログを調査することとなった。

だけど、アクセスログにはクライアントIPが表示されていなかったので、その原因調査と解決までを備忘録として残す。

現状確認

クライアントIPが表示されていない。。。

/var/log/httpd/hogehoge.com-access_log
- - - [28/Mar/2022:18:41:12 +0900] "GET /wpgs/wp-content/themes/xxxxx/assets/image/xxxxxx HTTP/1.1" 200 92291 "https://xxxx.xxxx.xxx.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/--.-.----.-- Safari/---.--"

そもそもクライアントIPを表示させる設定になっているのか??

原因調査〜その1〜

まずはログフォーマットを確認してみる。

/etc/httpd/conf/httpd.conf
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined-elb
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>

CustomLogの設定だと、一番上のフォーマット(combined)が使われていることがわかる。

アクセス元IPを表示させる%hは設定されている。

でも今回はELB経由でのアクセスだから、クライアントIPを表示させるには、%hじゃなくて%{X-Forwarded-For}iを設定しないといけない。

Infomation
ELB経由のアクセスログにクライアントIPを表示させるには
https://aws.amazon.com/jp/premiumsupport/knowledge-center/elb-capture-client-ip-addresses/

原因調査〜その2〜

/etc/httpd/conf/httpd.conf
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined-elb
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

CustomLog "logs/access_log" combined

では、2つ目のフォーマット(combined-elb)に%{X-Forwarded-For}iが設定されているから、CustomLogの設定を修正すればいいのか。

あと、アクセスログの出力先は、logs/hogehoge.com-access_log だから、CustomLogに設定されているlogs/access_logも修正しないといけないな。

・・・ん?

はてな.png

この推測だと現状のログ表示は、%hが設定されているんだから、ELBのIPが表示されるはず。

なんでログにハイフン(-)が表示されるんだ???

調査を進める。

探偵.png

原因調査〜その3〜

今回はVirtualHostを設定しているので、設定ファイルを見てみる。

Infomation
VirtualHostとは?
https://httpd.apache.org/docs/2.4/ja/vhosts/

/etc/httpd/conf.d/hogehoge.com.conf
<VirtualHost *:80>
    ServerName hogehoge.com
    ServerAlias hogehoge.com hugahuga.com
    DocumentRoot /var/www/hogehoge.com/
    CustomLog logs/hogehoge.com-access_log combined-elb
    ErrorLog logs/hogehoge.com-error_log
</VirtualHost>

こちらのCustomLogではcombined-elbを指定しているので、%{X-Forwarded-For}iが設定されているフォーマットが適用されていることがわかった。

ここまでの調査でCustomLog周りの設定で問題はなさそうだな。。。

じゃあなぜアクセスログにクライアントIPが表示されないのか。。

## 原因調査〜その4〜

何か別の設定が影響しているのではないか。。

X-Forwarded-For周辺の設定を確認してみる。

/etc/httpd/conf/httpd.conf
 >> cat /etc/httpd/conf/httpd.conf |grep X-Forwarded-For
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined-elb
RemoteIPHeader X-Forwarded-For

RemoteIPHeader!!!!

こいつだww!!

この設定で、X-Forwarded-ForHTTPヘッダーがクライアントIPを判定してるが、
そもそもApacheのログフォーマットの定義で、RemoteIPの場合は%aを使用する必要がある。

ってことで、以下に修正すればOK。

変更前
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined-elb

変更後
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined-elb

解決編

実際に変更してみた。

修正作業
> sed -i".bak" "s/{X-Forwarded-For}i/a/g" /etc/httpd/conf/httpd.conf    # 念のためバックアップ取ってファイル修正
> diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak              # 差分確認 
> apachectl configtest                                                                                                   # Apacheの構文チェック「Syntax OK」を確認
> systemctl restart httpd.service                                                               # Apache再起動

作業後、アクセスクライアントIPが表示されることが確認できた。

> tail -f hogehoge.com-access_log
xxx.xxx.xxx.xxx - - [28/Mar/2022:19:41:12 +0900] "GET /wpgs/wp-content/themes/xxxxxx/assets/image HTTP/1.1" 200 4604 "https://xxxx.xxxx.xxx.com" "Mozilla/5.0 (iPhone; CPU iPhone OS 15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Mobile/------ Safari/---.-"

お疲れ様でした。

16
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
16
6