20160728 로그보안
6 | 로그 파일 관리 |
(1) 로그 로테이션(Log Rotation)
/var/log 디렉토리안에 있는 많은 로그 파일은 기존의 파일에 첨가되는 형태로 기록되기 때문에 그냥 두게 되면 크기는 계속 커지게 된다. 그럼 관리하기 힘들고, 로그 파일안에서 검색하는 속도도 떨어지게 된다. 이를 방지하기 위해서 로그 파일을 정기적으로 조각으로 나누어야 한다. 이런 작업을 하는 프로그램으로 logrotate 명령어가 있다.
■ logrotate 명령어
■ /etc/logrotate.conf 파일
# cat /etc/logrotate.conf
# see "man logrotate" for details # rotate log files weekly weekly 특별히 명시하지 않은 로그 파일에 대해서는 일주일(weekly)마다 rotate 한다.
# keep 4 weeks worth of backlogs rotate 4 최대 4번까지 rotate를 허용한다.(EX: logfile, logfile.1, logfile.2, logfile.3, logfile.4)
# create new (empty) log files after rotating old ones create 로그 파일을 rotate 한 후에 비어 있는 로그 파일을 생성한다.
# uncomment this if you want your log files compressed #compress 로그 파일을 압축하는 옵션이다. 기본값은 활성화 되어 있지 않다. 용량 문제에 지장이 없다면 압축하지 않을 것을 권장한다.
# RPM packages drop log rotation information into this directory include /etc/logrotate.d 대부분의 RPM 패키지로 설치되는 데몬들은 이 디렉토리에 로그 파일 정책 파일이 생성된다. 각각의 로그 파일을 rotate 시킬수 있도록 하는 설정이다. # no packages own wtmp -- we'll rotate them here /var/log/wtmp { monthly minsize 1M create 0664 root utmp rotate 1 } 로그 파일(wtmp)은 어떤 패키지에 의해서도 설정되지 않기 때문에 따로 설정한다. 다른 로그 파일들은 /etc/logrotate.d 내의 파일들에 모두 각각 설정하고 있다. 한달마다 rotate 하며, 최대 1회까지만 rotate 한다. 관련 파일인 /var/log/utmp 파일을 664 퍼미션을 가지고 소유자는 root로 생성한다.
# system-specific logs may be also be configured here. 시스템에서 특별하게 지정하고 싶은 로그 파일들이 있다면 여기에 정의한다. /var/log/lastlog { monthly rotate 3 } 한달마다 rotate 하며 최대 3회가지 rotate 한다. |
# cd /etc/logrotate.d
# ls
acpid cups mgetty ppp rpm sa-update squid tux wpa_supplicant conman httpd named psacct samba setroubleshoot syslog vsftpd.log yum |
# cat syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true endscript } |
# ls -l /var/log/messages*
-rw------- 1 root root 240K Mar 23 05:55 /var/log/messages -rw------- 1 root root 485K Mar 21 01:22 /var/log/messages.1 -rw------- 1 root root 489K Mar 13 21:57 /var/log/messages.2 -rw------- 1 root root 663K Mar 7 13:15 /var/log/messages.3 -rw------- 1 root root 407K Feb 28 03:03 /var/log/messages.4 |
# ls -l /var/log/secure*
-rw------- 1 root root 2.9K Mar 23 03:48 /var/log/secure -rw------- 1 root root 6.2K Mar 21 00:19 /var/log/secure.1 -rw------- 1 root root 12K Mar 13 21:56 /var/log/secure.2 -rw------- 1 root root 12K Mar 7 13:03 /var/log/secure.3 -rw------- 1 root root 4.9K Feb 28 02:59 /var/log/secure.4 |
# ls -l /var/log/boot.log*
-rw------- 1 root root 0 Mar 21 01:22 /var/log/boot.log -rw------- 1 root root 0 Mar 14 04:02 /var/log/boot.log.1 -rw------- 1 root root 0 Mar 7 14:08 /var/log/boot.log.2 -rw------- 1 root root 0 Feb 28 04:02 /var/log/boot.log.3 -rw------- 1 root root 0 Feb 24 00:38 /var/log/boot.log.4 |
(2) 오래된 로그 삭제
로그 파일 이름이 고정되어 있는 경우: /var/log/messages, /var/log/server.log
로그 파일 이름이 고정되어 있지 않은 경우: /var/log/server_1020.log
실무에서 많이 사용되는 로그 파일 포맷: server_1020.log (10월20일 로그 파일)
--------------------------------------------
[참고] find 명령어 형식
# find / -name core –type f
# find / -user user01 –group user01
# find / -mtime [-7|7|+7]
# find / -size [-10M|10M|+10M]
# find / -perm [-755|755]
# find / -name core -type f -exec rm {} \;
--------------------------------------------
# find /Log_Dir -name "*.log" -type f -mtime +30 -exec rm -f {} \;
-> 정상적으로 동작하는 명령어
/Log_dir -------- server_0101.log
server_0102.log
server_0103.log
server_0104.log
......
# crontab –e
0 3 1 * * find /Log_Dir1 -name "*.log" -type f -mtime +30 -exec rm -f {} \; 10 3 1 * * find /Log_Dir2 -name "*.log" -type f -mtime +60 -exec rm -f {} \; 20 3 1 * * find /Log_Dir3 -name "*.log" -type f -mtime +90 -exec rm -f {} \; |
-> 매월 1일 3시 정각(새벽시간)에 /Log_Dir 안의 로그 파일들을 최근 30일 로그만 남기고 모두 삭제한다.
[EX] 오래된 로그 삭제 실습
# mkdir /waslog
# cd /waslog
# rdate -s 172.16.9.252 (# rdate -s time.bora.net)
----------------------------------------------
# date
Sat Jan 10 13:27:03 KST 2015
# date +'%m %d %H:%M:%S'
# date +%m%d
# date -d '+45 day' +%m%d
# date -d '1 day ago' +%m%d
# date -d '2 day ago' +%m%d
# date -d '3 day ago' +%m%d
# for i in `seq 0 4`
do
touch -t `date -d "$i day ago" +%m%d`1300 file_`date -d "$i day ago" +%m%d`.log
sleep 1
done
------------------------------------------------
# touch file_0116.log file_0115.log file_0114.log file_0113.log file_0112.log
#
# ls -l file*
-rw-r--r-- 1 root root 0 Jan 16 13:42 file_0112.log -rw-r--r-- 1 root root 0 Jan 16 13:42 file_0113.log -rw-r--r-- 1 root root 0 Jan 16 13:42 file_0114.log -rw-r--r-- 1 root root 0 Jan 16 13:42 file_0115.log -rw-r--r-- 1 root root 0 Jan 16 13:42 file_0116.log |
# date
-> 01월16일 13시42분
# touch -t 01151342 file_0115.log
# touch -t 01141342 file_0114.log
# touch -t 01131342 file_0113.log
# touch -t 01121342 file_0112.log
# ls -l
-rw-r--r-- 1 root root 0 Jan 12 13:42 file_0112.log -rw-r--r-- 1 root root 0 Jan 13 13:42 file_0113.log -rw-r--r-- 1 root root 0 Jan 14 13:42 file_0114.log -rw-r--r-- 1 root root 0 Jan 15 13:42 file_0115.log -rw-r--r-- 1 root root 0 Jan 16 13:42 file_0116.log |
Currunt 1 day ago 2 day ago 3 day ago 4 day ago
| | | | |
V V V V V
0116 0115 0114 0113 0112
# find /waslog -name "*.log" -type f -mtime +3
-> file_0112.log
# find /waslog -name "*.log" -type f -mtime 3
-> file_0113.log
# find /waslog -name "*.log" -type f -mtime -3
-> file_0114.log, file_0115.log, file_0116.log
# find /waslog -name "*.log" -type f -mtime +3 -exec rm -f {} \;
# find /waslog -name "*.log" -type f –mtime +3
[실습] 정보 수집
(주의) 운영체제 스냅샷
VMware > Snapshot > Take snapshot > 2015_0116
(고객) 오늘 중요한 파일(/etc/passwd)이 지워졌다고 보고가 들어 왔다.
(결론) 파일을 지운 사용자에 대한 정보를 수집한다.(최대한 많은 정보를 수집한다.)
----------------------------------------------------
# rdate -s 172.16.9.252
윈도우즈 putty를 통해 서버(172.16.10.2XX)에 접속(user01 사용자)
$ su - root
root 사용자로 전환
# crontab -l
# tar cvzf /backup/backup_`date +%m%d`.tar.gz /home
# cat /etc/passwd
# cp /etc/passwd /etc/passwd.old
# rm -f /etc/passwd
# exit
$ exit
----------------------------------------------------
----------------------------------------------------
< CTRL + ALT + <- >
오늘 중요한 파일(/etc/passwd)이 삭제 되었다.
(ㄱ) 오늘 파일이 지워졌다.
(ㄴ) 중요한 파일(/etc/passwd)이 지워졌다.
사용자의 서버 활동 사항을 추적하여 보고서로 만든다.(1시간)
-> 시간에 기반한 보고서가 만들어져야 한다.(MAC 분석 작업)
------------------------- [참고 1]-----------------------------------------
# rdate -s 172.16.9.252
(172.16.9.252)
# chkconfig time-dgram on
# chkconfig time-stream on
# service xinetd restart
윈도우즈 putty를 통해 서버(172.16.10.2XX)에 접속(user01 사용자)
# last (# last -f /var/log/wtmp)
# last -f /var/log/wtmp.1
# lastlog (/var/log/lastlog)
# lastb (/var/log/wtmp)
# who (/var/run/utmp)
$ su - root
root 사용자로 전환
# cat /var/log/secure
# crontab -l
# cat /var/log/cron
# tar cvzf /backup/backup_`date +%m%d`.tar.gz /home
# cat /var/log/backup.log
# cat /etc/passwd
# cp /etc/passwd /etc/passwd.old
# ls /etc/*passwd*
# rm -f /etc/passwd
# cat ~hacker/.bash_history
# cat /home/*/.bash_history | egrep '/etc/passwd'
cat /etc/passwd
rm -f /etc/passwd
# find /home -name .bash_history -exec egrep -l 'rm -f /etc/passwd' {} \;
/home/user01/.bash_history
# exit
$ exit
------------------------------------------------------------------------------------------
---------------------------------- [참고 2] -----------------------------------------------
# pinfo date
-> Examples of date 선택
-> 예제들 중 아래 명령어 예제를 찾는다.
# date -d '1970-01-01 UTC 1441155935 seconds' +"%Y-%m-%d %T %z"
-> 시간 출력 포맷을 변경하여 출력
# cat ~/.bash_history
#1441086536 2015.03.20 13:00:00 history
history
#1441086605
vi /etc/skel/.bash_logout
#1441088279
clear
#1441088283
unalias ls
#1441088291
useradd hacker
--------------------------------------------------------------------------------------------
[과제] 로그파일의 내용 중 자신의 로그인한 기록을 지우는 방법
/var/log/wtmp (data file) -> last CMD
/var/run/utmp (data file) -> who CMD
# cd /var/log
# mv wtmp wtmp.old
# last
# mv wtmp.old wtmp
# last
(Hacker) Text Log Format
# cd /var/log
# ls -l secure > secure.time
# vi secure
.... 적당한 라인 삭제 ....
# cat secure.time
# touch -t <시간> secure
(Hacker) Data Log Format
structure
# man utmp
# man wtmp
'모의해킹 침해대응 전문가 과정' 카테고리의 다른 글
20160729 방화벽 (0) | 2016.07.29 |
---|---|
20160728 방화벽 (0) | 2016.07.28 |
20160727 로그보안 (0) | 2016.07.27 |
20160727 리눅스 서버보안 (0) | 2016.07.27 |
20160726 리눅스 서버보안 (0) | 2016.07.26 |