Хеш пароля в линукс

How to create SHA512 password hashes on command line

In Linux I can create a SHA1 password hash using sha1pass mypassword . Is there a similar command line tool which lets me create sha512 hashes? Same question for Bcrypt and PBKDF2 .

There’s a sha512sum command that’s part of coreutils, and similarly openssl sha512 — but neither does the extra stuff that sha1pass does.

13 Answers 13

On any of the Red Hat distros such as Fedora, CentOS, or RHEL the command mkpasswd doesn’t include the same set of switches as the version typically included with Debian/Ubuntu.

NOTE: The command mkpasswd is actually part of the expect package, and should probably be avoided. You can find out what package it belongs to with either of these commands.

$ yum whatprovides "*/mkpasswd" -or- $ repoquery -q --file */mkpasswd 

Example

$ repoquery -q --file */mkpasswd expect-0:5.43.0-8.el5.x86_64 expect-0:5.43.0-8.el5.i386 

Both of these methods are superior to using rpm since the packages do not have to be installed to locate */mkpasswd .

Workarounds

To work around this you can use the following Python or Perl one-liners to generate SHA-512 passwords. Take note that these are salted:

Python (>= 3.3)

$ python -c 'import crypt,getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))' 
$ python -c 'import crypt; print(crypt.crypt("somesecret", crypt.mksalt(crypt.METHOD_SHA512)))' 

Python (2.x or 3.x)

$ python -c "import crypt, getpass, pwd; \ print(crypt.crypt('password', '\$6\$saltsalt\$'))" $6$saltsalt$qFmFH.bQmmtXzyBY0s9v7Oicd2z4XSIecDzlB5KiA2/jctKu9YterLp8wwnSq.qc.eoxqOmSuNp2xS0ktL3nh/ 

Note: $6$ designates sha512. Support for this method of specifying the algorithm is dependent on support in OS level crypt(3) library function (usually in libcrypt). It is not dependent on python version.

Perl

$ perl -e 'print crypt("password","\$6\$saltsalt\$") . "\n"' $6$saltsalt$qFmFH.bQmmtXzyBY0s9v7Oicd2z4XSIecDzlB5KiA2/jctKu9YterLp8wwnSq.qc.eoxqOmSuNp2xS0ktL3nh/ 

In these examples the password is the string «password» and the salt is «saltsalt». Both examples are using $6$ which denotes that you want crypt to use SHA-512.

For the python one-liner, you can use crypt.mksalt(crypt.METHOD_SHA512) to generate the salt instead of using a fixed one.

Before typing a clear-text password at the command line make sure you have «ignorespace» set in HISTCONTROL (i.e., do this first on CentOS/RHEL: echo ‘export HISTCONTROL=»ignoredups:ignorespace»‘ > /etc/profile.d/histcontrol.sh && source /etc/profile.d/histcontrol.sh). Otherwise, it’ll get saved in your ~/.bash_history.

the perl (and presumably the python) use the system function «crypt». So they aren’t portable, they require a crypt function that understands the requested hash type. The OSX crypt doesn’t — it just gives me back an old-style DES-encrypted string.

Читайте также:  Com терминал для linux

Yes, you’re looking for mkpasswd , which (at least on Debian) is part of the whois package. Don’t ask why.

anthony@Zia:~$ mkpasswd -m help Available methods: des standard 56 bit DES-based crypt(3) md5 MD5 sha-256 SHA-256 sha-512 SHA-512 

Unfortunately, my version at least doesn’t do bcrypt. If your C library does, it should (and the manpage gives a -R option to set the strength). -R also works on sha-512, but I’m not sure if its PBKDF-2 or not.

If you need to generate bcrypt passwords, you can do it fairly simply with the Crypt::Eksblowfish::Bcrypt Perl module.

$ openssl passwd --help Usage: passwd [options] Valid options are: . -6 SHA512-based password algorithm 

For consistent output you can specify the salt:

Output is something similar to:

$6$YOUR_SALT$q/oDR4twC1ik4RoxTJJtX.3MjenHVapkMaBbq2NcRHGQjqhIWRNcEVitYZhyIx98D7lF7qs0vLTq17X0nEr8I. 

