Eingeschränkter Zugriff zum Server

Bevor wir unseren Server für andere Leute zur Verfügung stellen, wollen wir natürlich sichergehen, dass da nichts passieren kann und wir ihnen den Zugriff so weit als nur möglich einschränken. Zu diesem Zweck installieren wir uns das Paket RSSH, das für ‚Restricted SSH‘ steht – also etwas, wo man den SSH Zugang einschränkt.

emerge -av rssh

Damit ist die grundlegende Installation erledigt und wir widmen uns nach vollzogenem Compile der Konfiguration. Damit unsere Gentoo Installation nun von der Shell etwas weis und sie verwenden darf, registrieren wir sie in der Liste der Shells, welche sich in /etc/shells befindet. Danach folgt ein kleiner Ausflug in die Datei /etc/rssh.conf, wo sich der eigentliche Zirkus nun abspielt. Hier ist meine Beispielkonfiguration:

# This is the default rssh config file

# set the log facility. „LOG_USER“ and „user“ are equivalent.
logfacility = LOG_USER

# Leave these all commented out to make the default action for rssh to lock
# users out completely…

allowscp
allowsftp
#allowcvs
#allowrdist
#allowrsync

# set the default umask
umask = 022

# If you want to chroot users, use this to set the directory where the root of
# the chroot jail will be located.
#
# if you DO NOT want to chroot users, LEAVE THIS COMMENTED OUT.
# chrootpath = /home

# You can quote anywhere, but quotes not required unless the path contains a
# space… as in this example.
#chrootpath = „/usr/local/my chroot“

##########################################
# EXAMPLES of configuring per-user options

#user=rudy:077:00010: # the path can simply be left out to not chroot
#user=rudy:077:00010 # the ending colon is optional

#user=rudy:011:00100: # cvs, with no chroot
#user=rudy:011:01000: # rdist, with no chroot
#user=rudy:011:10000: # rsync, with no chroot
#user=“rudy:011:00001:/usr/local/chroot“ # whole user string can be quoted
#user=rudy:01″1:00001:/usr/local/chroot“ # or somewhere in the middle, freak!
#user=rudy:’011:00001:/usr/local/chroot‘ # single quotes too

# if your chroot_path contains spaces, it must be quoted…
# In the following examples, the chroot_path is „/usr/local/my chroot“
#user=rudy:011:00001:“/usr/local/my chroot“ # scp with chroot
#user=rudy:011:00010:“/usr/local/my chroot“ # sftp with chroot
#user=rudy:011:00011:“/usr/local/my chroot“ # both with chroot

# Spaces before or after the ‚=‘ are fine, but spaces in chrootpath need
# quotes.
#user = „rudy:011:00001:/usr/local/my chroot“
#user = „rudy:011:00001:/usr/local/my chroot“ # neither do comments at line end

Solltet ihr euch nur dazu entschieden haben, ssh zu unterbinden und z.B. scp zu erlauben, dann ist das Thema schon so gut wie erledigt und ihr könnt den Benutzern diese Shell zuweisen.

Achtung: diese Shell NIE root zuweisen, da man sich sonst selbst aussperrt!

Unser nächstes Thema hier heisst nun die User in chroots zu bannen um ihnen die gängigen Möglichkeit des Missbrauchs unseres Servers zu nehmen. Dafür setzen wir den chrootpath in der rssh.conf und am Besten noch eine umask. Der Einfachheit halber möchte ich alle Benutzer in /home beheimaten. Also chroote ich auch dort.

umask = 022
chrootpath=“/home“

Wir erinnern uns: Pakete haben Abhängigkeiten, damit sie laufen. Nun gilt es diese in unsere chroot-Umgebung zu fummeln:

cd /home
mkdir -p usr/bin
cp /usr/bin/scp usr/bin
cp /usr/bin/rssh usr/bin
mkdir -p usr/libexec
cp /usr/libexec/rssh_chroot_helper usr/libexec
mkdir -p usr/lib/misc
cp /usr/lib/misc/sftp-server usr/lib/misc

Damit Dinge wie SCP auch korrekt laufen müssen diese auf ihre Abhängigkeiten hin überprüft werden. Dies geschieht am Besten mit ldd:

ldd /usr/bin/scp 
        libutil.so.1 => /lib/libutil.so.1 (0x4001c000) 
        libz.so.1 => /usr/lib/libz.so.1 (0x4001f000) 
        libnsl.so.1 => /lib/libnsl.so.1 (0x4002d000) 
        libcrypto.so.0.9.6 => /usr/lib/libcrypto.so.0.9.6 (0x40042000) 
        libc.so.6 => /lib/libc.so.6 (0x40106000) 
        libdl.so.2 => /lib/libdl.so.2 (0x40235000) 
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

Da SCP ohne diese Abhängigkeiten nicht läuft, kopieren wir die Abhängigkeiten. Theoretisch könnte man genauso gut auch Symlinks setzen, doch rate ich davon ab, da man damit die Abschottung der chroots wieder durchbricht und somit am echten System Schaden anrichten könnte und der ganze Aufwand des chroots somit umsonst wäre.

cd /home
mkdir lib
cp /lib/libutil.so.1 lib
cp /lib/libnsl.so.1 lib
cp /lib/libc.so.6 lib
cp /lib/libdl.so.2 lib
cp /lib/ld-linux.so.2 lib
mkdir -p usr/lib
cp /usr/lib/libz.so.1 usr/lib
cp /usr/lib/libcrypto.so.0.9.6 usr/lib

Die selbe Übung gilt es nun auch noch für die anderen Dateien zu machen, wie rssh, rssh_chroot_helper, den sftp_server und was noch alles in dem chroot angeboten werden soll. Dann wird es für unseren Testuser ernst, wenn wir ihm die Shell zuweisen: usermod -s /usr/bin/rssh testuser

Unser Testlauf:

sftp testuser@example.com
Connecting to example.com…
testuser@example.com’s password:
sftp> ls
.
..
.bash_profile
.bashrc
sftp> pwd
Remote working directory: /testuser
sftp> exit

Und nun das verbotene SSH:

ssh testuser@example.com
testuser@example.com’s password:

This account is restricted to scp or sftp.

If you believe this is in error, please contact your system administrator.

Connection to example.com closed.

… und da geht er hin – bzw seine Verbindung. Wenn man nun den SSH Dienst oder die Shell updated, sollte man die chroots nicht vergessen, da diese sonst noch immer auf den alten Dateien und Libraries laufen, was sich negativ auf die Systemsicherheit auswirkt.

Author:

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert