[OS]/Embedded&Linux

syslog daemon programming

하늘을닮은호수M 2006. 6. 11. 00:30
반응형
http://211.183.23.105/wiki/wiki.php/SysLog
1 syslog.h 파일 분석
syslog.h 파일에는 아래와 같은 내용이 있다. priorities 와 facilities 는 32 비트로 인코드 되어있다. 하위 3비트는 priority 를 나타내고, 상위 28 비트는 facility 를 나타내는것이다. 아래의 코드에서 facilities 들은 <<<3 을 하는 이유가 그것이다.

/*
* priorities/facilities are encoded into a single 32-bit quantity, where the
* bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
* (0-big number). Both the priorities and the facilities map roughly
* one-to-one to strings in the syslogd(8) source code. This mapping is
* included in this file.
*
* priorities (these are ordered)
*/
#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions */
#define LOG_ERR 3 /* error conditions */
#define LOG_WARNING 4 /* warning conditions */
#define LOG_NOTICE 5 /* normal but significant condition */
#define LOG_INFO 6 /* informational */
#define LOG_DEBUG 7 /* debug-level messages */

/* facility codes */
#define LOG_KERN (0<<3) /* kernel messages */
#define LOG_USER (1<<3) /* random user-level messages */
#define LOG_MAIL (2<<3) /* mail system */
#define LOG_DAEMON (3<<3) /* system daemons */
#define LOG_AUTH (4<<3) /* security/authorization messages */
#define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */
#define LOG_LPR (6<<3) /* line printer subsystem */
#define LOG_NEWS (7<<3) /* network news subsystem */
#define LOG_UUCP (8<<3) /* UUCP subsystem */
#define LOG_CRON (9<<3) /* clock daemon */
#define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */
#define LOG_FTP (11<<3) /* ftp daemon */

/* other codes through 15 reserved for system use */
#define LOG_LOCAL0 (16<<3) /* reserved for local use */
#define LOG_LOCAL1 (17<<3) /* reserved for local use */
#define LOG_LOCAL2 (18<<3) /* reserved for local use */
#define LOG_LOCAL3 (19<<3) /* reserved for local use */
#define LOG_LOCAL4 (20<<3) /* reserved for local use */
#define LOG_LOCAL5 (21<<3) /* reserved for local use */
#define LOG_LOCAL6 (22<<3) /* reserved for local use */
#define LOG_LOCAL7 (23<<3) /* reserved for local use */


