This shows you the differences between two versions of the page.
gsr:laboratoare:laborator-10 [2016/01/07 15:54] razvan.deaconescu [Conectarea sigură la un server] |
gsr:laboratoare:laborator-10 [2017/01/09 19:18] (current) alexandru.carp |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Laborator 10: TLS și certificate ====== | + | ====== Laborator 10: Securitate: hardening a server ====== |
+ | ===== 1. PAM ===== | ||
+ | |||
+ | *[01]. Instalați modulul de PAM cracklib (Hint: ''cracklib''). | ||
+ | <code> | ||
+ | root@heimdall:~# apt-get install libpam-cracklib | ||
+ | [...] | ||
+ | </code> | ||
+ | *[02]. Configurați cracklib în așa fel încât (Hint: ''/etc/pam.d/common-password'', ''man pam_cracklib'', /examples): | ||
+ | *[02].a. parolele utilizatorilor să aibă minim 12 caractere (Hint: ''man pam_cracklib'', /minlen) | ||
+ | *[02].b. parolele utilizatorilor să aibă minim 3 cifre (Hint: ''man pam_cracklib'', /dcredit) | ||
+ | *[02].c. parolele utilizatorilor să aibă minim 2 litere mari (Hint: ''man pam_cracklib'', /ucredit) | ||
+ | *[02].d. parolele utilizatorilor să aibă minim 4 litere mici (Hint: ''man pam_cracklib'', /lcredit) | ||
+ | *[02].e. parolele utilizatorilor să aibă minim 3 caractere speciale (Hint: ''man pam_cracklib'', /ocredit) | ||
+ | <code> | ||
+ | root@heimdall:~# cat /etc/pam.d/common-password | grep cracklib | ||
+ | password requisite pam_cracklib.so retry=3 difok=3 minlen=12 dcredit=-3 ucredit=-2 lcredit=-4 ocredit=-3 | ||
+ | </code> | ||
+ | |||
+ | *[03]. Autentificați-vă ca utilizator student și încercați să schimbați parola curentă cu: | ||
+ | *[03].a. o parolă de 1 caracter | ||
+ | *[03].b. o parolă care să nu corespundă cerințelor de mai sus | ||
+ | *[03].c. o parolă care să corespundă cerințelor de mai sus | ||
+ | <code> | ||
+ | student@heimdall:~$ passwd | ||
+ | Changing password for student. | ||
+ | (current) UNIX password: | ||
+ | New password: | ||
+ | BAD PASSWORD: it is WAY too short | ||
+ | New password: | ||
+ | BAD PASSWORD: is too simple | ||
+ | New password: | ||
+ | Retype new password: | ||
+ | passwd: password updated successfully | ||
+ | </code> | ||
+ | |||
+ | *[04]. Instalați modulul de PAM mkhomedir (Hint: ''libpam-modules''). | ||
+ | <code> | ||
+ | root@heimdall:~# apt-get install libpam-modules | ||
+ | [...] | ||
+ | </code> | ||
+ | |||
+ | *[05]. Configurați mkhomedir astfel încât la prima autentificare a unui utilizator să îi fie creat directorul home, dacă acesta nu era creat deja. (Hint: http://www.debian-administration.org/articles/403, ''man pam_mkhomedir'') | ||
+ | <code> | ||
+ | root@heimdall:~# cat /etc/pam.d/common-session| grep session | ||
+ | session required pam_mkhomedir.so skel=/etc/skel/ umask=0022 | ||
+ | </code> | ||
+ | *[06].a. Adăugați utilizatorul jonas.quinn fără a îi crea un director home. | ||
+ | *[06].b. Autentificați-vă ca jonas.quinn. A fost creat directorul home al acestuia ? | ||
+ | <code> | ||
+ | root@heimdall:~# groupadd jonas.quinn | ||
+ | |||
+ | root@heimdall:~# useradd -d /home/jonas.quinn -g jonas.quinn -s /bin/bash jonas.quinn | ||
+ | |||
+ | root@heimdall:~# ls -l /home | ||
+ | total 8 | ||
+ | drwxr-xr-x 3 nrush nrush 4096 Nov 16 21:53 nrush | ||
+ | drwxr-xr-x 3 student student 4096 Oct 18 19:15 student | ||
+ | |||
+ | root@heimdall:~# su - jonas.quinn | ||
+ | Creating directory '/home/jonas.quinn'. | ||
+ | |||
+ | jonas.quinn@heimdall:~$ pwd | ||
+ | /home/jonas.quinn | ||
+ | |||
+ | jonas.quinn@heimdall:~$ logout | ||
+ | |||
+ | root@heimdall:~# ls -l /home | ||
+ | total 12 | ||
+ | drwxr-xr-x 2 jonas.quinn jonas.quinn 4096 Nov 16 22:13 jonas.quinn | ||
+ | drwxr-xr-x 3 nrush nrush 4096 Nov 16 21:53 nrush | ||
+ | drwxr-xr-x 3 student student 4096 Oct 18 19:15 student | ||
+ | </code> | ||
+ | |||
+ | ===== 2. Criptarea datelor. GNU Privacy Guard (GPG) ===== | ||
+ | |||
+ | *Folosiți ''gpg --list-keys'' pentru a lista cheile GPG curente ale utilizatorului student. | ||
+ | *Dacă utilizatorul nu are chei GPG create, va fi generat directorul ''~/.gnupg''. | ||
+ | <code> | ||
+ | student@mjolnir:~$ gpg --list-keys | ||
+ | gpg: directory `/home/student/.gnupg' created | ||
+ | gpg: new configuration file `/home/student/.gnupg/gpg.conf' created | ||
+ | gpg: WARNING: options in `/home/student/.gnupg/gpg.conf' are not yet active during this run | ||
+ | gpg: keyring `/home/student/.gnupg/pubring.gpg' created | ||
+ | gpg: /home/student/.gnupg/trustdb.gpg: trustdb created | ||
+ | </code> | ||
+ | |||
+ | |||
+ | Operații uzuale cu chei GPG: | ||
+ | *generarea de chei | ||
+ | <code> | ||
+ | gpg --gen-key | ||
+ | </code> | ||
+ | |||
+ | *editare de chei | ||
+ | <code> | ||
+ | gpg --edit-key <user-id> | ||
+ | </code> | ||
+ | |||
+ | *ștergere de chei | ||
+ | <code> | ||
+ | gpg --delete-keys <user-id> | ||
+ | gpg --delete-secret-keys <user-id> | ||
+ | gpg --delete-secret-and-public-keys <user-id> | ||
+ | </code> | ||
+ | |||
+ | *listare chei | ||
+ | <code> | ||
+ | gpg --list-keys | ||
+ | gpg --list-public-keys | ||
+ | gpg --list-secret-keys | ||
+ | </code> | ||
+ | |||
+ | *exportare chei | ||
+ | <code> | ||
+ | gpg --export --armor --output my.pub <keyid> | ||
+ | gpg --export-secret-keys ... | ||
+ | </code> | ||
+ | |||
+ | *importare chei | ||
+ | <code> | ||
+ | gpg --import my.pub | ||
+ | </code> | ||
+ | |||
+ | *criptare (folosind cheia publică) | ||
+ | <code> | ||
+ | gpg --encrypt -r "Gogu <gogu@gogu.com>" --output info.dat.enc info.dat | ||
+ | gpg --encrypt -r "Gogu <gogu@gogu.com>" --armor --output info-armor.dat.enc info.dat | ||
+ | </code> | ||
+ | |||
+ | *decriptare (folosind cheia privată) | ||
+ | <code> | ||
+ | gpg --decrypt -r "Gogu <gogu@gogu.com>" --output info.dat.dec info.dat.enc | ||
+ | </code> | ||
+ | |||
+ | *semnare (folosind cheia privată) | ||
+ | <code> | ||
+ | gpg --sign -r "Gogu <gogu@gogu.com>" info.dat | ||
+ | gpg --sign --armor -r "Gogu <gogu@gogu.com>" info.dat | ||
+ | </code> | ||
+ | |||
+ | *verificare (folosind cheia publică) | ||
+ | <code> | ||
+ | gpg --verify -r "Gogu <gogu@gogu.com>" info.dat.gpg | ||
+ | gpg --verify -r "Gogu <gogu@gogu.com>" info.dat.asc | ||
+ | </code> | ||
+ | |||
+ | <note important> | ||
+ | Acest set de exerciții se desfășoară pe mașina fizică | ||
+ | </note> | ||
+ | *[01]. Creați utilizatorii locali ''laurentiu'' și ''mihai''. | ||
+ | <code> | ||
+ | root@mjolnir:~# groupadd laurentiu | ||
+ | |||
+ | root@mjolnir:~# useradd -d /home/laurentiu -m -g laurentiu -s /bin/bash laurentiu | ||
+ | |||
+ | root@mjolnir:~# echo "laurentiu:gsr" | chpasswd | ||
+ | |||
+ | root@mjolnir:~# groupadd mihai | ||
+ | |||
+ | root@mjolnir:~# useradd -d /home/mihai -m -g mihai -s /bin/bash mihai | ||
+ | |||
+ | root@mjolnir:~# echo "mihai:gsr" | chpasswd | ||
+ | </code> | ||
+ | |||
+ | *[02]. Autentificat ca ''laurentiu'': | ||
+ | *[02].a. Generați o pereche de chei de criptare (prima opțiune la ''--gen-key'') cu opțiunile implicite (2048 biți, cheia nu expiră, un nume/e-mail la alegere, un passphrase la alegere). (Hint: ''gpg --gen-key'') | ||
+ | <code> | ||
+ | laurentiu@mjolnir:~$ gpg --gen-key | ||
+ | gpg (GnuPG) 1.4.11; Copyright (C) 2010 Free Software Foundation, Inc. | ||
+ | This is free software: you are free to change and redistribute it. | ||
+ | There is NO WARRANTY, to the extent permitted by law. | ||
+ | |||
+ | gpg: directory `/home/laurentiu/.gnupg' created | ||
+ | gpg: new configuration file `/home/laurentiu/.gnupg/gpg.conf' created | ||
+ | gpg: WARNING: options in `/home/laurentiu/.gnupg/gpg.conf' are not yet active during this run | ||
+ | gpg: keyring `/home/laurentiu/.gnupg/secring.gpg' created | ||
+ | gpg: keyring `/home/laurentiu/.gnupg/pubring.gpg' created | ||
+ | Please select what kind of key you want: | ||
+ | (1) RSA and RSA (default) | ||
+ | (2) DSA and Elgamal | ||
+ | (3) DSA (sign only) | ||
+ | (4) RSA (sign only) | ||
+ | Your selection? 1 | ||
+ | RSA keys may be between 1024 and 4096 bits long. | ||
+ | What keysize do you want? (2048) | ||
+ | Requested keysize is 2048 bits | ||
+ | Please specify how long the key should be valid. | ||
+ | 0 = key does not expire | ||
+ | <n> = key expires in n days | ||
+ | <n>w = key expires in n weeks | ||
+ | <n>m = key expires in n months | ||
+ | <n>y = key expires in n years | ||
+ | Key is valid for? (0) | ||
+ | Key does not expire at all | ||
+ | Is this correct? (y/N) y | ||
+ | |||
+ | You need a user ID to identify your key; the software constructs the user ID | ||
+ | from the Real Name, Comment and Email Address in this form: | ||
+ | "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>" | ||
+ | |||
+ | Real name: Laurentiu | ||
+ | Email address: laurentiu@gsr.cs.pub.ro | ||
+ | Comment: | ||
+ | You selected this USER-ID: | ||
+ | "Laurentiu <laurentiu@gsr.cs.pub.ro>" | ||
+ | |||
+ | Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O | ||
+ | You need a Passphrase to protect your secret key. | ||
+ | |||
+ | We need to generate a lot of random bytes. It is a good idea to perform | ||
+ | some other action (type on the keyboard, move the mouse, utilize the | ||
+ | disks) during the prime generation; this gives the random number | ||
+ | generator a better chance to gain enough entropy. | ||
+ | |||
+ | Not enough random bytes available. Please do some other work to give | ||
+ | the OS a chance to collect more entropy! (Need 59 more bytes) | ||
+ | +++++ | ||
+ | ....+++++ | ||
+ | We need to generate a lot of random bytes. It is a good idea to perform | ||
+ | some other action (type on the keyboard, move the mouse, utilize the | ||
+ | disks) during the prime generation; this gives the random number | ||
+ | generator a better chance to gain enough entropy. | ||
+ | .......+++++ | ||
+ | ....+++++ | ||
+ | gpg: /home/laurentiu/.gnupg/trustdb.gpg: trustdb created | ||
+ | gpg: key 6F2EB1B5 marked as ultimately trusted | ||
+ | public and secret key created and signed. | ||
+ | |||
+ | gpg: checking the trustdb | ||
+ | gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model | ||
+ | gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u | ||
+ | pub 2048R/6F2EB1B5 2011-11-16 | ||
+ | Key fingerprint = E465 241F FBE9 36A2 4E8E 4E0D E73F 2C5A 6F2E B1B5 | ||
+ | uid Laurentiu <laurentiu@gsr.cs.pub.ro> | ||
+ | sub 2048R/2DFDA0A8 2011-11-16 | ||
+ | </code> | ||
+ | |||
+ | *[02].b. Afișați cheile disponibile. (Hint: ''gpg --list-keys'') | ||
+ | <code> | ||
+ | laurentiu@mjolnir:~$ gpg --list-keys | ||
+ | /home/laurentiu/.gnupg/pubring.gpg | ||
+ | ---------------------------------- | ||
+ | pub 2048R/6F2EB1B5 2011-11-16 | ||
+ | uid Laurentiu <laurentiu@gsr.cs.pub.ro> | ||
+ | sub 2048R/2DFDA0A8 2011-11-16 | ||
+ | </code> | ||
+ | |||
+ | *[02].c. Exportați cheia publică generată anterior în format armor în cadrul fișierului ''laur.pub''. (Hint: ''gpg --export'') | ||
+ | <code> | ||
+ | laurentiu@mjolnir:~$ gpg --export --armor --output laur.pub 6F2EB1B5 | ||
+ | |||
+ | laurentiu@mjolnir:~$ ls -l | ||
+ | total 4 | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1723 Nov 16 22:21 laur.pub | ||
+ | </code> | ||
+ | |||
+ | *[03]. Autentificat ca ''mihai'': | ||
+ | *[03].a. Copiați fișierul ''laur.pub'' din home-ul utilizatorului ''laurentiu'' în home-ul propriu. | ||
+ | <code> | ||
+ | mihai@mjolnir:~$ cp /home/laurentiu/laur.pub . | ||
+ | </code> | ||
+ | |||
+ | *[03].b. Importați cheia publică a utilizatorului ''laurentiu''. | ||
+ | <code> | ||
+ | mihai@mjolnir:~$ gpg --import laur.pub | ||
+ | gpg: directory `/home/mihai/.gnupg' created | ||
+ | gpg: new configuration file `/home/mihai/.gnupg/gpg.conf' created | ||
+ | gpg: WARNING: options in `/home/mihai/.gnupg/gpg.conf' are not yet active during this run | ||
+ | gpg: keyring `/home/mihai/.gnupg/secring.gpg' created | ||
+ | gpg: keyring `/home/mihai/.gnupg/pubring.gpg' created | ||
+ | gpg: /home/mihai/.gnupg/trustdb.gpg: trustdb created | ||
+ | gpg: key 6F2EB1B5: public key "Laurentiu <laurentiu@gsr.cs.pub.ro>" imported | ||
+ | gpg: Total number processed: 1 | ||
+ | gpg: imported: 1 (RSA: 1) | ||
+ | </code> | ||
+ | |||
+ | *[03].c. Listați cheile GPG proprii utilizatorului ''mihai''. | ||
+ | <code> | ||
+ | mihai@merlin:~$ gpg --list-keys | ||
+ | mihai@mjolnir:~$ gpg --list-keys | ||
+ | /home/mihai/.gnupg/pubring.gpg | ||
+ | ------------------------------ | ||
+ | pub 2048R/6F2EB1B5 2011-11-16 | ||
+ | uid Laurentiu <laurentiu@gsr.cs.pub.ro> | ||
+ | sub 2048R/2DFDA0A8 2011-11-16 | ||
+ | </code> | ||
+ | |||
+ | *[03].d. Folosiți ''dd'' pentru a genera fișierul ''gpg-test.dat'' cu dimensiunea de 1KB. Folosiți ''/dev/urandom'' pentru input. | ||
+ | <code> | ||
+ | mihai@mjolnir:~$ dd if=/dev/urandom of=gpg-test.dat bs=1K count=1 | ||
+ | 1+0 records in | ||
+ | 1+0 records out | ||
+ | 1024 bytes (1.0 kB) copied, 0.000195263 s, 5.2 MB/s | ||
+ | </code> | ||
+ | |||
+ | *[03].e. Folosiți GPG pentru a cripta fișierul ''gpg-test.dat'' în fișierul de ieșire ''gpg-test.enc''. Folosiți cheia/UID-ul utilizatorului ''laurentiu''. | ||
+ | <code> | ||
+ | mihai@mjolnir:~$ gpg --encrypt -r "Laurentiu <laurentiu@gsr.cs.pub.ro>" --output gpg-test.enc gpg-test.dat | ||
+ | gpg: 2DFDA0A8: There is no assurance this key belongs to the named user | ||
+ | |||
+ | pub 2048R/2DFDA0A8 2011-11-16 Laurentiu <laurentiu@gsr.cs.pub.ro> | ||
+ | Primary key fingerprint: E465 241F FBE9 36A2 4E8E 4E0D E73F 2C5A 6F2E B1B5 | ||
+ | Subkey fingerprint: FFF4 0D0A 97E8 46D6 7625 AC3F 4EEA 38B4 2DFD A0A8 | ||
+ | |||
+ | It is NOT certain that the key belongs to the person named | ||
+ | in the user ID. If you *really* know what you are doing, | ||
+ | you may answer the next question with yes. | ||
+ | |||
+ | Use this key anyway? (y/N) y | ||
+ | |||
+ | mihai@mjolnir:~$ ls -l | ||
+ | total 12 | ||
+ | -rw-r--r-- 1 mihai mihai 1024 Nov 16 22:25 gpg-test.dat | ||
+ | -rw-r--r-- 1 mihai mihai 1373 Nov 16 22:25 gpg-test.enc | ||
+ | -rw-r--r-- 1 mihai mihai 1723 Nov 16 22:23 laur.pub | ||
+ | </code> | ||
+ | |||
+ | *[03].f. Folosiți GPG pentru a cripta fișierul în format armor cu ieșirea în ''gpg-test-armor.enc''. Folosiți cheia/UID-ul utilizatorului ''laurentiu''. | ||
+ | <code> | ||
+ | mihai@mjolnir:~$ gpg --encrypt -r "Laurentiu <laurentiu@gsr.cs.pub.ro>" --armor --output gpg-test-armor.enc gpg-test.dat | ||
+ | gpg: 2DFDA0A8: There is no assurance this key belongs to the named user | ||
+ | |||
+ | pub 2048R/2DFDA0A8 2011-11-16 Laurentiu <laurentiu@gsr.cs.pub.ro> | ||
+ | Primary key fingerprint: E465 241F FBE9 36A2 4E8E 4E0D E73F 2C5A 6F2E B1B5 | ||
+ | Subkey fingerprint: FFF4 0D0A 97E8 46D6 7625 AC3F 4EEA 38B4 2DFD A0A8 | ||
+ | |||
+ | It is NOT certain that the key belongs to the person named | ||
+ | in the user ID. If you *really* know what you are doing, | ||
+ | you may answer the next question with yes. | ||
+ | |||
+ | Use this key anyway? (y/N) y | ||
+ | |||
+ | mihai@mjolnir:~$ ls -l | ||
+ | total 16 | ||
+ | -rw-r--r-- 1 mihai mihai 1957 Nov 16 22:26 gpg-test-armor.enc | ||
+ | -rw-r--r-- 1 mihai mihai 1024 Nov 16 22:25 gpg-test.dat | ||
+ | -rw-r--r-- 1 mihai mihai 1373 Nov 16 22:25 gpg-test.enc | ||
+ | -rw-r--r-- 1 mihai mihai 1723 Nov 16 22:23 laur.pub | ||
+ | </code> | ||
+ | |||
+ | *[04]. Autentificat ca ''laurentiu'': | ||
+ | *[04].a. Copiați fișierele ''gpg-test.dat'', ''gpg-test.enc'' și ''gpg-test-armor.enc'' din home-ul utilizatorului ''mihai''. | ||
+ | <code> | ||
+ | laurentiu@mjolnir:~$ cp /home/mihai/gpg-test* . | ||
+ | </code> | ||
+ | |||
+ | *[04].b. Folosiți GPG pentru a decripta fișierul ''gpg-test.enc'' în fișierul de ieșire ''gpg-test.dec'' folosind cheia/UID-ul creat. Comparați (folosind ''diff'' sau ''cmp'') fișierul ''gpg-test.dec'' și ''gpg-test.dat''. | ||
+ | <code> | ||
+ | laurentiu@mjolnir:~$ gpg --decrypt -r "Laurentiu <laurentiu@gsr.cs.pub.ro>" --output gpg-test.dec gpg-test.enc | ||
+ | |||
+ | You need a passphrase to unlock the secret key for | ||
+ | user: "Laurentiu <laurentiu@gsr.cs.pub.ro>" | ||
+ | 2048-bit RSA key, ID 2DFDA0A8, created 2011-11-16 (main key ID 6F2EB1B5) | ||
+ | |||
+ | gpg: encrypted with 2048-bit RSA key, ID 2DFDA0A8, created 2011-11-16 | ||
+ | "Laurentiu <laurentiu@gsr.cs.pub.ro>" | ||
+ | |||
+ | laurentiu@mjolnir:~$ ls -l | ||
+ | total 20 | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1957 Nov 16 22:27 gpg-test-armor.enc | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1024 Nov 16 22:27 gpg-test.dat | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1024 Nov 16 22:28 gpg-test.dec | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1373 Nov 16 22:27 gpg-test.enc | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1723 Nov 16 22:21 laur.pub | ||
+ | |||
+ | laurentiu@mjolnir:~$ diff gpg-test.dat gpg-test.dec | ||
+ | |||
+ | laurentiu@mjolnir:~$ cmp gpg-test.dat gpg-test.dec | ||
+ | </code> | ||
+ | |||
+ | *[04].c. Executați pasul anterior pentru fișierul ''gpg-test-armor.enc'' cu ieșirea în fișierul ''gpg-test-armor.dec''. | ||
+ | <code> | ||
+ | laurentiu@mjolnir:~$ gpg --decrypt -r "Laurentiu <laurentiu@gsr.cs.pub.ro>" --armor --output gpg-test-armor.dec gpg-test-armor.enc | ||
+ | |||
+ | You need a passphrase to unlock the secret key for | ||
+ | user: "Laurentiu <laurentiu@gsr.cs.pub.ro>" | ||
+ | 2048-bit RSA key, ID 2DFDA0A8, created 2011-11-16 (main key ID 6F2EB1B5) | ||
+ | |||
+ | gpg: encrypted with 2048-bit RSA key, ID 2DFDA0A8, created 2011-11-16 | ||
+ | "Laurentiu <laurentiu@gsr.cs.pub.ro>" | ||
+ | |||
+ | laurentiu@mjolnir:~$ ls -l | ||
+ | total 24 | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1024 Nov 16 22:30 gpg-test-armor.dec | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1957 Nov 16 22:27 gpg-test-armor.enc | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1024 Nov 16 22:27 gpg-test.dat | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1024 Nov 16 22:28 gpg-test.dec | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1373 Nov 16 22:27 gpg-test.enc | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1723 Nov 16 22:21 laur.pub | ||
+ | |||
+ | laurentiu@mjolnir:~$ diff gpg-test-armor.dec gpg-test.dat | ||
+ | |||
+ | laurentiu@mjolnir:~$ cmp gpg-test-armor.dec gpg-test.dat | ||
+ | </code> | ||
+ | |||
+ | *[04].d. Semnați fișierul ''gpg-test.dat'' în format armor folosind cheia/UID-ul creat. Fișierul de ieșire va fi ''gpg-test.dat.asc''. | ||
+ | <code> | ||
+ | laurentiu@mjolnir:~$ gpg --sign -r "Laurentiu <laurentiu@gsr.cs.pub.ro>" --armor --output gpg-test.dat.asc gpg-test.dat | ||
+ | gpg: WARNING: recipients (-r) given without using public key encryption | ||
+ | |||
+ | You need a passphrase to unlock the secret key for | ||
+ | user: "Laurentiu <laurentiu@gsr.cs.pub.ro>" | ||
+ | 2048-bit RSA key, ID 6F2EB1B5, created 2011-11-16 | ||
+ | |||
+ | laurentiu@merlin:~$ ls -l | ||
+ | total 28 | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1024 Nov 17 22:19 gpg-test-armor.dec | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1957 Nov 17 22:15 gpg-test-armor.enc | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1024 Nov 17 22:15 gpg-test.dat | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1933 Nov 17 22:23 gpg-test.dat.asc | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1024 Nov 17 22:17 gpg-test.dec | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1373 Nov 17 22:15 gpg-test.enc | ||
+ | -rw-r--r-- 1 laurentiu laurentiu 1719 Nov 17 21:58 laur.pub | ||
+ | </code> | ||
+ | |||
+ | *[05]. Autentificat ca ''mihai'': | ||
+ | *[05].a. Copiați fișierul ''gpg-test.dat.asc'' din home-ul utilizatorului ''laurentiu''. | ||
+ | <code> | ||
+ | mihai@mjolnir:~$ cp /home/laurentiu/gpg-test.dat.asc . | ||
+ | </code> | ||
+ | |||
+ | *[05].b. Verificați fișierul de semnătură folosind cheia importată anterior. | ||
+ | <code> | ||
+ | mihai@mjolnir:~$ gpg --verify -r "Laurentiu <laurentiu@gsr.cs.pub.ro>" --armor gpg-test.dat.asc | ||
+ | gpg: Signature made Wed 16 Nov 2011 10:31:09 PM EET using RSA key ID 6F2EB1B5 | ||
+ | gpg: Good signature from "Laurentiu <laurentiu@gsr.cs.pub.ro>" | ||
+ | gpg: WARNING: This key is not certified with a trusted signature! | ||
+ | gpg: There is no indication that the signature belongs to the owner. | ||
+ | Primary key fingerprint: E465 241F FBE9 36A2 4E8E 4E0D E73F 2C5A 6F2E B1B5 | ||
+ | </code> | ||
+ | ===== 3. Intrusion Prevention: Fail2ban ===== | ||
+ | |||
+ | *[01]. Pe **mașina virtuală** instalați ''fail2ban''. | ||
+ | <code> | ||
+ | root@heimdall:~# apt-get install fail2ban | ||
+ | [...] | ||
+ | </code> | ||
+ | |||
+ | *[02]. Pe **mașina virtuală** configurați fail2ban astfel încât (reporniți serviciul de fail2ban după realizarea configurărilor): | ||
+ | *[02].a. să fie activ pentru autentificările încercate prin ssh | ||
+ | *[02].b. să permită un număr maxim de 3 încercări | ||
+ | *[02].c. să folosească un timp de ban de 300 de secunde | ||
+ | *[02].d. Hint: ''/etc/fail2ban/jail.conf'', ''bantime'' | ||
+ | <code> | ||
+ | root@heimdall:~# cat /etc/fail2ban/jail.conf | ||
+ | [...] | ||
+ | [ssh] | ||
+ | |||
+ | enabled = true | ||
+ | port = ssh | ||
+ | filter = sshd | ||
+ | logpath = /var/log/auth.log | ||
+ | maxretry = 3 | ||
+ | bantime = 1200 | ||
+ | [...] | ||
+ | |||
+ | root@heimdall:~# /etc/init.d/fail2ban restart | ||
+ | Restarting authentication failure monitor: fail2ban. | ||
+ | </code> | ||
+ | |||
+ | *[03]. Pe mașina virtuală reconfigurați serverul de SSH astfel încât să fie permise autentificările pe bază de parolă. Eliminați restricția de autentificare doar în contul utilizatorului student. | ||
+ | <code> | ||
+ | root@heimdall:~# cat /etc/ssh/sshd_config | grep PasswordAuthentication | head -n 1 | ||
+ | PasswordAuthentication yes | ||
+ | |||
+ | root@heimdall:~# cat /etc/ssh/sshd_config | grep AllowUsers | ||
+ | |||
+ | root@heimdall:~# /etc/init.d/ssh restart | ||
+ | Restarting OpenBSD Secure Shell server: sshd. | ||
+ | </code> | ||
+ | |||
+ | *[04]. De pe **mașina fizică** încercați să vă autentificați pe **mașina virtuală** prin ssh folosind contul nrush. **Introduceți de 3 ori la rând parola utilizatorului root in mod greșit**. | ||
+ | *[04].a. De ce credeți că s-a blocat conexiunea ssh după introducerea parolei a treia oară ? | ||
+ | <code> | ||
+ | student@mjolnir:~$ ssh nrush@heimdall.local | ||
+ | nrush@heimdall.local's password: | ||
+ | Permission denied, please try again. | ||
+ | nrush@heimdall.local's password: | ||
+ | Permission denied, please try again. | ||
+ | nrush@heimdall.local's password: | ||
+ | Permission denied (publickey,password). | ||
+ | </code> | ||
+ | *[04]. Folosind ssh în mod verbose, încercați din nou să vă conectați de pe **mașina fizică** pe **mașina virtuală** folosind contul nrush. Ce observați ? | ||
+ | <code> | ||
+ | student@mjolnir:~$ ssh nrush@heimdall.local -vvv | ||
+ | OpenSSH_5.9p1 Debian-1, OpenSSL 1.0.0e 6 Sep 2011 | ||
+ | debug1: Reading configuration data /etc/ssh/ssh_config | ||
+ | debug1: /etc/ssh/ssh_config line 19: Applying options for * | ||
+ | debug2: ssh_connect: needpriv 0 | ||
+ | debug1: Connecting to heimdall.local [192.168.48.128] port 22. | ||
+ | </code> | ||
+ | *[05]. Conectați-vă la **mașina virtuală** în contul de root folosind interfața vmware. Afișați toate regulile ''iptables'' existente în acest moment. Hint: ''iptables -L''. | ||
+ | <code> | ||
+ | root@heimdall:~# iptables -L | ||
+ | Chain INPUT (policy ACCEPT) | ||
+ | target prot opt source destination | ||
+ | fail2ban-ssh tcp -- anywhere anywhere multiport dports ssh | ||
+ | |||
+ | Chain FORWARD (policy ACCEPT) | ||
+ | target prot opt source destination | ||
+ | |||
+ | Chain OUTPUT (policy ACCEPT) | ||
+ | target prot opt source destination | ||
+ | |||
+ | Chain fail2ban-ssh (1 references) | ||
+ | target prot opt source destination | ||
+ | DROP all -- mjolnir.local anywhere | ||
+ | RETURN all -- anywhere anywhere | ||
+ | </code> | ||
+ | *[06]. Ștergeți din chain-ul fail2ban-ssh prima regulă care blochează accesul stației mjolnir.local. Hint: ''iptables -D fail2ban-ssh 1'' | ||
+ | <code> | ||
+ | root@heimdall:~# iptables -D fail2ban-ssh 1 | ||
+ | </code> | ||
+ | *[07]. De pe **mașina fizică** încercați să vă autentificați din nou pe **mașina virtuală** folosind contul nrush. **Introduceți parola în mod corespunzător**. | ||
+ | <code> | ||
+ | student@mjolnir:~$ ssh nrush@heimdall.local | ||
+ | nrush@heimdall.local's password: | ||
+ | Linux heimdall 3.0.0-1-686-pae #1 SMP Sat Aug 27 16:41:03 UTC 2011 i686 | ||
+ | |||
+ | The programs included with the Debian GNU/Linux system are free software; | ||
+ | the exact distribution terms for each program are described in the | ||
+ | individual files in /usr/share/doc/ * /copyright. | ||
+ | |||
+ | Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent | ||
+ | permitted by applicable law. | ||
+ | Last login: Wed Nov 16 21:40:02 2011 from mjolnir.local | ||
+ | </code> | ||
+ | |||
+ | /* | ||
TLS (//Transport Layer Security//) reprezintă modul în care un canal TCP nesigur este securizat. TLS asigură un canal sigur (criptat) de comunicare între client și server. În general serverul își validează identitatea folosind un certificat care apoi este folosit și pentru stabilirea cheilor de criptare ale conexiunii. | TLS (//Transport Layer Security//) reprezintă modul în care un canal TCP nesigur este securizat. TLS asigură un canal sigur (criptat) de comunicare între client și server. În general serverul își validează identitatea folosind un certificat care apoi este folosit și pentru stabilirea cheilor de criptare ale conexiunii. | ||
Line 99: | Line 628: | ||
sudo tcpdump -i eth0 -n -A host google.com and tcp port 80 or tcp port 443 | sudo tcpdump -i eth0 -n -A host google.com and tcp port 80 or tcp port 443 | ||
</code> | </code> | ||
+ | |||
+ | <note important> | ||
+ | Înlocuiți ''eth0'' cu interfața activă a sistemului. | ||
+ | </note> | ||
Dacă reluăm comenzile de mai sus vom vedea că putem vizualiza cererea și răspunsul HTTP pentru conexiunea plain text (folosind ''netcat'') dar nu și pentru cererea și răspunsul HTTPS pentru conexiunea sigură. | Dacă reluăm comenzile de mai sus vom vedea că putem vizualiza cererea și răspunsul HTTP pentru conexiunea plain text (folosind ''netcat'') dar nu și pentru cererea și răspunsul HTTPS pentru conexiunea sigură. | ||
Line 112: | Line 645: | ||
===== Investigarea unui certificat la distanță ===== | ===== Investigarea unui certificat la distanță ===== | ||
- | Conectarea cu openssl s_client și obținut certificate chain-ul de la distanță și validat că e în regulă | + | După cum am observat mai sus, conectarea cu ''openssl s_client'' permite obținerea certificatelor de la distanță. Putem astfel obține un certificat la distanță folosind comanda<code> |
+ | echo | openssl s_client -connect open-source.cs.pub.ro:443 | ||
+ | </code> | ||
+ | |||
+ | Observăm că am obținut certificatul pentru ''koala.cs.pub.ro'' întrucât serverul folosește virtual hosting. Ca să activăm suportul de SNI, atunci folosim opțiunea ''-servername'':<code> | ||
+ | echo | openssl s_client -connect open-source.cs.pub.ro:443 -servername open-source.cs.pub.ro | ||
+ | </code> | ||
+ | |||
+ | Pentru a obține doar certificatul aferent serverului de la distanță (''open-source.cs.pub.ro'') folosim comanda<code> | ||
+ | echo | openssl s_client -connect open-source.cs.pub.ro:443 -servername open-source.cs.pub.ro 2> /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | ||
+ | </code> | ||
+ | |||
+ | Apoi, ca să investigăm, direct certificatul folosim comanda ''openssl x509''. Dacă ne interesează doar subiectul și datele vom folosi comanda<code> | ||
+ | echo | openssl s_client -connect open-source.cs.pub.ro:443 -servername open-source.cs.pub.ro 2> /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | openssl x509 -noout -subject -dates | ||
+ | </code> | ||
+ | |||
+ | Realizați același lucru și pentru server-ul de cursuri (''cs.curs.pub.ro'') pentru serverul ''systems.cs.pub.ro'' și pentru un alt server la alegere. Observați dacă sunt valide, care este issuer-ul și care este data de expirare. | ||
===== Generarea și semnarea unui certificat ===== | ===== Generarea și semnarea unui certificat ===== | ||
- | Crearea unei chei și a unui CSR; semnarea certificatului folosind "CA"-ul din sala de laborator (vom avea un certificat self-signed), obținerea CRT-ului. | + | Dorim să trecem prin toți pașii de generare, obținere și folosire a unui certificat. Adică: |
+ | * Alegem fiecare un nume de domeniu. | ||
+ | * Vom genera o cheie privată și o cerere de semnare (CSR: //certificate signing request//). | ||
+ | * Vom solicita crearea unui certificat pe baza CSR-ului către un CA. În cazul nostru va fi vorba de un sistem din laborator care semnează. | ||
+ | * Vom obține de la CA certificatul semnat. | ||
+ | * Vom instala și configura certificatul semnat șî cheia privată în cadrul serverelor pe care dorim să le folosim. | ||
- | ===== Verificarea unui certificat ===== | + | Pentru început fiecare dintre voi alege un nume de domeniu care să fie ''<nume>.ixlabs''. Discutați în prealabil cu asistentul ce nume să alegeți pentru a fi siguri că nu apar conflicte. ''<nume>'' poate fi prenumele vostru, o poreclă sau alt nume care vă place vouă. |
- | Verificarea că CRT-ul corespunde cheii, investigarea sa. | + | Mai jos vom folosi numele ''dorel.ixlabs'' pe care să îl înlocuiți voi cu numele vostru. |
- | Verificarea CRT-ului și cheii, validat că sunt corespunzătoare, pentru primul certificat investigat (la exercițiul de mai sus). | + | Apoi creați cheia privată folosind acest nume<code> |
+ | openssl genrsa -out dorel.ixlabs.key 2048 | ||
+ | </code> | ||
+ | |||
+ | După ce am generat cheia o putem investiga folosind comenzi precum<code> | ||
+ | openssl rsa -in dorel.ixlabs.key -noout -text | ||
+ | openssl rsa -in dorel.ixlabs.key -noout -modulus | ||
+ | </code> | ||
+ | |||
+ | Având cheia generată, acum putem genera CSR-ul aferent:<code> | ||
+ | openssl req -new -key dorel.ixlabs.key -out dorel.ixlabs.csr | ||
+ | </code> | ||
+ | Folosiți ca | ||
+ | * Organizational Name: ''Ixia'' | ||
+ | * Organizational Unit Name: ''Development'' | ||
+ | * Common Name: folosiți numele vostru de domeniu | ||
+ | Lăsați goale (dați ''Enter'') pentru ''Challenge assword'' și ''Optional Company Name''. | ||
+ | |||
+ | Apoi putem investiga CSR-ul folosind comanda<code> | ||
+ | openssl req -in dorel.ixlabs.csr -noout -text | ||
+ | </code> | ||
+ | |||
+ | Acum avem CSR-ul pe care să îl trimitem către CA pentru semnare. | ||
+ | |||
+ | Vom simula CA-ul pe un sistem din laborator. Veți copia CSR-ul pe sistemul în cauză pentru semnare. Pentru aceasta folosiți ''SSH'' cu o comandă de forma:<code> | ||
+ | scp dorel.ixlabs student@<adresa-IP-CA>:certs/ | ||
+ | </code> | ||
+ | unde ''<adresa-IP-CA>'' este adresa IP a CA-ului indicată pe tablă. | ||
+ | |||
+ | Apoi mergeți la asistent pentru a solicita semnarea certificatului și spuneți care este certificatul vostru. | ||
+ | |||
+ | După ce este semnat certificatul, copiați-l înapoi folosind tot ''SSH''. Copiați și certificatul CA-ului. | ||
+ | |||
+ | ===== Verificarea unui certificat și a cheii private ===== | ||
+ | |||
+ | Pentru a verifica faptul că certificatul obținut (să spunem că este ''dorel.ixlabs.crt'') va trebui să comparăm modulul său (al cheii publice) și modulul cheii private. | ||
+ | |||
+ | Pentru aceasta putem folosi comenzile<code> | ||
+ | openssl rsa -in dorel.ixlabs.key -noout -modulus | md5sum | ||
+ | openssl x509 -in dorel.ixlabs.crt -noout -modulus | md5sum | ||
+ | </code> | ||
+ | |||
+ | Dacă cele două rezultate corespund înseamnă că certificatul este valid și este corepunzător cheii private. Acum poate fi folosit într-o configurare pe un servere care să asigure o conexiune TLS. | ||
+ | |||
+ | Realizați verificarea certificatului pentru ''houdini.cs.pub.ro'' și a cheii private aferente folosind comenzi similare celor de mai sus. | ||
===== Configurarea HTTPS pentru un server web ===== | ===== Configurarea HTTPS pentru un server web ===== | ||
- | Configurare suport HTTPS pentru serverul web, pentru hostname-ul aferent în Apache cu certificatul generat și semnat la pasul anterior | + | Cu certificatul astfel obținut realizați o configurare a unui virtul host pe serverul Apache local (instalați-l dacă nu este instalat) cu suport HTTPS. |
+ | Pașii pe care trebuie să îi urmați sunt: | ||
+ | * Crearea unui fișier de configurare pentru hostname-ul ''dorel.ixlabs'' (sau numele aferent domeniului vostru). | ||
+ | * Crearea unui fișier de configurare pentru hostname-ul ''dorel.ixlabs'' cu suport HTTPS. | ||
+ | * Activarea acestora folosind comanda ''a2ensite''. | ||
+ | * Activarea suport-ului de TLS folosind comanda<code> | ||
+ | a2enmod ssl | ||
+ | </code> | ||
+ | * Copierea certificatului în directorul ''/etc/ssl/certs/'' și a cheii private în ''/etc/ssl/private/''. | ||
+ | * Configuarea suportului de TLS în configurarea ''dorel.ixlabs'' cu suport HTTPS. Vedeți liniile de configurare de [[https://httpd.apache.org/docs/2.4/ssl/ssl_howto.html#configexample|aici]]. | ||
+ | * Adăugarea unei intrări în ''/etc/hosts'' pentru numele ''dorel.ixlabs'' cu adresa locală (poate chiar adresa de loopback) ca să aveți rezolvat numele de domeniu. | ||
+ | * Repornirea serverului web. | ||
+ | * Încărcați în browser-ul certificatul CA-ului. | ||
+ | * Conectarea la serverul web folosind un URL de forma ''%%https://dorel.ixlabs%%'' pentru a valida configurarea corectă a serviciului. | ||
===== Configurarea IMAPS pentru un server de IMAP ===== | ===== Configurarea IMAPS pentru un server de IMAP ===== | ||
- | Instalarea și configurarea IMAPS cu certificatul generat și semnat la pasul anterior | + | Pe sistemul local instalați pachetul Courier IMAP<code> |
+ | sudo apt-get install courier-imap courier-imap-ssl | ||
+ | </code> | ||
+ | |||
+ | Configurați pentru IMAPS certificatul verificat anterior și apoi folosiți un client de e-mail pentru vă conecta la server prin IMAPS. | ||
+ | |||
+ | Folosiți și ''openssl s_client'' pentru a verifica folosirea corespunzătoare a certificatului pe server. | ||
+ | |||
+ | <note tip> | ||
+ | Puteți urmări indicațiile de [[http://www.cyberciti.biz/tips/ssl-certificate-installation-courier-imap-server.html|aici]]. | ||
+ | </note> | ||
- | /* | ||
~~SHOWSOLUTION~~ | ~~SHOWSOLUTION~~ |