Parę dni temu, na forum duga wyczytałem coś o przesyłaniu logów systemowych przez sieć. W sumie to nigdy mi to do głowy nie przyszło ale jeśli by się nad tym głębiej zastanowić, tego typu mechanizm może okazać się całkiem użyteczny. Na dobrą sprawę nie wiem jak to jest rozwiązane w debianie opartym o systemd, natomiast jeśli chodzi o inne inity -- openrc i sysvinit -- tego typu funkcjonalność można zaimplementować wykorzystując narzędzie rsyslog lub syslog-ng . Co prawda, oba się troszeczkę różnią między sobą jeśli chodzi o konfigurację ale każdy z nich może być klientem/serwerem dla tego drugiego.

Przesył logów przez sieć to jedna sprawa, a zabezpieczenie komunikacji by te logi nie szły po kablach w formie niezaszyfrowanej, to całkiem inna bajka. Na szczęście, zarówno rsyslog jak i syslog-ng są w kwestii szyfrowania logów kompatybilne i oba daemony mogą ze sobą współpracować bez większego problemu.

Niżej zostanie opisana konfiguracja debianowego serwera, na którym będzie nasłuchiwał daemon rsyslog. Dodatkowo, zostanie przedstawiona konfiguracja dwóch klientów, z których jeden będzie miał zainstalowanego syslog-ng , a drugi rsyslog. Z klientów logi zostaną przesłane do serwera. Dodatkowo postaramy się zaszyfrować ruch przy pomocy kanału TLS. Zanim jednak przejdziemy do konfiguracji daemonów, musimy przygotować pierw odpowiednio systemy -- tam gdzie mamy zainstalowanego rsysloga, musimy doinstalować pakiet rsyslog-gnutls , dodatkowo na każdym systemie potrzeby będzie nam także openssl .

Jeśli nie posiadamy ipv6, możemy skonfigurować rsysloga by nasłuchiwał tylko na ipv4 -- robimy to przez plik /etc/default/rsyslog :

RSYSLOGD_OPTIONS="-4"

Generowanie certyfikatów

By zestawić kanał TLS, potrzebne nam są certyfikaty. W debianie jest dostępne narzędzie easy-rsa, które ułatwia znacząco operowanie na certyfikatach ale w przypadku rsyslog i syslog-ng nie możemy z niego skorzystać -- trzeba używać certtool dostępnego w paczce gnutls-bin .

Dobrze jest przyjąć sobie pewną konwencję przy nazywaniu plików. W tym przypadku pliki certyfikatów będą nazywane host.domena + odpowiednia końcówka -- klucze prywatne będą mieć .key , certyfikaty będą mieć .crt . Wyjątkiem będzie jedynie CA, które będzie miało formę ca.domena.{key,crt}.

Narzędzie certtool przyjmuje predefiniowaną konfigurację dla certyfikatów. Dlatego też stwórzmy cztery pliki tekstowe, po jednym dla CA, serwera i dla każdego klienta. Te pliki umieścimy w katalogu /etc/CA/template/ , same certyfikaty zaś będą zlokalizowane w /etc/CA/ . Poniżej wzory plików pod certyfikaty:

Plik /etc/CA/template/ca_192.168.1.150

# X.509 Certificate options
#
# DN options

# The organization of the subject.
organization = "morfikownia"

# The organizational unit of the subject.
#unit = "sleeping dept."

# The state of the certificate owner.
#state = "Example"

# The country of the subject. Two letter code.
country = GB

# The common name of the certificate owner.
cn = "192.168.1.150"

# The serial number of the certificate. Should be incremented each time a new certificate is generated.
serial = 001

# In how many days, counting from today, this certificate will expire.
expiration_days = 3650

# Whether this is a CA certificate or not
ca

# Whether this key will be used to sign other certificates.
cert_signing_key

# Whether this key will be used to sign CRLs.
crl_signing_key

Plik /etc/CA/template/client_192.168.1.1

# X.509 Certificate options
#
# DN options

# The organization of the subject.
organization = "morfikownia"

# The organizational unit of the subject.
#unit = "sleeping dept."

# The state of the certificate owner.
state = "localhost"

# The country of the subject. Two letter code.
country = GB

# The common name of the certificate owner.
cn = "192.168.1.1"

# A user id of the certificate owner.
#uid = "scertowner"

# The serial number of the certificate. Should be incremented each time a new certificate is generated.
serial = 004

# In how many days, counting from today, this certificate will expire.
expiration_days = 3650

# X.509 v3 extensions

# DNS name(s) of the server
#dns_name = "server.example.com"
#dns_name = "server_alias.example.com"

# (Optional) Server IP address
ip_address = "192.168.1.150"

# Whether this certificate will be used for a TLS server
tls_www_server

