{"id":287,"date":"2014-05-24T06:49:26","date_gmt":"2014-05-24T13:49:26","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=287"},"modified":"2014-05-26T07:16:10","modified_gmt":"2014-05-26T14:16:10","slug":"hiding-passwords-in-scripts","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/hiding-passwords-in-scripts\/","title":{"rendered":"Hiding Passwords in Scripts"},"content":{"rendered":"

Sometimes you need to pass a password or even just a string on the command line which you would rather obscure. \u00a0For example:<\/p>\n

serverControl.sh -u admin -p $MYPASS -c shutdown<\/p>\n

Note anything below is not the ideal way of dealing with passwords you should probably use SSH keys if possible instead.<\/p>\n

Sometimes you really do not have a better option and this might be your only option. \u00a0Still it is a weak solution though to store passwords. \u00a0I simplified but you probably don't want to use obvious variable names or files either.<\/p>\n

Very simple base64 encoding:<\/strong><\/span><\/p>\n

$ echo \"passwd\" | base64\r\ncGFzc3dkCg==\r\n$ echo \"cGFzc3dkCg==\" | base64 --decode\r\npasswd\r\n\r\n# Use in script as follow or better use a file to store the string:\r\nMYENCPASS=\"cGFzc3dkCg==\" \r\nMYPASS=`echo \"$MYENCPASS\" | base64 --decode`<\/pre>\n

Using aesutil:<\/span><\/strong><\/p>\n

I saw someone mention aesutil on the Internet but it appears like few modern Linux distros comes with aesutil tools though.<\/p>\n

# mkrand generates a 15-character random\r\n$ SALT=`mkrand 15` passwd\r\n\r\n$ `echo \"passwd\" | aes -e -b -B -p $SALT`\r\ni\/b9pkcpQAPy7BzH2JlqHVoJc2mNTBM=\r\n\r\n# Use in script as follow or use a file to store the string:\r\nMYENCPASS=\"i\/b9pkcpQAPy7BzH2JlqHVoJc2mNTBM=\" \r\nMYPASS=`echo \"$MYENCPASS\" | aes -d -b -p $SALT`<\/pre>\n

Or maybe openssl is an option:<\/span><\/strong><\/p>\n

This is still very lame as you still have to use a password for the opensssl command. \u00a0 I just named it garbageKey but you are probably better off making it more obscure.<\/p>\n

# Test\r\n$ echo 'mySecretPassword' | openssl enc -base64 -e -aes-256-cbc -nosalt  -pass pass:garbageKey\r\nyQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=\r\n$ echo 'yQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=' | openssl enc -base64 -d -aes-256-cbc -nosalt -pass pass:garbageKey\r\nmySecretPassword\r\n\r\n# Use a hidden file\r\n$ echo 'mySecretPassword' | openssl enc -base64 -e -aes-256-cbc -nosalt  -pass pass:garbageKey > .hidden.lck \r\n$ cat .hidden.lck \r\nyQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=\r\n\r\n# In a script\r\n$ MYENCPASS=`cat .hidden.lck | openssl enc -base64 -d -aes-256-cbc -nosalt -pass pass:garbageKey`\r\n$ echo $MYENCPASS\r\nmySecretPassword\r\n<\/pre>\n

As you can see in the last example I used a hidden file also instead of keeping the encryption string in the file.<\/p>\n","protected":false},"excerpt":{"rendered":"

Sometimes you need to pass a password or even just a string on the command line which you would rather<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-security"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}