wie bereits in http://www.rootforum.org/forum/viewtopi ... 5&start=15 angekündigt, findet Ihr nachfolgend eine "step-by-step"-Anleitung zur Installation von Gentoo-Hardened-2005.1 (~x86) über ein Rescuesystem auf einem "handelsüblichen RootServer". Ziel des Ganzen ist eine weitestgehend sichere Basisinstallation des Grundsystems, sowie folgender Dienste:
Datenbank
1) MySQL 5.0.18 (UTF-8, MyISAM, InnoDB)
Mailserver
1) Postfix 2.2.5 (TLS/SSL, SMTP-AUTH)
2) Courier-IMAP 4.0.4 (POP3s, IMAPs)
3) ClamSMTP + ClamAV
Webserver (in Vorbereitung)
1) Apache 2.0.55 (MPM-Prefork)
2) mod_php 5.1.1 (HardenedPHP)
HINWEIS: Dieses HowTo richtet sich ausschliesslich an erfahrenere Admins, die in der Lage sind, auftretende Probleme weitestgehend selbstständig zu lösen.
Anmerkung: Da sich die Vorgehensweise nur geringfügig von einer normalen Gentoo-Installation unterscheidet und um eine weitere Republikation des offiziellen Gentoo-Handbuches zu vermeiden, habe ich nur die sich vom offiziellen Gentoo-Handbuch unterscheidenden Punkte kommentiert. Selbstverständlich bin auch ich kein perfekter Mensch, weshalb ich ab diesem Punkt davon ausgehe, dass der geneigte Leser das offizielle Gentoo-Handbuch sowohl gelesen, als auch verstanden hat und es zudem als Referenz bereithält.
Vorbereiten der Installation:
Formatieren und mounten der Festplatte:
Anmerkung: Da Jeder Admin andere Präferenzen an sein Partitionslayout stellt, beschränke ich mich hier auf das äusserste Minimum.
Code: Select all
# /dev/hda1 /boot ( 32MB)
# /dev/hda2 swap (512MB)
# /dev/hda3 / (xxxMB)
mke2fs /dev/hda1
tune2fs -c 0 -i 0 /dev/hda1
mke2fs -j /dev/hda3
tune2fs -c 0 -i 0 /dev/hda3
tune2fs -O dir_index /dev/hda3
tune2fs -o journal_data /dev/hda3
e2fsck -D /dev/hda3
mkswap /dev/hda2
swapon /dev/hda2
mkdir -p /mnt/gentoo
mount -t ext3 -o noatime /dev/hda3 /mnt/gentoo
mkdir -p /mnt/gentoo/boot
mount -t ext2 -o noatime /dev/hda1 /mnt/gentoo/boot
Installation des Hardened-Stage3-Tarballs:
Code: Select all
cd /mnt/gentoo
wget http://gentoo.osuosl.org/releases/x86/2005.1/stages/x86/hardened/stage3-x86-hardened-2.6-2005.1.tar.bz2
tar -xpvf /mnt/gentoo/stage3-x86-hardened-2.6-2005.1.tar.bz2 -C /mnt/gentoo/
cd ~
Kopieren der resolv.conf, /proc mounten und chroot ins Basissystem:
Code: Select all
cp -L /etc/resolv.conf /mnt/gentoo/etc/resolv.conf
mount -t proc none /mnt/gentoo/proc
chroot /mnt/gentoo /bin/bash
ENV aktualisieren und portage konfigurieren:
Code: Select all
env-update
source /etc/profile
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
mkdir -p /var/log/portage
mkdir -p /usr/local/portage
mkdir -p /etc/portage/profile
touch /etc/portage/package.use
touch /etc/portage/package.mask
touch /etc/portage/package.unmask
touch /etc/portage/package.keywords
touch /etc/portage/profile/virtuals
touch /etc/portage/profile/package.provided
touch /etc/portage/rsync_excludes
cat > /etc/portage/package.use << "EOF"
dev-db/mysql -berkdb
dev-lang/php -ncurses -readline -sasl -threads
dev-libs/cyrus-sasl -gdbm -mysql
mail-mta/postfix -mysql
net-libs/courier-authlib -gdbm -mysql
net-mail/courier-imap -gdbm
net-misc/openssh -pam
net-www/apache static-modules -threads
EOF
/etc/make.conf anlegen:
Code: Select all
cat > /etc/make.conf << "EOF"
ACCEPT_KEYWORDS="~x86"
CHOST="i686-pc-linux-gnu"
LDFLAGS="-Wl,-O1"
CFLAGS="-O2 -pipe -march=pentium4 -fomit-frame-pointer -fforce-addr"
CXXFLAGS="-O2 -pipe -march=pentium4 -fomit-frame-pointer -fforce-addr"
GENTOO_MIRRORS="ftp://ftp.mesh-solutions.com/gentoo ftp://ftp6.uni-muenster.de/pub/linux/distributions/gentoo http://distfiles.gentoo.org"
USE="acl aio apache2 authdaemond bcmath berkdb bzip2 bzlib cairo calendar caps chroot cli crypt curl dba dlloader dynagraph erandom fam ftp gd gdbm geoip gif glibc-omitfp gmp graphviz hardened hardenedphp iconv idn imagemagick imap innodb ithreads jpeg libedit libwww lzo lzw lzw-tiff maildir md5sum memlimit mhash mime mmap mpm-prefork mysql mysqli ncurses nls nocd no-suexec nptl pam pcntl pcre pear perl pic png posix postfix pwdb python readline sasl session sharedmem simplexml soap sockets spf spl srp srs ssl symlink sysfs sysvipc szip tcpd threads tiff tokenizer truetype ucs2 udev unicode urandom userlocales utf8 vda xml xml2 xmlrpc xsl zlib"
PORTAGE_NICENESS="3"
PORTDIR="/usr/portage"
PORTAGE_TMPFS="/dev/shm"
PORTAGE_TMPDIR="/var/tmp"
PKGDIR="/usr/portage/packages"
PORT_LOGDIR="/var/log/portage"
DISTDIR="/usr/portage/distfiles"
PORTDIR_OVERLAY="/usr/local/portage"
RSYNC_EXCLUDEFROM="/etc/portage/rsync_excludes"
FETCHCOMMAND="/usr/bin/wget -t 3 --passive-ftp -P ${DISTDIR} ${URI}"
RESUMECOMMAND="/usr/bin/wget -c -t 3 --passive-ftp -P ${DISTDIR} ${URI}"
FEATURES="autoconfig distlocks sandbox sfperms strict"
AUTOCLEAN="yes"
LINGUAS="en"
EOF
Portage-Tree initialisieren:
Code: Select all
emerge --sync
###FIXME### Es kann vorkommen, dass sich portage beim Initialisieren seines Caches mit folgender Fehlermeldung aufhängt:
###FIXME###
###FIXME### >>> Updating Portage cache:
###FIXME### Traceback (most recent call last):
###FIXME### File "/usr/bin/emerge", line 2705, in ?
###FIXME### oldcat = portage.catsplit(cp_list[0])[0]
###FIXME### IndexError: list index out of range
###FIXME###
###FIXME### Um die Initialisierung des Caches dennoch durchzuführen, bedarf es folgenden Befehls:
emerge --metadata
ENV aktualisieren und userlocales (glibc) festlegen:
Code: Select all
env-update
source /etc/profile
cat > /etc/locales.build << "EOF"
en_US/ISO-8859-1
en_US.UTF-8/UTF-8
EOF
Um unser Ziel ereichen zu können, müssen wir nun das komplette Basissystem rekompilieren/aktualisieren. Dieser Vorgang ist zwar sehr Zeitaufwendig, aber zwingend notwendig. Auch die Reihenfolge der Aktualisierungen ist wichtig, da das System ansonsten unbrauchbar wird und nur mit hohem Aufwand wieder repariert werden kann.
Kleine Empfehlung meinerseits: man nohup
Los geht's:
Code: Select all
emerge -pDNv sandbox portage
etc-update
emerge binutils-config gcc-config gnuconfig linux-headers glibc
emerge binutils
source /etc/profile
emerge libstdc++-v3 gcc
gcc-config 6
source /etc/profile
###FIXME### fix_libtool_files.sh 3.3.5
# System aufräumen:
rm -rf /usr/{,bin/,lib/ccache/bin/,share/{binutils,gcc}-data/}i386-pc-linux-gnu*
# /etc/make.conf aktualisieren:
sed 's/^(CFLAGS=".*)"$/1 -ftracer"/' -i /etc/make.conf
sed 's/^(CXXFLAGS=".*)"$/1 -ftracer -fvisibility-inlines-hidden"/' -i /etc/make.conf
env-update
source /etc/profile
emerge glibc binutils libstdc++-v3 gcc
source /etc/profile
emerge gdbm python
python-updater
emerge -P python
# System aufräumen:
rm -rf /usr/lib/python2.3
emerge perl
perl-cleaner all
# System aufräumen:
rm -rf /usr/lib/perl5/5.8.6
emerge -De system
emerge -P gcc
Aktualisieren der Konfugurationsdateien:
Anmerkung: Da wir bisher noch keine System-Konfiguration, abgesehen von portage, vorgenommen haben, übernehmen wir ersteinmal alle Konfigurationsänderungen, indem wir die Frage mit -3 beantworten.
Code: Select all
etc-update
Um sicherzustellen, dass wir keine möglicherweise veralteten, speicheresistenten Bins/Libs nutzen, verlassen wir kurz unsere Chroot-Umgebung und öffnen eine Neue:
Code: Select all
exit
chroot /mnt/gentoo /bin/bash
env-update
source /etc/profile
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
Wir müssen unser System nun ein zweites Mal vollständig rekompilieren, damit auch wirklich jedes Paket nur noch gegen die aktuell vorhanden Libs gelinkt ist und somit keine veralteten und/oder nicht mehr vorhandenen Funktionen nutzt:
Anmerkung: Wer auf linuxthreads (glibc) verzichten kann, der kann jetzt seine USE-Flags um "nptlonly" erweitern und spart nebenbei etwas Zeit beim Rekompilieren der glibc.
Code: Select all
emerge -De world
Gentoolkit installieren:
Code: Select all
emerge gentoolkit
/etc/fstab erstellen:
Code: Select all
cat > /etc/fstab << "EOF"
/dev/hda1 /boot ext2 defaults,noatime 1 2
/dev/hda2 none swap sw 0 0
/dev/hda3 / ext3 defaults,noatime 0 1
none /proc proc defaults 0 0
none /dev/shm tmpfs nodev,nosuid,noexec 0 0
EOF
Netzwerk konfigurieren:
Code: Select all
cat > /etc/hosts << "EOF"
127.0.0.1 localhost localhost
172.31.84.225 linux.domain.tld linux
EOF
nano -w /etc/conf.d/hostname
HOSTNAME="linux"
nano -w /etc/conf.d/domainname
DNSDOMAIN="domain.tld"
NISDOMAIN=""
nano -w /etc/conf.d/net
config_eth0=( "dhcp" )
rc-update add net.eth0 default
OpenSSH konfigurieren (Pub-Key only, non-root):
Code: Select all
cat > /etc/ssh/sshd_config << "EOF"
Port 22
Protocol 2
AddressFamily inet
PasswordAuthentication no
ChallengeResponseAuthentication no
PermitRootLogin forced-commands-only
Subsystem sftp /usr/lib/misc/sftp-server
AllowGroups wheel
EOF
rc-update add sshd default
ENV, less und man auf UTF-8 umstellen:
Code: Select all
cat > /etc/env.d/02locale << "EOF"
LC_ALL="en_US.UTF-8"
LANG="en_US.UTF-8"
EOF
cat >> /etc/env.d/70less << "EOF"
LESSCHARSET="utf-8"
EOF
nano -w /etc/man.conf
NROFF /usr/bin/nroff -mandoc -c
Wichtige Systemprogramme installieren:
Code: Select all
emerge syslog-ng
rc-update add syslog-ng default
emerge logrotate
emerge vixie-cron
rc-update add vixie-cron default
groupmod -n locate slocate
emerge slocate
emerge dhcpcd
Bootloader installieren (keine Remote-Console):
Code: Select all
emerge grub
cat > /boot/grub/grub.conf << "EOF"
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
timeout 5
default 0
title=Gentoo Linux (2005.1)
root (hd0,0)
kernel /boot/vmlinuz root=/dev/hda3
EOF
cp /proc/mounts /etc/mtab
grub --no-floppy
root (hd0,0)
setup (hd0)
quit
Bootloader installieren (mit Remote-Console):
Code: Select all
emerge grub
cat > /boot/grub/grub.conf << "EOF"
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
timeout 5
default 0
serial --unit=0 --speed=57600 --word=8 --parity=no --stop=1
terminal serial
title=Gentoo Linux (2005.1)
root (hd0,0)
kernel /boot/vmlinuz root=/dev/hda3 console=tty0 console=ttyS0,57600
EOF
cp /proc/mounts /etc/mtab
grub --no-floppy
root (hd0,0)
setup (hd0)
quit
# Remote-Console aktivieren:
cat >> /etc/inittab << "EOF"
s0:12345:respawn:/sbin/agetty -L 57600 ttyS0 vt102
EOF
Kernel installieren:
Code: Select all
emerge hardened-sources
cd /usr/src/linux
make menuconfig
make && make modules_install
cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.14-hardened-r4
cp System.map /boot/System.map-2.6.14-hardened-r4
cp .config /boot/config-2.6.14-hardened-r4
cd /boot
ln -sf vmlinuz-2.6.14-hardened-r4 vmlinuz
cd
Fehlende und/oder nützliche Pakete installieren/konfigurieren:
Code: Select all
emerge libaio sysfsutils coldplug udev fam ntp ed expect
emerge zip gnupg gmp mm utempter mime-types lynx lsof
emerge libgcrypt libmcrypt mcrypt bind-tools app-admin/sudo
nano -w /etc/conf.d/portmap
PORTMAP_OPTS="-l"
nano -w /etc/conf.d/ntp-client
NTPCLIENT_OPTS=" -b -s -p 4 -t 0.1 ptbtime1.ptb.de"
rc-update add famd default
rc-update add portmap default
rc-update add coldplug boot
rc-update add hotplug default
rc-update add ntp-client default
cat > /etc/cron.hourly/ntpdate << "EOF"
#!/bin/sh
/usr/sbin/ntpdate -b -s -p 4 -t 0.1 ptbtime1.ptb.de
EOF
chmod 0755 /etc/cron.hourly/ntpdate
/etc/sysctl konfigurieren:
Code: Select all
nano -w /etc/sysctl.conf
net.ipv4.ip_forward = 0
net.ipv4.ip_dynaddr = 0
net.ipv4.tcp_ecn = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
Grundlegende Limits setzen:
Code: Select all
cat >> /etc/security/limits.conf << "EOF"
* soft core 0
* hard core 0
* hard nproc 15
* hard rss 10000
* - maxlogins 2
EOF
cat >> /etc/limits << "EOF"
* L2 C0 U15 R10000
EOF
cat >> /etc/login.access << "EOF"
-:ALL EXCEPT wheel sync:console
EOF
Arbeitsuser anlegen und root-Paswort setzen:
Code: Select all
passwd root
useradd admin -m -k /etc/skel -g users -G cron,tty,wheel -d /home/admin -s /bin/bash
passwd admin
su - admin
mkdir ~/.ssh
nano -w ~/.ssh/authorized_keys
# @Home: ssh-keygen -t rsa
# Public-SSH-Key hier einfügen
chmod 0600 ~/.ssh/authorized_keys
exit
Reboot ins neue System:
Code: Select all
exit
umount /mnt/gentoo/proc
umount /mnt/gentoo/boot
umount /mnt/gentoo
reboot
Fortsetzung: http://www.rootforum.org/forum/viewtopi ... 263#240263
Gruss,
Joe User
PS: Kritik und Verbesserungsvorschläge sind selbstverständlich erwünscht.