# Whether this certificate will be used to encrypt data (needed
# in TLS RSA ciphersuites). Note that it is preferred to use different
# keys for encryption and signing.
encryption_key
signing_key

Plik /etc/CA/template/client_192.168.1.166

# X.509 Certificate options
#
# DN options

# The organization of the subject.
organization = "morfikownia"

# The organizational unit of the subject.
#unit = "sleeping dept."

# The state of the certificate owner.
state = "localhost"

# The country of the subject. Two letter code.
country = GB

# The common name of the certificate owner.
cn = "192.168.1.166"

# A user id of the certificate owner.
#uid = "scertowner"

# The serial number of the certificate. Should be incremented each time a new certificate is generated.
serial = 003

# In how many days, counting from today, this certificate will expire.
expiration_days = 3650

# X.509 v3 extensions

# DNS name(s) of the server
#dns_name = "server.example.com"
#dns_name = "server_alias.example.com"

# (Optional) Server IP address
ip_address = "192.168.1.150"

# Whether this certificate will be used for a TLS server
tls_www_server

# Whether this certificate will be used to encrypt data (needed
# in TLS RSA ciphersuites). Note that it is preferred to use different
# keys for encryption and signing.
encryption_key
signing_key

Plik /etc/CA/template/server_192.168.1.1

# X.509 Certificate options
#
# DN options

# The organization of the subject.
organization = "morfikownia"

# The organizational unit of the subject.
#unit = "sleeping dept."

# The state of the certificate owner.
state = "localhost"

# The country of the subject. Two letter code.
country = GB

# The common name of the certificate owner.
cn = "192.168.1.150"

# A user id of the certificate owner.
#uid = "scertowner"

# The serial number of the certificate. Should be incremented each time a new certificate is generated.
serial = 002

# In how many days, counting from today, this certificate will expire.
expiration_days = 3650

# X.509 v3 extensions

# DNS name(s) of the server
#dns_name = "server.example.com"
#dns_name = "server_alias.example.com"

# (Optional) Server IP address
ip_address = "192.168.1.150"

# Whether this certificate will be used for a TLS server
tls_www_server

# Whether this certificate will be used to encrypt data (needed
# in TLS RSA ciphersuites). Note that it is preferred to use different
# keys for encryption and signing.
encryption_key
signing_key

Kluczowe w powyższych plikach jest dobranie parametru cn , który musi odpowiadać nazwie lub adresie IP danego hosta. Jeśli ta nazwa lub adres IP nie będzie się zgadzać z nazwą lub adresem IP danego hosta, to przy uwierzytelnianiu podczas próby przesyłu logów, zostanie wyrzucony błąd i akcja się nie powiedzie.

Przechodzimy teraz do katalogu /etc/CA/ i generujemy klucze prywatne na podstawie zdefiniowanych szablonów:

# certtool --generate-privkey --rsa --sec-param high --outfile ca_192.168.1.150.key
Generating a 3072 bit RSA private key...

# certtool --generate-privkey --rsa --sec-param high --outfile server_192.168.1.150.key
Generating a 3072 bit RSA private key...

# certtool --generate-privkey --rsa --sec-param high --outfile client_192.168.1.1.key
Generating a 3072 bit RSA private key...

# certtool --generate-privkey --rsa --sec-param high --outfile client_192.168.1.166.key
Generating a 3072 bit RSA private key...

Jeden z powyższych kluczy, a konkretnie ten mający w swojej nazwie ca musi zostać podpisany przez samego siebie, tak by mógł podpisywać inne certyfikaty:

