20160727 로그보안
로그 서버 가이드, 로그 보안
■ 단원의 목적
(ㄱ) 로그 기록 체계
(ㄴ) 로그 기록 분석
(ㄷ) 로그 서버
1 | 리눅스 로그 개요 |
(1). 로그 기록 용도
▪ 사용자 및 서버의 활동 기록
▪ 시스템 공격에 대한 흔적 기록
▪ 서버 장애에 대한 흔적 기록
▪ 로그 기록을 통한 성능 카운트
-> 옛날 로그 기록은 거의 필요가 없다.(예: 6개월전, 1년전의 로그기록)
-> 따라서, 오래된 로그 기록들의 관리 정책을 잘 세워야 한다.
(주의) 로그 기록을 이해하지 못하는 관리자에게는 아무런 쓸모가 없는 파일에 불과하다.
따라서 로그 기록을 분석하는 방법도 중요하다.
[참고] 로그 기록이 사용되는 실무적인 예 |
(전제조건)사용자 활동량이 많은 서버(접속해서 사용하는 사용자들)
서버 관리자가 자신이 맡고 있는 서버에 접속하고 난후 작업 순서 ① 서버 관리자 접속 ② 서버 점검 포인트 첫번째, 현재 사용량 점검 - CPU I/O, MEM I/O, DISK I/O, Network I/O, 프로세스 사용량 점검 -> sar, vmstat, mpstat, netstat, top, ps, iostat, df, du, quot.... 두번째, 이전 사용량 점검 - 로그 기록 -> /var/log/messages, /var/log/secure, /var/log/xferlog.... 세번째, 서버의 목적에 맞는 사용량 점검 - 사용자가 사용하고 있는 프로세스 점검 -> top, gnome-system-monitor, ps, who, w, watch.... |
(2). 로그의 위치
다음은 유닉스/리눅스에서 쓰고 있는 일반적인 로그 파일들의 위치이다.
▪ /usr/adm 초기 유닉스, BSD 계열, HP-UX 9.X, SunOS 4.X
▪ /var/adm 최근 유닉스, SVR4 계열, Solaris, HP-UX 10.X 이후
▪ /var/log 일부 BSD 계열, FreeBSD, Linux
▪ /var/run 일부 Linux
(3) 리눅스 기본 로그 파일
[표] 리눅스 기본 로그 파일의 종류
로그파일 | 설 명 |
(T) /var/log/messages | 메일(mail), 뉴스(news), 인증(authpriv), 크론(cron) 메세지를 제외한 시스템의 전반적인 메세지를 기록하는 로그파일이다. 리눅스에서는 가장 많은 메세지를 담고 있는 파일이다. (TUI) # tail -f /var/log/messages (GUI) # gnome-system-log & |
(T) /var/log/secure | 개인인증을 기록하는 로그파일이다.(EX: su, telnet, ssh) |
(T) /var/log/dmesg | 시스템이 부팅할때 메세지를 기록하는 로그 파일이다. dmesg 명령어가 이 파일을 참조하여 정보를 출력한다. # dmesg |
(D) /var/log/lastlog | 사용자의 가장 마지막에 로그인한 시간을 기록하는 로그 파일이다. 바이너리 파일이므로 안의 내용을 cat 명령어를 통해 확인할 수 없다. lastlog 명령어가 이 파일을 참조하여 정보를 출력한다. # lastlog |
(T) /var/log/cron | crontab, at 명령어를 통해 실행했던 작업을 기록하는 로그 파일이다. - crond -> crontab CMD - atd -> at CMD |
(T) /var/log/maillog | sendmail 메일 관련 작업을 기록하는 로그 파일이다. |
(T) /var/log/xferlog | FTP 서버에서 업로드/다운로드을 기록하는 로그 파일이다. |
(D) /var/log/wtmp | 사용자의 로그인/로그아웃 시간을 기록하는 로그파일이다. 데이터 파일이므로 안의 내용을 cat 명령어를 통해 확인할 수 없다. last 명령어가 이 파일을 참조하여 정보를 출력한다. # last (# last oracle) |
(D) /var/run/utmp | 현재 로그인한 사용자 정보를 기록하는 로그 파일이다. 데이터 파일이므로 안의 내용을 cat 명령어를 통해 확인 할 수 없다. who 명령어가 이 파일을 참조하여 정보를 출력한다. # who |
T: Text, ASCII
D: Data
[EX1] 기본 로그 파일 확인
# cd /var/log
# ls
Xorg.0.log boot.log.1 cups/ maillog.1 prelink/ secure.2 wtmp Xorg.0.log.old boot.log.2 dmesg maillog.2 rpmpkgs setroubleshoot/ xferlog acpid btmp faillog messages rpmpkgs.1 spooler xferlog.1 anaconda.log conman/ gdm/ messages.1 rpmpkgs.2 spooler.1 xferlog.2 anaconda.syslog conman.old/ httpd/ messages.2 samba/ spooler.2 yum.log anaconda.xlog cron lastlog news/ scrollkeeper.log squid/ audit/ cron.1 mail/ pm/ secure tallylog boot.log cron.2 maillog ppp/ secure.1 vbox/ |
[EX2] 로그 확인(GUI 방식)
GNOME : GTK+
KDE : QT
# gnome-[TAB][TAB]
gnome-about gnome-pilot-make-password gnome-about-me gnome-power-inhibit-test gnome-accessibility-keyboard-properties gnome-power-manager gnome-at-properties gnome-power-preferences gnome-audio-profiles-properties gnome-screensaver gnome-background-properties gnome-screensaver-command gnome-calculator gnome-screensaver-preferences gnome-cd gnome-screenshot gnome-character-map gnome-search-tool gnome-control-center gnome-session gnome-default-applications-properties gnome-session-properties gnome-default-printer gnome-session-remove gnome-desktop-item-edit gnome-session-save gnome-dictionary gnome-sound-properties gnome-display-properties gnome-system-log gnome-doc-prepare gnome-system-monitor gnome-eject gnome-terminal gnome-file-share-properties gnome-text-editor gnome-font-properties gnome-theme-manager gnome-font-viewer gnome-theme-thumbnailer gnome-help gnome-thumbnail-font gnome-keybinding-properties gnome-typing-monitor gnome-keyboard-properties gnome-ui-properties gnome-keyring-daemon gnome-umount gnome-mount gnome-volume-control gnome-mouse-properties gnome-volume-manager gnome-network-preferences gnome-volume-properties gnome-open gnome-window-properties gnome-panel gnome-wm gnome-panel-screenshot |
# gnome-system-[TAB][TAB]
gnome-system-log gnome-system-monitor |
■ 시스템 모니터링
----------------------------+--------------------------------
TUI | GUI
----------------------------+--------------------------------
# top | # gnome-system-moniotor
# tail -f /var/log/messages | # gnome-system-log
----------------------------+--------------------------------
# gnome-system-log &
[참고] 새로운 로그 파일 추가
"File" > "Open" > "messages.1" 파일 선택
or
# tail –f /var/log/massages (# alias mlog='tail -f /var/log/messages' -> ~/.bashrc)
# tail -f /var/log/secure (# alias slog='tail -f /var/log/secure' -> ~/.bashrc)
2 | syslog 체계 |
log4j : 프로그램 로그
syslogd : 리눅스/유닉스 시스템 로그(EX: syslog())
CentOS 5.X 이하) syslogd
CentOS 6.X 이상) rsyslogd
(1) syslogd 데몬
Sysklogd provides two system utilities which provide support for system logging and kernel message trapping. Support of both internet and unix domain sockets enables this utility package to support both local and remote logging.
System logging is provided by a version of syslogd(8) derived from the stock BSD sources. Support for kernel logging is provided by the klogd(8) utility which allows kernel logging to be conducted in either a standalone fashion or as a client of syslogd.
Syslogd provides a kind of logging that many modern programs use. Every logged message contains at least a time and a hostname field, normally a program name field, too, but that depends on how trusty the logging program is.
While the syslogd sources have been heavily modified a couple of notes are in order. First of all there has been a systematic attempt to insure that syslogd follows its default, standard BSD behavior. The second important concept to note is that this version of syslogd interacts transparently with the version of syslog found in the standard libraries. If a binary linked to the standard shared libraries fails to function correctly we would like an example of the anomalous behavior. |
(2) syslogd 체계
리눅스 시스템에서 기존에 존재하는 로그 파일을 쓰지 않고 새로운 로그 파일을 생성하거나, 또는 로그 레벨을 변경하여 더 많은 메세지를 남기도록 하기 위해서는 /etc/syslog.conf 파일의 이해와 syslog 체계에 대해서 알아 두어야 한다.
리눅스 운영체제에서 메세지 생성에 관해서는 syslogd 데몬에 의해서 총괄 관리 되고 있다. 시스템 메세지가 생성이 되면 메세지는 syslogd 데몬에게 전달이 되고 syslogd 데몬은 /etc 디렉토리에 존재하는 syslog.conf 파일의 설정을 확인 하여 적당한 위치로 로그 기록을 출력 하게 된다.
/etc/syslog.conf 파일에는 “어디에서 메세지가 생성이 되면 어디에 메세지를 남겨라”라고 설정 되어 있다. 그럼, syslogd 데몬은 /etc/syslog.conf 파일에 설정된대로 생성된 메세지를 적당한 곳에 기록하게 된다.
시스템 로깅 관리는 syslogd 데몬을 통해 이루어지고, syslogd 데몬이 읽어 들이는 주설정 파일이 /etc/syslog.conf 파일이기 때문에 시스템 관리자에게 /etc/syslog.conf 파일을 관리하는 것은 중요한것이다.
|
[그림] syslogd 체계
3 | /etc/syslog.conf 파일 |
(1) /etc/syslog.conf 파일
The syslog.conf file is the main configuration file for the syslogd(8) which logs system messages on *nix systems. This file specifies rules for logging. For special features see the sysklogd(8) manpage.
Every rule consists of two fields, a selector field and an action field. These two fields are separated by one or more spaces or tabs. The selector field specifies a pattern of facilities and priorities belonging to the specified action.
Lines starting with a hash mark (‘‘#’’) and empty lines are ignored.
This release of syslogd is able to understand an extended syntax. One rule can be divided into several lines if the leading line is terminated with an back- slash (‘’\‘’). |
/etc/syslog.conf 파일의 형식은 Selector 필드와 Action 필드로 구분된다. Selector 필드는 다시 Facility와 Level로 구분된다. Facility는 메세지의 종류를 나타내고 Level은 메세지의 난이도(중요도)가 된다. Action 필드는 로그가 기록되는 곳을 지정하게 된다.
(/etc/syslog.conf 파일의 필드 구분)
Selector [TAB/SPACE] Action
Facility.Level [TAB/SPACE] Action
EX) user.notice /var/log/test.log
Facility : 메세지의 종류
Level : 메세지의 난이도(위험 수준)
Action : 메세지 기록 위치
(1-1). 메세지 종류(Facility)
EX) user.notice /var/log/test.log
메세지 종류 | 설 명 |
authpriv | 개인 인증을 요하는 프로그램 유형이 발생한 메세지(EX: su, telnet, ssh) |
cron | crontab(crond), at(atd) 프로그램이 발생한 메세지(EX: crontab, at) |
daemon | 시스템 데몬이 생성한 메세지. (EX: telnetd, ftpd) |
kern | 커널이 발생한 메세지 |
lpr | 프린트 유형의 프로그램이 발생한 메세지 |
메일시스템이 발생한 메세지. | |
mark | syslogd 데몬에 의해 생성된 일정한 시간간격의 반복적인 메세지 |
news | 유즈넷 뉴스 프로그램 유형이 발생한 메세지 |
security(auth) | 인증 프로그램이 생성한 메세지(EX: login) |
syslog | syslog 프로그램이 생성한 메세지(EX: syslogd) |
user | 사용자 프로그램에 의해 생성된 메세지(EX: top, system-config-*) |
uucp | UUCP(Unix to Unix Copy) 시스템이 발생한 메세지 |
local0-7 | 예약된 메세지 종류, 여분으로 남겨둔 유형(EX: local0, local1, local2, ...) |
* | 모든 메세지(단, mark 메세지 종류는 제외) |
(1-2). 메세지 난이도(Level)
메세지의 난이도(위험수위)를 가리키며 설정된 난이도 이상을 나타낸다. 메세지 난이도 앞에 '='를 사용하는 경우 해당 메세지 난이도의 위험도와 같은 경우를 의미한다.
EX) user.notice /var/log/test.log
메세지 레벨 | 설 명 | |
0 | emerg (panic) | 패닉상태, 모든 사용자에게 전달되어야 할 위험한 상황의 메세지 |
1 | alert | 즉각적인 조치를 위해야 상황의 메세지 |
2 | crit | 시스템에 문제가 생기는 단계의 메세지 |
3 | error (err) | 에러가 발생하는 경우의 메세지 |
4 | warning (warn) | 주의를 요하는 경고 메세지 |
5 | notice | 특별한 주의를 요하는 에러는 아닌 메세지 |
6 | info | 통계, 기본정보 메세지 |
7 | debug | 프로그램을 디버깅할 때 생성되는 메세지 |
8 | none | facility 뒤에 .none을 붙이면 해당 facility를 제외하겠다는 의미이다. 예를 들어 mail.none은 메일 관련 메세지를 제외한다는 뜻이다. |
(1-3). 메세지 기록 위치(Action)
EX) user.notice /var/log/test.log
메세지 기록 위치 | 설 명 |
/var/log/messages | 기록을 남길 특정한 로그 파일 선택(예: /var/log/file.log) |
/dev/console | 기록을 남길 콘솔에 로그 기록 남김(예: /dev/console) |
user01,root, * | 특정한 사용자나 모든 사용자 선택(예: user01, *) |
@hostA, @172.16.8.254 | 다른 호스트 선택(예: @172.16.8.254)-> 보통 로그 서버 선택 |
(2) /etc/syslog.conf 파일 분석
# cat /etc/syslog.conf
# Log all kernel messages to the console. # Logging much else clutters up the screen. #kern.* /dev/console 커널에 관련한 모든 메세지는 콘솔창에 출력한다.
# Log anything (except mail) of level info or higher. # Don't log private authentication messages! *.info;mail.none;news.none;authpriv.none;cron.none /var/log/messages 메일(mail),뉴스(news),개인인증(authpriv),크론(cron) 메세지를 제외한 대부분의 메세지를 /var/log/messages 파일에 기록한다.
# The authpriv file has restricted access. authpriv.* /var/log/secure 개인적인 인증에 관련한 부분은 /var/log/secure 파일에 기록한다.(su, sshd)
# Log all the mail messages in one place. mail.* -/var/log/maillog 메일에 관련한 모든 메세지는 /var/log/maillog 파일에 기록한다.
# Log cron stuff cron.* /var/log/cron 크론(cron) 메세지 모두를 /var/log/cron 파일에 기록한다.
# Everybody gets emergency messages *.emerg * 모든 경우 위험한 메세지(패닉 상태)는 모든 사용자에게 뿌린다.
# Save news errors of level crit and higher in a special file. uucp,news.crit /var/log/spooler uucp,news에 대한 critical 이상 메세지는 /var/log/spooler 파일에 기록한다.
# Save boot messages also to boot.log local7.* /var/log/boot.log 미리 예약된 local7 종류에 대한 모든 메세지(부팅시의 메세지)를 /var/log/boot.log 파일에 기록한다.
# # INN # news.=crit /var/log/news/news.crit news.=err /var/log/news/news.err news.notice /var/log/news/news.notice |
[참고] "mail.* -/var/log/maillog" 대해서 |
You may prefix each entry with the minus ‘‘-’’ sign to omit syncing the file after every logging. Note that you might lose information if the system crashes right behind a write attempt. Nevertheless this might give you back some per- formance, especially if you run programs that use logging in a very verbose man- ner. |
4 | logger 명령어 |
(1) logger 명령어
logger - a shell command interface to the syslog(3) system log module
Logger makes entries in the system log. It provides a shell command interface to the syslog(3) system log module.
-p pri Enter the message with the specified priority. The priority may be spec- ified numerically or as a ‘‘facility.level’’ pair. For example, ‘‘-p local3.info’’ logs the message(s) as informational level in the local3 facility. The default is ‘‘user.noticeecifi4 |
logger 명령어는
임의의 메세지를 생성시키는 역할을 가진다.
주로 테스트 용도로 사용된다.
(명령어 형식)
# logger "System rebooted" (# logger -p user.notice "System rebooted")
Default Value: user.notice
# logger -p user.err "System rebooted" (-p : priority)
# logger -p local0.notice "System Messages"
/etc/syslog.conf 파일에 auth.notice 라고 설정이 되어 있다면, notice는 notice 이상의 메세지를 모두 포함한다. (notice -> warning -> err -> crit -> emerg). logger 명령어를 통해 auth.notice 메세지와 auth.crit 메세지를 생성하게 되면, /etc/syslog.conf 파일에 따라 콘솔창에 로그 메세지가 남겨지게 된다.
===메세지 생성=== =======syslogd============ ======로그 모니터링======= /etc/syslog.conf /dev/console A auth.notice -----> auth.notice /dev/console----------------------+ |
# logger -p auth.notice "Messege1"
# logger -p auth.crit "Message2"
5 | syslog 실습 |
(네트워크 설정) VMware > VM > Settings > Network Adapter(NAT -> Bridge)
Hostname:linux200.example.com -> linux2XX.example.com
IP: 192.168.20.200/24 -> 172.16.10.2XX/16
Gateway: 192.168.20.100 -> 172.16.0.1
DNS: 168.126.63.1 -> 168.126.63.1
(6강의장 자리배치도)
200 201 202 203 204 205
206 207 208 209 210 211 212 213
214 215 216 217 218 219 220 221
222 223 224 225 226 227 228 229
(10강의장 자리배치도)
200 201 202 203 204 205 206 207
208 209 210 211 212 213 214
215 216 217 218 219 220
221 222 223 224 225 226 227
(1강의장 자리배치도)
200 201 202 203 204 205 206 207
208 209 210 211 212 213 214 215
216 217 218 219 220 221 222 223
224 225 226 227 228 229 230
# export LANG=C (# export LANG=en_US.UTF-8)
# setup
# vi /etc/hosts
# service network restart
# hostname -> linux2XX.example.com
# ifconfig eth0 -> 172.16.10.2XX/255.255.0.0
# netstat -nr -> 172.16.0.1
# cat /etc/resolv.conf -> 168.126.63.1
[EX1] 인증 관련 기록(기본 체계를 사용하는 경우)
===메세지 생성=== =======syslogd============ ======로그 모니터링======= su CMD /etc/syslog.conf /var/log/secure telnet CMD A ssh CMD -----> authpriv.* /var/log/secure ---------------+ |
■ /etc/syslog.conf 미리 정의
■ 로그파일 미리 정의
■ 메세지 생성
① /etc/syslog.conf 파일 설정 확인
# grep auth /etc/syslog.conf
# The authpriv file has restricted access. authpriv.* /var/log/secure |
② /var/log/secure 로그 파일 모니터링
[TERM1] 모니터링 윈도우
# tail -f /var/log/secure
(su 명령어 수행 로그) Mar 23 03:44:24 linuxXXX su: pam_unix(su-l:session): session opened for user user01 by root(uid=0) Mar 23 03:44:27 linuxXXX su: pam_unix(su-l:session): session closed for user user01
(telnet 명령어 수행 로그) Mar 23 03:46:36 linuxXXX login: pam_unix(remote:session): session opened for user root by LOGIN(uid=0) Mar 23 03:46:40 linuxXXX login: pam_unix(remote:session): session closed for user root
(ssh 명령어 수행 로그) Mar 23 03:48:26 linuxXXX sshd[5832]: Accepted password for root from 127.0.0.1 port 41144 ssh2 Mar 23 03:48:26 linuxXXX sshd[5832]: pam_unix(sshd:session): session opened for user root by (uid=0) Mar 23 03:48:34 linuxXXX sshd[5832]: pam_unix(sshd:session): session closed for user root |
③ su 명령어 수행
[TERM2] 명령어 수행 윈도우
# su - user01
$ exit
④ telnet 명령어 수행
[TERM2] 명령어 수행 윈도우
# telnet localhost
root 사용자로 로그인
# exit
#
⑤ ssh 명령어 수행
[TERM2] 명령어 수행 윈도우
# ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established. RSA key fingerprint is 40:36:17:cf:c8:4c:43:3d:0c:d5:aa:bf:08:0d:5b:fd. Are you sure you want to continue connecting (yes/no)? yes <----- 'y' 입력 Warning: Permanently added 'localhost' (RSA) to the list of known hosts. root@localhost's password: (root 암호 입력) Last login: Tue Mar 23 03:46:36 2010 from linuxXXX |
# exit
#
[EX2] 메일 로그 기록(기본 체계를 사용하는 경우)
===메세지 생성=== =======syslogd============ ======로그 모니터링======= /etc/syslog.conf /var/log/maillog A mail CMD -----> mail.* /var/log/maillog -----------------+ |
■ /etc/syslog.conf 미리 정의
■ 로그파일 미리 정의
■ 메세지 생성
① /etc/syslog.conf 파일 설정 확인
# grep mail /etc/syslog.conf
# Log all the mail messages in one place. mail.* -/var/log/maillog |
② /var/log/maillog 파일 모니터링
[TERM1] 모니터링 윈도우
# tail -f /var/log/maillog
Mar 23 03:56:57 linuxXXX sendmail[6096]: o2MIuv1v006096: from=root, size=39, class=0, nrcpts=1,\ msgid=<201003221856.o2MIuv1v006096@linuxXXX.com>, relay=root@localhost Mar 23 03:56:57 linuxXXX sendmail[6097]: o2MIuvGk006097: from=<root@linuxXXX.com>, size=311, class=0, nrcpts=1,\ msgid=<201003221856.o2MIuv1v006096@linuxXXX.com>, proto=ESMTP, daemon=MTA, relay=linuxXXX.com [127.0.0.1] Mar 23 03:56:57 linuxXXX sendmail[6096]: o2MIuv1v006096: to=user01, ctladdr=root (0/0), delay=00:00:00, \ xdelay=00:00:00, mailer=relay, pri=30039, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent \ (o2MIuvGk006097 Message accepted for delivery) Mar 23 03:56:57 linuxXXX sendmail[6098]: o2MIuvGk006097: to=<user01@linuxXXX.com>, ctladdr=<root@linuxXXX.com> \ (0/0), delay=00:00:00, xdelay=00:00:00, mailer=local, pri=30514, dsn=2.0.0, stat=Sent |
③ mail 명령어 수행
[TERM2] 명령어 수행 윈도우
# mailx user01 (# hostname | mailx -s "TEST1" user01)
Subject: hello user01 test . Cc: <ENTER> |
[EX3] 크론(cron) 로그 기록(기본 체계를 사용하는 경우)
===메세지 생성=== =======syslogd============ ======로그 모니터링======= /etc/syslog.conf /var/log/cron A crontab CMD -----> cron.* /var/log/cron ------------------+ |
■ /etc/syslog.conf 미리 정의
■ 로그파일 미리 정의
■ 메세지 생성
① /etc/syslog.conf 파일 설정 확인
# grep cron /etc/syslog.conf
# Log cron stuff cron.* /var/log/cron |
② /var/log/cron 파일 모니터링
[TERM1] 모니터링 윈도우
# tail -f /var/log/cron (# tail -0f /var/log/cron)
Mar 23 04:07:30 linuxXXX crontab[6971]: (root) BEGIN EDIT (root) <----- crontab -e Mar 23 04:07:43 linuxXXX crontab[6971]: (root) REPLACE (root) Mar 23 04:07:43 linuxXXX crontab[6971]: (root) END EDIT (root) Mar 23 04:07:53 linuxXXX crontab[6986]: (root) LIST (root) <----- crontab -l Mar 23 04:08:01 linuxXXX crond[4318]: (root) RELOAD (cron/root) Mar 23 04:08:01 linuxXXX crond[6988]: (root) CMD (/bin/date >> /tmp/date.log)<--- crontab 정기작업 수행 Mar 23 04:09:01 linuxXXX crond[7022]: (root) CMD (/bin/date >> /tmp/date.log) Mar 23 04:10:01 linuxXXX crond[7054]: (root) CMD (/bin/date >> /tmp/date.log) ..... (중략) ..... Mar 23 04:10:54 linuxXXX crontab[7087]: (root) DELETE (root) <----- crontab -r |
③ crontab 명령어 수행
[TERM2] 명령어 수행 윈도우
# crontab -e
* * * * * /bin/date >> /tmp/date.log |
분 시 일 월 요일 CMD
# crontab -l
* * * * * /bin/date >> /tmp/date.log |
..... 잠시 기다린다. (약 3분 정도) .....
# crontab -r
#
[EX4] 새로운 로그 파일 생성 실습
(작업 계획)
===메세지 생성=== =======syslogd============ ======로그 모니터링======= /etc/syslog.conf tail -f /var/log/file.log A local0.notice--+-->local0.notice /var/log/file.log -----------------+ | local0.crit----+
===메세지 생성=== =======syslogd============ ======로그 모니터링======= /etc/syslog.conf tail -f /var/log/file1.log A local0.notice--+-->local0.=notice /var/log/file1.log ---------------+ | local0.crit----+ |
■ /etc/syslog.conf 사용자가 정의
■ 로그파일 사용자가 생성
■ 메세지 사용자가 생성
① /etc/syslog.conf 파일 편집
# vi /etc/syslog.conf
..... (중략) ..... # # INN # news.=crit /var/log/news/news.crit news.=err /var/log/news/news.err news.notice /var/log/news/news.notice
# # Syslog Test # local0.notice /var/log/file.log |
-> /etc/syslog.conf 파일에서 개인적으로 선언하기 좋은 라인은 문서의 마지막 부분이다.
② 로그 파일 생성
# touch /var/log/file.log
# chmod 600 /var/log/file.log (600: rw------- root root)
# ls -l /var/log/file.log
-rw------- 1 root root 0 Mar 23 04:17 file.log |
③ 서비스 적용
# service syslog restart
Shutting down kernel logger: [ OK ] Shutting down system logger: [ OK ] Starting system logger: [ OK ] Starting kernel logger: [ OK ] |
■ kernel logger -> klogd
■ system logger -> syslogd
④ 로그 파일 모니터링
[TERM1] 모니터링 윈도우
# tail -f /var/log/file.log
Mar 23 04:24:03 linuxXXX root: Notice Messages(linuxXXX) Mar 23 04:25:26 linuxXXX root: Critical Messages(linuxXXX) |
■ 출력된 메시지 로그 해석
Mar 23 04:24:03 메세지 생성 시간
linuxXXX 메세지 생성 서버
root: 메세지 생성 주체
Notice Messages(linuxXXX) 메세지
⑤ 메세지 생성
[TERM2] 명령어 수행 윈도우
# logger -p local0.notice "Notice Messages(linuxXXX)"
# logger -p local0.crit "Critical Messages(linuxXXX)"
⑥ /etc/syslog.conf 파일 편집
# vi /etc/syslog.conf
..... (중략) ..... # # INN # news.=crit /var/log/news/news.crit news.=err /var/log/news/news.err news.notice /var/log/news/news.notice
# # Syslog Test # local0.notice /var/log/file.log local1.=notice /var/log/file1.log |
⑦ 로그 파일 생성
# touch /var/log/file1.log
# chmod 600 /var/log/file1.log
⑧ 서비스 적용
# service syslog restart
Shutting down kernel logger: [ OK ] Shutting down system logger: [ OK ] Starting system logger: [ OK ] Starting kernel logger: [ OK ] |
⑨ 로그 파일 모니터링
[TERM1] 모니터링 윈도우
# tail -f /var/log/file1.log
Mar 23 04:39:03 linuxXXX root: Notice Messages(linuxXXX) |
⑩ 메세지 생성
[TERM2] 명령어 수행 윈도우
# logger -p local1.notice "Notices Messages(linuxXXX)"
# logger -p local1.crit "Critical Messages(linuxXXX)"
-> 메세지가 남겨지지는 않는다.
-> /var/log/messages 파일안에 내용을 확인
[참고] 추가적인 실습(백업 로그를 생성하는 방법) |
■ 백업 로그를 생성하는 방법
백업 스크립트의 로그 파일 생성 방법 (ㄱ) 리다이렉션 기호를 사용하는 경우 # /root/bin/backup.sh > /var/log/backup.log 2>&1
(ㄴ) 백업 스크립안에 구문을 사용하는 경우 # vi /root/bin/backup.sh -------------------------------------- #!/bin/bash exec 1> /var/log/backup.log 2>&1 .......
--------------------------------------
① 실습 준비
/home/* --- backup.sh ---> /backup/backup_0830.tar.gz
■ /home : 백업 받을 디렉토리 ■ /backup : 백업 파일이 생성되는 디렉토리 ■ /root/bin/backup.sh : 백업 스크립트
# mkdir -p /backup # mkdir -p /root/bin # vi /root/bin/backup.sh ---------------------------------------------- #!/bin/bash
cd /home tar cvzf /backup/backup_`date +%m%d`.tar.gz . ----------------------------------------------
# chmod 700 /root/bin/backup.sh
② 백업 스크립트 동작 확인 # /root/bin/backup.sh -> 출력 내용 생략
# ls -l /backup -> backup_0830.tar.gz (# tar tvzf /backup/backup_0830.tar.gz)
③ 백업스크립트 실행시 백업 로그 생성 # /root/bin/backup.sh > /var/log/backup.log 2>&1 # cat /var/log/backup.log -> 출력 내용 생략
④ 백업 스크립트에 기능 추가 및 확인 백업 스크립트에 추가되는 기능(백업 시작 시간과 백업 종료 시간 기록)
# vi /root/bin/backup2.sh --------------------------------------------------- #!/bin/bash
echo >> /var/log/backup.log echo >> /var/log/backup.log
echo "===== Backup START : `date` ===========" >> /var/log/backup.log
/root/bin/backup.sh >> /var/log/backup.log 2>&1
echo "===== Backup STOP : `date` ===========" >> /var/log/backup.log
---------------------------------------------------
# chmod 700 /root/bin/backup2.sh
# > /var/log/backup.log # /root/bin/backup2.sh #
# more /var/log/backup.log -> 출력 내용 생략
# /root/bin/backup2.sh # more /var/log/backup.log -> 출력 내용 생략
# vi /root/bin/backup.sh ---------------------------------------------- #!/bin/bash
cd /home zztar cvzf /backup/backup.`date +%m%d`.tar.gz .
----------------------------------------------
# /root/bin/backup2.sh # cat /var/log/backup.log -> 출력 내용 생략
⑤ 백업 스크립트에 기능 추가 및 확인 백업 스크립트에 추가되는 기능(백업 성공 유/무 기록)
# vi /root/bin/backup2.sh --------------------------------------------------- #!/bin/bash
echo >> /var/log/backup.log echo >> /var/log/backup.log
echo "===== Backup START : `date` ===========" >> /var/log/backup.log
/root/bin/backup.sh >/dev/null 2>&1 if [ $? -eq 0 ] ; then echo "Backup : [ O K ]" >> /var/log/backup.log else echo "Backup : [ FAIL ]" >> /var/log/backup.log fi
echo "===== Backup STOP : `date` ===========" >> /var/log/backup.log
---------------------------------------------------
# /root/bin/backup2.sh # cat /var/log/backup.log -> 출력 내용 생략
⑥ 백업 스크립트에 기능 추가 및 확인 백업 스크립트에 추가되는 기능(logger CMD + syslogd 연동)
# vi /root/bin/backup2.sh --------------------------------------------------- #!/bin/bash
echo >> /var/log/backup.log
echo "===== Backup START : `date` ===========" >> /var/log/backup.log
/root/bin/backup.sh >/dev/null 2>&1 if [ $? -eq 0 ] ; then logger -p local0.notice "Backup [ O K ]" else logger -p local0.warn "Backup [ FAIL ]" fi
echo "===== Backup STOP : `date` ===========" >> /var/log/backup.log
---------------------------------------------------
# /root/bin/backup2.sh # cat /var/log/file.log -> 출력 내용 생략
■ 완성된 백업 스크립트
# vi /root/bin/backup2.sh --------------------------------------------------- #!/bin/bash
LOG_FILE=/var/log/backup.log echo >> $LOG_FILE
echo "===== Backup START : `date` ===========" >> $LOG_FILE
/root/bin/backup.sh >>/dev/null 2>&1 if [ $? -eq 0 ] ; then echo "Backup [ O K ]" >> $LOG_FILE else echo "Backup [ FAIL ]" >> $LOG_FILE fi
echo "===== Backup STOP : `date` ===========" >> $LOG_FILE
---------------------------------------------------
■ crontab 등록한 예 # crontab -e * * * * * /root/bin/backup2.sh /* 실습용 구문 */ 0 1 * * * /root/bin/backup2.sh /* 실무용 구문 */
(복원) # crontab -r |
(정리) 로그 생성
(ㄱ) 기본 로그 체계를 사용하는 경우
- /var/log/secure
- /var/log/maillog
- /var/log/cron
(ㄴ) 새로운 로그를 생성하는 경우
- syslogd 체계를 사용하는 경우
- 스크립트 내에서 직접 생성하는 경우
(ㄷ) 원격 로그 서버 구축
[EX5] 로그 서버 구축
여러대의 서버를 관리하는 경우(예: 100대 이상) 각 서버의 로그 기록을 각각 확인하고 관리하는 방법은 많은 시간을 소비하는 작업이 된다. 따라서, 여러대의 서버에 쌓이는 로그 기록을 한대의 로그 서버로 전송할수 있다.
HOSTA 서버에서는 자신의 서버에서 생성되는 local0.notice 이상의 메세지들을 HOSTB 서버로 넘기는 설정을 하고 HOSTB 서버에서는 이 넘겨진 메세지를 /var/log/file.log 파일에 남도록 설정한다.
다음은 로그 서버 HOSTB와 로그 기록을 전송해 주는 HOSTA 서버의 예이다.
(172.16.9.XXX) (172.16.9.252) ======== HOSTA ========= ========= HOSTB ========== Messg. -> syslogd -> @HOSTB -------------------> syslogd /etc/syslog.conf /etc/syslog.conf local0.notice @HOSTB local0.notice /var/log/file.log
[그림] 로그 서버 1 |
(linuxXXX) 172.16.9.XXX
① /etc/syslog.conf 파일 편집
# vi /etc/syslog.conf
[수정전] # # Syslog Test # local0.notice local1.=notice /var/log/file1.log [수정후] # # Syslog Test # local0.notice @172.16.9.252 local1.=notice /var/log/file1.log |
② /etc/sysconfig/syslog 파일 편집
/etc/sysconfig/syslog 파일은 Log Server 쪽에서 설정하는 파일이다.
# vi /etc/sysconfig/syslog
# Options to syslogd # -m 0 disables 'MARK' messages. # -r enables logging from remote machines # -x disables DNS lookups on messages recieved with -r # See syslogd(8) for more details SYSLOGD_OPTIONS="-r -m 0" <----- '-r' 옵션 추가 # Options to klogd # -2 prints all kernel oops messages twice; once for klogd to decode, and # once for processing with 'ksymoops' # -x disables all klogd processing of oops messages entirely # See klogd(8) for more details KLOGD_OPTIONS="-x" # SYSLOG_UMASK=077 # set this to a umask value to use for all log files as in umask(1). # By default, all permissions are removed for "group" and "other". |
-> 리눅스 시스템은 기본적으로 시스템 로그 메세지를 외부로 보내질 못한다.
-> 따라서, 설정이 별도로 필요하다.
-> /etc/sysconfig/syslog 설정은 로그서버(EX:172.16.9.252) 서버에서 수행이 되어 있어야 한다.
-> 실습시에서는 모든 서버에서 수행한다.
[참고] syslogd 옵션에 대해서 |
# man syslogd -m interval The syslogd logs a mark timestamp regularly. The default interval between two -- MARK -- lines is 20 minutes. This can be changed with this option. Setting the interval to zero turns it off entirely.
-r This option will enable the facility to receive message from the network using an internet domain socket with the syslog service (see services(5)). The default is to not receive any messages from the network.
This option is introduced in version 1.3 of the sysklogd package. Please note that the default behavior is the oppo- site of how older versions behave, so you might have to turn this on. |
③ 서비스 적용 및 확인
# service syslog restart
Shutting down kernel logger: [ OK ] Shutting down system logger: [ OK ] Starting system logger: [ OK ] Starting kernel logger: [ OK ] |
# pgrep -lf syslogd
9643 syslogd -r -m 0 |
# logger -p local0.notice "Notice Messages(linuxXXX)"
(linux252) 172.16.9.252
# tail -f /var/log/file.log
Mar 23 05:18:59 172.16.9.140 root: Notice Messages(linux140) Mar 23 05:18:59 172.16.9.141 root: Notice Messages(linux141) ..... (중략) ..... |
[EX6] 로그 서버 구축 2
여러대의 서버(예: 50대)가 존재하고 이 서버들의 로그 기록을 로그 서버(예: HOSTB)로 넘길려고 할때 모든 로그 기록을 넘기는 것을 적당하지 않다. HOSTB 서버를 쓰고 있는 것은 여러대의 서버의 이상 징후를 발견하기 위한 설정이므로 warning 이상의 레벨의 기록들만 넘기는 것이 적당할 수 있다.(반드시는 아니다.) 다시 말해서, 여러대의 서버에 존재하는 모든 기록을 로그 서버로 넘기면 로그 서버에 남겨지는 로그 기록은 엄청나게 많을 것이다. 이것을 방지하기 위해서 에러가 발생하는 기록들에 대해서만 로그 서버에 남기도록 설정하면 좋다.
(172.16.9.XXX) (172.16.9.252) ======== HOSTA ========= ========= HOSTB ========== Messg. -> syslogd -> @HOSTB -------------------> syslogd /etc/syslog.conf /etc/syslog.conf local0.notice /var/log/file.log local0.crit /var/log/file.log local0.crit @HOSTB
[그림] 로그 서버 2 |
(linuxXXX) 172.16.9.XXX
① /etc/syslog.conf 파일 편집
# vi /etc/syslog.conf
[수정전] # # Syslog Test # local0.notice @172.16.9.252 local1.=notice /var/log/file1.log [수정후] # # Syslog Test # local0.notice /var/log/file.log local0.crit @172.16.9.252 local1.=notice /var/log/file1.log |
② 서비스 적용
# service syslog restart
③ /var/log/file.log 파일 모니터링
[TERM1] 모니터링 윈도우
# tail -f /var/log/file.log
-> 자신의 서버쪽에는 두가지 기록이 모두 남겨진다.
④ 메세지 생성
[TERM2] 명령어 수행 윈도우
# logger -p local0.notice "Notice Messages(linuxXXX)"
# logger -p local0.crit "Critical Messages(linuxXXX)"
(linux252) 172.16.9.252
# tail -f /var/log/file.log
Mar 23 05:49:01 linuxXXX root: Critical messages(linuxXXX) Mar 23 05:50:02 linuxXXX root: Critical messages(linuxXXX) |
[참고] 로그 서버에 모든 기록 남기기 |
■ (모든 기록을 로그 서버로 넘기는 경우) # mv /etc/syslog.conf /etc/syslog.conf.orig # vi /etc/syslog.conf *.* @172.16.9.252 # service syslog restart
■ (모든 기록을 로그 서버로 넘기는 경우) # cp /etc/syslog.conf /etc/syslog.conf.orig # vi /etc/syslog.conf *.info;mail.none;news.none;authpriv.none;cron.none @172.16.9.252 authpriv.* @172.16.9.252 mail.* @172.16.9.252 cron.* @172.16.9.252 # service syslog restart
■ (일부 기록만 로그 서버로 넘기는 경우) # cp /etc/syslog.conf /etc/syslog.conf.orig # vi /etc/syslog.conf ...... (중략) ..... *.warning @172.16.9.252 # service syslog restart |
[참고] 로그 서버에 모든 기록 남기기 |
■ (한대의 서버를 실시간적으로 모니터링 하는 방법) # tail -f /var/log/messages
■ (여러대의 서버를 실시간적으로 모니터링 하는 방법) # tail -f /var/log/server.log |
[참고] syslogd 데몬의 포트 번호 확인 |
# cat /etc/services | grep syslog syslog 514/udp syslog-conn 601/tcp # Reliable Syslog Service syslog-conn 601/udp # Reliable Syslog Service
# netstat -anu | grep :514 udp 0 0 0.0.0.0:514 0.0.0.0:* |
'모의해킹 침해대응 전문가 과정' 카테고리의 다른 글
20160728 방화벽 (0) | 2016.07.28 |
---|---|
20160728 로그보안 (0) | 2016.07.28 |
20160727 리눅스 서버보안 (0) | 2016.07.27 |
20160726 리눅스 서버보안 (0) | 2016.07.26 |
20160725 리눅스 서버보안 (0) | 2016.07.25 |