SSH

Server authentication


One of the first things that happens when the SSH connection is being established is that the server sends its public key to the client, and proves (thanks to public-key cryptography) to the client that it knows the associated private key. This authenticates the server: if this part of the protocol is successful, the client that the server is who it pretends it is.

The client may check that the server is a known one, and not some rogue server trying to pass off as the right one. SSH provides only a simple mechanism to verify the server's legitimacy: it remembers servers you've already connected to, in the ~/.ssh/known_hosts file on the client machine (there's also a system-wide file /etc/ssh/known_hosts). The first time you connect to a server, you need to check by some other means that the public key presented by the server is really the public key of the server you wanted to connect to. If you have the public key of the server you're about to connect to, you can add it to ~/.ssh/known_hosts on the client manually. Authenticating the server has to be done before you send any confidential data to it. In particular, if the user authentication involves a password, the password must not be sent to an unauthenticated server.



User authentication


The server only lets a remote user log in if that user can prove that they have the right to access that account. Depending on the server's configuration and the user's choice, the user may present one of several forms of credentials (the list below is not exhaustive).

The user may present the password for the account that he is trying to log into; the server then verifies that the password is correct. The user may present a public key and prove that he possesses the private key associated with that public key. This is exactly the same method that is used to authenticate the server, but now the user is trying to prove its identity and the server is verifying it. The login attempt is accepted if the user proves that he knows the private key and the public key is in the account's authorization list (~/.ssh/authorized_keys on the server). Another type of method involves delegating part of the work of authenticating the user to the client machine. This happens in controlled environments such as enterprises, when many machines share the same accounts. The server authenticates the client machine by the same mechanism that is used the other way round, then relies on the client to authenticate the user.



SSH, oder Secure SHell ist ein Programm und ein Netzwerkprotokoll. Mit SSH ist es zum Beispiel möglich, sich auf einem anderen Computer einzuloggen und dort Programme auszuführen. SSH ermöglicht eine sichere Verbindung zwischen zwei Rechnern durch Authentifizierung und Verschlüsselung.

Putty ist ein SSH-Client mit dem ein Verbindung zum Remot Host aufgebaut wird. Dort kann dann in einem Terminalfenster wie auf dem Lokalen Host gearbeitet werden.

Mit Hilfe von scp (Secure CoPy) können Dateien vom und zum lokalen Host kopiert werden

detlef@detlef:/backup/1blu$ scp ftp12345-6789012@www.detlefhahn.de:chang.txt  .
ftp12345-6789012@www.detlefhahn.de's password: 
chang.txt  

Leider muß dabei dass Kennwort des Remote Hoste angegeben werden. so dass sich das nicht für scripts eignet, da dann dort das Kennwort im Klartext steht.
Um das Kennwort zu übergeben muss ggf. sshpass installiert werden.

root@detlef:aptitude search sshpass
p   sshpass      - Non-interactive ssh password authentication     
root@detlef: aptitude install sshpass
Die folgenden NEUEN Pakete werden zusätzlich installiert:
  sshpass 
0 Pakete aktualisiert, 1 zusätzlich installiert, 0 werden entfernt und 27 nicht aktualisiert.
11,2 kB an Archiven müssen heruntergeladen werden. Nach dem Entpacken werden 65,5 kB zusätzlich belegt sein.
Holen: 1 http://ftp.de.debian.org/debian/ wheezy/main sshpass amd64 1.05-1 [11,2 kB]
11,2 kB wurden in 0 s heruntergeladen (118 kB/s)
Vormals nicht ausgewähltes Paket sshpass wird gewählt.
(Lese Datenbank ... 114942 Dateien und Verzeichnisse sind derzeit installiert.)
Entpacken von sshpass (aus .../sshpass_1.05-1_amd64.deb) ...
Trigger für man-db werden verarbeitet ...
sshpass (1.05-1) wird eingerichtet ...
                                                    
root@detlef:~> 


sshpass -f file_with_password ssh user@server ls -la

Damit wird eine ssh Verbindung zum Server aufgebaut und das Kommando ls -la ausgeführt.
Sie müssen aber vorher (einmalig) einen manuellen ssh-Login zu dem Server gemacht haben, damit die Adresse in der Datei ~/.ssh/known_hosts aufgenommen wird. Ansonsten wird sshpass nicht arbeiten.

sshpass -p "password" scp -r user@example.com:/some/remote/path /some/local/path
Wenn Sie sich von Windows , die Putty Version von scp ("pscp") erlabt ihnen das Password mit dem Parameter -pw anzugebn.
http://www.how2s.org/index.php/Howto_use_scp_without_password_prompt_%28batch_mode%2C_scp_-B%29
Assume you want to use a script/cron job that transfers data via scp from server AAA to server BBB, 
you would not want to have the password prompt all the time.

You need access to both servers, then exchange the public keys, in order to be able to access 
server BBB from server AAA without being prompted for the password.

This is how it works. Assume you are user FOO on server AAA and user BAR on server BBB.

Log into server AAA as FOO and enter

ssh-keygen -d

do not enter any passphrase. This will generate a couple of files in the folder ~/.ssh/

SCP the file rsa_id.pub (this is your public key) to the SERVER BBB:

scp ~/.ssh/id_dsa.pub BAR@BBB:/home/BAR/.ssh/authorized_keys2

ATTENTION: if authorized_keys2 already exists on server BBB, append the content of id_dsa.pub to the existing file!

Do the same vice versa (if you want access to both)

This now allows you to transfer files from one server to the other without being asked for the password. For example:

FOO@AAA:/my/project/folder$ find . -type f -ctime -10 -print -exec scp -B '{}' BAR@BBB:/my/project/folder/'{}' ';'

this syncs me all files in folders and subfolders created within the last 10 days to the 
same folder structure from server AAA to server BBB 


Im Hidden Verzeichnis .ssh des Users
detlef@detlef:~$ ll .ssh
insgesamt 20
-rw------- 1 detlef detlef  668 Feb  2 13:56 id_dsa
-rw-r--r-- 1 detlef detlef  603 Feb  2 13:56 id_dsa.pub
-rw------- 1 detlef detlef 1679 Feb  2 13:54 id_rsa
-rw-r--r-- 1 detlef detlef  395 Feb  2 13:54 id_rsa.pub
-rw-r--r-- 1 detlef detlef  884 Feb  2 12:34 known_hosts
detlef@detlef:

Meinen Public Key anzeigen
detlef@detlef:~$ ssh-keygen -l
Enter file in which the key is (/home/detlef/.ssh/id_rsa): 
2048 a7:31:49:db:91:77:0e:fa:14:76:38:1c:8d:74:4b:e5  detlef@detlef (RSA)
detlef@detlef:~$ 

detlef@detlef:~$ ssh-keygen -l
Enter file in which the key is (/home/detlef/.ssh/id_rsa): .ssh/id_dsa 
1024 f4:e2:4a:18:bd:8d:ba:3b:f4:f6:6b:99:7c:c4:1a:1e  detlef@detlef (DSA)
detlef@detlef:~$ 


Auch für den Host existieren Keyfiles
detlef@detlef:$ cd /etc/ssh 
detlef@detlef:/etc/ssh$ ssh-keygen -l 
Enter file in which the key is (/home/detlef/.ssh/id_rsa): ./ssh_host_rsa_keyb>
2048 fa:ec:a6:db:ca:e2:e9:dc:94:68:48:16:0a:77:5a:41  root@detlef (RSA)
detlef@detlef:/etc/ssh$ 
  
detlef@detlef:/etc/ssh$ ssh-keygen -l 
Enter file in which the key is (/home/detlef/.ssh/id_rsa): ./ssh_host_dsa_key
1024 af:a7:56:9c:05:92:50:13:1f:b1:f4:90:c1:3a:84:20  root@detlef (DSA)
detlef@detlef:/etc/ssh$ 

Der SSH-Agent


So weit, so gut. Praktisch wäre es allerdings, wenn man nicht jedesmal die "pass phrase" eingeben müsste. Dann würde die Verwendung von Public-Keys nicht nur zur Sicherheit beitragen, sondern auch zur Bequemlichkeit (selten genug, dass sich das beides unter einen Hut bringen lässt).


Hier kommt der SSH-Agent ins Spiel. Im SSH-Agenten kann man seine(n) privaten Schlüssel ablegen, so dass sich der Agent von nun an um die Authentifizierung kümmert. Man muss also nur einmal seine "pass phrase" angeben, und der Agent behält dann den Schlüssel bis zu seiner Beendigung (normalerweise beim Abmelden des Benutzers ("logout")) im Speicher. Der SSH-Agent wird bei einer Ubuntu-Desktop-Sitzung automatisch im Hintergrund gestartet. Zur Interaktion mit dem SSH-Agenten dient das Programm ssh-add, wobei die Option -l die augenblicklich gespeicherten Schlüssel auflistet:

detlef@detlef:~$ ssh-add -l
The agent has no identities.
detlef@detlef:~$ ssh-add
Identity added: /home/detlef/.ssh/id_rsa (/home/detlef/.ssh/id_rsa)
Identity added: /home/detlef/.ssh/id_dsa (/home/detlef/.ssh/id_dsa)
detlef@detlef:~$ ssh-add -l
2048 a7:31:49:db:91:77:0e:fa:14:76:38:1c:8d:74:4b:e5 /home/detlef/.ssh/id_rsa (RSA)
1024 f4:e2:4a:18:bd:8d:ba:3b:f4:f6:6b:99:7c:c4:1a:1e /home/detlef/.ssh/id_dsa (DSA)
detlef@detlef:~$ 

Sicherheitslöcher


"Ich habe zwar einige Schlüssel im meinem Verzeichnis doch weiß ich leider nicht ob diese sicher sind?" — Aufgrund eines Sicherheitslochs in der SSL Bibliothek wurden einige unsicher Schlüssel erzeugt. Debian bietet mit dem Paket openssh-blacklist eine Ansammlung von Fingerprints von betroffenen Schlüsseln. Um zu überprüfen ob die eigenen Schlüssel hiervon betrofen sind kann wie folgt vorgegangen werden:


detlef@detlef:~$ ssh-vulnkey -v
# /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key:1: Not blacklisted: RSA 2048 fa:ec:a6:db:ca:e2:e9:dc:94:68:48:16:0a:77:5a:41 root@detlef
# /etc/ssh/ssh_host_dsa_key
/etc/ssh/ssh_host_dsa_key:1: Not blacklisted: DSA 1024 af:a7:56:9c:05:92:50:13:1f:b1:f4:90:c1:3a:84:20 root@detlef
# /home/detlef/.ssh/id_rsa
/home/detlef/.ssh/id_rsa:1: Not blacklisted: RSA 2048 a7:31:49:db:91:77:0e:fa:14:76:38:1c:8d:74:4b:e5  detlef@detlef
# /home/detlef/.ssh/id_dsa
/home/detlef/.ssh/id_dsa:1: Not blacklisted: DSA 1024 f4:e2:4a:18:bd:8d:ba:3b:f4:f6:6b:99:7c:c4:1a:1e  detlef@detlef
#
# No blacklisted keys!
detlef@detlef:~$ 

known_hosts


Wie finde ich heraus ob ich unsicheren Rechnern vertraue? Die Vertrauenswürdigkeit eines Systems wird durch die Ablage des Schlüssels in die ~/.ssh/known_hosts Datei hergestellt. Wenn der Schlüssel dort eingetragen ist, wird keine Warnung mehr von SSH herausgegeben falls eine Verbindung mit dem System hergestellt wird. Es kann durchaus passieren dass die Datei known_hosts alte Einträge enthält welche mittlerweile als unsicher gelten. Dies lässt sich wie folgt herausfinden:


detlef@detlef:~$  cat ./.ssh/known_hosts | ssh-vulnkey -v -
-:1: Not blacklisted: RSA 2048 13:b6:98:13:44:c2:45:bd:92:84:c3:f9:1f:4c:b2:6c -
-:2: Not blacklisted: RSA 2048 13:b6:98:13:44:c2:45:bd:92:84:c3:f9:1f:4c:b2:6c -
#
# No blacklisted keys!
detlef@detlef:~$ 

Zeige alle unsicheren Fingerprints der Datei known_hosts: detlef@detlef:~$ cat ./.ssh/known_hosts | ssh-vulnkey -v - | grep COMPROMISED |cut -f5 -d " " | uniq ACHTUNG die nachfolgende CODE Zeile entfernt Vertraunswürdige Hosteinträge in der known_hosts Datei

detlef@detlef:~$ for i in `cat ~/.ssh/known_hosts | ssh-vulnkey -v - | grep COMPROMISED |cut -f2 -d ":" | uniq` ; do sed -i "$i d" ~/.ssh/known_hosts; done 
 ==============================================================================================
detlef@detlef:/backup/1blu$ ssh -o stricthostkeychecking=ask ftp12345-678912@ www.detlefhahn.de
ssh: Could not resolve hostname : Name or service not known
 
detlef@detlef:/backup/1blu$ ssh -o stricthostkeychecking=ask ftp12345-678912@www.detlefhahn.de
ftp12345-678912@www.detlefhahn.de's password: 
Permission denied, please try again.
ftp12345-678912@www.detlefhahn.de's password: 

???? evtl weil bereits Putty Verbindung vorhanden ??? Nee Blödkopp !! Weil username falsch ist

detlef@detlef:/backup/1blu$ ssh -o stricthostkeychecking=ask ftp12345-6789012@www.detlefhahn.de
ftp12345-6789012@www.detlefhahn.de's password: 
Linux sh4-17 2.6.24-31-server #1 SMP Tue Feb 14 13:12:22 UTC 2012 x86_64

The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. To access official Ubuntu documentation, please visit: http://help.ubuntu.com/

 
You have new mail.
Last login: Fri Feb 22 10:35:32 2013 from dslb-188-097-152-153.pools.arcor-ip.net
ftp12345-6789012@sh2-12:~$ 


Public Key des 1blu-Servers

ftp12345-6789012@sh2-12:~$ ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub
2048 13:b6:98:13:44:c2:45:bd:92:84:c3:f9:1f:4c:b2:6c /etc/ssh/ssh_host_rsa_key.pub
ftp12345-6789012@sh2-12:~$
----------------------------------------------------------------------------

detlef@detlef:/etc/ssh$ ssh  ftp12345-6789012@www.detlefhahn.de
ftp12345-6789012@www.detlefhahn.de's password: 
Linux sh4-17 2.6.24-31-server #1 SMP Tue Feb 14 13:12:22 UTC 2012 x86_64

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
No mail.
Last login: Fri Feb 22 10:37:05 2013 from dslb-188-097-152-153.pools.arcor-ip.net
ftp12345-6789012@sh2-12:~$ cd $HOME
ftp12345-6789012@sh2-12:~$ ls -l /etc/ssh
total 4208
-rw-r--r-- 1 root root 2064867 2008-05-13 14:36 blacklist.DSA-1024
-rw-r--r-- 1 root root 2064867 2008-05-13 14:36 blacklist.RSA-2048
-rw-r--r-- 1 root root  132777 2008-05-14 16:39 moduli
-rw-r--r-- 1 root root    1595 2008-05-14 16:39 ssh_config
-rw-r--r-- 1 root root    1909 2011-10-31 08:05 sshd_config
-rw------- 1 root root     672 2011-07-21 02:31 ssh_host_dsa_key
-rw-r--r-- 1 root root     599 2011-07-21 02:31 ssh_host_dsa_key.pub
-rw------- 1 root root    1675 2011-07-21 02:31 ssh_host_rsa_key
-rw-r--r-- 1 root root     391 2011-07-21 02:31 ssh_host_rsa_key.pub



=============================================================================== Links:
http://www.online-tutorials.net/security/secure-ssh-tutorial-part-2-identity-pubkey-authentifizierung/tutorials-t-69-202.html
http://serverfault.com/questions/318474/how-to-pass-password-to-scp-command-used-in-bash-script
http://www.lysium.de/blog/index.php?/archives/186-How-to-get-ssh-server-fingerprint-information.html

http://www.ostc.de/howtos/ssh-HOWTO.txt
http://wiki.ubuntuusers.de/SSH
http://umija.org/howto%3Assh


Private Keys liegen in~/.ssh
ll -a .ssh
insgesamt 32
drwx------   2 detlef detlef   84 Feb  2  2013 .
drwxrwxr-x 108 detlef detlef 8192 Feb  8 09:47 ..
-rw-------   1 detlef detlef  668 Feb  2  2013 id_dsa
-rw-r--r--   1 detlef detlef  603 Feb  2  2013 id_dsa.pub
-rw-------   1 detlef detlef 1679 Feb  2  2013 id_rsa
-rw-r--r--   1 detlef detlef  395 Feb  2  2013 id_rsa.pub
-rw-r--r--   1 detlef detlef  884 Feb  2  2013 known_hosts
detlef@detlef:~$ 

less .ssh/known_hosts
less .ssh/id_rsa.pub
less .ssh/id_rsa

detlef@detlef:~$ sudo openssl rsa -in /etc/ssh/ssh_host_rsa_key -noout -text

detlef@detlef:~$ sudo openssl dsa -in /etc/ssh/ssh_host_dsa_key -noout -text


Systemweite ssh-Files
detlef@detlef:~$ ll /etc/ssh
insgesamt 168
-rw-r--r-- 1 root root 136156 Aug 24  2012 moduli
-rw-r--r-- 1 root root   1669 Aug 24  2012 ssh_config
-rw-r--r-- 1 root root   2489 Jan  2  2013 sshd_config
-rw------- 1 root root    668 Jan  2  2013 ssh_host_dsa_key
-rw-r--r-- 1 root root    601 Jan  2  2013 ssh_host_dsa_key.pub
-rw------- 1 root root    227 Jan  2  2013 ssh_host_ecdsa_key
-rw-r--r-- 1 root root    173 Jan  2  2013 ssh_host_ecdsa_key.pub
-rw------- 1 root root   1679 Jan  2  2013 ssh_host_rsa_key
-rw-r--r-- 1 root root    393 Jan  2  2013 ssh_host_rsa_key.pub
detlef@detlef:~$ 

More details about the known_hosts file format are given in the manual page for sshd down 
in the section "SSH_KNOWN_HOSTS FILE FORMAT" 
And if you want to turn off hashing of known hosts, that is done in ~/.ssh/config. 


Keys werden erzeugt mit ssh-keygen

ssh-keygen -t rsa