# certtool --generate-self-signed --load-privkey ca_192.168.1.150.key  --template ./template/ca_192.168.1.150 --outfile ca_192.168.1.150.crt
Generating a self signed certificate...
X.509 Certificate Information:
        Version: 3
        Serial Number (hex): 01
        Validity:
                Not Before: Fri Oct 10 17:21:24 UTC 2014
                Not After: Mon Oct 07 17:21:24 UTC 2024
        Subject: CN=192.168.1.150,O=morfikownia,C=GB
        Subject Public Key Algorithm: RSA
        Algorithm Security Level: High (3072 bits)
                Modulus (bits 3072):
                        00:ad:c1:99:35:a1:0b:80:ba:98:82:38:5b:14:fc:8e
                        f7:f2:2e:52:06:99:99:14:df:90:05:79:86:17:4d:21
                        1b:40:68:4a:0f:94:0a:c3:33:58:ab:d1:d1:32:6f:3f
                        07:8f:29:9f:ff:3e:39:ea:2b:fe:98:db:1f:3e:ae:43
                        d1:e6:11:89:86:b6:18:b8:9c:5d:c7:80:ea:97:1d:07
                        d8:bc:ba:cc:ee:43:e6:e9:cc:bd:eb:77:f8:d5:42:e4
                        4f:2d:1a:43:bd:c3:95:2c:31:a1:0f:40:cc:29:36:b7
                        bf:fa:1c:df:bb:ef:d5:92:17:c4:06:72:47:6a:09:d3
                        66:ee:15:4b:e0:cc:d7:36:c8:9b:2d:19:dc:34:a7:e4
                        2d:ee:a3:3e:cb:dd:67:f1:ce:4a:ca:6b:c1:43:38:01
                        e4:ae:34:07:30:e6:74:91:4f:48:4b:ab:99:d9:ba:f2
                        0b:9a:21:0c:35:f9:e3:a4:3b:ea:b7:92:cc:f2:b6:72
                        ba:ad:f6:e8:d2:2d:8f:e5:0c:3b:62:35:47:36:dc:6f
                        f6:30:96:c5:d9:fb:06:34:6d:74:ac:d8:82:80:d6:7f
                        fe:52:75:91:12:f0:cf:fc:cc:0d:ab:be:c8:4a:f0:77
                        a5:44:ed:2a:a3:60:69:bd:1f:d3:04:b5:50:4b:e2:a5
                        22:2b:54:30:82:50:5b:84:0a:af:c7:e4:01:ca:e6:64
                        24:cb:be:de:18:a5:49:06:a1:a8:34:15:a5:e4:37:70
                        b6:88:6d:18:51:25:ca:7c:dc:69:28:cd:69:ba:9d:8d
                        58:f4:0d:2a:5b:42:c7:b2:a8:07:53:3f:c0:65:18:9f
                        94:43:86:a7:a1:60:3c:06:33:ce:cd:25:74:46:7d:7e
                        91:70:e6:9e:dd:e8:2f:f8:1c:bb:0e:9d:10:d5:73:42
                        3a:55:81:4e:a0:89:10:49:f9:31:2b:bd:0e:65:4d:a3
                        13:86:45:8f:40:9f:70:ba:aa:5f:27:6d:0f:30:37:1b
                        9f
                Exponent (bits 24):
                        01:00:01
        Extensions:
                Basic Constraints (critical):
                        Certificate Authority (CA): TRUE
                Key Usage (critical):
                        Certificate signing.
                        CRL signing.
                Subject Key Identifier (not critical):
                        bc4400db4349aac3698872b989d0931c4fe0006d
Other Information:
        Public Key ID:
                bc4400db4349aac3698872b989d0931c4fe0006d
        Public key's random art:
                +--[ RSA 3072]----+
                |+.. .o+.         |
                | oE. =..         |
                | .o + o .        |
                |.= O   +         |
                |= & .   S        |
                |o+ =   . .       |
                |. o     .        |
                |                 |
                |                 |
                +-----------------+



Signing certificate...

Teraz za pomocą tak stworzonego CA generujemy certyfikaty dla serwera:

