1 2 3 4 5 6 | netstat -puten Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 xxx.xxx.xxx.xxx:22 xxx.xxx.xxx.xxx:60229 ESTABLISHED 0 1813924954 5750/sshd: meinUser [ tcp 0 0 xxx.xxx.xxx.xxx:22 152.136.59.221:32271 ESTABLISHED 0 1824030803 14307/sshd: [accept |
Ich weiß, ich weiß, Server wird neu aufgesetzt, macht keinen Sinn das reparieren zu wollen.
Trotzdem wüsste ich gerne wie das möglich sein kann. Vielleicht habe ich auch einen Denkfehler, aber genau deshalb auch dieser Post hier.
1. Firewall Folgende iptables Regeln habe ich laufen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # openVPN erlauben iptables -A INPUT -p udp --dport 1194 -j ACCEPT iptables -A OUTPUT -p udp --sport 1194 -j ACCEPT # Wireguard erlauben iptables -A INPUT -p udp --dport 51820 -j ACCEPT iptables -A OUTPUT -p udp --sport 51820 -j ACCEPT # SSH Zugriff ist nur von zu Hause (dyndns Adresse) und meinem Arbeitsrechner aus erlaubt iptables -A INPUT -p tcp --dport 22 -s $(dig +short meineDynDNS-Adresse),x.x.x.x -j ACCEPT iptables -A INPUT -p tcp --dport 22 -j DROP # apt-get erlauben iptables -A OUTPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT iptables -A OUTPUT -p udp --dport 53 -m state --state NEW -j ACCEPT iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Webserver freigeben iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT # mariadb freigeben iptables -A INPUT -p tcp --dport 3306 -j ACCEPT # Alles andere verwerfen iptables -A INPUT -j DROP |
Ich habe es auch getestet. Ich erhalte keine SSH Verbindung, wenn ich mich nicht zuvor via VPN mit meinem Heimnetzwerk verbinde oder aber ich an meinem Arbeitsrechner sitze. Alles andere wird geblockt. Hier die erste Frage:
Warum gilt das nicht für die Chinesen, die sich da munter via SSH auf meinem Server tummeln? ❓
2. SSH-Email Ich habe eine /etc/ssh/sshrc angelegt, welche mir jedes Mal eine Email sendet, wenn sich jemand auf dem Server einloggt, komplett mit Usernamen, IP und Zeitstempel. Funktioniert eins a jedes Mal wenn ich mich einlogge, aber wieso nicht, wenn jemand fremdes es tut wie hier geschehen? Genau dafür war das ganze ja schließlich gedacht!
sshrc Inhalt
1 2 3 4 5 6 | ip=`echo $SSH_CONNECTION | cut -d " " -f 1` name=`nslookup $ip | grep "name =" | cut -d " " -f 3` logger -t ssh-login $USER login from $ip - $name echo "Benutzer -> $USER <- hat sich von IP: $ip / Hostname: $name per SSH auf <MeinServer> angemeldet" | sendemail -q -u "<MeinServer> | SSH Login: $USER" -f <absender> -t <empfänger> -s mail.gmx.net:587 -o -xu <benutzername> -xp <passwort> |
3. SSH-key Selbstverständlich habe ich meinen SSH-Server geschützt. Der Zugang als root ist untersagt, Anmeldung via Passwort ebenfalls, nur ein passender SSH-Schlüssel darf angenommen werden. Die Schlüssel habe ich gestern frisch getauscht. Also auch hier die Frage, wieso kann jemand einen SSH Zugriff etablieren, ohne einen gültigen Schlüssel zu besitzen?
Hier meine /etc/ssh/sshd_config, der Lesbarkeit halber nur aktive Attribute (das Verzeichnis sshd_config.d ist übrigens leer, ebenso wie ssh_config.d)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Include /etc/ssh/sshd_config.d/*.conf KbdInteractiveAuthentication no UsePAM yes X11Forwarding yes PrintMotd no AcceptEnv LANG LC_* Subsystem sftp /usr/lib/openssh/sftp-server LoginGraceTime 120 PermitRootLogin no StrictModes yes PasswordAuthentication no PubkeyAuthentication yes AuthorizedKeysFile %h/.ssh/authorized_keys Banner /etc/ssh/banner.msg |
ssh_config
1 2 3 4 5 6 | Include /etc/ssh/ssh_config.d/*.conf Host * SendEnv LANG LC_* HashKnownHosts yes GSSAPIAuthentication yes |
Fazit
Fail2ban läuft ebenfalls mit, aber dieser ist noch nicht zu 100% fertig konfiguriert. Hat irgendjemand eine Idee, was ich falsch gemacht habe? Was müsste ich anders machen, wenn ich die Kiste nun neu aufsetze, damit das nicht wieder passiert? Ich dachte eigentlich ich hätte alles nötige getan.
Auf dem Server läuft ein apache-Webserver mit einem Dokuwiki, sowie meine Nextcloud. Das System halte ich selbstverständlich immer aktuell.
Was mich am Meisten verwirrt ist tatsächlich
1 | 152.136.59.221:22072 ESTABLISHED 0 1822595569 13140/sshd: [accept |
wieso da eine ssh Verbindung zustande kommt, OHNE Username, OHNE dass die Firewallregeln greifen, OHNE dass ein SSH-Key benötigt wird und OHNE irgendeine Benachrichtung. Im Log stehen tatsächlich auch nur Fehler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | cat /var/log/auth.log | grep 152.136.59.221 Sep 20 11:29:09 h2777672 sshd[4156]: Invalid user user from 152.136.59.221 port 42251 Sep 20 11:29:09 h2777672 sshd[4156]: Received disconnect from 152.136.59.221 port 42251:11: Bye Bye [preauth] Sep 20 11:29:09 h2777672 sshd[4156]: Disconnected from invalid user user 152.136.59.221 port 42251 [preauth] Sep 20 11:36:30 h2777672 sshd[5354]: fatal: Timeout before authentication for 152.136.59.221 port 11757 Sep 20 11:43:24 h2777672 sshd[7101]: fatal: Timeout before authentication for 152.136.59.221 port 24771 Sep 20 11:52:35 h2777672 sshd[9287]: fatal: Timeout before authentication for 152.136.59.221 port 9301 Sep 20 11:54:51 h2777672 sshd[9839]: fatal: Timeout before authentication for 152.136.59.221 port 41393 Sep 20 11:57:08 h2777672 sshd[10364]: fatal: Timeout before authentication for 152.136.59.221 port 26556 Sep 20 11:59:25 h2777672 sshd[10901]: fatal: Timeout before authentication for 152.136.59.221 port 45454 Sep 20 12:01:43 h2777672 sshd[11445]: fatal: Timeout before authentication for 152.136.59.221 port 20823 Sep 20 12:04:01 h2777672 sshd[12022]: fatal: Timeout before authentication for 152.136.59.221 port 42340 Sep 20 12:06:17 h2777672 sshd[12619]: fatal: Timeout before authentication for 152.136.59.221 port 47512 Sep 20 12:08:36 h2777672 sshd[13140]: fatal: Timeout before authentication for 152.136.59.221 port 22072 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | cat /var/log/auth.log | grep 121.196.222.27 Sep 20 11:13:37 h2777672 sshd[820]: Connection closed by 121.196.222.27 port 45608 Sep 20 11:14:08 h2777672 sshd[1459]: Invalid user admin from 121.196.222.27 port 47394 Sep 20 11:14:08 h2777672 sshd[1459]: Connection closed by invalid user admin 121.196.222.27 port 47394 [preauth] Sep 20 11:15:41 h2777672 sshd[1466]: Invalid user admin from 121.196.222.27 port 54648 Sep 20 11:16:05 h2777672 sshd[1466]: Connection closed by invalid user admin 121.196.222.27 port 54648 [preauth] Sep 20 11:16:53 h2777672 sshd[1557]: Invalid user admin from 121.196.222.27 port 33678 Sep 20 11:16:53 h2777672 sshd[1557]: Connection closed by invalid user admin 121.196.222.27 port 33678 [preauth] Sep 20 11:19:12 h2777672 sshd[1929]: Invalid user admin from 121.196.222.27 port 40928 Sep 20 11:19:14 h2777672 sshd[1929]: Connection closed by invalid user admin 121.196.222.27 port 40928 [preauth] Sep 20 11:21:32 h2777672 sshd[2425]: Connection reset by 121.196.222.27 port 48166 Sep 20 11:22:48 h2777672 sshd[2759]: Invalid user centos from 121.196.222.27 port 55446 Sep 20 11:22:48 h2777672 sshd[2759]: Connection closed by invalid user centos 121.196.222.27 port 55446 [preauth] Sep 20 11:26:10 h2777672 sshd[3415]: Invalid user default from 121.196.222.27 port 41734 Sep 20 11:27:24 h2777672 sshd[3334]: fatal: Timeout before authentication for 121.196.222.27 port 34480 Sep 20 11:27:27 h2777672 sshd[3415]: Connection closed by invalid user default 121.196.222.27 port 41734 [preauth] Sep 20 11:29:31 h2777672 sshd[4107]: Connection reset by 121.196.222.27 port 56244 Sep 20 11:29:45 h2777672 sshd[3955]: Invalid user guest from 121.196.222.27 port 48996 Sep 20 11:30:11 h2777672 sshd[3955]: fatal: Timeout before authentication for 121.196.222.27 port 48996 Sep 20 11:31:58 h2777672 sshd[4608]: Invalid user postgres from 121.196.222.27 port 35276 Sep 20 11:32:17 h2777672 sshd[4608]: Connection closed by invalid user postgres 121.196.222.27 port 35276 [preauth] Sep 20 11:34:37 h2777672 sshd[5138]: Invalid user pi from 121.196.222.27 port 42486 Sep 20 11:35:07 h2777672 sshd[5138]: Connection closed by invalid user pi 121.196.222.27 port 42486 [preauth] Sep 20 11:36:01 h2777672 sshd[5301]: Invalid user hadoop from 121.196.222.27 port 49752 Sep 20 11:36:03 h2777672 sshd[5301]: Connection closed by invalid user hadoop 121.196.222.27 port 49752 [preauth] Sep 20 11:40:41 h2777672 sshd[6882]: Invalid user sonar from 121.196.222.27 port 43334 Sep 20 11:40:42 h2777672 sshd[6882]: Connection closed by invalid user sonar 121.196.222.27 port 43334 [preauth] Sep 20 11:41:36 h2777672 sshd[6993]: Invalid user lighthouse from 121.196.222.27 port 50590 Sep 20 11:42:18 h2777672 sshd[6993]: Connection closed by invalid user lighthouse 121.196.222.27 port 50590 [preauth] Sep 20 11:45:31 h2777672 sshd[7918]: Invalid user pi from 121.196.222.27 port 36250 Sep 20 11:45:32 h2777672 sshd[7918]: Connection closed by invalid user pi 121.196.222.27 port 36250 [preauth] Sep 20 11:46:06 h2777672 sshd[8205]: Connection reset by 121.196.222.27 port 44124 Sep 20 11:50:43 h2777672 sshd[9062]: Connection closed by authenticating user root 121.196.222.27 port 58640 [preauth] Sep 20 11:52:37 h2777672 sshd[9605]: Connection closed by authenticating user root 121.196.222.27 port 44910 [preauth] Sep 20 11:53:21 h2777672 sshd[9526]: Connection closed by authenticating user root 121.196.222.27 port 39166 [preauth] Sep 20 11:56:27 h2777672 sshd[10200]: fatal: Timeout before authentication for 121.196.222.27 port 52172 Sep 20 11:56:30 h2777672 sshd[10605]: Connection reset by 121.196.222.27 port 36708 Sep 20 11:57:08 h2777672 sshd[10749]: Connection closed by authenticating user root 121.196.222.27 port 38442 [preauth] Sep 20 11:58:05 h2777672 sshd[11055]: Connection closed by authenticating user root 121.196.222.27 port 45702 [preauth] Sep 20 12:01:31 h2777672 sshd[11536]: Connection closed by authenticating user root 121.196.222.27 port 52886 [preauth] Sep 20 12:03:14 h2777672 sshd[12016]: Connection closed by authenticating user root 121.196.222.27 port 60196 [preauth] Sep 20 12:03:42 h2777672 sshd[12359]: Connection reset by 121.196.222.27 port 39202 Sep 20 12:05:51 h2777672 sshd[12820]: Invalid user ubnt from 121.196.222.27 port 46470 Sep 20 12:05:54 h2777672 sshd[12820]: Connection closed by invalid user ubnt 121.196.222.27 port 46470 [preauth] Sep 20 12:06:31 h2777672 sshd[13106]: Invalid user user from 121.196.222.27 port 53740 Sep 20 12:07:05 h2777672 sshd[13106]: Connection closed by invalid user user 121.196.222.27 port 53740 [preauth] |
Bearbeitet von sebix:
Titel mit den neuen Erkenntnissen präzisiert und an den neuen Themenschwerpunkt angepasst