587 lines
25 KiB
TeX
Executable File
587 lines
25 KiB
TeX
Executable File
\section{SSH und SFTP}
|
|
\begin{justify}
|
|
Eine Remote Verbindung mittels {\ttfamily SSH} ist einer {\ttfamily Telnet} Verbindung vorzuziehen, da im Gegensatz zu
|
|
einer {\ttfamily Telnet} Session die "Ubertragung verschl"usselt durchgef"uhrt wird.
|
|
In der nachfolgenden Tabelle ist ein Auszug der Optionen f"ur SSH (aus Linux User 11.2017).
|
|
\begin{longtable}[l]{p{3cm}p{13cm}}
|
|
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
|
\hline
|
|
\hline
|
|
\endfirsthead
|
|
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
|
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
|
\hline
|
|
\hline
|
|
\endhead
|
|
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
|
\endfoot
|
|
\caption{Optionen SSH} \\
|
|
\endlastfoot
|
|
-2 & Nur Protokollversion 2\\
|
|
-4 & Nur IPv4-Adressen\\
|
|
-6 & Nur IPv6-Adressen\\
|
|
-b IP & IP-Adresse verwenden\\
|
|
-C & Kompression verwenden\\
|
|
-F Datei & Angabe alternative Konfigurationsdatei\\
|
|
-i Datei & Angabe der Private-Key Datei\\
|
|
-p Port & Angabe der Portnummer\\
|
|
-v & Ausgabe der Meldungen\\
|
|
-t & Interaktive Men"us\\
|
|
-w Rechner:Port & Weiterleiten der Standardein- und -ausgabe vom lokalen System zum entfernten System\\
|
|
-X & Display ungesch"utzt umleiten\\
|
|
-Y & Display gesch"utzt umleiten\\
|
|
-N & F"ur Tunnelzwecke, kein Command\\
|
|
\end{longtable}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: SSH Konfiguration
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{SSH Konfiguration}
|
|
Die Konfiguration von SSH wird in der Datei {\ttfamily sshd\_config} im Verzeichnis {\ttfamily /etc/ssh} vorgenommen.
|
|
In der nachfolgenden Tabelle sind einige Optionen aufgelistet.
|
|
\begin{longtable}[l]{p{5cm}p{2cm}p{9cm}}
|
|
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Schl"ussel}} & \multicolumn {1}{l}{\textbf{Wert}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
|
\hline
|
|
\hline
|
|
\endfirsthead
|
|
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
|
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Option}} & \multicolumn {1}{l}{\textbf{Wert}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
|
\hline
|
|
\hline
|
|
\endhead
|
|
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
|
\endfoot
|
|
\caption{Optionen sshd\_config} \\
|
|
\endlastfoot
|
|
PermitRootLogin & no / yes & Root-Zugang erlauben\\
|
|
AllowGroups & Gruppe & Nur Benutzer in Gruppe d"urfen sich anmelden.\\
|
|
AllowUsers & User & Nur Benutzer User darf sich anmelden\\
|
|
PasswordAuthentication & no / yes & Unterbindet das Anmelden mit einem Passwort\\
|
|
ChallengeResponseAuthentication & no / yes & Nur "uber einen Schl"ussel anmelden\\
|
|
PermitEmptyPasswords & no / yes & Keine leeren Kennw"orter erlauben\\
|
|
Port & Nummer & Abweichenden Port festlegen\\
|
|
ListenAddress & IP & Netzwerkschnittstelle benutzen, je IP-Adresse eine Zeile\\
|
|
MaxSessions & &Auf kritischen Systemen so niedrig wie m"oglich (1)\\
|
|
MaxStartups & &Auf kritischen Systemen so niedrig wie m"oglich (1)\\
|
|
ServerAliveInterval & Sekunden & Nach welcher Zeit der SSH Server ein Null Paket sendet\\
|
|
X11Forwarding & no / yes & X11 Ausgabe aktivieren\\
|
|
\end{longtable}
|
|
\newpage
|
|
In der nachfolgenden Tabelle sind die wichtigsten Client Dateien f"ur SSH aufgelistet. Sie befinden sich
|
|
alle im Verzeichnis {\ttfamily \verb|~|/.ssh}.
|
|
\begin{longtable}[l]{p{3cm}p{13cm}}
|
|
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Name}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
|
\hline
|
|
\hline
|
|
\endfirsthead
|
|
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
|
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Name}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
|
\hline
|
|
\hline
|
|
\endhead
|
|
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
|
\endfoot
|
|
\caption{Client Dateien} \\
|
|
\endlastfoot
|
|
config & Benutzerdefinerte Einstellungen, "uberschreibt die Systemeinstellungen\\
|
|
known\_hosts & Liste der Systeme, wo schon eine Verbindung aufgebaut wurde und der Schl"ussel akzeptiert worden ist.\\
|
|
authorized\_keys & "Offentlicher Schl"ussel, wenn der Rechner als Server fungiert.\\
|
|
id\_rsa & Privater Schl"ussel\\
|
|
id\_rsa.pub & "Offentlicher Schl"ussel\\
|
|
\end{longtable}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Root Login disable
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Root Login disable}
|
|
In der Datei {\ttfamily /etc/ssh/sshd\_config} ist der Eintrag {\ttfamily PermitRootLogin} auf
|
|
{\ttfamily no} zu setzten, um das Root Login zu verbieten. F"ur {\ttfamily PermitRootLogin}
|
|
kann man folgende Parameter setzen:\\
|
|
\begin{longtable}[l]{p{3cm}p{13cm}}
|
|
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Parameter}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
|
\hline
|
|
\hline
|
|
\endfirsthead
|
|
\multicolumn{2}{r}{{\bfseries continued from previous page.}} \\
|
|
\rowcolor{hellgrau}\multicolumn{1}{l}{\textbf{Parameter}} & \multicolumn {1}{l}{\textbf{Beschreibung}} \\
|
|
\hline
|
|
\hline
|
|
\endhead
|
|
\multicolumn{2}{r}{{\bfseries Continued on next page}} \\
|
|
\endfoot
|
|
\caption{Parameter PermitRootLogin} \\
|
|
\endlastfoot
|
|
yes & Anmelden am System erlaubt\\
|
|
no & Anmelden am System nicht erlaubt\\
|
|
without-password & Das Anmelden ist ohne Passwort m"oglich\\
|
|
forced-commands-only & Anmelden mit public key m"oglich, aber nur in Verbindung mit der command Option.\\
|
|
\end{longtable}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: User Verbindungen
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{User Verbindungen}
|
|
Der Eintrag {\ttfamily AllowUsers} in der Datei {\ttfamily /etc/ssh/sshd\_config} listet alle zugelassenen User auf, die eine SSH Verbindung aufbauen d"urfen.\\
|
|
Die Syntax f"ur den Eintrag lautet: {\ttfamily AllowUsers <user1> <user2>}\\
|
|
Ebenso muss die Variable {\ttfamily PasswordAuthentication} auf {\ttfamily yes} gesetzt werden.
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: SSH Banner
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{SSH Banner}
|
|
M"ochte man bei einer SSH Verbindung ein Banner anzeigen, so muss in der Datei
|
|
{\ttfamily /etc/ssh/sshd\_config} die Zeile {\ttfamily \verb|#|Banner} auskommentiert und der Pfad zur Banner Datei
|
|
angegeben werden.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiel Banner]
|
|
uws@tux>grep Banner /etc/ssh/sshd_config
|
|
Banner /etc/ssh/sshd-banner
|
|
|
|
uws@tux>cat /etc/ssh/sshd-banner
|
|
*************************************************
|
|
WARNING:
|
|
========
|
|
Only authorized User to used this system.
|
|
Are you not authorized User, leave this session.
|
|
*************************************************
|
|
\end{lstlisting}
|
|
\newpage
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: SSH Dienst
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{SSH Dienst}
|
|
\begin{justify}
|
|
Bei jeder "Anderung an der SSH Konfiguration muss der Dienst neu gestartet werden.
|
|
\end{justify}
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Start SSH Service]
|
|
root@tux>/etc/init.d/sshd restart
|
|
|
|
root@tux>service sshd restart
|
|
|
|
root@tux>#unter SystemD
|
|
root@tux>systemctl restart sshd
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: X11 Forwarding
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{X11 Forwarding}
|
|
M"ochte man die Graphische Ausgabe auf seinem Rechner haben, so schaltet man auf dem Zielrechner das {\ttfamily X11Forwarding} ein. In der Datei {\ttfamily /etc/ssh/sshd\_config} werden die Parameter {\ttfamily X11Forwarding yes, X11DisplayOffset 10} und {\ttfamily X11UseLocalhost yes} aktiviert.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiel X11 Forwarding]
|
|
uws@tux># -X = enable, -x = disable
|
|
uws@tux>ssh -X -C uws@tux01
|
|
|
|
uws@tux01>export DISPLAY='tux:10.0'
|
|
|
|
uws@tux01>xclock &
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: SSH Key erstellen
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{SSH Key erstellen}
|
|
Einen Schl"ussel kann man mit {\ttfamily ssh-keygen} erstellen. Es werden zwei Schl"ussel erstellt, ein
|
|
{\ttfamily Privater} und ein {\ttfamily Public} Schl"ussel. Den Public Schl"ussel kann an andere Remote Systeme
|
|
gesendet werden. L"asst man bei der Passwort Abfrage das Passwort leer, so kann man sich sp"ater an
|
|
dem Remote System ohne Passwort anmelden. Kann gut in Scripte verwendet werden.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Create SSH Key]
|
|
uws@tux>ssh-keygen -t rsa -b 4096 -f ~/.ssh/Remote1.rsa -C "Remote1 connection"
|
|
\end{lstlisting}
|
|
-t => Typ des Schl"ussels, hier RSA\\
|
|
-b => L"ange des Schl"ussels, hier 4096 Bits. Je l"anger, desto schwerer zu entschl"usseln.\\
|
|
-f => Name der Schlüsseldatei\\
|
|
-C => Kommentar
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: SSH Key send
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{SSH Key senden}
|
|
Den erstellen Public Key Schl"ussel kann man mit {\ttfamily ssh-copy-id} zu den Remote Systemen "ubertragen.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Send SSH Key]
|
|
uws@tux>ssh-copy-id -i <KeyDatei> <user>@<hostname>
|
|
|
|
uws@tux>ssh-copy-id -i ~/.ssh/Remote1.rsa.pub uws@tux10
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: SSH Key entfernen
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{SSH Key entfernen}
|
|
Hat sich der SSH Key eines Remote Zieles ver"andert, so kann man den SSH Key aus der Datei
|
|
{\ttfamily known\_host} mit dem folgenden Befehlt entfernen.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Delete SSH Key]
|
|
uws@tux>ssh-keygen -R tux10 -f /home/uws/.ssh/known_hosts
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Alias
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Alias}
|
|
In der Datei {\ttfamily ~/.ssh/config} k"onnen Verbindungen definiert werden, die dann mit einem Alias aufgerufen werden. Die config-Datei darf nur f"ur den User bearbeitbar sein. Es muss unbedingt mit {\ttfamily chmod 600} auf diese Datei gemacht werden, sonst kommt es zu einer Fehlermedung. Mit {\ttfamily man ssh\verb|_|config} kann man sich Informationen über die Config-Datei anzeigen lassen.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiel Alias]
|
|
uws@tux>cat ~/.ssh/config
|
|
Host Remote1
|
|
HostName 10.81.3.16
|
|
User Carl
|
|
CheckHostIP no
|
|
IdentityFile ~/.ssh/Remote1.rsa # Schlüsseldatei
|
|
Port 2222
|
|
LocalForward 80 localhost:8080
|
|
ForwardX11 yes # make -x as default
|
|
|
|
uws@tux>ssh Remote1
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: TCP-Stealth
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{TCP-Stealth}
|
|
Wird in der Datei {\ttfamily /etc/ssh/sshd\_config} die Option {\ttfamily TCPStealthSecret} auf dem Server / Client
|
|
aktiviert, so klappt es mit der Verbindung. Ansonsten wird sie abgelehnt. Der eingetragene Wert ist ein
|
|
Autorisierung-Token. Nur wenn beide gleich sind, wird eine Verbindung ausgebaut.
|
|
Diesen Modus kann man nur einschalten, wenn auch der Kernel mit {\ttfamily TCPStealthSecret} kompiliert
|
|
worden ist.
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Match
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Match}
|
|
Mit Match kann man Parameter gezielt setzen. Entweder für User, Gruppen, Computer, Ports,
|
|
Adressen oder Local Adressen. Die Syntax eines Match Blocks ist:
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Syntax Match sshd\_config]
|
|
Match <Condition> <Value>
|
|
Parameter <new Value>
|
|
Parameter <new Value>
|
|
\end{lstlisting}
|
|
In dem Block kann man folgende Parameter setzen.
|
|
\begin{table}[ht]
|
|
\begin{tabular}{|l|l|p{12cm}|} % l => Text left, c => center, r => right, p => zeilenumbruch
|
|
%\hline \rowcolor{hellgrau}\emph{\textbf{Option}} & \emph{\textbf{Beschreibung}}\\
|
|
AcceptEnd & AllowAgentForwarding& AllowGroups\\
|
|
AllowStreamLocalForwarding & AllowTcpForwarding & AllowUsers\\
|
|
AuthenticationMethods & AuthorizedKeyCommand & AuthorizedKeyCommandUser\\
|
|
AuthorizedKeysFile & AuthorizedPrincipalsCommand & AuthorizedPrincipalsCommandUser\\
|
|
AuthorizedPrincipalsFile & Banner & ChrootDirectory\\
|
|
DenyGroups & DenyUsers & ForceCommand\\
|
|
GatewayPorts & GSSAPIAuthentication & HostbasedAcceptKeyTypes\\
|
|
HostbasedAuthentication & HostbasedUsesNameFromPacketOnly & IPQoS\\
|
|
KbdInteractiveAuthentication & KerberosAuthentication & MaxAuthTries\\
|
|
MaxSessions & PasswordAuthentication & PermitEmptyPasswords\\
|
|
PermitOpen & PermitRootLogin & PermitTTY\\
|
|
PermitTunnel & PermitUserRC & PubkeyAcceptedKeyTypes\\
|
|
PubkeyAuthentication & RekeyLimit & RevokedKeys\\
|
|
RhostsRSAAuthentication & RSAAuthentication & StreanLocalBindMask\\
|
|
StreamLocalBindUnlink & TrustedUserCAKeys & X11DisplayOffsett\\
|
|
X11Forwarding\\
|
|
\end{tabular}
|
|
\caption{Parameter Match Block}
|
|
\end{table}
|
|
\newpage
|
|
\begin{justify}
|
|
Als Beispiel wird ein Anmelden als Root ohne Passwort erlaubt und ein User muss sich mit Kennwort anmelden.
|
|
\end{justify}
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiel Match]
|
|
PermitRootLogin no
|
|
PasswordAuthentication no
|
|
AllowUsers paul root
|
|
|
|
Match Address 192.168.70.0/24,192.168.8.0/24,127.0.0.1
|
|
PermitRootLogin without-password
|
|
PasswordAuthentication yes
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Chroot Sftp
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Chroot f"ur Sftp}
|
|
Mit {\ttfamily ChrootDirectory} kann man wie in Linux eine Chroot Umgebung f"ur Sftp-User einrichten. In dieser
|
|
Umgebung k"onnen die Sftp-User nicht die anderen Verzeichnisse sehen und auch nicht dorthin wechseln.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiel SFTP Chroot]
|
|
root@tux># create group sftpuser
|
|
root@tux>groupadd sftuser
|
|
|
|
root@tux># create user paul
|
|
root@tux>useradd -d /sftp/paul -G sftpuser -s /bin/false paul
|
|
|
|
root@tux># create directories and permissions
|
|
root@tux>mkdir -p /sftp/paul
|
|
root@tux>chown -R root:root /sftp
|
|
root@tux>chmod -R 775 /sftp
|
|
root@tux>mkdir -p /sftp/paul/daten
|
|
root@tux>chown paul:sftuser /sftp/paul/daten
|
|
root@tux>chmod 775 /sftp/paul/daten
|
|
|
|
root@tux># edit /etc/ssh/sshd_config
|
|
# disable next
|
|
#Subsystem sftp /usr/lib/ssh/sftp-server
|
|
|
|
# new value
|
|
Subsystem sftp internal-sftp
|
|
|
|
# ChrootPermission root:root 775
|
|
# Directorys under Chroot, paul:sftpuser 775
|
|
Match User paul
|
|
ChrootDirectory /sftp/paul
|
|
X11Forwarding no
|
|
AllowTcpForwarding no
|
|
PermitTTY no
|
|
Banner /etc/ssh/sftp.txt
|
|
ForceCommand internal-sftp
|
|
|
|
root@tux># restart sshd service
|
|
root@tux>systemctl restart sshd
|
|
\end{lstlisting}
|
|
\newpage
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Sftp Examples
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Sftp Beispiele}
|
|
Mit SFTP kann man Daten von und zu einem anderen Rechner tranferieren.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=SFTP Beispiele]
|
|
uws@tux># Hile anzeigen
|
|
sftp>help
|
|
|
|
uws@tux># Verbindung zu Remote Host
|
|
uws@tux>sftp -P <CustomPort> <RemoteUser>@<RemoteHost>
|
|
|
|
uws@tux># Datei senden von Remote Host
|
|
sftp>put FileName.zip
|
|
|
|
uws@tux># Datei holen von Quell Host
|
|
sftp>get FileName.sh
|
|
|
|
uws@tux># Ablegen unter anderen Namen
|
|
sftp>get FileName.txt FileNameNew.txt
|
|
|
|
uws@tux># Verzeichnis rekursiv transferieren
|
|
sftp>get -r RemoteDir
|
|
|
|
uws@tux># Verzeichnis wechseln
|
|
sftp>cd /tmp
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Send Mail after login
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Login Mail}
|
|
M"ochte man eine Mail erhalten, wenn sich ein User mittels {\ttfamily ssh} auf dem Rechner angemeldet hat, so kann man das mit einem Script machen, das sich im Verzeichnis /etc/profile.d befindet. Voraussetzung ist ein Mail System wie der nullmailer. Damit identifiziert werden kann, ob das Login per ssh erfolgt ist, wird gepr"uft, ob die Variable {\ttfamily SSH\verb|_|CLIENT} nicht leer ist.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiel ssh\_mail.sh]
|
|
root@tux>cat /etc/profile.d/ssh_mail.sh
|
|
#!/bin/env bash
|
|
#
|
|
# Prerequisites: nullmailer is installed and configured
|
|
# Copy this file to /etc/profile.d - thats it
|
|
#
|
|
MailFile=/tmp/ssh_mail.txt
|
|
ssh_ip=$(echo $SSH_CLIENT | awk '{print $1}')
|
|
ssh_user=$(who -m | awk '{print $1}')
|
|
ssh_console=$(who -m | awk '{print $2}')
|
|
ssh_login_date=$(who -m | awk '{print $3}')
|
|
ssh_login_time=$(who -m | awk '{print $4}')
|
|
ssh_port=$(echo $SSH_CLIENT | awk '{print $3}')
|
|
|
|
printf "SSH Login on $(hostname)\n\n" > $MailFile
|
|
printf " User: $ssh_user\n" >> $MailFile
|
|
printf " SSH IP: $ssh_ip\n" >> $MailFile
|
|
printf " Date: $ssh_login_date $ssh_login_time\n" >> $MailFile
|
|
printf "Console: $ssh_console\n" >> $MailFile
|
|
printf " Port: $ssh_port\n" >> $MailFile
|
|
|
|
if [ -n "$SSH_CLIENT" ]
|
|
then
|
|
cat $MailFile | mail -s "Login on $(hostname) from $ssh_user" uws@seabaer-ag.de
|
|
fi
|
|
\end{lstlisting}
|
|
\newpage
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Security
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Security - Fail2ban}
|
|
\subsubsection{Installation}
|
|
Um einen Server für Brut Force Attacken abzusichern, kann man {\ttfamily fail2ban} installieren. Erfolgen mehrere abgelehnte Anfragen von einer IP-Adresse, so wird diese IP-Adresse für einen bestimmten Zeitraum auf die Black-List gesetzt. Damit fail2ban funktioniert, muss eine Firewall installiert sein. Entweder {\ttfamily iptables} oder {\ttfamily ufw}.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Install fail2ban]
|
|
uws@tux># Check iptables firewall exist
|
|
uws@tux>sudo iptables -L
|
|
uws@tux># Check ufw firewall exist
|
|
uws@tux>sudo ufw status
|
|
|
|
uws@tux># Install fail2ban
|
|
uws@tux>sudo apt install fail2ban # Debian
|
|
uws@tux>sudo pacman -S fail2ban # Arch Linux, Manjaro
|
|
\end{lstlisting}
|
|
\subsubsection{Konfiguration}
|
|
Nach der Installation kann fail2ban konfiguriert werden. Hierzu legen wir die Datei {\ttfamily my-jail.conf} in dem Verzeichnis /etc/fail2ban/jail.d an. Man kann auch in dem Verzeichnis /etc/fail2ban die Datei {\ttfamily jail.local} anlegen umd Fail2ban zu konfigurieren.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Configuration]
|
|
uws@tux>cat /etc/fail2ban/jail.d/my-jail.conf
|
|
[DEFAULT]
|
|
# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
|
|
# ban a host which matches an address in this list. Several addresses can be
|
|
# defined using space (and/or comma) separator.
|
|
ignoreip = 127.0.0.1/8 ::1 10.23.44.0/24
|
|
|
|
# "bantime" is the number of seconds that a host is banned.
|
|
bantime = 1d
|
|
|
|
# A host is banned if it has generated "maxretry" during the last "findtime"
|
|
# seconds [m] Minutes
|
|
findtime = 600
|
|
|
|
# "maxretry" is the number of failures before a host get banned.
|
|
maxretry = 5
|
|
|
|
# Destination email address used solely for the interpolations in
|
|
# jail.{conf,local,d/*} configuration files.
|
|
#destemail = root@localhost
|
|
destemail = uws@seabaer-ag.de
|
|
|
|
# Sender email address used solely for some actions
|
|
sender = root@localhost
|
|
|
|
# Default banning action (e.g. iptables, iptables-new,
|
|
# iptables-multiport, shorewall, etc) It is used to define
|
|
# action_* variables. Can be overridden globally or per
|
|
# section within jail.local file
|
|
# banaction = ufw
|
|
banaction = iptables-multiport
|
|
# Choose default action. To change, just override value of 'action' with the
|
|
# interpolation to the chosen action shortcut (e.g. action_mw, action_mwl, etc) in jail.local
|
|
# globally (section [DEFAULT]) or per specific section
|
|
action = %(action_mw)s
|
|
|
|
[sshd]
|
|
enabled = true
|
|
|
|
maxretry = 3
|
|
|
|
findtime = 1d
|
|
|
|
bantime = 3d
|
|
\end{lstlisting}
|
|
Nach dem Anlegen der Datei muss nun der fail2ban Dienst neu gestartet werden.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Restart Service]
|
|
uws@tux>sudo systemctl restart fail2ban
|
|
\end{lstlisting}
|
|
Nach einer Änderng an der Konfiguration kann man mit {\ttfamily fail2ban-client reload} die neue Konfiguration laden.
|
|
\subsubsection{Befehle}
|
|
Als erstes prüfen wir, ob fail2ban funktioniert. Wenn fail2ban funktioniert, wird ein pong zurükgegeben.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Funktionskontrolle]
|
|
uws@tux>sudo fail2ban-client ping
|
|
Server replied: pong
|
|
\end{lstlisting}
|
|
Die aktiven Jails kann ausgegeben werden.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Jails]
|
|
uws@tux>sudo fail2ban-client status
|
|
Status
|
|
|- Number of jail: 8
|
|
`- Jail list: dovecot, nginx-http-auth, pam-generic, postfix, recidive, sasl, sshd
|
|
\end{lstlisting}
|
|
Alle fehlgeschlagene SSH-Anmeldungen kann man sich anzeigen lassen.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Anzeige SSH]
|
|
uws@tux>sudo fail2ban-client status sshd
|
|
Status for the jail: sshd
|
|
|- Filter
|
|
| |- Currently failed: 3
|
|
| |- Total failed: 7
|
|
| `- File list: /var/log/auth.log
|
|
`- Actions
|
|
|- Currently banned: 84
|
|
|- Total banned: 84
|
|
`- Banned IP list: 103.101.225.67 103.176.96.75 104.168.68.119
|
|
\end{lstlisting}
|
|
\newpage
|
|
\begin{justify}
|
|
Ausgenommene IP-Adressen können auch ausgegeben werden.
|
|
\end{justify}
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Anzeige ignoreip]
|
|
uws@tux>sudo fail2ban-client get sshd ignoreip
|
|
These IP addresses/networks are ignored:
|
|
|- 127.0.0.0/8
|
|
|- ::1
|
|
`- 10.23.44.0/24
|
|
\end{lstlisting}
|
|
Ausgabe der Bantime für einen Service. Die Ausgabe erfolgt in Sekunden.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Anzeige bantime]
|
|
uws@tux>sudo fail2ban-client get sshd bantime
|
|
259200
|
|
\end{lstlisting}
|
|
Die Anzahl der erlaupten Versuche für die Anmeldung kann man sich auch ausgeben lassen.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Anzeige maxretry]
|
|
uws@tux>sudo fail2ban-client get sshd maxretry
|
|
3
|
|
\end{lstlisting}
|
|
Welche Log-Datei für die Auswertung genommen wird.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Anzeige logpath]
|
|
uws@tux>sudo fail2ban-client get sshd logpath
|
|
`- /var/log/auth.log
|
|
\end{lstlisting}
|
|
Anzeige der IP-Adressen, die gesperrt (banned) sind.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Anzeige banned]
|
|
uws@tux>sudo fail2ban-client get sshd banned
|
|
['193.32.162.212', '193.24.211.200', '46.29.239.136', '120.26.239.198']
|
|
\end{lstlisting}
|
|
Eine IP-Adresse kann auch manuell der Bannliste hinzugefügt werden, ebenso kann man auch eine davon entfernen.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Bannliste]
|
|
uws@tux>sudo fail2ban-client set sshd banip <IP>
|
|
|
|
uws@tux>sudo fail2ban-client set sshd unbanip <IP>
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Error
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Error}
|
|
Taucht in der {\ttfamily /var/log/messages} folgender Fehler auf:
|
|
{\ttfamily sshd syslogin\_perform\_logout:logout() returned an error}, so ist die Datei {\ttfamily utmp} im
|
|
Verzeichnis {\ttfamily /var/run} neu anzulegen.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Recreate utmp]
|
|
root@tux>rm /var/run/utmp
|
|
|
|
root@tux>touch /var/run/utmp
|
|
\end{lstlisting}
|
|
\newpage
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Tastaturlayout
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Tastaturlayout}
|
|
Wird eine Verbindung von z.B. einem iPad mit externer Tastatur aufgebaut und in der Shell ist dann das Englische Layout aktiv, so ist in der Datei {\ttfamily /etc/X11/xorg.conf.d/00-keyboard.conf} die Zeile mit {\ttfamily Identifier} auf {\ttfamily Keyboard Defaults} zu ändern. Ist die Datei nicht vorhanden, die Datei mit folgendem Inhalt erstellen:
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=00-keyboard.conf]
|
|
uws@tux>cat /etc/X11/xorg.conf.d/00-keyboard.conf
|
|
# Read and parsed by systemd-localed. It's probably wise not to edit this file
|
|
# manually too freely.
|
|
Section "InputClass"
|
|
Identifier "Keyboard Defaults"
|
|
MatchIsKeyboard "on"
|
|
Option "XkbLayout" "de"
|
|
EndSection
|
|
\end{lstlisting}
|
|
%-------------------------------------------------------------------------------
|
|
% Subsection: Beispiele
|
|
%-------------------------------------------------------------------------------
|
|
\subsection{Beispiele}
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiele]
|
|
uws@tux># Default Verbindung
|
|
uws@tux>ssh uws@tux10
|
|
|
|
uws@tux># Mit Angabe der Schlüsseldatei
|
|
uws@tux>ssh -i ~/.ssh tux10.rsa uws@tux10
|
|
|
|
uws@tux># Dateien packen und senden
|
|
uws@tux>tar -czvf - <path> | ssh user@host 'cd <path> && tar -xzpvf -'
|
|
|
|
uws@tux># Dateien holen und entpacken
|
|
uws@tux>ssh user@host 'cd <path> && tar -czvf - <path>' | tar -xzfv -
|
|
\end{lstlisting}
|
|
\end{justify}
|