# certtool --generate-certificate --template template/server_192.168.1.150 --load-privkey server_192.168.1.150.key --load-ca-certificate ca_192.168.1.150.crt --load-ca-privkey ca_192.168.1.150.key --outfile server_192.168.1.150.crt
Generating a signed certificate...
X.509 Certificate Information:
        Version: 3
        Serial Number (hex): 02
        Validity:
                Not Before: Fri Oct 10 17:25:52 UTC 2014
                Not After: Mon Oct 07 17:25:52 UTC 2024
        Subject: CN=192.168.1.150,O=morfikownia,ST=localhost,C=GB
        Subject Public Key Algorithm: RSA
        Algorithm Security Level: High (3072 bits)
                Modulus (bits 3072):
                        00:ef:68:16:03:9e:45:e8:1b:48:41:83:5d:a6:15:cb
                        ab:cb:02:2c:29:04:6c:ba:62:08:e1:8b:1c:a2:e4:5f
                        c3:08:53:33:a5:a4:18:a0:47:1a:be:3d:db:c5:1e:48
                        db:cf:c4:fd:e9:0f:48:c7:70:a5:82:80:01:42:30:4c
                        e8:75:86:71:9e:48:34:86:49:11:84:8c:9e:95:e1:52
                        73:f6:93:29:18:a0:ed:eb:68:0f:c2:1e:e4:fd:21:d1
                        46:04:e0:e1:de:ad:be:cd:9d:27:d8:ac:56:e8:84:cd
                        07:6c:65:3c:bb:60:98:01:f0:a9:d9:a8:b7:84:3c:2f
                        d3:3b:53:55:ea:46:1b:67:10:a3:f3:e7:2e:41:96:b2
                        cf:25:06:7b:fd:6b:99:fd:8b:05:3a:83:0e:11:69:a7
                        36:b5:02:16:92:e0:6f:34:99:20:92:db:ae:e7:29:4c
                        ff:14:e6:6b:4b:0f:e4:a2:02:c7:07:db:56:e6:b8:8b
                        f4:2a:83:4f:93:c6:27:01:b6:88:fa:b3:f2:3f:b0:4b
                        98:12:5a:83:64:53:b1:af:14:2e:0d:b7:5b:5a:a6:72
                        d7:71:d6:ca:5a:17:9c:dd:96:2d:aa:8e:10:67:1b:c0
                        a9:61:26:69:3e:22:04:4c:4f:0a:e8:85:81:db:ff:53
                        f0:ba:14:86:c4:f5:b1:7a:a1:66:e5:6c:4a:c0:67:8c
                        e5:d7:95:03:85:1a:ab:69:52:66:3c:80:e0:8d:aa:e9
                        5e:55:21:08:d7:14:6e:10:a2:44:fe:02:b0:95:88:b3
                        ef:9d:1d:ab:78:d6:49:2c:ec:12:b0:d8:0b:22:34:5a
                        6a:79:f1:ec:5b:32:d9:98:48:13:bf:49:81:32:05:98
                        f8:7f:22:85:e0:cc:16:64:bf:9c:2c:e6:50:6b:5c:92
                        5b:24:df:e3:d9:05:9d:5f:ab:a8:9b:fb:20:60:22:bc
                        b9:9f:7c:1b:04:42:5b:f7:32:c4:6c:4e:7f:16:05:00
                        db
                Exponent (bits 24):
                        01:00:01
        Extensions:
                Basic Constraints (critical):
                        Certificate Authority (CA): FALSE
                Subject Alternative Name (not critical):
                        IPAddress: 192.168.1.150
                Key Purpose (not critical):
                        TLS WWW Server.
                Key Usage (critical):
                        Digital signature.
                        Key encipherment.
                Subject Key Identifier (not critical):
                        478762e130a5b7c72d5b09a6c1e2d0d4690cb809
                Authority Key Identifier (not critical):
                        bc4400db4349aac3698872b989d0931c4fe0006d
Other Information:
        Public Key ID:
                478762e130a5b7c72d5b09a6c1e2d0d4690cb809
        Public key's random art:
                +--[ RSA 3072]----+
                |     .==o.       |
                |  E .o *=. .     |
                |   ..o+.B = .    |
                |    oo + O + .   |
                |      . S = +    |
                |         o +     |
                |          .      |
                |                 |
                |                 |
                +-----------------+



Signing certificate...

Oraz dwóch pozostałych klientów:

# certtool --generate-certificate --template template/client_192.168.1.1 --load-privkey client_192.168.1.1.key --load-ca-certificate ca_192.168.1.150.crt --load-ca-privkey ca_192.168
.1.150.key --outfile client_192.168.1.1.crt
Generating a signed certificate...
X.509 Certificate Information:
        Version: 3
        Serial Number (hex): 04
        Validity:
                Not Before: Fri Oct 10 17:28:38 UTC 2014
                Not After: Mon Oct 07 17:28:38 UTC 2024
        Subject: CN=192.168.1.1,O=morfikownia,ST=localhost,C=GB
        Subject Public Key Algorithm: RSA
        Algorithm Security Level: High (3072 bits)
                Modulus (bits 3072):
                        00:e4:98:d0:b0:1a:be:de:fa:9e:2f:05:08:88:f5:fc
                        99:48:21:e1:20:71:44:ed:bc:8c:4a:4e:61:47:28:85
                        fe:01:c3:42:96:64:3d:f4:6f:b3:74:7d:ca:3c:79:a0
                        98:4d:e4:b2:e7:1d:17:0e:a7:25:14:71:a5:72:3b:c2
                        3a:0b:77:37:27:03:95:43:93:31:c3:7d:50:53:7b:45
                        8c:a2:5f:6c:a9:97:70:98:48:3b:69:2a:1b:fe:69:e5
                        55:02:71:89:f2:2e:41:f0:7a:7b:6b:ff:cb:a2:df:45
                        14:7e:38:25:89:71:4b:10:d4:0b:1a:e6:06:eb:05:78
                        49:65:a8:84:e0:18:52:1c:12:54:87:a1:db:f0:84:0a
                        ad:93:b6:a0:fb:8c:78:a7:5f:42:4e:6a:8f:83:6b:5d
                        54:24:89:7d:c3:da:68:93:e6:9e:29:d7:fb:92:50:67
                        18:bc:5a:ae:17:0b:49:8c:87:64:d9:b7:d1:10:99:bb
                        ad:86:44:50:f5:59:e6:ff:6f:92:9e:e4:85:5c:3a:8c
                        29:c7:0d:23:cf:4a:52:22:0b:e4:e3:77:15:cf:9a:df
                        ee:82:f2:20:de:45:f8:47:de:b1:9c:92:43:05:61:00
                        ba:1c:bf:40:d8:ac:0e:45:86:6e:ed:21:58:2f:bd:a4
                        69:a8:dd:cf:f6:89:7b:83:13:98:59:c8:fb:b9:67:ba
                        c7:43:21:5e:25:5d:8b:de:e4:de:b1:ad:25:64:05:c4
                        bf:6b:c7:b7:cc:a5:e5:65:83:a7:e0:54:04:50:44:52
                        ab:f0:9d:2b:f8:50:1d:5f:be:80:f2:d6:d0:6b:69:db
                        c7:d2:49:78:7a:96:ac:ae:7a:82:ad:71:f5:2c:92:31
                        4d:e4:01:80:81:a3:a9:d6:bf:54:64:76:2b:d4:7b:b8
                        fc:53:2e:15:90:c7:61:47:b8:f7:e4:52:53:02:5e:ce
                        05:0f:70:13:f3:a4:85:15:fe:f4:5d:77:8f:f5:aa:fa
                        f1
                Exponent (bits 24):
                        01:00:01
        Extensions:
                Basic Constraints (critical):
                        Certificate Authority (CA): FALSE
                Subject Alternative Name (not critical):
                        IPAddress: 192.168.1.150
                Key Purpose (not critical):
                        TLS WWW Server.
                Key Usage (critical):
                        Digital signature.
                        Key encipherment.
                Subject Key Identifier (not critical):
                        058eea0f11847f3183af40ab136a1d535eb31138
                Authority Key Identifier (not critical):
                        bc4400db4349aac3698872b989d0931c4fe0006d
