20.04.2014 Views

Log Server Centralizzato - Infn

Log Server Centralizzato - Infn

Log Server Centralizzato - Infn

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong><br />

CCR2003 - Paestum - 11 Giugno 2003<br />

Sandro.Angius@lnf.infn.it - Claudio.Soprano@lnf.infn.it


<strong>Server</strong>, Farm, Apparati di Rete:<br />

tutti generano log sul loro funzionamento!<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 2


Come vengono prodotti i log?<br />

• I sistemi unix usano il demone standard syslogd<br />

(rfc3164) per definire cosa e dove inviare i messaggi<br />

generati dai servizi e dalle applicazioni<br />

• La maggior parte degli apparati di rete e’ capace di<br />

inviare i propri messaggi di diagnostica tramite il<br />

protocollo syslog<br />

• Non tutte le applicazioni pero’ supportano l’invio dei<br />

messaggi via syslog<br />

• Generalmente i log risiedono sul sistema che li genera!<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 3


<strong>Log</strong> NON centralizzati significa:<br />

• Definire su ogni sistema le politiche per la gestione dei log<br />

– Cosa, Come, Dove e per quanto tempo mantenere i log<br />

• Dover accedere sul sistema interessato per analizzare i log<br />

– Nel caso di servizi ridondati (es: http, proxy, smtp, etc.) occorre verificare i log su tutti i<br />

server che offrono lo stesso servizio<br />

• Maggiore difficolta’ nell’analisi, report e archiviazione<br />

• Possibili “DOS” che puntano al blocco di un servizio per esaurimento<br />

dello spazio disco (se i log sono sul disco sistema…)<br />

• I log sono modificabili da eventuali intrusi…<br />

• Ogni cambio di politica comporta dover riconfigurare tutti i sistemi!<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 4


Come centralizzare ?<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 5


Un primo passo: syslog su sistema dedicato<br />

514/UDP<br />

Syslogd<br />

syslog.conf<br />

# syslog.conf dei client<br />

*.* @syslogsrv.lnf.infn.it<br />

# Estratto da un possibile syslog.conf<br />

# per un syslogd server centrale<br />

authpriv.*<br />

mail.*<br />

cron.*<br />

uucp,news.crit<br />

local7.*<br />

local6.*<br />

local5.*<br />

/var/log/secure<br />

/var/log/maillog<br />

/var/log/cron<br />

/var/log/spooler<br />

/var/log/boot.log<br />

/var/log/myapps.log<br />

/var/log/others.log<br />

.. omissis ..<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 6


Syslog: Formato Messaggi (RFC3164)<br />

Syslog Packet (massimo 1024 bytes)<br />

Priority<br />

(3..5 bytes)<br />

Header<br />

Message<br />

Timestamp Hostname Tag Content<br />

• Priority = 8 * Facility + Severity<br />

• Header<br />

– Timestamp: Data e Ora del messaggio<br />

– Hostname: Nome dell’host origine del messaggio<br />

• Message<br />

– Tag: Programma origine del messaggio<br />

– Content: Messaggio informativo<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 7


etc/syslog.conf standard<br />

# Configuration file example for syslogd<br />

# Store critical stuff in critical<br />

*.=crit;kern.none<br />

/var/adm/critical<br />

# Kernel messages are first, stored in the kernel<br />

file,<br />

# critical messages and higher ones also go to another<br />

# host and to the console<br />

#<br />

kern.*<br />

/var/adm/kernel<br />

kern.crit<br />

@finlandia<br />

kern.crit<br />

/dev/console<br />

kern.info;kern.!err<br />

/var/adm/krninfo<br />

# The tcp wrapper loggs with mail.info, we display<br />

# all the connections on tty12<br />

#<br />

mail.=info /dev/tty12<br />

# Store all mail concerning stuff in a file<br />

#<br />

mail.*;mail.!=info<br />

/var/adm/mail<br />

# <strong>Log</strong> all mail.info and news.info messages to info<br />

#<br />

mail,news.=info/var/adm/info<br />

# <strong>Log</strong> info and notice messages to messages file<br />

#<br />

*.=info;*.=notice;\<br />

mail.none /var/log/messages<br />

# <strong>Log</strong> info messages to messages file<br />

#<br />

*.=info;\<br />

mail,news.none /var/log/messages<br />

# Emergency messages will be displayed using wall<br />

#<br />

*.=emerg *<br />

# Messages of the priority alert will be directed<br />

# to the operator<br />

#<br />

*.alert root,joey<br />

*.* @finlandia<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 8


Syslog standard: le limitazioni<br />

• Un solo “net source” (udp/514) oltre al /dev/log<br />

• Alcune implementazioni prevedono l’uso di socket aggiuntive<br />