with: 6 between «$» indicating the algorithm («-6» above), followed by YOUR_SALT and «$», finishing with the SHA512 sum.

You can use the doveadm utility, which is included in the dovecot package.

Result example:

$6$0JvQ1LLFESzA16.I$JVdKAIq0igudTq06BMqzT9rL1gRawMPwLr9U3/kBMKUqZdONfa0wubC89C35LKl3aE16CRH57BfGb4ygPLggL1 

Just cut and you’ll get your SHA512 hashed string.

Doesn’t work for me as a regular user: doveadm(jotik): Fatal: Error reading configuration: stat(/etc/dovecot/dovecot.conf) failed: Permission denied (euid=1000(jotik) egid=100(users) missing +x perm: /etc/dovecot, we’re not in group 97(dovecot), dir owned by 0:97 mode=0750)

«Permission denied» is not related to the original question. The default dovecot setting is not to let end-user read any files in the /etc/dovecot subdirectory, particularily users (and password ) file. Become root, and then you can see those files.

The point is that doveadm cannot be run by a regular user, so this command is not a solution to the original question for someone who isn’t (or cannot become) root. That someone typically isn’t interested in reading any files in the /etc/dovecot directory, and shouldn’t need to just in order to generate a password hash.

then enter the word you want hashed.

I don’t believe grub is on c7 so you can use: python -c ‘import crypt,getpass; print(crypt.crypt(getpass.getpass(),crypt.mksalt(crypt.METHOD_SHA512)))’

To expand on @slm’s workarounds above, if you’re worried about someone getting a hold of your bash history and seeing the plain text password, you can insert raw_input() in the python statement where the salt and password fields go so it prompts you for them. The text isn’t masked while you’re typing, but it won’t show up in your bash history. You could also start the command with a leading space, but I always forget to do that.

python -c "import crypt, getpass, pwd; print crypt.crypt(raw_input(), '\$6\$' + raw_input() + '\$')" 

You can also configure bash to not record commands prefixed with a space adding HISTCONTROL=ignorespace to your .bashrc file. When you run a command you want to exclude from your history simply type a space then the actual command.

Читайте также:  Linux debug shared library loading

sha512 htpasswd

Command which asks for user and password and generates a regular htpasswd-file:

python -c 'import crypt,getpass; print(getpass.getpass("Name: ")+":"+crypt.crypt(getpass.getpass(),crypt.mksalt(crypt.METHOD_SHA512)))' >> htpasswd 

Works with all python versions > 2.5.

I did the same thing with NodeJs before:

 echo "console.log(require('crypto').createHmac('sha512', 'nonbase64key').update('password').digest('hex'))" | node 

it’s equivalent in python is:

python3 -c 'import hashlib;import base64;import hmac;print(hmac.new(b"nonbase64key", "password".encode(), hashlib.sha512).hexdigest())' 

And the equivalent shell command is:

echo -n "password" | openssl sha512 -hmac "nonbase64key" 

All examples will be using SHA-512, as password placeholder and as salt placeholder.

mkpasswd

Note: mkpasswd binary is installed via the package whois on Debian / Ubuntu only. On other Linux distribution such as ArchLinux, Fedora, CentOS, openSUSE, etc. mkpasswd is provided by the expect package but is an totally different utility which is available as expect_mkpasswd on Debian / Ubuntu. whois of all other Linux distro doesn’t include mkpasswd but the source (C lang) can be found on the original repository https://github.com/rfc1036/whois.

# With a random password and random salt mkpasswd -m sha-512 # With a random random salt mkpasswd -m sha-512 '' # Choosing both password and salt mkpasswd -m sha-512 '' '' # Read password from stdin to avoid leaking it in shell command history mkpasswd -m sha-512 --stdin 

OpenSSL

# With a random random salt openssl passwd -6 '' # Choosing both password and salt openssl passwd -6 --salt '' '' # Read password from stdin to avoid leaking it in shell command history openssl passwd -6 -stdin openssl passwd -6 