Other Information:
        Public Key ID:
                058eea0f11847f3183af40ab136a1d535eb31138
        Public key's random art:
                +--[ RSA 3072]----+
                |   .....o        |
                |  o..E+* .       |
                | . o+.++= .      |
                |. oo.+o. .       |
                |.o..=o  S        |
                |+. o..           |
                |..  o            |
                |     o           |
                |      .          |
                +-----------------+



Signing certificate...
root:/etc/CA# certtool --generate-certificate --template template/client_192.168.1.166 --load-privkey client_192.168.1.166.key --load-ca-certificate ca_192.168.1.150.crt --load-ca-privkey ca_192
.168.1.150.key --outfile client_192.168.1.166.crt
Generating a signed certificate...
X.509 Certificate Information:
        Version: 3
        Serial Number (hex): 03
        Validity:
                Not Before: Fri Oct 10 17:28:50 UTC 2014
                Not After: Mon Oct 07 17:28:50 UTC 2024
        Subject: CN=192.168.1.166,O=morfikownia,ST=localhost,C=GB
        Subject Public Key Algorithm: RSA
        Algorithm Security Level: High (3072 bits)
                Modulus (bits 3072):
                        00:af:8f:0d:9e:a0:a5:8e:09:2b:a3:d7:a3:3c:b5:5b
                        03:70:f0:70:7e:0d:f4:67:9b:30:6f:c5:10:88:86:24
                        97:20:cc:cb:cb:72:41:0b:0c:a0:17:9a:c8:0d:56:e0
                        79:11:63:fe:fe:36:53:95:62:fd:dc:e0:98:ac:42:8e
                        3a:15:63:99:70:a5:68:5b:d4:b7:ec:0a:a3:cd:72:d5
                        09:74:aa:ff:e1:0b:05:d6:5d:66:c6:8d:22:cc:95:86
                        e4:a2:e9:7f:83:03:b5:ae:07:b8:c2:ae:bd:58:6b:75
                        3d:11:13:b2:61:a1:67:de:80:03:5f:b1:e7:d8:23:00
                        47:75:a0:1b:38:13:6b:a8:6c:f5:b0:9e:46:19:1d:a8
                        96:a6:76:0f:2f:e1:e9:f0:6e:47:7b:8d:0b:6c:45:b3
                        32:5a:e1:d3:10:67:0a:20:9a:56:64:a7:00:06:f0:02
                        56:53:55:76:31:10:34:c7:3f:19:b7:7c:a0:69:95:e3
                        8b:25:c2:6d:4e:19:db:db:56:23:19:c0:f4:5e:ad:a8
                        12:e3:c0:54:e2:32:e5:75:f6:32:df:4f:a0:cd:da:98
                        23:f0:01:2f:0e:3f:94:07:5f:a8:26:91:58:30:34:9e
                        2c:05:0e:2f:6f:b0:09:84:b5:6c:ce:1c:55:77:d5:6b
                        94:3e:e1:e7:d8:d6:de:33:10:e9:36:56:6f:54:66:3d
                        3b:af:d3:73:09:f8:b9:2b:d4:61:c7:92:b5:3d:8a:96
                        3c:10:7b:03:0d:0b:16:eb:5a:28:55:5a:25:25:af:39
                        db:47:c4:cd:d9:ca:c2:b8:71:fb:2c:61:d5:73:a2:58
                        09:98:65:a1:2a:24:0f:a9:95:76:e3:f6:49:43:3b:f7
                        f0:28:79:24:66:5e:89:09:ea:d1:3c:d3:ac:43:9c:07
                        38:2c:f5:66:6f:8d:80:6c:dd:cf:38:86:5a:10:0e:4d
                        50:86:88:ab:0c:d6:47:0f:f8:43:39:0f:3d:e6:58:10
                        ef
                Exponent (bits 24):
                        01:00:01
        Extensions:
                Basic Constraints (critical):
                        Certificate Authority (CA): FALSE
                Subject Alternative Name (not critical):
                        IPAddress: 192.168.1.150
                Key Purpose (not critical):
                        TLS WWW Server.
                Key Usage (critical):
                        Digital signature.
                        Key encipherment.
                Subject Key Identifier (not critical):
                        a5c15b37cd8af252d51b8bb6e1f466316d855887
                Authority Key Identifier (not critical):
                        bc4400db4349aac3698872b989d0931c4fe0006d