• “Destinazioni” limitate<br />

• “solo” 20 “facilities” di cui solo 8 non riservate<br />

– kern, user, mail, daemon, auth, authpriv, syslog, lpr, news, uucp, cron, ftp<br />

– General Purpose: local0 … local7<br />

• 8 “severities”<br />

– emerg, alert, crit, err, warning, notice, info, debug<br />

• Alcune implementazioni prevedono l’invio ad una pipe/fifo<br />

• Nessuna possibilita’ di filtrare/catalogare i log in base al<br />

contenuto<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 9


Un possibile passo successivo:<br />

uso di un parser per i log<br />

514/UDP<br />

Syslogd<br />

syslogd.conf<br />

<strong>Log</strong><br />

Parser<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 10


Oppure: Syslog Next Generation<br />

514/UDP<br />

TCP/UDP Ports<br />

Pipe / FIFO<br />

Syslogd<br />

Next Generation<br />

Internal<br />

syslog-ng.conf<br />

TCP<br />

UDP<br />

Pipe<br />

FIFO<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 11


Syslog NG<br />

• Compatibile con il protocollo di syslogd<br />

• “Sources” :<br />

– udp/514, fifo/pipe, file, tcp/udp, internal,<br />

unix-stream/dgram, sun-stream<br />

• Possibili output:<br />

– file, fifo/pipe, tcp/udp, program, usertty, unix-stream/dgram<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 12


Syslog NG: Filtri<br />

Priority<br />

(3..5 bytes)<br />

Syslog Packet (massimo 1024 bytes)<br />

Header<br />

Message<br />

Timestamp Hostname Tag Content<br />

Per facility(faciliy[,facility]) e level(pri[,pri1..pri2[,pri3]])<br />

Per nodo: host(regexp)<br />

Per applicativo: program(regexp)<br />

Per contenuto: match(regexp)<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 13


Syslog NG: Message Paths<br />

Opzioni Generali<br />

Opzioni “source” Opzioni “filter” Opz. “destination”<br />

Source<br />

Source<br />

Source<br />

Source<br />

F<br />

i<br />

l<br />

t<br />

e<br />

r<br />

F<br />

i<br />

l<br />

t<br />

e<br />

r<br />

F<br />

i<br />

l<br />

t<br />

e<br />

r<br />

F<br />

i<br />

l<br />

t<br />

e<br />

r<br />

F<br />

i<br />

l<br />

t<br />

e<br />

r<br />

Destination<br />

Destination<br />

Destination<br />

Destination<br />

Destination<br />

Destination<br />

Destination<br />

M e s s a g e P a t h s<br />

• In Syslog NG si definiscono, tramite le statement log, dei “Message paths”, costituiti da:<br />

– uno o piu’ “sorgenti” (statement source)<br />

– una o piu’ regole di “filtro” (statement filter)<br />

– una o piu’ “destinazioni” (statement destination)<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 14


Syslog NG: Sintassi<br />

• 5 gli statement utilizzati per la configurazione: source, filter, destination, log, options<br />

• source { source-driver(params); source-driver(params); ... };<br />

• filter { expression; };<br />

• destination { destination-driver(params); destination-driver(params); ... };<br />

• log { source(s1); source(s2); ... filter(f1); filter(f2); …<br />

destination(d1); destination(d2); ... flags(flag1[, flag2...]); };<br />

• options { option1(params); option2(params); ... };<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 15


Syslog NG: Macro e Opzioni<br />

• L’output su file permette di usare una serie di<br />

macro che facilitano la catalogazione<br />

• Macro (output su file):<br />

– DATE, DAY, FACILITY, FULLDATE, FULLHOST, HOST, HOUR ,ISODATE, MIN,<br />

MONTH, MSG or MESSAGE, PRIORITY or LEVEL, PROGRAM, SEC, TAG, TZ,<br />

TZOFFSET, WEEKDAY, YEAR<br />

• Opzioni (output su file):<br />

– create_dirs, dir_perm, global, fsync, group, log_fifo_size, owner, perm, remove_if_older<br />

num, sync_freq, template, template_escape<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 16


Syslog NG: un mini esempio (1/4)<br />

options {<br />

create_dirs (no); dir_perm(0755); dns_cache(yes);<br />

use_dns(yes);<br />

log_fifo_size(2048); log_msg_size(8192);<br />

long_hostnames(on); use_fqdn(yes); perm(0644);<br />

sync(0); time_reopen (10);<br />

};<br />

source src {<br />

unix-stream("/dev/log");<br />

internal();<br />

pipe("/proc/kmsg");<br />

};<br />

source net { udp(); };<br />

source special {tcp(ip(127.0.0.1) port(4800) keep-alive(yes)); };<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 17