2 syslog.conf(5) 설정하기
RTFM ! man page 만한 내용도 없다. man syslog.conf /etc/syslog.conf 파일은 syslogd, 즉 syslog 데몬의 설정 파일이다. syslog 데몬은 /etc/syslog.conf 파일을 읽어서 로그파일을 저장할 설정 정보를 읽게 된다. :-(
모든 syslog 의 설정 룰은 selector 필드와 action 필드로 구성되며 이 필드는 하나이상의 탭 또는 스페이스로 구분된다. selector 필드는 다시 두개의 필드로 나뉘는데 하나는 facility 필드고, 하나는 priority 필드이다. 이 두개의 필드는 '.' 으로 구분되며 두 필드는 모두 대소문자를 구분하지 않으며 숫자로 표현해도 된다. facility 와 priority 는 syslog(3) 메뉴얼 페이지에 설명되어있다.


facility 의 종류
auth
authpriv
cron
daemon
kern
lpr
mail
mark
news
security (same as auth)
syslog
user
uucp
local0 - local7
priority 의 종류
debug
info
notice
warning
warn (same as warning)
err
error (same as err)
crit
alert
emerg
panic (same as emerg)
error, warn, panic 은 더이상 사용되지 않는다.
syslog.conf 의 예제는 man 페이지를 참고하라.. 잘 나와있다.

2.1 원격지에 로그를 남기기 위한 설정
2.1.1 syslog 서버 설정
원격지에 로그를 남기려면 일단 syslogd(syslog 데몬)이 Remote 로부터 로그를 받도록 설정해야 한다.
syslog 데몬에 관한 자세한 내용은 sysklogd(8) 를 참고. 리모트 시스로그를 받으려면 syslogd 를 -r 옵션을 주고 실행하면 된다. 시작 시크립트가 들어있는 폴더(redhat : /etc/rc.d/init.d/, Debian: /etc/init.d) 에 보면 syslogd 를 실행하는 스크립트가 있는데 디폴트로 리모트 로그를 받지 않도록 설정되어있을 것이다.

#! /bin/sh
# /etc/init.d/sysklogd: start the system log daemon.

PATH=/bin:/usr/bin:/sbin:/usr/sbin

pidfile=/var/run/syslogd.pid
binpath=/sbin/syslogd

test -x $binpath || exit 0

# Options for start/restart the daemons
# For remote UDP logging use SYSLOGD="-r"
#
#SYSLOGD=""
SYSLOGD="-r" // 요렇게 수정!!

create_xconsole()
{
if [ ! -e /dev/xconsole ]; then
mknod -m 640 /dev/xconsole p
else
chmod 0640 /dev/xconsole
fi
chown root.adm /dev/xconsole
}


syslog 서버는 전송되는 모든 syslog 를 받아들이게 되는데 이때 받아들인 syslog 는 로컬의 syslog.conf 를 통해서 저장이 된다. 테스트를 위해 아래와 같은 내용을 syslog.conf 에 추가한다.

auth,authpriv.* /var/log/auth-mine.log

수정이 되었으면 syslog 를 재시작한다.


2.1.2 client 측의 syslog.conf 설정
이제 syslog.conf 를 수정해서 원하는 특정 로그만 리모트의 syslog 서버로 전송하도록 수정한다. syslog.conf 에 아래와 같은 내용을 추가한다.

auth.warn @192.168.200.133

이것은 auth.warn syslog 가 발생하면 192.168.200.133 호스트의 syslog 데몬으로 syslog 를 전송하게 한다. 디폴트로 syslog 데몬은 UDP 514 포트를 사용한다. syslog 를 구동하면 server 의 /var/log/auth-mine 파일을 보면 client 에서 보낸 syslog 가 쌓인것을 확인할 수 있다.
2.1.3 remote syslog inside..
양쪽(server/client)모두 설정을 마치고, syslog 를 재시작하면 설정대로 원격지 서버로 syslog 가 전송된다.
그럼 client 는 syslog 를 어떤 형태로 전송할까? udp 데몬을 하나 만들어서 테스트 하거나 스니핑을 해보면 아래의 형태로 데이터가 전송되는 것을 확인 할 수 있었다.
#vim c
<36>sniper: [SNIPER] [Attack_Name=(0004)XMAS Port Scan], [Time=2000/09/10 20:06:40],
[Hacker=133.200.168.192], [Victim=71.200.168.192], [Protocol=tcp/23821], [Risk=Medium], [Handling=Defence], [Information=3COM : 00:50:DA:CF:C0:39]

36 은 priorities 와 facilities 의 32비트값이다. 가장먼저 syslog.h 파일을 본것이 큰 도움이 되었다. 36 에서 하위 3 비트는 LOG_WARNING 이고, 상위 28 비트는 LOG_AUTH 를 나타내는 것이다. 따라서 캡쳐된 syslog 메세지는
auth.warn @192.168.200.133

설정때문에 발생한 로그라는 것을 확인 할 수 있었다.


3 기타 syslog 관련 자료
joinC syslog 사용하기 -1

joinC syslog 사용하기 -2
반응형

'[OS] > Embedded&Linux' 카테고리의 다른 글

C프로그래머를 위한 VIM 사용법  (0) 2006.12.07
Windows Services for UNIX  (0) 2006.07.21
리눅스 시스템 모니터링과 문제 찾기  (0) 2006.05.12
vi block 지정 tip  (0) 2006.04.22
vi manual  (0) 2006.04.22