Other Information:
        Public Key ID:
                a5c15b37cd8af252d51b8bb6e1f466316d855887
        Public key's random art:
                +--[ RSA 3072]----+
                |              ...|
                |       .     *Eo |
                |        o o = * .|
                |         * + + =.|
                |        S o * = o|
                |         + + + + |
                |        . . o +  |
                |         .   o   |
                |                 |
                +-----------------+



Signing certificate...

W tej chwili, katalog /etc/CA/ powinien się prezentować następująco:

# tree /etc/CA/
/etc/CA/
├── ca_192.168.1.150.crt
├── ca_192.168.1.150.key
├── client_192.168.1.1.crt
├── client_192.168.1.1.key
├── client_192.168.1.166.crt
├── client_192.168.1.166.key
├── server_192.168.1.150.crt
├── server_192.168.1.150.key
└── template
    ├── ca_192.168.1.150
    ├── client_192.168.1.1
    ├── client_192.168.1.166
    └── server_192.168.1.150

1 directory, 12 files

Przesyłamy tak stworzone certyfikaty na odpowiednie maszyny przy pomocy scp. W tym przypadku, certyfikaty CA i serwera nie będą kopiowane -- trzeba tylko przesłać certyfikaty do maszyn klienckich. Trzeba także pamiętać by skopiować także certyfikat samego CA:

# scp ca_192.168.1.150.crt client_192.168.1.1.* 192.168.1.1:/etc/syslog-ng.cert/
root@192.168.1.1's password:
ca_192.168.1.150.crt                 100% 1525     1.5KB/s   00:00
client_192.168.1.1.crt               100% 1643     1.6KB/s   00:00
client_192.168.1.1.key               100% 8394     8.2KB/s   00:00

# scp ca_192.168.1.150.crt client_192.168.1.166.* 192.168.1.166:/etc/rsyslog.cert/
root@192.168.1.166's password:
ca_192.168.1.150.crt                 100% 1525     1.5KB/s   00:00
client_192.168.1.166.crt             100% 1647     1.6KB/s   00:00
client_192.168.1.166.key             100% 8404     8.2KB/s   00:00

Konfiguracja serwera rsyslog

Edytujemy plik /etc/rsyslog.conf i dopisujemy tam poniższe linijki:

#################
#### MODULES ####
#################

# make gtls driver the default
$DefaultNetstreamDriver gtls

# certificate files
$DefaultNetstreamDriverCAFile /etc/CA/ca_192.168.1.150.crt
$DefaultNetstreamDriverCertFile /etc/CA/server_192.168.1.150.crt
$DefaultNetstreamDriverKeyFile /etc/CA/server_192.168.1.150.key

$ModLoad imuxsock # provides support for local system logging
$ModLoad imklog   # provides kernel logging support
#$ModLoad immark  # provides --MARK-- message capability

# provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514                      # start up listener at port 514

$InputTCPServerStreamDriverMode 1           # run driver in TLS-only mode
#$InputTCPServerStreamDriverAuthMode anon   # client is NOT authenticated
#$ActionSendStreamDriverAuthMode x509/name  # authenticate by hostname
#$InputTCPServerStreamDriverPermittedPeer *.mhouse.lh
$InputTCPServerStreamDriverPermittedPeer 192.168.1.*

