{"id":8889,"date":"2024-12-14T01:02:00","date_gmt":"2024-12-14T06:02:00","guid":{"rendered":"https:\/\/www.both.org\/?p=8889"},"modified":"2024-12-12T21:45:27","modified_gmt":"2024-12-13T02:45:27","slug":"how-i-create-encrypted-passwords","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=8889","title":{"rendered":"How I create encrypted passwords"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"8889\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>I sometimes need to create an encrypted password for use in scripts when adding one or more new user accounts to hosts in my lab. It wouldn&#8217;t be very secure to use an unencrypted password in a script so encrypting the password it first is an important step for security.<\/p>\n\n\n\n<p>There&#8217;s an interesting command that we can use to create an encrypted password, <strong>mkpasswd<\/strong>. This command allows us to specify the salt to use to create the password, thus allowing us to duplicate the hash if we have both the salt and the plaintext password. Fortunately, we can&#8217;t recreate the plaintext password even if we have the hash and the salt. <\/p>\n\n\n\n<p>WIkipedia has an excellent article on the use of a random <a href=\"https:\/\/en.wikipedia.org\/wiki\/Salt_(cryptography)\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Salt_(cryptography)\" target=\"_blank\" rel=\"noreferrer noopener\">salt<\/a> to generate &#8220;perturbances&#8221; in the encryption algorithm used to generate the password hash.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Simple password creation<\/h2>\n\n\n\n<p>The command syntax is <strong>mkpasswd PASSWORD [SALT]<\/strong>. The salt can be explicitly specified or, if not supplied, generated randomly at the time the password is hashed. Here&#8217;s a simple example using &#8220;password&#8221; as the password. All the commands in this article can be performed by a non-root user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dboth@david:~$ <strong>mkpasswd password<\/strong>\n$y$j9T$rAEn1ihmO6rhaE0w.1Z1R0$T7JD74ZeOi2B63TDJpx6aAdjFUEEjVofnOHUvmTcbX7\ndboth@david:~$ <strong>mkpasswd password<\/strong>\n$y$j9T$DB6589thfttCAfDmZ0SBX\/$eLSf0XsQ9A8aeopBTXEeRhUjEhkF1w7KUEvMaWpzwh4\ndboth@david:~$ <strong>mkpasswd password<\/strong>\n$y$j9T$etVZ43jAi5k3HMeMFewfa.$jEqC0ugThDK2135ZOCdiaDcPlY8fuEA6je6zLRgBst8<\/code><\/pre>\n\n\n\n<p>Let&#8217;s examine the structure of the password hash. There are four fields separated by the $ character. <\/p>\n\n\n\n<p>The first field defines the type of encryption used, in this case, &#8220;y&#8221; means Yescrypt. This is the default encryption method used by the <strong>mkpasswd<\/strong> command as well as the <strong>passwd<\/strong> command when setting passwords for accounts from the command line. The default encryption method for password <a href=\"https:\/\/skybert.net\/debian-linux\/yescript-replaces-sha512-for-password-hashing\/\" data-type=\"link\" data-id=\"https:\/\/skybert.net\/debian-linux\/yescript-replaces-sha512-for-password-hashing\/\" target=\"_blank\" rel=\"noreferrer noopener\">changed<\/a> in 2021 from sha512 &#8212; designated with a 6 in this first field &#8212; to Yescrypt because it is more resistant to cracking. Fedora, Debian, Arch, and Ubuntu all use this more secure form of encryption for passwords. Passwords created originally using sha512 will be replaced with Yescrypt passwords when the password is changed. <\/p>\n\n\n\n<p>The second field is a default set of options for creating the password hash, j9T. Those details are beyond the scope of this article. <\/p>\n\n\n\n<p>The salt is located in the third field of the password string.  In this example, the salt is both random and different for all three instances. <\/p>\n\n\n\n<p>The fourth and last field is the hashed password. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">With a user defined salt<\/h2>\n\n\n\n<p>The <strong>mkpasswd<\/strong> command allows the user to define the salt using the -S (&#8211;salt=) option but Yescrypt doesn&#8217;t allow this. The Yescrypt method always uses long and randomly generated salts to ensure greater security. However less secure methods like sha512 and MD5 allow the user to define the salt.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dboth@david:~$ <strong>mkpasswd -m md5 password -S 12345678<\/strong>\n$1$12345678$o2n\/JiO\/h5VviOInWJ4OQ\/\ndboth@david:~$ <strong>mkpasswd -m md5 password -S 12345678<\/strong>\n$1$12345678$o2n\/JiO\/h5VviOInWJ4OQ\/\ndboth@david:~$ <strong>mkpasswd -m sha-512 password -S 12345678<\/strong>\n$6$12345678$I8tr4xFAC6\/TtjYWdp0LWEjQre2LcYm2jdSMNLQDIyqRv.cKo7KMD5\/HpzVVFKpUQlIekr\/Vw.OdImtRM85fg\/\ndboth@david:~$ <strong>mkpasswd -m sha-512 password -S 12345678<\/strong>\n$6$12345678$I8tr4xFAC6\/TtjYWdp0LWEjQre2LcYm2jdSMNLQDIyqRv.cKo7KMD5\/HpzVVFKpUQlIekr\/Vw.OdImtRM85fg\/<\/code><\/pre>\n\n\n\n<p>This allows the same password hash to be created by using the same salt and plaintext password. This reduces the overall security of the passwords and makes cracking attacks more effective because one factor, the salt, can be reused in each attempt at brute-forcing a password. This isn&#8217;t possible with Yescrypt.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Supported encryption methods<\/h2>\n\n\n\n<p>The mkpasswd command supports twelve hashing methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dboth@david:~$ <strong>mkpasswd -m help<\/strong>\nAvailable methods:\nyescrypt        Yescrypt\ngost-yescrypt   GOST Yescrypt\nscrypt          scrypt\nbcrypt          bcrypt\nbcrypt-a        bcrypt (obsolete $2a$ version)\nsha512crypt     SHA-512\nsha256crypt     SHA-256\nsunmd5          SunMD5\nmd5crypt        MD5\nbsdicrypt       BSDI extended DES-based crypt(3)\ndescrypt        standard 56 bit DES-based crypt(3)\nnt              NT-Hash<\/code><\/pre>\n\n\n\n<p>Most of these hashing methods are demonstrably less secure than Yescrypt. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>I thought this would be an easy article, but it turns out &#8212; not so much. The <strong>mkpasswd<\/strong> command, despite having few options, is far more complex than I originally thought, and the use of the newest encryption methods is an important part of security. I learned more about creating passwords and the additional security afforded by the Yescrypt method, so this was a profitable day.<\/p>\n\n\n\n<p>Of course the strength of the password itself is a critical factor in the security of any account.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I sometimes need to create an encrypted password for use in scripts when adding one or more new<\/p>\n","protected":false},"author":2,"featured_media":2700,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5,75],"tags":[662,261],"class_list":["post-8889","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-security","tag-password","tag-security"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8889"}],"version-history":[{"count":22,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8889\/revisions"}],"predecessor-version":[{"id":8914,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8889\/revisions\/8914"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2700"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}