{"id":291,"date":"2020-03-27T18:07:02","date_gmt":"2020-03-27T17:07:02","guid":{"rendered":"http:\/\/www.itgarage.ch\/?p=291"},"modified":"2025-01-24T17:33:09","modified_gmt":"2025-01-24T16:33:09","slug":"linux-shell-2-faktor-authentifizierung","status":"publish","type":"post","link":"http:\/\/www.itgarage.ch\/index.php\/2020\/03\/27\/linux-shell-2-faktor-authentifizierung\/","title":{"rendered":"Linux\/Shell &#8211; 2-Faktor Authentifizierung"},"content":{"rendered":"\n<p>Shell-Zug\u00e4nge f\u00fcr Unix\/Linux-Systeme sind kritisch sofern sie <em>nur<\/em> aus Benutzername und Passwort bestehen. Auch die schl\u00fcsselbasierte Authentification ist keine Sicherheit wenn ein mobiles Ger\u00e4t abhanden kommt. Jeder Unix-SysAdmin weiss um diesen Umstand und ist darob wenig erfreut. Sind die Credentials bekannt, das Passwort geknackt oder der Schl\u00fcssel in falschen H\u00e4nden, stehen dem Eindringling keine H\u00fcrden mehr im Weg, <em>sudo<\/em> sei Dank, das System zu \u00fcbernehmen,<br>Im Firmennetzwerk mag das kein gr\u00f6sseres Problem darstellen da die Systeme in physischer Sicherheit stehen. Erfolgt aber der Zugriff von aussen, reicht das nicht mehr. Eine einfach umzusetzende Massnahme stellt die 2-Faktor-Authentifizierung  (2FA)  dar. Wir beziehen ein weiteres Ger\u00e4t, z.B. das Smartphone, mit ein. Die Chance, das alle Ger\u00e4te abhanden kommen und entsperrt werden k\u00f6nnen ist kleiner als nur ein <em>single<\/em> Passwort zu ergattern.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Google-Authenticator und PAM<br><\/h3>\n\n\n\n<p>Um f\u00fcr <em>login<\/em> und <em>sudo <\/em>2-Faktor-Authentifizierung zu erm\u00f6glichen ist PAM auf unixoiden Systemen zust\u00e4ndig. PAM &#8211; <em>Plugable Authentication<\/em> <em>Module<\/em> ist zust\u00e4ndig f\u00fcr die Authentifizierung der Benutzer. Google bietet f\u00fcr PAM eine Bibliothek an, welche eine 2FA durchf\u00fchrt.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Google-Authenticator installieren<br><\/h3>\n\n\n\n<p>Als erstes installierst du den Google-Authenticator aus dem App-Store f\u00fcr dein Smartphone (iOS oder Android). Du kannst ihn allerdings noch nicht starten.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PAM-Libraries auf deinem Linux installieren<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u00d6ffne ein Terminal zu dem Unix\/Linux-System welches du absichern willst<\/li>\n\n\n\n<li>Installiere die PAM-Libraries (z.B. mittes: &#171;<em>sudo apt \u00a0install libpam-google-authenticator&#187;<\/em><\/li>\n\n\n\n<li>Gib dein sudo-Passwort ein <\/li>\n\n\n\n<li>Best\u00e4tige die Installation<\/li>\n\n\n\n<li>Las die Installation beenden<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">PAM-Konfiguration<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>Achtung:<\/strong> Nach diesem Schritt kannst du nur noch mit einem Code einloggen!<\/p>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading has-quaternary-color has-text-color\">Plan B!!!<\/h3>\n\n\n\n<p>Damit wir uns nicht versehentlich ausschliessen, kommt unser Plan B ins Spiel:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Starte eine Screen-Session: &#171;screen&lt;ENTER>&#187;<\/li>\n\n\n\n<li>Kopiere die Datei &#171;\/etc\/pam.d\/common-auth&#187; nach &#171;\/tmp&#187;<br>&#171;cp \/etc\/pam.d\/common-auth \/tmp&#187;<\/li>\n\n\n\n<li>Wir kreieren nun einen Loop der alle 10 Minuten das gesicherte &#171;\/tmp\/common-auth&#187; wieder nach &#171;\/etc\/pam.d\/&#187; kopiert. Geht etwas schief, k\u00f6nnen wir nach 10 Minuten wieder &#171;normal&#187; einloggen<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:classic font-size:14 line-height:17 h-align:1 nums:true show-plain-default:true lang:sh decode:true \">screen\ncp \/etc\/pam.d\/common-auth \/tmp\nwhile true ; do cp \/tmp\/common-auth \/etc\/pam.d ; sleep 600 ; done  &amp;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Geht etwas schief, wird nach 10 Minuten das original &#171;common-auth&#187; nach &#171;\/etc\/pam.d&#187; zur\u00fcckkopiert. Starte dies in einer &#171;screen&#187;-Session!<\/p>\n\n\n\n<p>Finde folgende Zeile in der Datei &#171;\/etc\/pam.d\/common-auth&#187;:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"font-size:14 line-height:17 lang:sh decode:true \">auth    [success=1 default=ignore]      pam_unix.so nullok_secure<\/pre><\/div>\n\n\n\n<p>F\u00fcge die folgende Zeile <strong>oberhalb<\/strong> ein:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"font-size:14 line-height:17 lang:sh decode:true \">auth required pam_google_authenticator.so<\/pre><\/div>\n\n\n\n<p class=\"has-text-color\" style=\"color:#da160f\"><strong>Achtung:<\/strong> Sobald du diese Datei speicherst, kannst du dich nicht mehr ohne Code einloggen. Es ist dringend geraten, die aktuelle Session zu behalten! Oder 10 Minuten zu warten, dann kommt ja unser Plan B.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Barcode und secret Key generieren<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"float-enable:true lang:sh decode:true \">rat@bora &lt;~&gt; $ google-authenticator\nDo you want authentication tokens to be time-based (y\/n) y\nWarning: pasting the following URL into your browser exposes the OTP secret to Google:\n  https:\/\/www.google.com\/chart?chs=xx\n&lt;BARCODE&gt;\nYour new secret key is: 236PYE3KISLUZ62USHCNWGE462\n#\nYour verification code is 487956\nYour emergency scratch codes are:\n  80892822\n  95740500\n  41425327\n  15750665\n  88508262\nDo you want me to update your \"\/home\/rat\/.google_authenticator\" file? (y\/n) y\nDo you want to disallow multiple uses of the same authentication\ntoken? This restricts you to one login about every 30s, but it increases\nyour chances to notice or even prevent man-in-the-middle attacks (y\/n) y\nBy default, a new token is generated every 30 seconds by the mobile app.\nIn order to compensate for possible time-skew between the client and the server,\nwe allow an extra token before and after the current time. This allows for a\ntime skew of up to 30 seconds between authentication server and client. If you\nexperience problems with poor time synchronization, you can increase the window\nfrom its default size of 3 permitted codes (one previous code, the current\ncode, the next code) to 17 permitted codes (the 8 previous codes, the current\ncode, and the 8 next codes). This will permit for a time skew of up to 4 minutes\nbetween client and server.\nDo you want to do so? (y\/n) n\nIf the computer that you are logging into isn't hardened against brute-force\nlogin attempts, you can enable rate-limiting for the authentication module.\nBy default, this limits attackers to no more than 3 login attempts every 30s.\nDo you want to enable rate-limiting? (y\/n)\nnIf the computer that you are logging into isn't hardened against brute-force\nlogin attempts, you can enable rate-limiting for the authentication module.\nBy default, this limits attackers to no more than 3 login attempts every 30s.\nDo you want to enable rate-limiting? (y\/n) n\nrat@bora &lt;~&gt; $<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Google-Authenticator einrichten<\/h3>\n\n\n\n<p>Starte auf deinem Smartphone die Google-Authenticator-App:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"1334\" src=\"http:\/\/www.itgarage.ch\/wp-content\/uploads\/2020\/03\/IMG_4037.png\" alt=\"\" class=\"wp-image-295\"\/><\/figure>\n\n\n\n<p>Scanne den Barcode oder trage den Schl\u00fcssel (&#171;Your new secrete key is:&#187;) manuel ein. Du kannst nun in einem zweiten Terminal probieren auf dein Zielsystem einzuloggen:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SSH-Konfiguration<\/h3>\n\n\n\n<p>Um die 2FA f\u00fcr SSH zu benutzen, passe die Datei: &#171;\/etc\/pam.d\/sshd&#187; an. F\u00fcge die folgende Zeile am Dateiende dazu:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"font-size:14 line-height:17 lang:sh decode:true \">auth required pam_google_authenticator.so nullok<\/pre><\/div>\n\n\n\n<p>und passe die Datei &#171;\/etc\/<em>ssh\/sshd_confi<\/em>g&#187; an:<br><br>Anstatt:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">ChallengeResponseAuthentication no<\/pre><\/div>\n\n\n\n<p>setze:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">ChallengeResponseAuthentication yes<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>und starte den SSH-Service neu:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">sudo systemctl restart sshd.service<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Log-In<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>Erfahre den Code auf deinem Smartphone:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"1334\" src=\"http:\/\/www.itgarage.ch\/wp-content\/uploads\/2020\/03\/IMG_4038-1.png\" alt=\"\" class=\"wp-image-297\"\/><\/figure>\n\n\n\n<p>Verbinde dich in einem Terminal mit deinem Linux-System:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"font-size:14 line-height:17 lang:sh decode:true \">ssh 192.168.1.127\nVerification code:881892\nPassword:&lt;password&gt;\nLast login: Fri Mar 27 16:04:51 2020 from 192.168.1.101\nHave a lot of fun...\nrat@bora:~&gt;<\/pre><\/div>\n\n\n\n<p class=\"has-quaternary-color has-text-color\"><strong>Vergiss nicht Plan B zu beenden!<\/strong><\/p>\n\n\n\n<p class=\"has-primary-color has-text-color\">Dieser Vorgang ist f\u00fcr jeden berechtigten Benutzer zu wiederholen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fazit:<\/h2>\n\n\n\n<p>Ein kleiner Eingriff, durch den die Sicherheit f\u00fcr Fernzugriffe\/-logins massiv gesteigert werden kann. Die Codes auf der App sind jeweils 30s g\u00fcltig. Nachteil ist, dass <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.google.android.apps.authenticator2&amp;hl=de\">Google Authenticator<\/a> oder <a href=\"https:\/\/authy.com\">Authy<\/a> auf dem Smartphone installiert werden m\u00fcssen.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shell-Zug\u00e4nge f\u00fcr Unix\/Linux-Systeme sind kritisch sofern sie nur aus Benutzername und Passwort bestehen. Auch die schl\u00fcsselbasierte Authentification ist keine Sicherheit&hellip;<\/p>\n","protected":false},"author":1,"featured_media":31,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oss","category-learning-bits"],"_links":{"self":[{"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/posts\/291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/comments?post=291"}],"version-history":[{"count":10,"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/posts\/291\/revisions"}],"predecessor-version":[{"id":398,"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/posts\/291\/revisions\/398"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/media\/31"}],"wp:attachment":[{"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/media?parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/categories?post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.itgarage.ch\/index.php\/wp-json\/wp\/v2\/tags?post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}