W przypadku gdybyśmy nie chcieli sprawdzać certyfikatów klienckich, trzeba odkomentować $InputTCPServerStreamDriverAuthMode anon i zakomentować wszystko poniżej. Jeśli wykorzystujemy domeny zamiast adresów IP, w $InputTCPServerStreamDriverPermittedPeer podajemy nazwy oraz odhashować trzeba także$ActionSendStreamDriverAuthMode x509/name . Wszelkie możliwe dyrektywy, które mogą zostać użyte w powyższym pliku, są dostępne i przyzwoicie opisane pod tym linkiem [1].

Konfiguracja klienta rsyslog

Mając ustawiony już serwer, zaprogramujemy klienta tak by przesłał wszystkie logi jakie są generowane w systemie do serwera skonfigurowanego wyżej. W tym celu edytujemy plik /etc/rsyslog.conf na kliencie i dopisujemy w nim poniższe linijki:

#################
#### MODULES ####
#################

# make gtls driver the default
$DefaultNetstreamDriver gtls

# certificate files
$DefaultNetstreamDriverCAFile /etc/rsyslog.cert/ca_192.168.1.150.crt
$DefaultNetstreamDriverCertFile /etc/rsyslog.cert/client_192.168.1.166.crt
$DefaultNetstreamDriverKeyFile /etc/rsyslog.cert/client_192.168.1.166.key

$ModLoad imuxsock # provides support for local system logging
$ModLoad imklog   # provides kernel logging support
#$ModLoad immark  # provides --MARK-- message capability

# provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514

# set up the action
$ActionSendStreamDriverMode 1                   # require TLS for the connection
#$ActionSendStreamDriverAuthMode x509/name      # authenticate by hostname
#$ActionSendStreamDriverAuthMode anon           # server is NOT authenticated
$ActionSendStreamDriverPermittedPeer 192.168.1.150
*.* @@(o)192.168.1.150:514

Ostatnia linijka będzie przesyłać wszystkie logi *.* protokołem tcp @@ na zdalny serwer. W przypadku protokołu udp, byłby jeden znak @ zamiast dwóch.

Konfiguracja klienta syslog-ng

Podobnie jak w przypadku klienta rsyslog, również chcemy przesłać wszystkie logi przez sieć, z tym, że musimy pierw zrobić jeden zabieg na certyfikatach -- konkretnie to musimy nazwać certyfikat CA po jego hashu. Przy pomocy openssl możemy wygenerować hash. Wydajemy zatem na kliencie poniższe polecenia:

# openssl x509 -noout -hash -in ca_192.168.1.150.crt 
f31e2e50

# ln -s ./ca_192.168.1.150.crt f31e2e50.0

# ls -al
drwxr-xr-x    2 root     root          4096 Oct 10 20:03 .
drwxr-xr-x    1 root     root          4096 Oct 10 15:27 ..
-rw-r--r--    1 root     root          1525 Oct 10 19:36 ca_192.168.1.150.crt
-rw-r--r--    1 root     root          1643 Oct 10 19:36 client_192.168.1.1.crt
-rw-------    1 root     root          8394 Oct 10 19:36 client_192.168.1.1.key
lrwxrwxrwx    1 root     root            22 Oct 10 20:03 f31e2e50.0 -> ./ca_192.168.1.150.crt

Przechodzimy teraz do konfiguracji syslog-ng . Poniżej znajduje się mój plik konfiguracyjny:

@version:3.0

options {
    # disable the chained hostname format in logs
    chain_hostnames(off);
    
    # the time to wait before a died connection is re-established
    time_reopen (600);
    
    # the time to wait before an idle destination file is closed
    time_reap(0);
    
    # the number of lines buffered before written to file
    # you might want to increase this if your disk isn't catching with
    # all the log messages you get or if you want less disk activity
    # (say on a laptop)
    flush_lines(0);
    
    # the number of lines fitting in the output queue
    log_fifo_size(256);
    
    # enable or disable directory creation for destination files
    create_dirs(no);
    
    # default owner, group, and permissions for log files
    owner(root);
    perm(0600);
    #group("log");
    
    # default owner, group, and permissions for created directories
    #dir_owner(root);
    #dir_group(root);
    #dir_perm(0755);
    
    # enable or disable DNS usage
    # syslog-ng blocks on DNS queries, so enabling DNS may lead to
    # a Denial of Service attack
    use_dns(no);

    # maximum length of message in bytes
    # this is only limited by the program listening on the /dev/log Unix
    # socket, glibc can handle arbitrary length log messages, but -- for
    # example -- syslogd accepts only 1024 bytes
    log_msg_size(1024);

    # Disable statistic log messages.
    stats_freq(0);
    
    # "--MARK--" entries in the log
    mark_freq (1800);
    
    keep_hostname(yes);
    use_fqdn(no);
    long_hostnames(on);
#   ts_format(iso);      #make ISO-8601 timestamps
};