Ruby

# With a random password and random salt ruby -e 'require "securerandom"; puts SecureRandom.alphanumeric(20).crypt("$6$" + rand(36 ** 8).to_s(36))' # With a random random salt ruby -e 'puts "".crypt("$6$" + rand(36 ** 8).to_s(36))' # Choosing both password and salt ruby -e 'puts "".crypt("$6$")' # Read password from stdin to avoid leaking it in shell command history ruby -e 'require "io/console"; puts IO::console.getpass.crypt("$6$" + rand(36 ** 8).to_s(36))' 

Note: for those who complains that Random#rand is a PRNG, you can use the secure SecureRandom#rand but it’s not very important is rand is used only to generate the salt which is publicly available in the hash at the end.

ruby -e 'require "securerandom"; puts "".crypt("$6$" + SecureRandom.rand(36 ** 8).to_s(36))' 

Perl

# Choosing both password and salt perl -le 'print crypt "", "\$6\$\$"' 

Python

# With a random random salt python -c 'import crypt; print(crypt.crypt("", crypt.mksalt(crypt.METHOD_SHA512)))' # Choosing both password and salt python -c 'import crypt; print(crypt.crypt("", "$6$"))' # Read password from stdin to avoid leaking it in shell command history python -c 'import crypt,getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512))) 

grub-crypt

Note: The grub package doesn’t include grub-crypt in many distros.

# With a random random salt # Read password from stdin to avoid leaking it in shell command history grub-crypt --sha-512 

Источник

Читайте также:  Файловый сервер linux настройка

/etc/shadow – HowTo: Generate Password Hash in Linux

Linux stores users’ encrypted passwords, as well as other security information, such as account or password expiration values, in the /etc/shadow file.

Someday you may need to edit the /etc/shadow file manually to set or change ones password.

Unlike the /etc/passwd that is readable for everyone, the /etc/shadow file MUST be readable by the ROOT user only.

For this you would have to generate password hash in the format compatible with /etc/shadow .

Cool Tip: Want to create a USER with ROOT privileges? This can be very dangerous! But if you insist… Read more →

There is no need to install any additional tools as it can be easily done from the Linux command line using Python.

Generate Password Hash for /etc/shadow

The $ID indicates the type of encryption, the $SALT is a random (up to 16 characters) string and $ENCRYPTED is a password’s hash.

Hash Type ID Hash Length
MD5 $1 22 characters
SHA-256 $5 43 characters
SHA-512 $6 86 characters

Cool Tip: Got a hash but don’t know what type is it? Find out how to easily identify different hash types! Read more →

Use the below commands from the Linux shell to generate hashed password for /etc/shadow with the random salt.

Generate MD5 password hash:

python -c "import random,string,crypt; randomsalt = ''.join(random.sample(string.ascii_letters,8)); print crypt.crypt('MySecretPassword', '\$1\$%s\$' % randomsalt)" --- $1$YjOzcqrf$Zqx4sx5CQRuEIFCdOLAJV0

Generate SHA-256 password hash:

python -c "import random,string,crypt; randomsalt = ''.join(random.sample(string.ascii_letters,8)); print crypt.crypt('MySecretPassword', '\$5\$%s\$' % randomsalt)" --- $5$LgsPuaeR$OCtm.3tpbS/wyOZAIy6dsVNP4x0GyohyGebkIz15e88

Generate SHA-512 password hash:

python -c "import random,string,crypt; randomsalt = ''.join(random.sample(string.ascii_letters,8)); print crypt.crypt('MySecretPassword', '\$6\$%s\$' % randomsalt)" --- $6$HMpFTkgb$WqzuqMqYbjWsXFrOtvZPo.1gIkH6HiXJGr4QPv.k26jE.3mE.sdf3dds[. ]

Hope these commands will be helpful.

Just don’t forget to replace MySecretPassword with YourSecretPassword.

As you can see, it is really very easy to generate hashes for the /etc/shadow from the Linux command line using Python.

Particularly for the reason that the Python is installed by default on the most Linux distributions.

Источник

Оцените статью
Adblock
detector