Syslog NG: un mini esempio (2/4)<br />

destination console { usertty("root"); };<br />

destination smtprelay { file("/var/log/mailing/smtprelay.log"); };<br />

destination smtpclient { file("/var/log/mailing/smtpclient.log"); };<br />

destination imap { file("/var/log/mailing/imap.log"); };<br />

destination l_messages { file("/var/log/logsrv/messages.log"); };<br />

destination example {<br />

file(“/var/log/example/$HOST/$YEAR/$MONTH/$DAY/$FACILITY.log<br />

owner(root) group(root) perm(0600) create_dirs(yes) dir_perm(0700));<br />

};<br />

destination sms { program("/usr/custom/bin/sendsms +393xxxzzzyyyy“); };<br />

destination swatch { program("/usr/bin/swatch --read-pipe=\"cat /dev/fd/0\"");};<br />

destination other { udp(“192.169.12.234” port(514)); };<br />

destination network { file("/var/log/net/$HOST/gen.log" create_dirs(yes)); };<br />

destination _notmatched_ { file("/var/log/notmatched.log"); };<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 18


Syslog NG: un mini esempio (3/4)<br />

filter f_mail { facility(mail); };<br />

filter f_smtprelay { host("smtp1") or host(“smtp2"); };<br />

filter f_smtpclient { host(“imap1") or host(“wmail") or host(“serv3"); };<br />

filter f_sendmail { program("sendmail"); };<br />

filter f_imap { program("imapd"); };<br />

filter f_network<br />

};<br />

{ host("swcalc1") or host("swcalc2") or<br />

host("swcalcfarm1") or host("swaeta") or<br />

host("swaetb"); or … etc etc …<br />

filter f_diskmsg { match(“disk fault on”); };<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 19


Syslog NG: un mini esempio (4/4)<br />

log { source(src); filter(f_emergency); destination(console); };<br />

log { source(src);<br />

destination(l_messages);flags(final);};<br />

log { source(net); filter(f_network); destination(network); flags(final);};<br />

log { source(net); filter(f_diskmsg); destination(sms);};<br />

log { source(net); filter(f_smtpclient); filter(f_imap);<br />

destination(imap);<br />

flags(final);};<br />

log { source(net); filter(f_smtpclient); filter(f_sendmail);<br />

destination(smtpclient);flags(final);};<br />

log { source(net); filter(f_smtprelay); filter(f_sendmail);<br />

destination(smtprelay); flags(final);};<br />

log { source(net); destination(example); flags(final);};<br />

log { source(net); destination(_notmatched_); };<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 20


Come inviare i log?<br />

• Non tutte le applicazioni hanno la possibilita’ di ridirigere<br />

i loro log verso un server tipo syslog!<br />

– Ma tutti scrivono dei file o possono indirizzare delle pipe/fifo…<br />

• Si puo’ usare il comando “logger” per inviare i messaggi<br />

verso syslog:<br />

– tail -f /var/log/squidaccess.log --lines=0 --follow=name |<br />

logger -p local6.info -t squid<br />

– Oppure “|/usr/bin/logger -p local6.info -t myappl –i”<br />

se l’applicazione puo’ scrivere direttamente su pipe/fifo<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 21


Generare log da applicazioni “C”<br />

#include <br />

char *ident = "MyApplication";<br />

int syslogopt = LOG_PID;<br />

int facility = LOG_USER;<br />

openlog(ident, syslogopt, facility);<br />

char *sysmsg = "Info via syslog";<br />

int priority = LOG_ERR | LOG_USER;<br />

syslog(priority, sysmsg);<br />

closelog();<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 22


Generare log da applicazioni “Perl”<br />

use Sys::Syslog;<br />

$ident = “MyApplication”;<br />

openlog($ident, ’pid’, ’user’);<br />

syslog(’mail|warning’, ’this is a test: %d’, time);<br />

closelog();<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 23


Dai log ai DB via SQL<br />

• Syslog NG permette di scrivere i logs su <strong>Server</strong> SQL<br />

usando delle “destination” di tipo pipe/fifo e dei<br />

“templates” (versione 1.5 e successive)<br />

• <strong>Server</strong> SQL gia’ implementati tramite “template” creati da<br />

utenti:<br />

– Mysql<br />

– Oracle<br />

– PostgreSQL<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 24


Da Syslog NG a MySql <strong>Server</strong>(1/2)<br />

Da una tabella:<br />

CREATE DATABASE syslog;<br />

USE syslog;<br />

CREATE TABLE logs (<br />

host varchar(32) default NULL, facility varchar(10) default NULL,<br />

priority varchar(10) default NULL, level varchar(10) default NULL,<br />

tag varchar(10) default NULL, date date default NULL,<br />

time time default NULL,<br />

program varchar(15) default NULL,<br />

msg text, seq int(10) unsigned NOT NULL auto_increment,<br />

PRIMARY KEY (seq), KEY host (host), KEY seq (seq), KEY program (program),<br />

KEY time (time), KEY date (date), KEY priority (priority),<br />

KEY facility (facility)<br />

) TYPE=MyISAM;<br />

E per tramite di una pipe:<br />

mkfifo /tmp/mysql.pipe<br />

mysql -u theuserid --password=thepassword syslogdb < /tmp/mysql.pipe<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 25


Da Syslog NG a MySql <strong>Server</strong>(2/2)<br />

Puo’ essere usata la seguente destination pipe:<br />

destination d_mysql {<br />

pipe("/tmp/mysql.pipe"<br />

template("INSERT INTO logs (host, facility, priority,<br />

level, tag, date, time, program, msg) VALUES( '$HOST',<br />

'$FACILITY', '$PRIORITY', '$LEVEL', '$TAG',<br />

'$YEAR-$MONTH-$DAY', '$HOUR:$MIN:$SEC',<br />

'$PROGRAM', '$MSG' );\n")<br />

};<br />

);<br />

template-escape(yes)<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 26


Da Syslog NG a Oracle <strong>Server</strong><br />

destination d_oracle {<br />

pipe("/dev/ora.pipe"<br />

template("INSERT INTO logs (LL_HOST, LL_facility,<br />

LL_priority, LL_level, LL_tag, LL_DATE, LL_program, LL_msg)<br />

VALUES ( '$HOST', '$FACILILITY', '$PRIORITY', '$LEVEL',<br />

'$TAG', to_date('$YEAR.$MONTH.$DAY $HOUR:$MIN:$SEC',<br />

'yyyy.mm.dd hh24:mi:ss'), '$PROGRAM', substr('$MSG',1,511));<br />

\n COMMIT;\n")<br />

};<br />

);<br />

template-escape(yes)<br />

Per lo script di comunicazione con Oracle: http://vermeer.org/syslog/scripts/<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 27


Da Syslog NG a PostgreSQL<br />

destination d_postgres {<br />

file("/spool/syslog2pgsql/log.$YEAR.$MONTH.$DAY.$HOUR.$MIN.$SEC"<br />

template("INSERT INTO msg_table VALUES \( '$R_ISODATE',<br />

'$S_ISODATE', '$HOST', '$FACILITY', '$PRIORITY',<br />

'$MSG'\)\;\n")<br />

template_escape(yes)<br />

};<br />

);<br />

owner(postgres)<br />

Per lo script di comunicazione con PostgreSQL: http://www.umialumni.com/~ben/SYSLOG-DOC.html<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 28


Considerazioni<br />

• Il log server centrale DEVE essere molto sicuro!<br />

• Attenzione all’uso della macro $PROGRAM<br />

– Puo’ essere usata per produrre un “DOS” !<br />

• L’uso corretto delle macro puo’ rendere superfluo il<br />

“logrotate” dei file<br />

• Considerare l’uso di “tcp” invece di “udp” per i messaggi<br />

di log che non devono essere assolutamente “persi”<br />

• Rendere “poco” visibile il log server centrale<br />

– Mantenere i log (anche) in locale!<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 29


<strong>Log</strong> <strong>Server</strong>: quanto chiuso?<br />

Sources<br />

SyslogNG<br />

(Archive)<br />

SyslogNG<br />

(Monitor)<br />

TTY<br />

SSH<br />

• Sistema “sicuro” == Sistema spento e scollegato dalla rete!<br />

• Occorre un compromesso, ad esempio:<br />

• “Adeguata” protezione dei server “Sources”<br />

• Creazione di un log server (Archive) accessibile solo da console e che preveda<br />

la scrittura dei log su supporti WORM.<br />

• <strong>Log</strong> server in cascata (Monitor) per la normale attivita’ di controllo<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 30


Riferimenti<br />

• http://www.balabit.com/products/syslog_ng/<br />

– Versione attuale: 1.6.0rc3<br />

– http://www.balabit.com/downloads/syslog-ng/<br />

• RFC 3164<br />

– http://www.ietf.org/rfc/rfc3164.txt?number=3164<br />

• Mailing list:<br />

– https://lists.balabit.hu/mailman/listinfo/syslog-ng<br />

• FAQ:<br />

– http://www.campin.net/syslog-ng/faq.html<br />

CCR2003 - <strong>Log</strong> <strong>Server</strong> <strong>Centralizzato</strong> - 11 Giugno 2003 31


Domande ?<br />

http://www.lnf.infn.it/~angius/logsrv.pdf<br />

Sandro.Angius@lnf.infn.it - Claudio.Soprano@lnf.infn.it

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!