source s_all {
    # message generated by Syslog-NG
    internal();
    # standard Linux log source (this is the default place for the syslog() function to send logs to)
    unix-stream("/dev/log");
    # messages from the kernel
    file("/proc/kmsg" program_override("kernel"));  
};

source s_localhost {
    tcp(ip(127.0.0.1) port(514));
    udp(ip(127.0.0.1) port(514));
};

destination d_messages {
    file("/var/log/messages");
};

destination d_network {
#   tcp( "192.168.1.150" port(514) );
    tcp( "192.168.1.150" port(514)
        tls( ca_dir("/etc/syslog-ng.cert")
            key_file("/etc/syslog-ng.cert/client_192.168.1.1.key") 
            cert_file("/etc/syslog-ng.cert/client_192.168.1.1.crt")
#           peer_verify(optional-untrusted)
            peer_verify(required-trusted)
            )
        );
};


log {
    source(s_all);
    source(s_localhost);
    destination(d_messages);
    destination(d_network);
};

Interesuje nas głównie zwrotka destination d_network . Jeśli jakieś opcje są niejasne, wszystkie z nich są dokładnie opisane pod tym linkiem [2].

Rozdzielenie logów

Zbierane w ten sposób logi są mieszane z logami systemowymi. Jeśli tych logów nie jest dużo, lub też mamy kilka stacji roboczych, z których zbieramy logi, taka niedogodność niezbyt może się nam dać we znaki. Natomiast jeśli logów mamy dużo i pochodzą przy tym z wielu hostów, przydałoby się jakoś te logi podzielić i poupychać w odpowiednich plikach, tak by ułatwić sobie ich przeglądanie w późniejszym czasie.

By rozdzielić logi, dopisujemy poniższe linijki w sekcji RULES ale tak, by znajdowały się one przed wszystkimi lokalnymi regułami, przykładowo:

###############
#### RULES ####
###############

if $fromhost-ip startswith "192.168.1.1" then /var/log/router.log
& stop
...

Pierwsza linijka zapisze logi z adresu 192.168.1.1 do pliku /var/log/router.log . Druga linijka zaś kończy przetwarzanie tych logów, tj. nie są one przetwarzane przez kolejne reguły w pliku konfiguracyjnym rsysloga. Jeśli nie dodamy & ~ , komunikaty zostaną zalogowane i do powyższego pliku i do standardowych plików rsysloga.

Problemy

Jeśli z jakichś powodów mamy problemy z połączeniem, możemy zastopować daemona rsyslog i odpalić go w interaktywnym trybie debug:

# rsyslogd -nd

Powyższe polecenie wygeneruje dość pokaźny log, w którym na pewno się znajdują informacje wskazujące gdzie leży problem.

Możemy także wygenerować sobie testową wiadomość i sprawdzić czy zostanie przesłana i ewentualnie zaszyfrowana przez sieć:

# logger -t test my syslog-test-message

Z tym, że by sprawdzić czy komunikat jest szyfrowany, trzeba posłużyć się jakimś snifferem, np. wiresharkiem.

Makulatura

Pisane w oparciu o:

https://help.ubuntu.com/community/GnuTLS [3]
http://www.rsyslog.com/doc/rsyslog_tls.html [4]
http://wiki.libvirt.org/page/TLSSetup [5]
http://www.balabit.com/sites/default/files/documents/syslog-ng-ose-3.4-guides/en/syslog-ng-ose-v3.4-guide-admin/html/bk01-toc.html [6]
http://help.papertrailapp.com/kb/configuration/encrypting-remote-syslog-with-tls-ssl/ [7]
http://wiki.gentoo.org/wiki/Rsyslog [8]


Przypisy:

  1. http://www.rsyslog.com/doc/rsyslog_conf_global.html
  2. http://www.balabit.com/sites/default/files/documents/syslog-ng-ose-3.4-guides/en/syslog-ng-ose-v3.4-guide-admin/html/tlsoptions.html
  3. https://help.ubuntu.com/community/GnuTLS
  4. http://www.rsyslog.com/doc/rsyslog_tls.html
  5. http://wiki.libvirt.org/page/TLSSetup
  6. http://www.balabit.com/sites/default/files/documents/syslog-ng-ose-3.4-guides/en/syslog-ng-ose-v3.4-guide-admin/html/bk01-toc.html
  7. http://help.papertrailapp.com/kb/configuration/encrypting-remote-syslog-with-tls-ssl/
  8. http://wiki.gentoo.org/wiki/Rsyslog