<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Peek Read Info (Posts about sudo)</title><link>https://peekread.info/</link><description></description><atom:link href="https://peekread.info/tags/sudo.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2024 &lt;a href="mailto:dugite-code@peekread.info"&gt;Dugite-Code&lt;/a&gt; 
&lt;a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"&gt;
&lt;img alt="Creative Commons License BY-SA"
width="88px" height="31px" style="border-width:0; margin-bottom:12px;"
src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png"&gt;&lt;/a&gt;</copyright><lastBuildDate>Wed, 14 Feb 2024 06:33:10 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>I shouldn't use sudo nano</title><link>https://peekread.info/tech/20210727-i-shouldn-t-use-sudo-nano/</link><dc:creator>Dugite-Code</dc:creator><description>&lt;p&gt;Over on &lt;a href="https://www.reddit.com/r/linux"&gt;/r/linux&lt;/a&gt; a user going by &lt;a href="https://www.reddit.com/user/AlternOSx"&gt;/u/AlternOSx&lt;/a&gt; posted a short You should Know:  &lt;a href="https://www.reddit.com/r/linux/comments/osah05/ysk_do_not_use_sudo_vimnanoemacs_to_edit_a_file/"&gt;YSK : Do not use 'sudo vim/nano/emacs..' to edit a file. Instead, set your $EDITOR and use sudoedit or sudo -e.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Long story short &lt;code&gt;sudoedit&lt;/code&gt; copies the file you want to edit to &lt;code&gt;/tmp/file.xxx&lt;/code&gt; and then opens it with an &lt;strong&gt;unprivileged&lt;/strong&gt; instance of your editor of choice. It then overwrites the source file when you are finished editing, protecting from accidental privilege escalation of commands through your text editor.&lt;/p&gt;
&lt;p&gt;Knowing this I came up with a quick way to enforce this best practice by added this function into my &lt;code&gt;.bashrc&lt;/code&gt; file. Hopefully I can retrain myself not to use &lt;code&gt;sudo nano&lt;/code&gt; all the time.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="cp"&gt;# Define the default editor in this case nano.&lt;/span&gt;
&lt;span class="n"&gt;EDITOR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;nano&lt;/span&gt;

&lt;span class="cp"&gt;# Catch calls to sudo.&lt;/span&gt;
&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sudo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;$1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"$EDITOR"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;then&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="cp"&gt;# The editor has been called&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"$2"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;then&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="cp"&gt;# If the file is writable by the current user just use the editor as normal.&lt;/span&gt;

