Linux/Apache/Apache.md
... ...
@@ -1,696 +0,0 @@
1
-[[_TOC_]]
2
-- [[Perl/ApacheErrorLogFormatter]]
3
-
4
-# ドキュメント
5
-- [Apache HTTP Server Documentation](http://httpd.apache.org/docs/)
6
- - [現行バージョン](http://httpd.apache.org/docs/current/)
7
- - [Version 2.4](http://httpd.apache.org/docs/2.4/)
8
- - [Version 2.2](http://httpd.apache.org/docs/2.2/)
9
-
10
- - [mod_authz_host](http://httpd.apache.org/docs/current/mod/mod_authz_host.html) ([mod_access](http://httpd.apache.org/docs/2.0/mod/mod_access.html))
11
- - Allow, Deny, Order ディレクティブ
12
-
13
- - [mod_setenvif](http://httpd.apache.org/docs/current/mod/mod_setenvif.html)
14
-
15
- - [Apache の SSL/TLS 暗号化](http://httpd.apache.org/docs/current/ssl/)
16
-
17
-# Apache用モジュールをコンパイルできるようにする
18
-- httpd-devel (apxs) をインストールしておく。
19
-```
20
-# yum install httpd-devel
21
-```
22
-
23
-# バーチャルホスト設定
24
-- [バーチャルホスト説明書](http://httpd.apache.org/docs/current/vhosts/)
25
-
26
-## 設定
27
-- /etc/httpd/conf.d/VirtualHosts.conf
28
-```apache
29
-# Use name-based virtual hosting.
30
-NameVirtualHost *:80
31
-
32
-<VirtualHost *:80>
33
- ServerName www.takeash.net
34
- DocumentRoot /var/www/html/
35
-</VirtualHost>
36
-
37
-<VirtualHost *:80>
38
- ServerName vh1.takeash.net
39
- DocumentRoot /var/www/vh1-html/
40
- <Directory "/var/www/vh1-html">
41
-# AllowOverride All
42
- </Directory>
43
-</VirtualHost>
44
-```
45
-
46
-## エラー対策
47
-- バーチャルホストが表示されない。
48
- - エラーメッセージ
49
-```
50
-[warn] _default_ VirtualHost overlap on port 80, the first has precedence
51
-```
52
- - 原因<br />
53
-NameVirtualHost ディレクティブが設定されていない。
54
- - 対処<br />
55
-NameVirtualHost ディレクティブを設定する。<br />
56
-httpd.conf を修正するより conf.d に VirtualHosts.conf として専用のファイルを作成しておく方が忘れなくていいかも。
57
-
58
-# Digest 認証
59
-- [認証、承認、アクセス制御](http://httpd.apache.org/docs/current/howto/auth.html)
60
-- [mod_auth_digest](http://httpd.apache.org/docs/current/mod/mod_auth_digest.html)
61
- - レルム/ユーザ/パスワード追加
62
-```
63
-htdigest [-c] passwdfile <realm> <username>
64
-```
65
- - /etc/httpd/conf.d/DigestAuth.conf
66
-```apache
67
-<Directory "/var/www/html/Download/<realm>">
68
- AllowOverride AuthConfig
69
- AuthType Digest
70
- AuthName "<realm>"
71
- AuthUserFile /var/www/passwd/passwords_digest
72
- Require user <username>
73
- Options None
74
- Options Indexes
75
- DirectoryIndex index.html index.htm index.php
76
- Order allow,deny
77
- Allow from all
78
-</Directory>
79
-```
80
-
81
-# mod_ssl
82
-- インストール
83
-```
84
-# yum install mod_ssl
85
-```
86
-
87
-## 自己署名証明書使用
88
-
89
-- SAN 項目を追加した設定ファイルを作成。
90
-```
91
-# cd /etc/pki/tls/
92
-# cp openssl.cnf openssl-san.cnf
93
-```
94
-
95
-- openssl.cnf と openssl-san.cnf の差分
96
-```
97
---- openssl.cnf
98
-+++ openssl-san.cnf
99
-@@ -104,7 +104,7 @@
100
- ####################################################################
101
- [ req ]
102
- default_bits = 2048
103
--default_md = sha1
104
-+default_md = sha256
105
- default_keyfile = privkey.pem
106
- distinguished_name = req_distinguished_name
107
- attributes = req_attributes
108
-@@ -222,6 +222,11 @@
109
-
110
- basicConstraints = CA:FALSE
111
- keyUsage = nonRepudiation, digitalSignature, keyEncipherment
112
-+subjectAltName=@alt_names
113
-+
114
-+[ alt_names ]
115
-+DNS.1=takeash.net
116
-+DNS.2=*.takeash.net
117
-
118
-[ v3_ca ]
119
-
120
-```
121
-- 証明書フォルダへ移動
122
-```
123
-# cd certs/
124
-```
125
-
126
-- サーバー用秘密鍵作成 (server.key)
127
-```
128
-# openssl genrsa -aes128 2048 > server.key
129
-```
130
-
131
-- パスフレーズ削除<br />
132
-httpd 再起動時にパスフレーズが要求されないようにするため。
133
-```
134
-# openssl rsa -in server.key -out server.key
135
-```
136
-
137
-- サーバー用自己署名証明書作成 (server.crt)
138
-```
139
-# openssl req -utf8 -new -key server.key -x509 -days 3650 -out server.crt -set_serial 0 \
140
- -subj '/C=JP/ST=Tokyo/L=Chuo-ku/O=TakeAsh.net/CN=takeash.net' -extensions v3_req -config ../openssl-san.cnf
141
-```
142
- - サブジェクト例 (TakeAsh.net)
143
-
144
- | 項目 | 用途 | サンプル |
145
- | --- | --- | --- |
146
- | C | 国名コード | JP |
147
- | ST | 都道府県 | Tokyo |
148
- | L | 区市町村 | Chuo-ku |
149
- | O | 組織名 | TakeAsh.net |
150
- | CN | コモンネーム(ドメイン名) | takeash.net |
151
- - 証明書確認<br />
152
-「X509v3 extensions - X509v3 Subject Alternative Name」項目が存在すれば SAN が含まれている。
153
-```
154
-# openssl x509 -in server.crt -text
155
-...
156
- X509v3 Subject Alternative Name:
157
- DNS:takeash.net, DNS:*.takeash.net
158
-...
159
-```
160
-
161
-- /etc/httpd/conf.d/ssl.conf (抜粋)
162
-```
163
-SSLCertificateFile /etc/pki/tls/certs/server.crt
164
-SSLCertificateKeyFile /etc/pki/tls/certs/server.key
165
-DocumentRoot "/var/www/html"
166
-SSLProtocol all -SSLv2 -SSLv3
167
-```
168
-
169
-- httpd 再起動
170
- - CentOS 6
171
-```
172
-# service httpd restart
173
-```
174
- - CentOS 7
175
-```
176
-# systemctl restart httpd
177
-```
178
-
179
-- 動作テスト
180
- - https://takeash.net/cgi-bin/etc/PrintEnv.cgi
181
- - https://www.takeash.net/cgi-bin/etc/PrintEnv.cgi
182
-
183
-- [OpenSSL CSR with Alternative Names one-line - End Point Blog](http://blog.endpoint.com/2014/10/openssl-csr-with-alternative-names-one.html)
184
-- [SAN対応 x.509 証明書を取得するためのCSRを作成する - Qiita](http://qiita.com/saitara/items/eda74ac6a950122b5f31)
185
-- [subjectAltNameでバーチャルホスト - Kung Noi Blog](http://www.goodnai.com/blog/2010/05/07/subjectaltname%E3%81%A7%E3%83%90%E3%83%BC%E3%83%81%E3%83%A3%E3%83%AB%E3%83%9B%E3%82%B9%E3%83%88/)
186
-- [Webサーバー間通信内容暗号化(Apache+mod_SSL) - CentOSで自宅サーバー構築](http://centossrv.com/apache-ssl.shtml)
187
-- [SSL Server Test (Powered by Qualys SSL Labs)](https://www.ssllabs.com/ssltest/)
188
-- [ssl certificate - how to add subject alernative name to ssl certs? - Stack Overflow](https://stackoverflow.com/questions/%38%37%34%34%36%30%37)
189
- - keytool -certreq -ext SAN=dns:example.com,ip:192.168.0.1
190
- - [keytool(ja)](https://docs.oracle.com/javase/jp/8/docs/technotes/tools/windows/keytool.html) / [keytool(en)](http://docs.oracle.com/javase/8/docs/technotes/tools/windows/keytool.html)
191
-
192
-## Certbot 使用
193
-- Certbot 使用前準備
194
- - ホスト名が正引きできること。(ワイルドカード不可)
195
- - バーチャルホストのサーバ名と要求するドメイン名のどれかが一致すること。
196
- - https でアクセス可能になっていること。
197
- - https ポート解放
198
-```
199
-# firewall-cmd --add-service=https --permanent
200
-```
201
-
202
-- Certbot インストール (EPEL リポジトリ)
203
-```
204
-# yum install python-certbot-apache
205
-```
206
-
207
-- 証明書取得<br />
208
-取得に成功すると「/etc/letsencrypt/live/<ドメイン1>/」に証明書が作成される。
209
-```
210
-# certbot --apache certonly -m <メールアドレス> -d <ドメイン1> [-d <ドメイン2> ...] --agree-tos
211
-```
212
-
213
-- /etc/httpd/conf.d/ssl.conf (抜粋)
214
-```
215
-Listen 443 https
216
-SSLEngine on
217
-SSLCertificateFile /etc/letsencrypt/live/<ドメイン1>/cert.pem
218
-SSLCertificateKeyFile /etc/letsencrypt/live/<ドメイン1>/privkey.pem
219
-SSLCertificateChainFile /etc/letsencrypt/live/<ドメイン1>/chain.pem
220
-```
221
-
222
-- /etc/httpd/conf.d/VirtualHosts.conf (抜粋)
223
-バーチャルホスト毎に SSL 設定が必要。
224
-```
225
-<VirtualHost *:80 *:443>
226
- ServerName vh1.<ドメイン1>
227
- DocumentRoot /var/www/vh1-html/
228
- SSLEngine on
229
- SSLCertificateFile /etc/letsencrypt/live/<ドメイン1>/cert.pem
230
- SSLCertificateKeyFile /etc/letsencrypt/live/<ドメイン1>/privkey.pem
231
- SSLCertificateChainFile /etc/letsencrypt/live/<ドメイン1>/chain.pem
232
- <Directory "/var/www/vh1-html">
233
- AllowOverride All
234
- </Directory>
235
-</VirtualHost>
236
-```
237
-
238
-- 自動更新スクリプト /etc/cron.monthly/certbot.sh
239
-```bash
240
-#!/bin/bash
241
-/bin/certbot renew
242
-```
243
-
244
-- [Certbot](https://certbot.eff.org/)
245
-- [Webサーバー間通信内容暗号化(Apache+mod_SSL+Certbot) - CentOSで自宅サーバー構築](http://centossrv.com/apache-certbot.shtml)
246
-
247
-## Certbot 使用(ワイルドカード, 手動)
248
-- Certbot インストール (EPEL リポジトリ)
249
-```
250
-# yum -y install yum-utils
251
-# yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
252
-# yum install certbot-apache
253
-```
254
-
255
-- 証明書取得(手動)<br />
256
-途中HTTPへのテキストファイルの配置とDNSへのTXTレコードの追加を指示されるので、追加してからEnterを押して先へ進む。<br />
257
-取得に成功すると「/etc/letsencrypt/live/<ドメイン>/」に証明書が作成される。
258
-```
259
-# certbot certonly --manual --server https://acme-v02.api.letsencrypt.org/directory -d "*.example.com" -d example.com
260
-```
261
-
262
-- /etc/httpd/conf.d/ssl.conf (抜粋)
263
-```
264
-Listen 443 https
265
-SSLEngine on
266
-SSLCertificateFile /etc/letsencrypt/live/<ドメイン>/cert.pem
267
-SSLCertificateKeyFile /etc/letsencrypt/live/<ドメイン>/privkey.pem
268
-SSLCertificateChainFile /etc/letsencrypt/live/<ドメイン>/chain.pem
269
-```
270
-
271
-- /etc/httpd/conf.d/VirtualHosts.conf (抜粋)
272
-バーチャルホスト毎に SSL 設定が必要。
273
-```
274
-<VirtualHost *:80 *:443>
275
- ServerName vh1.<ドメイン1>
276
- DocumentRoot /var/www/vh1-html/
277
- SSLEngine on
278
- SSLCertificateFile /etc/letsencrypt/live/<ドメイン1>/cert.pem
279
- SSLCertificateKeyFile /etc/letsencrypt/live/<ドメイン1>/privkey.pem
280
- SSLCertificateChainFile /etc/letsencrypt/live/<ドメイン1>/chain.pem
281
- <Directory "/var/www/vh1-html">
282
- AllowOverride All
283
- </Directory>
284
-</VirtualHost>
285
-```
286
-
287
-- apache 再起動
288
-```
289
-# systemctl restart httpd
290
-```
291
-
292
-- 動作確認
293
- ```
294
- $ openssl s_client -connect <ホスト>:443
295
- ```
296
- - 設定失敗
297
- ```
298
- CONNECTED(00000003)
299
- 140139752064912:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:s23_clnt.c:794:
300
- ---
301
- no peer certificate available
302
- ---
303
- No client certificate CA names sent
304
- ---
305
- SSL handshake has read 7 bytes and written 289 bytes
306
- ---
307
- New, (NONE), Cipher is (NONE)
308
- Secure Renegotiation IS NOT supported
309
- Compression: NONE
310
- Expansion: NONE
311
- No ALPN negotiated
312
- SSL-Session:
313
- Protocol : TLSv1.2
314
- Cipher : 0000
315
- Session-ID:
316
- Session-ID-ctx:
317
- Master-Key:
318
- Key-Arg : None
319
- Krb5 Principal: None
320
- PSK identity: None
321
- PSK identity hint: None
322
- Start Time: 1535884386
323
- Timeout : 300 (sec)
324
- Verify return code: 0 (ok)
325
- ---
326
- ```
327
- - 設定成功
328
- ```
329
- CONNECTED(00000003)
330
- depth=2 O = Digital Signature Trust Co., CN = DST Root CA X3
331
- verify return:1
332
- depth=1 C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
333
- verify return:1
334
- depth=0 CN = *.<ドメイン1>
335
- verify return:1
336
- ---
337
- Certificate chain
338
- 0 s:/CN=*.<ドメイン1>
339
- i:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
340
- 1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
341
- i:/O=Digital Signature Trust Co./CN=DST Root CA X3
342
- ---
343
- Server certificate
344
- -----BEGIN CERTIFICATE-----
345
- MIIGETCCBPmgAwIBAgISA3VBvI0cSyzAQGtpIaQKQRZxMA0GCSqGSIb3DQEBCwUA
346
- ...
347
- ```
348
-
349
-- 証明書更新<br />
350
-「--manual」で取得した場合は「renew」による自動更新ができないので、既存の証明書を削除し同名で取得し直す。
351
-```
352
-# certbot delete
353
-```
354
-
355
-- 証明書の有効期限を表示<br />
356
-getExpireDate.sh
357
-```bash
358
-#!/bin/bash
359
-
360
-CommonName=example.net
361
-
362
-NotAfter=`openssl x509 -noout -dates -in /etc/letsencrypt/live/${CommonName}/fullchain.pem | \
363
- grep notAfter | \
364
- sed -e "s/notAfter=//" | \
365
- date -f - --iso-8601`
366
-echo ${NotAfter}
367
-```
368
-
369
-## Certbot 使用(ワイルドカード, 自動)
370
-- Certbot, DNS Plugin インストール (EPEL リポジトリ)
371
-```
372
-# yum -y install yum-utils
373
-# yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
374
-# yum install certbot-apache python2-certbot-dns-rfc2136
375
-```
376
-
377
-- BIND 用認証キーの作成
378
-Kcertbot-key.+165+43987.key, Kcertbot-key.+165+43987.private の2つのファイルが作成される。
379
-```
380
-# cd /etc/named/
381
-# dnssec-keygen -a HMAC-SHA512 -b 512 -n HOST certbot-key
382
-```
383
-
384
-- 認証ファイル /etc/named/certbot_rfc2136.ini , ファイルモード 600
385
-```
386
-# Target DNS server
387
-dns_rfc2136_server = 127.0.0.1
388
-# Target DNS port
389
-dns_rfc2136_port = 53
390
-# TSIG key name
391
-dns_rfc2136_name = certbot-key.
392
-# TSIG key secret
393
-dns_rfc2136_secret = <Kcertbot-key.+165+43987.key のハッシュ値>
394
-# TSIG key algorithm
395
-dns_rfc2136_algorithm = HMAC-SHA512
396
-```
397
-
398
-- /etc/named.conf に追加
399
-```
400
-key "certbot-key." {
401
- algorithm hmac-sha512;
402
- secret "<Kcertbot-key.+165+43987.key のハッシュ値>";
403
-};
404
-
405
-view "internal" {
406
- match-clients { localhost; localnets; };
407
- match-destinations { localhost; localnets; };
408
- recursion yes;
409
-
410
- zone "." IN {
411
- type hint;
412
- file "named.ca";
413
- };
414
-
415
- include "/etc/named.rfc1912.zones";
416
- include "/etc/named.root.key";
417
- include "/etc/named/<ドメイン>.lan.zone";
418
-};
419
-
420
-view "external" {
421
- match-clients { any; };
422
- match-destinations { any; };
423
- recursion no;
424
- include "/etc/named/<ドメイン>.wan.zone";
425
- include "/etc/named/_acme-challenge.<ドメイン>.wan.zone";
426
-};
427
-```
428
-
429
-- /etc/named/<ドメイン>.lan.zone
430
-```
431
-zone "<ドメイン>" {
432
- type master;
433
- file "<ドメイン>.lan.db";
434
- update-policy {
435
- grant certbot-key. name _acme-challenge.<ドメイン>. txt;
436
- };
437
-};
438
-```
439
-
440
-- /etc/named/<ドメイン>.wan.zone
441
-```
442
-zone "<ドメイン>" {
443
- type master;
444
- file "<ドメイン>.wan.db";
445
- allow-query { any; };
446
- update-policy {
447
- grant certbot-key. name _acme-challenge.<ドメイン>. txt;
448
- };
449
-};
450
-```
451
-
452
-- /etc/named/_acme-challenge.<ドメイン>.wan.zone
453
-```
454
-zone "_acme-challenge.<ドメイン>" {
455
- type master;
456
- file "_acme-challenge.<ドメイン>.wan.db";
457
- allow-query { any; };
458
- update-policy {
459
- grant certbot-key. name _acme-challenge.<ドメイン>. txt;
460
- };
461
-};
462
-```
463
-
464
-- /var/named/<ドメイン>.wan.db
465
-```
466
-$TTL 86400
467
-@ IN SOA ns1.<ドメイン>. root.<ドメイン>. (
468
- 2018090500 ; Serial
469
- 28800 ; Refresh
470
- 14400 ; Retry
471
- 2592000 ; Expire
472
- 86400 ; Minimum
473
- )
474
- IN NS ns1.<ドメイン>.
475
- IN MX 10 mail.<ドメイン>.
476
-@ IN A <グローバル IP アドレス>
477
-ns1 IN A <グローバル IP アドレス>
478
-www IN A <グローバル IP アドレス>
479
-mail IN A <グローバル IP アドレス>
480
-_acme-challenge IN NS ns1.<ドメイン>.
481
-* IN A <グローバル IP アドレス>
482
-<ドメイン>. IN TXT "v=spf1 a mx ~all"
483
-```
484
-
485
-- /var/named/_acme-challenge.<ドメイン>.wan.db
486
-```
487
-$TTL 86400
488
-@ IN SOA ns1.<ドメイン>. root.<ドメイン>. (
489
- 2018090500 ; Serial
490
- 1h ; Refresh
491
- 15m ; Retry
492
- 30d ; Expire
493
- 1h ; Minimum
494
- )
495
- IN NS ns1.<ドメイン>.
496
-```
497
-
498
-- `<ドメイン>.jnl: create: permission denied`(/var/named/data/named.run) 対策
499
-```
500
-# chmod 770 /var/named/
501
-# setsebool -P named_write_master_zones 1
502
-```
503
-
504
-- 証明書取得
505
-```
506
-# certbot certonly \
507
- --dns-rfc2136 \
508
- --dns-rfc2136-credentials /etc/named/certbot_rfc2136.ini \
509
- -d "*.<ドメイン>" -d <ドメイン>
510
-```
511
-
512
-# mod_security
513
-## インストール
514
-- EPELリポジトリ
515
-```
516
-# yum install mod_security mod_security_crs
517
-```
518
-
519
-## 設定
520
-- /etc/httpd/conf.d/mod_security.conf (抜粋)
521
-```apache
522
- # Maximum request body size we will
523
- # accept for buffering
524
- #SecRequestBodyLimit 131072
525
- SecRequestBodyLimit 5242880
526
- SecRequestBodyNoFilesLimit 51200
527
-```
528
-
529
-- /etc/httpd/modsecurity.d/modsecurity_localrules.conf
530
-```apache
531
-# Drop your local rules in here.
532
-
533
-# White List IP
534
-SecRule REMOTE_ADDR "@pmFromFile /etc/httpd/modsecurity.d/whitelist_ip.txt" \
535
- "phase:1,id:'1000001',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
536
-
537
-# White List URI
538
-SecRule REQUEST_URI "@pmFromFile /etc/httpd/modsecurity.d/whitelist_uri.txt" \
539
- "phase:1,id:'1000002',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
540
-
541
-# White List URI 2
542
-SecRule REQUEST_URI "@rx ^\/Etc\/" \
543
- "phase:1,id:'1000003',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
544
-
545
- # White List Sub-Domain
546
- SecRule REQUEST_HEADERS:Host "@pmFromFile /etc/httpd/modsecurity.d/whitelist_subdomain.txt" \
547
- "phase:1,id:'1000004',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
548
-
549
-# ZmEu Attack / phpMyAdmin
550
-SecRule REQUEST_URI "@rx (?i)\/(php-?My-?Admin[^\/]*|mysqlmanager|myadmin|pma2005|pma\/scripts|w00tw00t[^\/]+)\/" \
551
- "severity:alert,id:'0000013',deny,log,status:400,msg:'Unacceptable folder.',severity:'2'"
552
-```
553
- - mod_security-2.7.1 でエラーが出るから適当にid追加したけど、idの振り方のルールってどこにあるのかな?
554
-```
555
-ModSecurity: No action id present within the rule
556
-```
557
-
558
-- /etc/httpd/modsecurity.d/whitelist_ip.txt<br />
559
-mod_security による制限を行わない IP アドレスを列挙する。<br />
560
-コメントは行頭から「#」で始める。
561
-```
562
-# localhost
563
-127.0.0.1
564
-
565
-# example.com
566
-xxx.xxx.xxx.xxx
567
-
568
-# example.net
569
-yyy.yyy.yyy.yyy
570
-```
571
-
572
-- /etc/httpd/modsecurity.d/whitelist_uri.txt<br />
573
-mod_security による制限を行わない URI を列挙する。
574
-```
575
-/cgi-bin/etc/PrintEnv.cgi
576
-/cgi-bin/etc/PrintEnv_txt.cgi
577
-/cgi-bin/etc/index.cgi
578
-/cgi-bin/etc/testCGI.cgi
579
-```
580
-
581
-- /etc/httpd/modsecurity.d/whitelist_subdomain.txt<br />
582
-mod_security による制限を行わないホスト名を列挙する。
583
-```
584
-# WebApp1
585
-vh1.takeash.net
586
-```
587
-
588
-- /etc/httpd/modsecurity.d/activated_rules/modsecurity_crs_20_protocol_violations.conf
589
- - id:958291 "Range: 0-", mp4 等のストリーミングや分割ダウンロードが行われるファイルのダウンロードで引っかかる。
590
-
591
-## リンク
592
-- http://modsecurity.org/
593
- - [Reference Manual](https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual)
594
- - [The Open Web Application Security Project](https://www.owasp.org/)
595
-- [hashdos攻撃をmod_securityで防御する(CentOS+yum編) - 徳丸浩の日記](http://blog.tokumaru.org/2012/01/hashdosmodsecuritycentosyum.html)
596
-- [Attack by ZmEu - The Linux Page](http://linux.m2osw.com/zmeu-attack) phpMyAdmin 脆弱性スキャンスクリプト対策
597
-- [黒ぶちメガネのblog » mod_securityのホワイトリスト、ブラックリストの書き方メモ](http://www.kurobuti.com/blog/?p=3775)
598
-- [禁煙できないSEの独り言: ModSecurity 2.5.12の導入](http://hiro-system.blog.ocn.ne.jp/blog/2010/04/modsecurity_251.html)
599
-- [(続)spammer対策 - ねこ様にもてあそばれる日々(2006-05-03)](http://m9841.info/?date=20060503#p02)
600
-- [mod_securityでWebサーバを守る(第1回) - ソフテック](http://www.softek.co.jp/Sec/mod_security1.html)
601
-- Webアプリケーションに潜むセキュリティホール
602
- - [Webアプリケーションファイアウォールによる防御](http://www.atmarkit.co.jp/fsecurity/rensai/webhole11/webhole01.html)
603
- - [mod_securityのXSS対策ルールを作成する](http://www.atmarkit.co.jp/fsecurity/rensai/webhole12/webhole01.html)
604
-- [UNIX的なアレ:gihyo.jp出張所](http://gihyo.jp/admin/serial/01/unix)
605
- - [第19回 知っておきたいApacheの基礎知識 その15](http://gihyo.jp/admin/serial/01/unix/0019)
606
-
607
-# mod_geoip
608
-## インストール
609
-- EPELリポジトリ
610
-```
611
-yum install mod_geoip
612
-```
613
-
614
-## 設定
615
-- /etc/cron.monthly/updateGeoIP<br />
616
-データベースの自動更新スクリプト
617
-```bash
618
-wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
619
-gunzip GeoIP.dat.gz
620
-mv -f GeoIP.dat /usr/share/GeoIP/GeoIP.dat
621
-/sbin/restorecon -v /usr/share/GeoIP/GeoIP.dat
622
-```
623
-- GeoIP が表示されない場合は、SELinux のラベルを確認。
624
-```
625
-# ls -Z /usr/share/GeoIP/
626
-Good) unconfined_u:object_r:usr_t:s0
627
-NG) unconfined_u:object_r:admin_home_t:s0
628
-```
629
-ラベルが正しくない場合は下記コマンドで修正する。
630
-```
631
-# sealert -a /var/log/audit/audit.log
632
-# /sbin/restorecon -v /usr/share/GeoIP/GeoIP.dat
633
-```
634
-ラベル修正後、httpd を再起動すること。
635
-```
636
-# service httpd restart
637
-```
638
-
639
-## リンク
640
-- [MaxMind](http://www.maxmind.com/)
641
- - [mod_geoip2 Apache module](http://dev.maxmind.com/geoip/mod_geoip2)
642
- - [GeoLite Free Downloadable Databases](http://dev.maxmind.com/geoip/geolite)
643
-- [Apache2 mod_geoip CentOS うざい国を弾くモジュール « ORBIT SPACE](http://www.orsx.net/blog/archives/2510)
644
-
645
-# Apacheでhttp-equiv属性値を反映させる
646
-- [META要素「http-equiv属性値とHTTPヘッダー」について考える](http://www.infoaxia.com/tools/blog/archives/cat3/)
647
-- [Apache mod_xml_charset](http://www.yoshidam.net/XML_ja.html)
648
-
649
-- デフォルトでは、LastModifiedしか反映されない。
650
-- apxsはhttpd-devel-xxx.rpmをインストールすることで使用できるようになる。
651
-- うちの Fedora Core 3 環境だとmod_html_metaのコンパイルでエラーが出るんでまだ使えていない。引き続き検討。
652
-
653
-# Apache に DoS 攻撃対策 mod_evasive
654
-- [mod_evasive](http://www.zdziarski.com/blog/?page_id=442) @ [Jonathan Zdziarski's Domain](http://www.zdziarski.com/)
655
-- [Apache DoS攻撃対策 mod_evasiveインストール&設定 &#8211; Linux](http://www.makizou.com/archives/1341) @ [MAKIZOU.COM](http://www.makizou.com/)
656
-
657
-# cgi-bin ディレクトリでファイル名を省略したときに index.cgi を実行する設定
658
-- httpd.conf に ScriptAliasMatch を追加する。
659
-
660
-- 修正前
661
-```apache
662
-ScriptAlias /cgi-bin/ /var/www/cgi-bin/
663
-```
664
-
665
-- 修正後
666
-```apache
667
-ScriptAliasMatch ^/cgi-bin/(.*)\.cgi /var/www/cgi-bin/$1.cgi
668
-ScriptAliasMatch ^/cgi-bin/(.*)/? /var/www/cgi-bin/$1/index.cgi
669
-ScriptAliasMatch ^/cgi-bin$ /var/www/cgi-bin/index.cgi
670
-ScriptAlias /cgi-bin/ /var/www/cgi-bin/
671
-```
672
-
673
-- index.cgi の例
674
-```perl
675
-#!/usr/local/bin/perl
676
-
677
-use strict;
678
-use warnings;
679
-use utf8;
680
-use CGI::Pretty;
681
-
682
-my $q = new CGI;
683
-my $host = $q->url(-base => 1);
684
-print $q->redirect( $host . '/' );
685
-
686
-# EOF
687
-```
688
-
689
-- [Apacheで、http://hoge.hoge/cgi-bin/foo/にアクセスできるようにする方法](http://blogs.dion.ne.jp/fit_si/archives/5941241.html)
690
-
691
-# Apache で外部からの直リンクを禁止する
692
-- [404 Blog Not Found:Apache - ホットリンクを禁止する](http://blog.livedoor.jp/dankogai/archives/50804992.html)
693
-
694
-# Basic認証にタイムアウトを設定する
695
-- [mod_auth_timeout](http://secure.linuxbox.com/tiki/tiki-index.php?page=mod_auth_timeout)
696
-- [サードパーティー製認証モジュール](http://www.thinkit.co.jp/article/120/3/2.html)
Linux/Apache/Home.md
... ...
@@ -0,0 +1,696 @@
1
+[[_TOC_]]
2
+- [[Perl/ApacheErrorLogFormatter]]
3
+
4
+# ドキュメント
5
+- [Apache HTTP Server Documentation](http://httpd.apache.org/docs/)
6
+ - [現行バージョン](http://httpd.apache.org/docs/current/)
7
+ - [Version 2.4](http://httpd.apache.org/docs/2.4/)
8
+ - [Version 2.2](http://httpd.apache.org/docs/2.2/)
9
+
10
+ - [mod_authz_host](http://httpd.apache.org/docs/current/mod/mod_authz_host.html) ([mod_access](http://httpd.apache.org/docs/2.0/mod/mod_access.html))
11
+ - Allow, Deny, Order ディレクティブ
12
+
13
+ - [mod_setenvif](http://httpd.apache.org/docs/current/mod/mod_setenvif.html)
14
+
15
+ - [Apache の SSL/TLS 暗号化](http://httpd.apache.org/docs/current/ssl/)
16
+
17
+# Apache用モジュールをコンパイルできるようにする
18
+- httpd-devel (apxs) をインストールしておく。
19
+```
20
+# yum install httpd-devel
21
+```
22
+
23
+# バーチャルホスト設定
24
+- [バーチャルホスト説明書](http://httpd.apache.org/docs/current/vhosts/)
25
+
26
+## 設定
27
+- /etc/httpd/conf.d/VirtualHosts.conf
28
+```apache
29
+# Use name-based virtual hosting.
30
+NameVirtualHost *:80
31
+
32
+<VirtualHost *:80>
33
+ ServerName www.takeash.net
34
+ DocumentRoot /var/www/html/
35
+</VirtualHost>
36
+
37
+<VirtualHost *:80>
38
+ ServerName vh1.takeash.net
39
+ DocumentRoot /var/www/vh1-html/
40
+ <Directory "/var/www/vh1-html">
41
+# AllowOverride All
42
+ </Directory>
43
+</VirtualHost>
44
+```
45
+
46
+## エラー対策
47
+- バーチャルホストが表示されない。
48
+ - エラーメッセージ
49
+```
50
+[warn] _default_ VirtualHost overlap on port 80, the first has precedence
51
+```
52
+ - 原因<br />
53
+NameVirtualHost ディレクティブが設定されていない。
54
+ - 対処<br />
55
+NameVirtualHost ディレクティブを設定する。<br />
56
+httpd.conf を修正するより conf.d に VirtualHosts.conf として専用のファイルを作成しておく方が忘れなくていいかも。
57
+
58
+# Digest 認証
59
+- [認証、承認、アクセス制御](http://httpd.apache.org/docs/current/howto/auth.html)
60
+- [mod_auth_digest](http://httpd.apache.org/docs/current/mod/mod_auth_digest.html)
61
+ - レルム/ユーザ/パスワード追加
62
+```
63
+htdigest [-c] passwdfile <realm> <username>
64
+```
65
+ - /etc/httpd/conf.d/DigestAuth.conf
66
+```apache
67
+<Directory "/var/www/html/Download/<realm>">
68
+ AllowOverride AuthConfig
69
+ AuthType Digest
70
+ AuthName "<realm>"
71
+ AuthUserFile /var/www/passwd/passwords_digest
72
+ Require user <username>
73
+ Options None
74
+ Options Indexes
75
+ DirectoryIndex index.html index.htm index.php
76
+ Order allow,deny
77
+ Allow from all
78
+</Directory>
79
+```
80
+
81
+# mod_ssl
82
+- インストール
83
+```
84
+# yum install mod_ssl
85
+```
86
+
87
+## 自己署名証明書使用
88
+
89
+- SAN 項目を追加した設定ファイルを作成。
90
+```
91
+# cd /etc/pki/tls/
92
+# cp openssl.cnf openssl-san.cnf
93
+```
94
+
95
+- openssl.cnf と openssl-san.cnf の差分
96
+```
97
+--- openssl.cnf
98
++++ openssl-san.cnf
99
+@@ -104,7 +104,7 @@
100
+ ####################################################################
101
+ [ req ]
102
+ default_bits = 2048
103
+-default_md = sha1
104
++default_md = sha256
105
+ default_keyfile = privkey.pem
106
+ distinguished_name = req_distinguished_name
107
+ attributes = req_attributes
108
+@@ -222,6 +222,11 @@
109
+
110
+ basicConstraints = CA:FALSE
111
+ keyUsage = nonRepudiation, digitalSignature, keyEncipherment
112
++subjectAltName=@alt_names
113
++
114
++[ alt_names ]
115
++DNS.1=takeash.net
116
++DNS.2=*.takeash.net
117
+
118
+[ v3_ca ]
119
+
120
+```
121
+- 証明書フォルダへ移動
122
+```
123
+# cd certs/
124
+```
125
+
126
+- サーバー用秘密鍵作成 (server.key)
127
+```
128
+# openssl genrsa -aes128 2048 > server.key
129
+```
130
+
131
+- パスフレーズ削除<br />
132
+httpd 再起動時にパスフレーズが要求されないようにするため。
133
+```
134
+# openssl rsa -in server.key -out server.key
135
+```
136
+
137
+- サーバー用自己署名証明書作成 (server.crt)
138
+```
139
+# openssl req -utf8 -new -key server.key -x509 -days 3650 -out server.crt -set_serial 0 \
140
+ -subj '/C=JP/ST=Tokyo/L=Chuo-ku/O=TakeAsh.net/CN=takeash.net' -extensions v3_req -config ../openssl-san.cnf
141
+```
142
+ - サブジェクト例 (TakeAsh.net)
143
+
144
+ | 項目 | 用途 | サンプル |
145
+ | --- | --- | --- |
146
+ | C | 国名コード | JP |
147
+ | ST | 都道府県 | Tokyo |
148
+ | L | 区市町村 | Chuo-ku |
149
+ | O | 組織名 | TakeAsh.net |
150
+ | CN | コモンネーム(ドメイン名) | takeash.net |
151
+ - 証明書確認<br />
152
+「X509v3 extensions - X509v3 Subject Alternative Name」項目が存在すれば SAN が含まれている。
153
+```
154
+# openssl x509 -in server.crt -text
155
+...
156
+ X509v3 Subject Alternative Name:
157
+ DNS:takeash.net, DNS:*.takeash.net
158
+...
159
+```
160
+
161
+- /etc/httpd/conf.d/ssl.conf (抜粋)
162
+```
163
+SSLCertificateFile /etc/pki/tls/certs/server.crt
164
+SSLCertificateKeyFile /etc/pki/tls/certs/server.key
165
+DocumentRoot "/var/www/html"
166
+SSLProtocol all -SSLv2 -SSLv3
167
+```
168
+
169
+- httpd 再起動
170
+ - CentOS 6
171
+```
172
+# service httpd restart
173
+```
174
+ - CentOS 7
175
+```
176
+# systemctl restart httpd
177
+```
178
+
179
+- 動作テスト
180
+ - https://takeash.net/cgi-bin/etc/PrintEnv.cgi
181
+ - https://www.takeash.net/cgi-bin/etc/PrintEnv.cgi
182
+
183
+- [OpenSSL CSR with Alternative Names one-line - End Point Blog](http://blog.endpoint.com/2014/10/openssl-csr-with-alternative-names-one.html)
184
+- [SAN対応 x.509 証明書を取得するためのCSRを作成する - Qiita](http://qiita.com/saitara/items/eda74ac6a950122b5f31)
185
+- [subjectAltNameでバーチャルホスト - Kung Noi Blog](http://www.goodnai.com/blog/2010/05/07/subjectaltname%E3%81%A7%E3%83%90%E3%83%BC%E3%83%81%E3%83%A3%E3%83%AB%E3%83%9B%E3%82%B9%E3%83%88/)
186
+- [Webサーバー間通信内容暗号化(Apache+mod_SSL) - CentOSで自宅サーバー構築](http://centossrv.com/apache-ssl.shtml)
187
+- [SSL Server Test (Powered by Qualys SSL Labs)](https://www.ssllabs.com/ssltest/)
188
+- [ssl certificate - how to add subject alernative name to ssl certs? - Stack Overflow](https://stackoverflow.com/questions/%38%37%34%34%36%30%37)
189
+ - keytool -certreq -ext SAN=dns:example.com,ip:192.168.0.1
190
+ - [keytool(ja)](https://docs.oracle.com/javase/jp/8/docs/technotes/tools/windows/keytool.html) / [keytool(en)](http://docs.oracle.com/javase/8/docs/technotes/tools/windows/keytool.html)
191
+
192
+## Certbot 使用
193
+- Certbot 使用前準備
194
+ - ホスト名が正引きできること。(ワイルドカード不可)
195
+ - バーチャルホストのサーバ名と要求するドメイン名のどれかが一致すること。
196
+ - https でアクセス可能になっていること。
197
+ - https ポート解放
198
+```
199
+# firewall-cmd --add-service=https --permanent
200
+```
201
+
202
+- Certbot インストール (EPEL リポジトリ)
203
+```
204
+# yum install python-certbot-apache
205
+```
206
+
207
+- 証明書取得<br />
208
+取得に成功すると「/etc/letsencrypt/live/<ドメイン1>/」に証明書が作成される。
209
+```
210
+# certbot --apache certonly -m <メールアドレス> -d <ドメイン1> [-d <ドメイン2> ...] --agree-tos
211
+```
212
+
213
+- /etc/httpd/conf.d/ssl.conf (抜粋)
214
+```
215
+Listen 443 https
216
+SSLEngine on
217
+SSLCertificateFile /etc/letsencrypt/live/<ドメイン1>/cert.pem
218
+SSLCertificateKeyFile /etc/letsencrypt/live/<ドメイン1>/privkey.pem
219
+SSLCertificateChainFile /etc/letsencrypt/live/<ドメイン1>/chain.pem
220
+```
221
+
222
+- /etc/httpd/conf.d/VirtualHosts.conf (抜粋)
223
+バーチャルホスト毎に SSL 設定が必要。
224
+```
225
+<VirtualHost *:80 *:443>
226
+ ServerName vh1.<ドメイン1>
227
+ DocumentRoot /var/www/vh1-html/
228
+ SSLEngine on
229
+ SSLCertificateFile /etc/letsencrypt/live/<ドメイン1>/cert.pem
230
+ SSLCertificateKeyFile /etc/letsencrypt/live/<ドメイン1>/privkey.pem
231
+ SSLCertificateChainFile /etc/letsencrypt/live/<ドメイン1>/chain.pem
232
+ <Directory "/var/www/vh1-html">
233
+ AllowOverride All
234
+ </Directory>
235
+</VirtualHost>
236
+```
237
+
238
+- 自動更新スクリプト /etc/cron.monthly/certbot.sh
239
+```bash
240
+#!/bin/bash
241
+/bin/certbot renew
242
+```
243
+
244
+- [Certbot](https://certbot.eff.org/)
245
+- [Webサーバー間通信内容暗号化(Apache+mod_SSL+Certbot) - CentOSで自宅サーバー構築](http://centossrv.com/apache-certbot.shtml)
246
+
247
+## Certbot 使用(ワイルドカード, 手動)
248
+- Certbot インストール (EPEL リポジトリ)
249
+```
250
+# yum -y install yum-utils
251
+# yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
252
+# yum install certbot-apache
253
+```
254
+
255
+- 証明書取得(手動)<br />
256
+途中HTTPへのテキストファイルの配置とDNSへのTXTレコードの追加を指示されるので、追加してからEnterを押して先へ進む。<br />
257
+取得に成功すると「/etc/letsencrypt/live/<ドメイン>/」に証明書が作成される。
258
+```
259
+# certbot certonly --manual --server https://acme-v02.api.letsencrypt.org/directory -d "*.example.com" -d example.com
260
+```
261
+
262
+- /etc/httpd/conf.d/ssl.conf (抜粋)
263
+```
264
+Listen 443 https
265
+SSLEngine on
266
+SSLCertificateFile /etc/letsencrypt/live/<ドメイン>/cert.pem
267
+SSLCertificateKeyFile /etc/letsencrypt/live/<ドメイン>/privkey.pem
268
+SSLCertificateChainFile /etc/letsencrypt/live/<ドメイン>/chain.pem
269
+```
270
+
271
+- /etc/httpd/conf.d/VirtualHosts.conf (抜粋)
272
+バーチャルホスト毎に SSL 設定が必要。
273
+```
274
+<VirtualHost *:80 *:443>
275
+ ServerName vh1.<ドメイン1>
276
+ DocumentRoot /var/www/vh1-html/
277
+ SSLEngine on
278
+ SSLCertificateFile /etc/letsencrypt/live/<ドメイン1>/cert.pem
279
+ SSLCertificateKeyFile /etc/letsencrypt/live/<ドメイン1>/privkey.pem
280
+ SSLCertificateChainFile /etc/letsencrypt/live/<ドメイン1>/chain.pem
281
+ <Directory "/var/www/vh1-html">
282
+ AllowOverride All
283
+ </Directory>
284
+</VirtualHost>
285
+```
286
+
287
+- apache 再起動
288
+```
289
+# systemctl restart httpd
290
+```
291
+
292
+- 動作確認
293
+ ```
294
+ $ openssl s_client -connect <ホスト>:443
295
+ ```
296
+ - 設定失敗
297
+ ```
298
+ CONNECTED(00000003)
299
+ 140139752064912:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:s23_clnt.c:794:
300
+ ---
301
+ no peer certificate available
302
+ ---
303
+ No client certificate CA names sent
304
+ ---
305
+ SSL handshake has read 7 bytes and written 289 bytes
306
+ ---
307
+ New, (NONE), Cipher is (NONE)
308
+ Secure Renegotiation IS NOT supported
309
+ Compression: NONE
310
+ Expansion: NONE
311
+ No ALPN negotiated
312
+ SSL-Session:
313
+ Protocol : TLSv1.2
314
+ Cipher : 0000
315
+ Session-ID:
316
+ Session-ID-ctx:
317
+ Master-Key:
318
+ Key-Arg : None
319
+ Krb5 Principal: None
320
+ PSK identity: None
321
+ PSK identity hint: None
322
+ Start Time: 1535884386
323
+ Timeout : 300 (sec)
324
+ Verify return code: 0 (ok)
325
+ ---
326
+ ```
327
+ - 設定成功
328
+ ```
329
+ CONNECTED(00000003)
330
+ depth=2 O = Digital Signature Trust Co., CN = DST Root CA X3
331
+ verify return:1
332
+ depth=1 C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
333
+ verify return:1
334
+ depth=0 CN = *.<ドメイン1>
335
+ verify return:1
336
+ ---
337
+ Certificate chain
338
+ 0 s:/CN=*.<ドメイン1>
339
+ i:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
340
+ 1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
341
+ i:/O=Digital Signature Trust Co./CN=DST Root CA X3
342
+ ---
343
+ Server certificate
344
+ -----BEGIN CERTIFICATE-----
345
+ MIIGETCCBPmgAwIBAgISA3VBvI0cSyzAQGtpIaQKQRZxMA0GCSqGSIb3DQEBCwUA
346
+ ...
347
+ ```
348
+
349
+- 証明書更新<br />
350
+「--manual」で取得した場合は「renew」による自動更新ができないので、既存の証明書を削除し同名で取得し直す。
351
+```
352
+# certbot delete
353
+```
354
+
355
+- 証明書の有効期限を表示<br />
356
+getExpireDate.sh
357
+```bash
358
+#!/bin/bash
359
+
360
+CommonName=example.net
361
+
362
+NotAfter=`openssl x509 -noout -dates -in /etc/letsencrypt/live/${CommonName}/fullchain.pem | \
363
+ grep notAfter | \
364
+ sed -e "s/notAfter=//" | \
365
+ date -f - --iso-8601`
366
+echo ${NotAfter}
367
+```
368
+
369
+## Certbot 使用(ワイルドカード, 自動)
370
+- Certbot, DNS Plugin インストール (EPEL リポジトリ)
371
+```
372
+# yum -y install yum-utils
373
+# yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
374
+# yum install certbot-apache python2-certbot-dns-rfc2136
375
+```
376
+
377
+- BIND 用認証キーの作成
378
+Kcertbot-key.+165+43987.key, Kcertbot-key.+165+43987.private の2つのファイルが作成される。
379
+```
380
+# cd /etc/named/
381
+# dnssec-keygen -a HMAC-SHA512 -b 512 -n HOST certbot-key
382
+```
383
+
384
+- 認証ファイル /etc/named/certbot_rfc2136.ini , ファイルモード 600
385
+```
386
+# Target DNS server
387
+dns_rfc2136_server = 127.0.0.1
388
+# Target DNS port
389
+dns_rfc2136_port = 53
390
+# TSIG key name
391
+dns_rfc2136_name = certbot-key.
392
+# TSIG key secret
393
+dns_rfc2136_secret = <Kcertbot-key.+165+43987.key のハッシュ値>
394
+# TSIG key algorithm
395
+dns_rfc2136_algorithm = HMAC-SHA512
396
+```
397
+
398
+- /etc/named.conf に追加
399
+```
400
+key "certbot-key." {
401
+ algorithm hmac-sha512;
402
+ secret "<Kcertbot-key.+165+43987.key のハッシュ値>";
403
+};
404
+
405
+view "internal" {
406
+ match-clients { localhost; localnets; };
407
+ match-destinations { localhost; localnets; };
408
+ recursion yes;
409
+
410
+ zone "." IN {
411
+ type hint;
412
+ file "named.ca";
413
+ };
414
+
415
+ include "/etc/named.rfc1912.zones";
416
+ include "/etc/named.root.key";
417
+ include "/etc/named/<ドメイン>.lan.zone";
418
+};
419
+
420
+view "external" {
421
+ match-clients { any; };
422
+ match-destinations { any; };
423
+ recursion no;
424
+ include "/etc/named/<ドメイン>.wan.zone";
425
+ include "/etc/named/_acme-challenge.<ドメイン>.wan.zone";
426
+};
427
+```
428
+
429
+- /etc/named/<ドメイン>.lan.zone
430
+```
431
+zone "<ドメイン>" {
432
+ type master;
433
+ file "<ドメイン>.lan.db";
434
+ update-policy {
435
+ grant certbot-key. name _acme-challenge.<ドメイン>. txt;
436
+ };
437
+};
438
+```
439
+
440
+- /etc/named/<ドメイン>.wan.zone
441
+```
442
+zone "<ドメイン>" {
443
+ type master;
444
+ file "<ドメイン>.wan.db";
445
+ allow-query { any; };
446
+ update-policy {
447
+ grant certbot-key. name _acme-challenge.<ドメイン>. txt;
448
+ };
449
+};
450
+```
451
+
452
+- /etc/named/_acme-challenge.<ドメイン>.wan.zone
453
+```
454
+zone "_acme-challenge.<ドメイン>" {
455
+ type master;
456
+ file "_acme-challenge.<ドメイン>.wan.db";
457
+ allow-query { any; };
458
+ update-policy {
459
+ grant certbot-key. name _acme-challenge.<ドメイン>. txt;
460
+ };
461
+};
462
+```
463
+
464
+- /var/named/<ドメイン>.wan.db
465
+```
466
+$TTL 86400
467
+@ IN SOA ns1.<ドメイン>. root.<ドメイン>. (
468
+ 2018090500 ; Serial
469
+ 28800 ; Refresh
470
+ 14400 ; Retry
471
+ 2592000 ; Expire
472
+ 86400 ; Minimum
473
+ )
474
+ IN NS ns1.<ドメイン>.
475
+ IN MX 10 mail.<ドメイン>.
476
+@ IN A <グローバル IP アドレス>
477
+ns1 IN A <グローバル IP アドレス>
478
+www IN A <グローバル IP アドレス>
479
+mail IN A <グローバル IP アドレス>
480
+_acme-challenge IN NS ns1.<ドメイン>.
481
+* IN A <グローバル IP アドレス>
482
+<ドメイン>. IN TXT "v=spf1 a mx ~all"
483
+```
484
+
485
+- /var/named/_acme-challenge.<ドメイン>.wan.db
486
+```
487
+$TTL 86400
488
+@ IN SOA ns1.<ドメイン>. root.<ドメイン>. (
489
+ 2018090500 ; Serial
490
+ 1h ; Refresh
491
+ 15m ; Retry
492
+ 30d ; Expire
493
+ 1h ; Minimum
494
+ )
495
+ IN NS ns1.<ドメイン>.
496
+```
497
+
498
+- `<ドメイン>.jnl: create: permission denied`(/var/named/data/named.run) 対策
499
+```
500
+# chmod 770 /var/named/
501
+# setsebool -P named_write_master_zones 1
502
+```
503
+
504
+- 証明書取得
505
+```
506
+# certbot certonly \
507
+ --dns-rfc2136 \
508
+ --dns-rfc2136-credentials /etc/named/certbot_rfc2136.ini \
509
+ -d "*.<ドメイン>" -d <ドメイン>
510
+```
511
+
512
+# mod_security
513
+## インストール
514
+- EPELリポジトリ
515
+```
516
+# yum install mod_security mod_security_crs
517
+```
518
+
519
+## 設定
520
+- /etc/httpd/conf.d/mod_security.conf (抜粋)
521
+```apache
522
+ # Maximum request body size we will
523
+ # accept for buffering
524
+ #SecRequestBodyLimit 131072
525
+ SecRequestBodyLimit 5242880
526
+ SecRequestBodyNoFilesLimit 51200
527
+```
528
+
529
+- /etc/httpd/modsecurity.d/modsecurity_localrules.conf
530
+```apache
531
+# Drop your local rules in here.
532
+
533
+# White List IP
534
+SecRule REMOTE_ADDR "@pmFromFile /etc/httpd/modsecurity.d/whitelist_ip.txt" \
535
+ "phase:1,id:'1000001',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
536
+
537
+# White List URI
538
+SecRule REQUEST_URI "@pmFromFile /etc/httpd/modsecurity.d/whitelist_uri.txt" \
539
+ "phase:1,id:'1000002',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
540
+
541
+# White List URI 2
542
+SecRule REQUEST_URI "@rx ^\/Etc\/" \
543
+ "phase:1,id:'1000003',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
544
+
545
+ # White List Sub-Domain
546
+ SecRule REQUEST_HEADERS:Host "@pmFromFile /etc/httpd/modsecurity.d/whitelist_subdomain.txt" \
547
+ "phase:1,id:'1000004',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
548
+
549
+# ZmEu Attack / phpMyAdmin
550
+SecRule REQUEST_URI "@rx (?i)\/(php-?My-?Admin[^\/]*|mysqlmanager|myadmin|pma2005|pma\/scripts|w00tw00t[^\/]+)\/" \
551
+ "severity:alert,id:'0000013',deny,log,status:400,msg:'Unacceptable folder.',severity:'2'"
552
+```
553
+ - mod_security-2.7.1 でエラーが出るから適当にid追加したけど、idの振り方のルールってどこにあるのかな?
554
+```
555
+ModSecurity: No action id present within the rule
556
+```
557
+
558
+- /etc/httpd/modsecurity.d/whitelist_ip.txt<br />
559
+mod_security による制限を行わない IP アドレスを列挙する。<br />
560
+コメントは行頭から「#」で始める。
561
+```
562
+# localhost
563
+127.0.0.1
564
+
565
+# example.com
566
+xxx.xxx.xxx.xxx
567
+
568
+# example.net
569
+yyy.yyy.yyy.yyy
570
+```
571
+
572
+- /etc/httpd/modsecurity.d/whitelist_uri.txt<br />
573
+mod_security による制限を行わない URI を列挙する。
574
+```
575
+/cgi-bin/etc/PrintEnv.cgi
576
+/cgi-bin/etc/PrintEnv_txt.cgi
577
+/cgi-bin/etc/index.cgi
578
+/cgi-bin/etc/testCGI.cgi
579
+```
580
+
581
+- /etc/httpd/modsecurity.d/whitelist_subdomain.txt<br />
582
+mod_security による制限を行わないホスト名を列挙する。
583
+```
584
+# WebApp1
585
+vh1.takeash.net
586
+```
587
+
588
+- /etc/httpd/modsecurity.d/activated_rules/modsecurity_crs_20_protocol_violations.conf
589
+ - id:958291 "Range: 0-", mp4 等のストリーミングや分割ダウンロードが行われるファイルのダウンロードで引っかかる。
590
+
591
+## リンク
592
+- http://modsecurity.org/
593
+ - [Reference Manual](https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual)
594
+ - [The Open Web Application Security Project](https://www.owasp.org/)
595
+- [hashdos攻撃をmod_securityで防御する(CentOS+yum編) - 徳丸浩の日記](http://blog.tokumaru.org/2012/01/hashdosmodsecuritycentosyum.html)
596
+- [Attack by ZmEu - The Linux Page](http://linux.m2osw.com/zmeu-attack) phpMyAdmin 脆弱性スキャンスクリプト対策
597
+- [黒ぶちメガネのblog » mod_securityのホワイトリスト、ブラックリストの書き方メモ](http://www.kurobuti.com/blog/?p=3775)
598
+- [禁煙できないSEの独り言: ModSecurity 2.5.12の導入](http://hiro-system.blog.ocn.ne.jp/blog/2010/04/modsecurity_251.html)
599
+- [(続)spammer対策 - ねこ様にもてあそばれる日々(2006-05-03)](http://m9841.info/?date=20060503#p02)
600
+- [mod_securityでWebサーバを守る(第1回) - ソフテック](http://www.softek.co.jp/Sec/mod_security1.html)
601
+- Webアプリケーションに潜むセキュリティホール
602
+ - [Webアプリケーションファイアウォールによる防御](http://www.atmarkit.co.jp/fsecurity/rensai/webhole11/webhole01.html)
603
+ - [mod_securityのXSS対策ルールを作成する](http://www.atmarkit.co.jp/fsecurity/rensai/webhole12/webhole01.html)
604
+- [UNIX的なアレ:gihyo.jp出張所](http://gihyo.jp/admin/serial/01/unix)
605
+ - [第19回 知っておきたいApacheの基礎知識 その15](http://gihyo.jp/admin/serial/01/unix/0019)
606
+
607
+# mod_geoip
608
+## インストール
609
+- EPELリポジトリ
610
+```
611
+yum install mod_geoip
612
+```
613
+
614
+## 設定
615
+- /etc/cron.monthly/updateGeoIP<br />
616
+データベースの自動更新スクリプト
617
+```bash
618
+wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
619
+gunzip GeoIP.dat.gz
620
+mv -f GeoIP.dat /usr/share/GeoIP/GeoIP.dat
621
+/sbin/restorecon -v /usr/share/GeoIP/GeoIP.dat
622
+```
623
+- GeoIP が表示されない場合は、SELinux のラベルを確認。
624
+```
625
+# ls -Z /usr/share/GeoIP/
626
+Good) unconfined_u:object_r:usr_t:s0
627
+NG) unconfined_u:object_r:admin_home_t:s0
628
+```
629
+ラベルが正しくない場合は下記コマンドで修正する。
630
+```
631
+# sealert -a /var/log/audit/audit.log
632
+# /sbin/restorecon -v /usr/share/GeoIP/GeoIP.dat
633
+```
634
+ラベル修正後、httpd を再起動すること。
635
+```
636
+# service httpd restart
637
+```
638
+
639
+## リンク
640
+- [MaxMind](http://www.maxmind.com/)
641
+ - [mod_geoip2 Apache module](http://dev.maxmind.com/geoip/mod_geoip2)
642
+ - [GeoLite Free Downloadable Databases](http://dev.maxmind.com/geoip/geolite)
643
+- [Apache2 mod_geoip CentOS うざい国を弾くモジュール « ORBIT SPACE](http://www.orsx.net/blog/archives/2510)
644
+
645
+# Apacheでhttp-equiv属性値を反映させる
646
+- [META要素「http-equiv属性値とHTTPヘッダー」について考える](http://www.infoaxia.com/tools/blog/archives/cat3/)
647
+- [Apache mod_xml_charset](http://www.yoshidam.net/XML_ja.html)
648
+
649
+- デフォルトでは、LastModifiedしか反映されない。
650
+- apxsはhttpd-devel-xxx.rpmをインストールすることで使用できるようになる。
651
+- うちの Fedora Core 3 環境だとmod_html_metaのコンパイルでエラーが出るんでまだ使えていない。引き続き検討。
652
+
653
+# Apache に DoS 攻撃対策 mod_evasive
654
+- [mod_evasive](http://www.zdziarski.com/blog/?page_id=442) @ [Jonathan Zdziarski's Domain](http://www.zdziarski.com/)
655
+- [Apache DoS攻撃対策 mod_evasiveインストール&設定 &#8211; Linux](http://www.makizou.com/archives/1341) @ [MAKIZOU.COM](http://www.makizou.com/)
656
+
657
+# cgi-bin ディレクトリでファイル名を省略したときに index.cgi を実行する設定
658
+- httpd.conf に ScriptAliasMatch を追加する。
659
+
660
+- 修正前
661
+```apache
662
+ScriptAlias /cgi-bin/ /var/www/cgi-bin/
663
+```
664
+
665
+- 修正後
666
+```apache
667
+ScriptAliasMatch ^/cgi-bin/(.*)\.cgi /var/www/cgi-bin/$1.cgi
668
+ScriptAliasMatch ^/cgi-bin/(.*)/? /var/www/cgi-bin/$1/index.cgi
669
+ScriptAliasMatch ^/cgi-bin$ /var/www/cgi-bin/index.cgi
670
+ScriptAlias /cgi-bin/ /var/www/cgi-bin/
671
+```
672
+
673
+- index.cgi の例
674
+```perl
675
+#!/usr/local/bin/perl
676
+
677
+use strict;
678
+use warnings;
679
+use utf8;
680
+use CGI::Pretty;
681
+
682
+my $q = new CGI;
683
+my $host = $q->url(-base => 1);
684
+print $q->redirect( $host . '/' );
685
+
686
+# EOF
687
+```
688
+
689
+- [Apacheで、http://hoge.hoge/cgi-bin/foo/にアクセスできるようにする方法](http://blogs.dion.ne.jp/fit_si/archives/5941241.html)
690
+
691
+# Apache で外部からの直リンクを禁止する
692
+- [404 Blog Not Found:Apache - ホットリンクを禁止する](http://blog.livedoor.jp/dankogai/archives/50804992.html)
693
+
694
+# Basic認証にタイムアウトを設定する
695
+- [mod_auth_timeout](http://secure.linuxbox.com/tiki/tiki-index.php?page=mod_auth_timeout)
696
+- [サードパーティー製認証モジュール](http://www.thinkit.co.jp/article/120/3/2.html)