Linux/보안장비 운용

아파치 인증 설정하기

GGkeeper 2022. 3. 8. 16:09

실습> 아파치 인증 설정하기

goaccess 로 실시간 로그: /var/www/html/webServerLog
http://192.168.108.101/webServerLog: 아파치를 통해서 ID/PW를 사용할 수 있도록 설정한다.

1. goaccess 실행 중지
기존 로그는 Ctrl + C를 눌러서 종료한다.
# goaccess /var/log/httpd/access_log --log-format=COMBINED \
-a -o /var/www/html/report.html --real-time-html
WebSocket server ready to accept new client connections
Accepted: 13 192.168.108.1
Active: 1
^C
SIGINT caught!
Closing GoAccess...
Handled self-pipe to close event loop.
Stopping WebSocket server...

2. 디렉터리 생성
# cd /var/www/html
# mkdir webServerLog
# cd webServerLog

3. 설정 파일 수정
/etc/httpd/conf/httpd.conf 웹서버 설정파일 맨 마지막에 분산 설정파일 허용을 추가한다.
# vi /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/webServerLog">
    # 분산 설정파일 허용
    AllowOverride All
    Options Indexes 
</Directory>

4. 웹서버 재시작
아파치 웹서버의 설정을 검사하고 문제가 없다면 웹서버를 재시작한다.
# httpd -t
Syntax OK

# systemctl reload httpd

5. 접근제어 파일 생성
webServerLog 디렉터리 밑에는 아무나 접근하면 안되므로 아파치 인증을 사용한다.
.htaccess : 인증을 설정할 디렉터리에 생성한다. (/var/www/html/webServerLog)
.htpasswd : 사용자와 그룹 인증에 필요한 id/pw를 넣어둔다. (웹 디렉터리 밖에 위치시킨다. /var/www/htpasswd)

# pwd
/var/www/html/webServerLog

# vi .htaccess
-- .htaccess --
# 인증에 사용할 영역
AuthName "Admin Auth:"
# 사용자를 인증할 방법 (기본 Basic)
AuthType Basic
# 사용자
AuthUserFile "/var/www/htpasswd/.htpasswd"
<Limit GET POST>
# 사용자로 접근을 허용
require valid-user
</Limit>
-- .htaccess --

# cd ../..
# pwd
/var/www

# mkdir htpasswd
# cd htpasswd
# chmod 700 .
# chown apache .
# ls -ld
drwx------. 2 apache root 23  3월  7 17:56 .


# htpasswd
htpasswd 
Usage:
htpasswd [-cimB25dpsDv] [-C cost] [-r rounds] passwordfile username
htpasswd -b[cmB25dpsDv] [-C cost] [-r rounds] passwordfile username password

htpasswd -n[imB25dps] [-C cost] [-r rounds] username
htpasswd -nb[mB25dps] [-C cost] [-r rounds] username password
 -c  Create a new file.
 -n  Don't update file; display results on stdout.
 -b  Use the password from the command line rather than prompting for it.
 -i  Read password from stdin without verification (for script usage).
 -m  Force MD5 encryption of the password (default).
 -2  Force SHA-256 crypt() hash of the password (secure).
 -5  Force SHA-512 crypt() hash of the password (secure).
 -B  Force bcrypt aencryption of the password (very secure).
 -C  Set the computing time used for the bcrypt algorithm
     (higher is more secure but slower, default: 5, valid: 4 to 31).
 -r  Set the number of rounds used for the SHA-256, SHA-512 algorithms
     (higher is more secure but slower, default: 5000).
 -d  Force CRYPT encryption of the password (8 chars max, insecure).
 -s  Force SHA-1 encryption of the password (insecure).
 -p  Do not encrypt the password (plaintext, insecure).
 -D  Delete the specified user.
 -v  Verify password for the specified user.
On other systems than Windows and NetWare the '-p' flag will probably not work.
The SHA-1 algorithm does not use a salt and is less secure than the MD5 algorithm.

# htpasswd -5 -c .htpasswd webadmin  
New password:  <-- P@ssw0rd 입력
Re-type new password:  <-- P@ssw0rd 입력
Adding password for user webadmin

# ls -a
.  ..  .htpasswd
# cat .htpasswd 
webamin:$6$1s/ZE4AGJRZPFKH1$ddqiUBCECkEEzXndhcJtyTI8vHoSpsZgdAVMUDZn97ILaIF2xiaMYLoYjHqVBqGxHDTjTYMZ2sMX.AyDcCqEX/

웹 브라우저로 접속할 때 로그인 창이 뜨면 성공!!!
http://192.168.108.101/webServerLog

6. goaccess 실행 중지
기존 로그는 Ctrl + C를 눌러서 종료한다.
# cd
# goaccess /var/log/httpd/access_log --log-format=COMBINED \
-a -o /var/www/html/webServerLog/report.html --real-time-html

웹 브라우저로 접속할 때 로그인 창에서 webamin/P@ssw0rd로 로그인한다.
http://192.168.108.101/webServerLog/report.html

7. 쉘스크립트 작성
goaccess 명령어를 쉘스크립트로 작성해서 백그라운드 프로세스로 실행한다.
파일명: /root/bin/goaccess.sh
# mkdir ~/bin
# cd ~/bin
# install /dev/null goaccess.sh
# vi goaccess.sh
-- goaccess.sh --
#!/bin/sh
# 파일명: goaccess.sh
# 프로그램 설명: goaccess 를 실행하는 스크립트
# 작성자: 리눅스마스터넷
# 작성일: 2022. 03. 07. (월) 19:08:12 KST
# 버전:2022030701

accessLogFile="/var/log/httpd/access_log"
reportFile="/var/www/html/webServerLog/report.html"

# goaccess가 백그라운드 프로세스로 실행한다.
goaccess $accessLogFile --log-format=COMBINED -a -o $reportFile --real-time-html &
-- goaccess.sh --

# goaccess.sh 
WebSocket server ready to accept new client connections


웹 브라우저로 접속해서 Total requests 부분에 숫자가 변하면 goaccess 프로세스가 백그라운드 프로세스로 실행중인 것이다.
http://192.168.108.101/webServerLog/report.html


SELinux가 On으로 설정되어 있다면 .htpasswd 파일의 보안문맥이 user_home_t로 생성된다.
이걸 아래처럼 httpd_user_content_t로 변경한다.
생성된 .htpasswd 파일을 httpd_sys_content_t or httpd_user_content_t 로 보안문맥(컨텍스트)을 변경한다.
httpd_user_content_t 로 변경할 때는 httpd_enable_homedirs 가 on으로 설정되어 있어야 한다.
>>> SELinux 세팅 <<<
user_home_t : /home 디렉터리에서 파일 생성 시
httpd_sys_content_t : /var/www/html/webServerLog
httpd_user_content_t : /home/username/public_html/webServerLog  <-- 가상호스트를 세팅했을 경우
httpd_enable_homedirs : off -> on 
>>> SELinux 세팅 <<<