&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;$EDITOR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"$2"&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="cp"&gt;# The file is not writable use sudoedit.&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sudoedit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"$2"&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;fi&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="cp"&gt;# Use sudo as normal.&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;sudo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"$@"&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;fi&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</description><category>linux</category><category>security</category><category>sudo</category><guid>https://peekread.info/tech/20210727-i-shouldn-t-use-sudo-nano/</guid><pubDate>Mon, 26 Jul 2021 16:00:00 GMT</pubDate></item><item><title>TOTP with sudo (Google Auth)</title><link>https://peekread.info/tech/20200610-totp-with-sudo-google-auth/</link><dc:creator>Dugite-Code</dc:creator><description>&lt;p&gt;I was reading the posts over on lobste.rs and saw this post: &lt;a href="https://lobste.rs/s/hs7yjv/is_sudo_almost_useless"&gt;Is &lt;code&gt;sudo&lt;/code&gt; almost useless?&lt;/a&gt;. Typically I see sudo as a safety belt to protect you from doing something stupid with administrator privileges rather than a security shield. But that doesn't mean it can't be both&lt;/p&gt;
&lt;p&gt;As with ssh, outlined in my previous post &lt;a href="https://peekread.info/tech/20190614-totp-with-ssh-google-auth/"&gt;TOTP with SSH (Google Auth)&lt;/a&gt;, you can certainly boost your sudo usefulness security wise by throwing 2FA via &lt;a href="https://github.com/google/google-authenticator-libpam"&gt;google-authenticator-libpam&lt;/a&gt; on top of it.&lt;/p&gt;
&lt;h3&gt;Install google-authenticator-libpam&lt;/h3&gt;
&lt;p&gt;On debian/ubuntu:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="w"&gt;    &lt;/span&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;apt&lt;span class="w"&gt; &lt;/span&gt;update&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;apt&lt;span class="w"&gt; &lt;/span&gt;install&lt;span class="w"&gt; &lt;/span&gt;google-authenticator-libpam
&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;Set-up your secret keys&lt;/h3&gt;
&lt;p&gt;We now need to create the secret key, this should not be kept in the user folder, after all what is the point of 2FA if the user we are authenticating can just read the secret files. In my case I keep them in the root dir&lt;/p&gt;
&lt;p&gt;Replace the variable &lt;code&gt;${USER}&lt;/code&gt; if/when you create a key for a user other than the active one.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;google-authenticator&lt;span class="w"&gt; &lt;/span&gt;-s&lt;span class="w"&gt; &lt;/span&gt;/root/.sudo_totp/&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;USER&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;/.google_authenticator
sudo&lt;span class="w"&gt; &lt;/span&gt;chmod&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;600&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-R&lt;span class="w"&gt; &lt;/span&gt;/root/.sudo_totp/
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You will see a QR code/secret key that you can scan with a TOTP app like &lt;a href="https://f-droid.org/en/packages/org.shadowice.flocke.andotp/"&gt;andotp&lt;/a&gt;, &lt;a href="https://play.google.com/store/apps/details?id=com.authy.authy"&gt;authy&lt;/a&gt;, &lt;a href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2"&gt;google authenticator&lt;/a&gt; or in my case I added it to my yubikey. There are also your emergency scratch codes that you should record somewhere safe.&lt;/p&gt;
&lt;h3&gt;Enable in PAM&lt;/h3&gt;
&lt;p&gt;You now need to let PAM know it should be checking the codes. There are two ways to do this, Mandatory and Only if secret key exists. I have it as Mandatory any user using sudo &lt;strong&gt;MUST&lt;/strong&gt; have a secret key&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;/etc/pam.d/sudo&lt;/code&gt; add the following configuration lines to the end of the file.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="c1"&gt;# Use Google Auth -- Mandatory&lt;/span&gt;
auth&lt;span class="w"&gt; &lt;/span&gt;required&lt;span class="w"&gt; &lt;/span&gt;pam_google_authenticator.so&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;secret&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/root/.sudo_totp/&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;USER&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;/.google_authenticator&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;root

&lt;span class="c1"&gt;# Use Google Auth -- Only if secret key exists&lt;/span&gt;
&lt;span class="c1"&gt;# auth required pam_google_authenticator.so secret=/root/.sudo_totp/${USER}/.google_authenticator user=root nullok&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;Bonus do this for su as well&lt;/h3&gt;
&lt;p&gt;You can do the same thing for su as well however obviously the user variable will be root rather than the user attempting to elevate their privilege's.&lt;/p&gt;
&lt;p&gt;Setup the key as before, just for the root user&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;google-authenticator&lt;span class="w"&gt; &lt;/span&gt;-s&lt;span class="w"&gt; &lt;/span&gt;/root/.google_authenticator
sudo&lt;span class="w"&gt; &lt;/span&gt;chmod&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;600&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-R&lt;span class="w"&gt; &lt;/span&gt;/root/.google_authenticator
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In &lt;code&gt;/etc/pam.d/su&lt;/code&gt; add the following configuration lines to the end of the file.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="c1"&gt;# Use Google Auth -- Mandatory&lt;/span&gt;
auth&lt;span class="w"&gt; &lt;/span&gt;required&lt;span class="w"&gt; &lt;/span&gt;pam_google_authenticator.so&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;secret&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/root/.google_authenticator&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;root
&lt;/pre&gt;&lt;/div&gt;</description><category>linux</category><category>quick post</category><category>security</category><category>sudo</category><guid>https://peekread.info/tech/20200610-totp-with-sudo-google-auth/</guid><pubDate>Tue, 09 Jun 2020 16:00:00 GMT</pubDate></item></channel></rss>