How to check password with Linux?

Clash Royale CLAN TAG#URR8PPP
I want to check, from the linux command line, if a given cleartext password is the same of a crypted password on a /etc/shadow
(I need this to authenticate web users. I'm running an embedded linux.)
I have access to the /etc/shadow file itself.
linux command-line password embedded
add a comment |
I want to check, from the linux command line, if a given cleartext password is the same of a crypted password on a /etc/shadow
(I need this to authenticate web users. I'm running an embedded linux.)
I have access to the /etc/shadow file itself.
linux command-line password embedded
Log in as the user with the password?
– Kusalananda
Sep 29 '11 at 14:39
The test must be done automatically, I can't manually type the password from the web server
– michelemarcon
Sep 29 '11 at 14:43
add a comment |
I want to check, from the linux command line, if a given cleartext password is the same of a crypted password on a /etc/shadow
(I need this to authenticate web users. I'm running an embedded linux.)
I have access to the /etc/shadow file itself.
linux command-line password embedded
I want to check, from the linux command line, if a given cleartext password is the same of a crypted password on a /etc/shadow
(I need this to authenticate web users. I'm running an embedded linux.)
I have access to the /etc/shadow file itself.
linux command-line password embedded
linux command-line password embedded
edited Sep 29 '11 at 16:17
rozcietrzewiacz
29.2k47292
29.2k47292
asked Sep 29 '11 at 14:33
michelemarconmichelemarcon
1,06062034
1,06062034
Log in as the user with the password?
– Kusalananda
Sep 29 '11 at 14:39
The test must be done automatically, I can't manually type the password from the web server
– michelemarcon
Sep 29 '11 at 14:43
add a comment |
Log in as the user with the password?
– Kusalananda
Sep 29 '11 at 14:39
The test must be done automatically, I can't manually type the password from the web server
– michelemarcon
Sep 29 '11 at 14:43
Log in as the user with the password?
– Kusalananda
Sep 29 '11 at 14:39
Log in as the user with the password?
– Kusalananda
Sep 29 '11 at 14:39
The test must be done automatically, I can't manually type the password from the web server
– michelemarcon
Sep 29 '11 at 14:43
The test must be done automatically, I can't manually type the password from the web server
– michelemarcon
Sep 29 '11 at 14:43
add a comment |
5 Answers
5
active
oldest
votes
You can easily extract the encrypted password with awk. You then need to extract the prefix $algorithm$salt$ (assuming that this system isn't using the traditional DES, which is strongly deprecated because it can be brute-forced these days).
correct=$(</etc/shadow awk -v user=bob -F : 'user == $1 print $2')
prefix=$correct%"$correct#$*$*$"
For password checking, the underlying C function is crypt, but there's no standard shell command to access it.
On the command line, you can use a Perl one-liner to invoke crypt on the password.
supplied=$(echo "$password" |
perl -e '$_ = <STDIN>; chomp; print crypt($_, $ARGV[0])' "$prefix")
if [ "$supplied" = "$correct" ]; then …
Since this can't be done in pure shell tools, if you have Perl available, you might as well do it all in Perl. (Or Python, Ruby, … whatever you have available that can call the crypt function.) Warning, untested code.
#!/usr/bin/env perl
use warnings;
use strict;
my @pwent = getpwnam($ARGV[0]);
if (!@pwent) die "Invalid username: $ARGV[0]n";
my $supplied = <STDIN>;
chomp($supplied);
if (crypt($supplied, $pwent[1]) eq $pwent[1])
exit(0);
else
print STDERR "Invalid password for $ARGV[0]n";
exit(1);
On an embedded system without Perl, I'd use a small, dedicated C program. Warning, typed directly into the browser, I haven't even tried to compile. This is meant to illustrate the necessary steps, not as a robust implementation!
/* Usage: echo password | check_password username */
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv)
char password[100];
struct spwd shadow_entry;
char *p, *correct, *supplied, *salt;
if (argc < 2) return 2;
/* Read the password from stdin */
p = fgets(password, sizeof(password), stdin);
if (p == NULL) return 2;
*p = 0;
/* Read the correct hash from the shadow entry */
shadow_entry = getspnam(username);
if (shadow_entry == NULL) return 1;
correct = shadow_entry->sp_pwdp;
/* Extract the salt. Remember to free the memory. */
salt = strdup(correct);
if (salt == NULL) return 2;
p = strchr(salt + 1, '$');
if (p == NULL) return 2;
p = strchr(p + 1, '$');
if (p == NULL) return 2;
p[1] = 0;
/*Encrypt the supplied password with the salt and compare the results*/
supplied = crypt(password, salt);
if (supplied == NULL) return 2;
return !!strcmp(supplied, correct);
A different approach is to use an existing program such as su or login. In fact, if you can, it would be ideal to arrange for the web application to perform whatever it needs via su -c somecommand username. The difficulty here is to feed the password to su; this requires a terminal. The usual tool to emulate a terminal is expect, but it's a big dependency for an embedded system. Also, while su is in BusyBox, it's often omitted because many of its uses require the BusyBox binary to be setuid root. Still, if you can do it, this is the most robust approach from a security point of view.
I like thesuapproach.
– Benjohn
Aug 1 '17 at 17:11
add a comment |
Have a look at man 5 shadow and man 3 crypt. From the latter, you can learn that password hashes in /etc/shadow have the following form:
$id$salt$encrypted
where id defines the type of encryption and, reading further, can be one of
ID | Method
---------------------------------------------------------
1 | MD5
2a | Blowfish (not in mainline glibc; added in some
| Linux distributions)
5 | SHA-256 (since glibc 2.7)
6 | SHA-512 (since glibc 2.7)
Depending on the type of hash, you need to use the appropriate function/tool for generating and verifying the password "by hand". If the system contains mkpasswd program, you can use it as suggested here. (You take the salt from the shadow file, if that wasn't obvious.) For example, with md5 passwords :
mkpasswd -5 <the_salt> <the_password>
will generate the string that should match /etc/shadow entry.
1
On my Debian wheezy I had a completely different syntax for the commandmkpasswd, which I had to install usingapt-get install whois. The command line for the shadow line<user>:$6$<salt>$<pwd>:wasmkpasswd -msha-512 <password> <salt>
– Daniel Alder
Jul 9 '14 at 9:34
add a comment |
There was a similar question asked on Stack Overflow. cluelessCoder provided a script using expect, which you may or may not have on your embedded system.
#!/bin/bash
#
# login.sh $USERNAME $PASSWORD
#this script doesn't work if it is run as root, since then we don't have to specify a pw for 'su'
if [ $(id -u) -eq 0 ]; then
echo "This script can't be run as root." 1>&2
exit 1
fi
if [ ! $# -eq 2 ]; then
echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
exit 1
fi
USERNAME=$1
PASSWORD=$2
#since we use expect inside a bash-script, we have to escape tcl-$.
expect << EOF
spawn su $USERNAME -c "exit"
expect "Password:"
send "$PASSWORDr"
#expect eof
set wait_result [wait]
# check if it is an OS error or a return code from our command
# index 2 should be -1 for OS erro, 0 for command return code
if [lindex $wait_result 2] == 0
exit [lindex $wait_result 3]
else
exit 1
EOF
add a comment |
Bear in mind that, assuming the system is properly configured, the program will need to be run as root.
A better solution than reading the shadow file directly and writing your own code around crypt would be to just use the pam bindings.
The squid tarball used to come with a simple CLI tool for verifying usernames/passwords using stdio - so simple to adapt to using arguments - although the version I hacked previously was hardly a pin-up poster for structured programming. A quick google and it looks like the more recent versions have been cleaned up significantly but still a few 'goto's in there.
add a comment |
#! /bin/bash
# (GPL3+) Alberto Salvia Novella (es20490446e)
passwordHash ()
password=$1
salt=$2
encryption=$3
hashes=$(echo $password
passwordIsValid ()
user=$1
password=$2
encryption=$(secret "encryption" $user)
salt=$(secret "salt" $user)
salted=$(secret "salted" $user)
hash=$(passwordHash $password $salt $encryption)
[ $salted = $hash ] && echo "true"
secret ()
secret=$1
user=$2
shadow=$(shadow $user)
if [ $secret = "encryption" ]; then
position=1
elif [ $secret = "salt" ]; then
position=2
elif [ $secret = "salted" ]; then
position=3
fi
echo $(substring $shadow "$" $position)
shadow ()
user=$1
shadow=$(cat /etc/shadow
substring ()
string=$1
separator=$2
position=$3
substring=$string//"$separator"/$'2'
IFS=$'2' read -a substring <<< "$substring"
echo $substring[$position]
passwordIsValid $@
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f21705%2fhow-to-check-password-with-linux%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can easily extract the encrypted password with awk. You then need to extract the prefix $algorithm$salt$ (assuming that this system isn't using the traditional DES, which is strongly deprecated because it can be brute-forced these days).
correct=$(</etc/shadow awk -v user=bob -F : 'user == $1 print $2')
prefix=$correct%"$correct#$*$*$"
For password checking, the underlying C function is crypt, but there's no standard shell command to access it.
On the command line, you can use a Perl one-liner to invoke crypt on the password.
supplied=$(echo "$password" |
perl -e '$_ = <STDIN>; chomp; print crypt($_, $ARGV[0])' "$prefix")
if [ "$supplied" = "$correct" ]; then …
Since this can't be done in pure shell tools, if you have Perl available, you might as well do it all in Perl. (Or Python, Ruby, … whatever you have available that can call the crypt function.) Warning, untested code.
#!/usr/bin/env perl
use warnings;
use strict;
my @pwent = getpwnam($ARGV[0]);
if (!@pwent) die "Invalid username: $ARGV[0]n";
my $supplied = <STDIN>;
chomp($supplied);
if (crypt($supplied, $pwent[1]) eq $pwent[1])
exit(0);
else
print STDERR "Invalid password for $ARGV[0]n";
exit(1);
On an embedded system without Perl, I'd use a small, dedicated C program. Warning, typed directly into the browser, I haven't even tried to compile. This is meant to illustrate the necessary steps, not as a robust implementation!
/* Usage: echo password | check_password username */
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv)
char password[100];
struct spwd shadow_entry;
char *p, *correct, *supplied, *salt;
if (argc < 2) return 2;
/* Read the password from stdin */
p = fgets(password, sizeof(password), stdin);
if (p == NULL) return 2;
*p = 0;
/* Read the correct hash from the shadow entry */
shadow_entry = getspnam(username);
if (shadow_entry == NULL) return 1;
correct = shadow_entry->sp_pwdp;
/* Extract the salt. Remember to free the memory. */
salt = strdup(correct);
if (salt == NULL) return 2;
p = strchr(salt + 1, '$');
if (p == NULL) return 2;
p = strchr(p + 1, '$');
if (p == NULL) return 2;
p[1] = 0;
/*Encrypt the supplied password with the salt and compare the results*/
supplied = crypt(password, salt);
if (supplied == NULL) return 2;
return !!strcmp(supplied, correct);
A different approach is to use an existing program such as su or login. In fact, if you can, it would be ideal to arrange for the web application to perform whatever it needs via su -c somecommand username. The difficulty here is to feed the password to su; this requires a terminal. The usual tool to emulate a terminal is expect, but it's a big dependency for an embedded system. Also, while su is in BusyBox, it's often omitted because many of its uses require the BusyBox binary to be setuid root. Still, if you can do it, this is the most robust approach from a security point of view.
I like thesuapproach.
– Benjohn
Aug 1 '17 at 17:11
add a comment |
You can easily extract the encrypted password with awk. You then need to extract the prefix $algorithm$salt$ (assuming that this system isn't using the traditional DES, which is strongly deprecated because it can be brute-forced these days).
correct=$(</etc/shadow awk -v user=bob -F : 'user == $1 print $2')
prefix=$correct%"$correct#$*$*$"
For password checking, the underlying C function is crypt, but there's no standard shell command to access it.
On the command line, you can use a Perl one-liner to invoke crypt on the password.
supplied=$(echo "$password" |
perl -e '$_ = <STDIN>; chomp; print crypt($_, $ARGV[0])' "$prefix")
if [ "$supplied" = "$correct" ]; then …
Since this can't be done in pure shell tools, if you have Perl available, you might as well do it all in Perl. (Or Python, Ruby, … whatever you have available that can call the crypt function.) Warning, untested code.
#!/usr/bin/env perl
use warnings;
use strict;
my @pwent = getpwnam($ARGV[0]);
if (!@pwent) die "Invalid username: $ARGV[0]n";
my $supplied = <STDIN>;
chomp($supplied);
if (crypt($supplied, $pwent[1]) eq $pwent[1])
exit(0);
else
print STDERR "Invalid password for $ARGV[0]n";
exit(1);
On an embedded system without Perl, I'd use a small, dedicated C program. Warning, typed directly into the browser, I haven't even tried to compile. This is meant to illustrate the necessary steps, not as a robust implementation!
/* Usage: echo password | check_password username */
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv)
char password[100];
struct spwd shadow_entry;
char *p, *correct, *supplied, *salt;
if (argc < 2) return 2;
/* Read the password from stdin */
p = fgets(password, sizeof(password), stdin);
if (p == NULL) return 2;
*p = 0;
/* Read the correct hash from the shadow entry */
shadow_entry = getspnam(username);
if (shadow_entry == NULL) return 1;
correct = shadow_entry->sp_pwdp;
/* Extract the salt. Remember to free the memory. */
salt = strdup(correct);
if (salt == NULL) return 2;
p = strchr(salt + 1, '$');
if (p == NULL) return 2;
p = strchr(p + 1, '$');
if (p == NULL) return 2;
p[1] = 0;
/*Encrypt the supplied password with the salt and compare the results*/
supplied = crypt(password, salt);
if (supplied == NULL) return 2;
return !!strcmp(supplied, correct);
A different approach is to use an existing program such as su or login. In fact, if you can, it would be ideal to arrange for the web application to perform whatever it needs via su -c somecommand username. The difficulty here is to feed the password to su; this requires a terminal. The usual tool to emulate a terminal is expect, but it's a big dependency for an embedded system. Also, while su is in BusyBox, it's often omitted because many of its uses require the BusyBox binary to be setuid root. Still, if you can do it, this is the most robust approach from a security point of view.
I like thesuapproach.
– Benjohn
Aug 1 '17 at 17:11
add a comment |
You can easily extract the encrypted password with awk. You then need to extract the prefix $algorithm$salt$ (assuming that this system isn't using the traditional DES, which is strongly deprecated because it can be brute-forced these days).
correct=$(</etc/shadow awk -v user=bob -F : 'user == $1 print $2')
prefix=$correct%"$correct#$*$*$"
For password checking, the underlying C function is crypt, but there's no standard shell command to access it.
On the command line, you can use a Perl one-liner to invoke crypt on the password.
supplied=$(echo "$password" |
perl -e '$_ = <STDIN>; chomp; print crypt($_, $ARGV[0])' "$prefix")
if [ "$supplied" = "$correct" ]; then …
Since this can't be done in pure shell tools, if you have Perl available, you might as well do it all in Perl. (Or Python, Ruby, … whatever you have available that can call the crypt function.) Warning, untested code.
#!/usr/bin/env perl
use warnings;
use strict;
my @pwent = getpwnam($ARGV[0]);
if (!@pwent) die "Invalid username: $ARGV[0]n";
my $supplied = <STDIN>;
chomp($supplied);
if (crypt($supplied, $pwent[1]) eq $pwent[1])
exit(0);
else
print STDERR "Invalid password for $ARGV[0]n";
exit(1);
On an embedded system without Perl, I'd use a small, dedicated C program. Warning, typed directly into the browser, I haven't even tried to compile. This is meant to illustrate the necessary steps, not as a robust implementation!
/* Usage: echo password | check_password username */
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv)
char password[100];
struct spwd shadow_entry;
char *p, *correct, *supplied, *salt;
if (argc < 2) return 2;
/* Read the password from stdin */
p = fgets(password, sizeof(password), stdin);
if (p == NULL) return 2;
*p = 0;
/* Read the correct hash from the shadow entry */
shadow_entry = getspnam(username);
if (shadow_entry == NULL) return 1;
correct = shadow_entry->sp_pwdp;
/* Extract the salt. Remember to free the memory. */
salt = strdup(correct);
if (salt == NULL) return 2;
p = strchr(salt + 1, '$');
if (p == NULL) return 2;
p = strchr(p + 1, '$');
if (p == NULL) return 2;
p[1] = 0;
/*Encrypt the supplied password with the salt and compare the results*/
supplied = crypt(password, salt);
if (supplied == NULL) return 2;
return !!strcmp(supplied, correct);
A different approach is to use an existing program such as su or login. In fact, if you can, it would be ideal to arrange for the web application to perform whatever it needs via su -c somecommand username. The difficulty here is to feed the password to su; this requires a terminal. The usual tool to emulate a terminal is expect, but it's a big dependency for an embedded system. Also, while su is in BusyBox, it's often omitted because many of its uses require the BusyBox binary to be setuid root. Still, if you can do it, this is the most robust approach from a security point of view.
You can easily extract the encrypted password with awk. You then need to extract the prefix $algorithm$salt$ (assuming that this system isn't using the traditional DES, which is strongly deprecated because it can be brute-forced these days).
correct=$(</etc/shadow awk -v user=bob -F : 'user == $1 print $2')
prefix=$correct%"$correct#$*$*$"
For password checking, the underlying C function is crypt, but there's no standard shell command to access it.
On the command line, you can use a Perl one-liner to invoke crypt on the password.
supplied=$(echo "$password" |
perl -e '$_ = <STDIN>; chomp; print crypt($_, $ARGV[0])' "$prefix")
if [ "$supplied" = "$correct" ]; then …
Since this can't be done in pure shell tools, if you have Perl available, you might as well do it all in Perl. (Or Python, Ruby, … whatever you have available that can call the crypt function.) Warning, untested code.
#!/usr/bin/env perl
use warnings;
use strict;
my @pwent = getpwnam($ARGV[0]);
if (!@pwent) die "Invalid username: $ARGV[0]n";
my $supplied = <STDIN>;
chomp($supplied);
if (crypt($supplied, $pwent[1]) eq $pwent[1])
exit(0);
else
print STDERR "Invalid password for $ARGV[0]n";
exit(1);
On an embedded system without Perl, I'd use a small, dedicated C program. Warning, typed directly into the browser, I haven't even tried to compile. This is meant to illustrate the necessary steps, not as a robust implementation!
/* Usage: echo password | check_password username */
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv)
char password[100];
struct spwd shadow_entry;
char *p, *correct, *supplied, *salt;
if (argc < 2) return 2;
/* Read the password from stdin */
p = fgets(password, sizeof(password), stdin);
if (p == NULL) return 2;
*p = 0;
/* Read the correct hash from the shadow entry */
shadow_entry = getspnam(username);
if (shadow_entry == NULL) return 1;
correct = shadow_entry->sp_pwdp;
/* Extract the salt. Remember to free the memory. */
salt = strdup(correct);
if (salt == NULL) return 2;
p = strchr(salt + 1, '$');
if (p == NULL) return 2;
p = strchr(p + 1, '$');
if (p == NULL) return 2;
p[1] = 0;
/*Encrypt the supplied password with the salt and compare the results*/
supplied = crypt(password, salt);
if (supplied == NULL) return 2;
return !!strcmp(supplied, correct);
A different approach is to use an existing program such as su or login. In fact, if you can, it would be ideal to arrange for the web application to perform whatever it needs via su -c somecommand username. The difficulty here is to feed the password to su; this requires a terminal. The usual tool to emulate a terminal is expect, but it's a big dependency for an embedded system. Also, while su is in BusyBox, it's often omitted because many of its uses require the BusyBox binary to be setuid root. Still, if you can do it, this is the most robust approach from a security point of view.
edited Jun 29 '17 at 15:46
answered Sep 29 '11 at 22:29
GillesGilles
535k12810801598
535k12810801598
I like thesuapproach.
– Benjohn
Aug 1 '17 at 17:11
add a comment |
I like thesuapproach.
– Benjohn
Aug 1 '17 at 17:11
I like the
su approach.– Benjohn
Aug 1 '17 at 17:11
I like the
su approach.– Benjohn
Aug 1 '17 at 17:11
add a comment |
Have a look at man 5 shadow and man 3 crypt. From the latter, you can learn that password hashes in /etc/shadow have the following form:
$id$salt$encrypted
where id defines the type of encryption and, reading further, can be one of
ID | Method
---------------------------------------------------------
1 | MD5
2a | Blowfish (not in mainline glibc; added in some
| Linux distributions)
5 | SHA-256 (since glibc 2.7)
6 | SHA-512 (since glibc 2.7)
Depending on the type of hash, you need to use the appropriate function/tool for generating and verifying the password "by hand". If the system contains mkpasswd program, you can use it as suggested here. (You take the salt from the shadow file, if that wasn't obvious.) For example, with md5 passwords :
mkpasswd -5 <the_salt> <the_password>
will generate the string that should match /etc/shadow entry.
1
On my Debian wheezy I had a completely different syntax for the commandmkpasswd, which I had to install usingapt-get install whois. The command line for the shadow line<user>:$6$<salt>$<pwd>:wasmkpasswd -msha-512 <password> <salt>
– Daniel Alder
Jul 9 '14 at 9:34
add a comment |
Have a look at man 5 shadow and man 3 crypt. From the latter, you can learn that password hashes in /etc/shadow have the following form:
$id$salt$encrypted
where id defines the type of encryption and, reading further, can be one of
ID | Method
---------------------------------------------------------
1 | MD5
2a | Blowfish (not in mainline glibc; added in some
| Linux distributions)
5 | SHA-256 (since glibc 2.7)
6 | SHA-512 (since glibc 2.7)
Depending on the type of hash, you need to use the appropriate function/tool for generating and verifying the password "by hand". If the system contains mkpasswd program, you can use it as suggested here. (You take the salt from the shadow file, if that wasn't obvious.) For example, with md5 passwords :
mkpasswd -5 <the_salt> <the_password>
will generate the string that should match /etc/shadow entry.
1
On my Debian wheezy I had a completely different syntax for the commandmkpasswd, which I had to install usingapt-get install whois. The command line for the shadow line<user>:$6$<salt>$<pwd>:wasmkpasswd -msha-512 <password> <salt>
– Daniel Alder
Jul 9 '14 at 9:34
add a comment |
Have a look at man 5 shadow and man 3 crypt. From the latter, you can learn that password hashes in /etc/shadow have the following form:
$id$salt$encrypted
where id defines the type of encryption and, reading further, can be one of
ID | Method
---------------------------------------------------------
1 | MD5
2a | Blowfish (not in mainline glibc; added in some
| Linux distributions)
5 | SHA-256 (since glibc 2.7)
6 | SHA-512 (since glibc 2.7)
Depending on the type of hash, you need to use the appropriate function/tool for generating and verifying the password "by hand". If the system contains mkpasswd program, you can use it as suggested here. (You take the salt from the shadow file, if that wasn't obvious.) For example, with md5 passwords :
mkpasswd -5 <the_salt> <the_password>
will generate the string that should match /etc/shadow entry.
Have a look at man 5 shadow and man 3 crypt. From the latter, you can learn that password hashes in /etc/shadow have the following form:
$id$salt$encrypted
where id defines the type of encryption and, reading further, can be one of
ID | Method
---------------------------------------------------------
1 | MD5
2a | Blowfish (not in mainline glibc; added in some
| Linux distributions)
5 | SHA-256 (since glibc 2.7)
6 | SHA-512 (since glibc 2.7)
Depending on the type of hash, you need to use the appropriate function/tool for generating and verifying the password "by hand". If the system contains mkpasswd program, you can use it as suggested here. (You take the salt from the shadow file, if that wasn't obvious.) For example, with md5 passwords :
mkpasswd -5 <the_salt> <the_password>
will generate the string that should match /etc/shadow entry.
edited May 23 '17 at 12:40
Community♦
1
1
answered Sep 29 '11 at 16:27
rozcietrzewiaczrozcietrzewiacz
29.2k47292
29.2k47292
1
On my Debian wheezy I had a completely different syntax for the commandmkpasswd, which I had to install usingapt-get install whois. The command line for the shadow line<user>:$6$<salt>$<pwd>:wasmkpasswd -msha-512 <password> <salt>
– Daniel Alder
Jul 9 '14 at 9:34
add a comment |
1
On my Debian wheezy I had a completely different syntax for the commandmkpasswd, which I had to install usingapt-get install whois. The command line for the shadow line<user>:$6$<salt>$<pwd>:wasmkpasswd -msha-512 <password> <salt>
– Daniel Alder
Jul 9 '14 at 9:34
1
1
On my Debian wheezy I had a completely different syntax for the command
mkpasswd, which I had to install using apt-get install whois. The command line for the shadow line <user>:$6$<salt>$<pwd>: was mkpasswd -msha-512 <password> <salt>– Daniel Alder
Jul 9 '14 at 9:34
On my Debian wheezy I had a completely different syntax for the command
mkpasswd, which I had to install using apt-get install whois. The command line for the shadow line <user>:$6$<salt>$<pwd>: was mkpasswd -msha-512 <password> <salt>– Daniel Alder
Jul 9 '14 at 9:34
add a comment |
There was a similar question asked on Stack Overflow. cluelessCoder provided a script using expect, which you may or may not have on your embedded system.
#!/bin/bash
#
# login.sh $USERNAME $PASSWORD
#this script doesn't work if it is run as root, since then we don't have to specify a pw for 'su'
if [ $(id -u) -eq 0 ]; then
echo "This script can't be run as root." 1>&2
exit 1
fi
if [ ! $# -eq 2 ]; then
echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
exit 1
fi
USERNAME=$1
PASSWORD=$2
#since we use expect inside a bash-script, we have to escape tcl-$.
expect << EOF
spawn su $USERNAME -c "exit"
expect "Password:"
send "$PASSWORDr"
#expect eof
set wait_result [wait]
# check if it is an OS error or a return code from our command
# index 2 should be -1 for OS erro, 0 for command return code
if [lindex $wait_result 2] == 0
exit [lindex $wait_result 3]
else
exit 1
EOF
add a comment |
There was a similar question asked on Stack Overflow. cluelessCoder provided a script using expect, which you may or may not have on your embedded system.
#!/bin/bash
#
# login.sh $USERNAME $PASSWORD
#this script doesn't work if it is run as root, since then we don't have to specify a pw for 'su'
if [ $(id -u) -eq 0 ]; then
echo "This script can't be run as root." 1>&2
exit 1
fi
if [ ! $# -eq 2 ]; then
echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
exit 1
fi
USERNAME=$1
PASSWORD=$2
#since we use expect inside a bash-script, we have to escape tcl-$.
expect << EOF
spawn su $USERNAME -c "exit"
expect "Password:"
send "$PASSWORDr"
#expect eof
set wait_result [wait]
# check if it is an OS error or a return code from our command
# index 2 should be -1 for OS erro, 0 for command return code
if [lindex $wait_result 2] == 0
exit [lindex $wait_result 3]
else
exit 1
EOF
add a comment |
There was a similar question asked on Stack Overflow. cluelessCoder provided a script using expect, which you may or may not have on your embedded system.
#!/bin/bash
#
# login.sh $USERNAME $PASSWORD
#this script doesn't work if it is run as root, since then we don't have to specify a pw for 'su'
if [ $(id -u) -eq 0 ]; then
echo "This script can't be run as root." 1>&2
exit 1
fi
if [ ! $# -eq 2 ]; then
echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
exit 1
fi
USERNAME=$1
PASSWORD=$2
#since we use expect inside a bash-script, we have to escape tcl-$.
expect << EOF
spawn su $USERNAME -c "exit"
expect "Password:"
send "$PASSWORDr"
#expect eof
set wait_result [wait]
# check if it is an OS error or a return code from our command
# index 2 should be -1 for OS erro, 0 for command return code
if [lindex $wait_result 2] == 0
exit [lindex $wait_result 3]
else
exit 1
EOF
There was a similar question asked on Stack Overflow. cluelessCoder provided a script using expect, which you may or may not have on your embedded system.
#!/bin/bash
#
# login.sh $USERNAME $PASSWORD
#this script doesn't work if it is run as root, since then we don't have to specify a pw for 'su'
if [ $(id -u) -eq 0 ]; then
echo "This script can't be run as root." 1>&2
exit 1
fi
if [ ! $# -eq 2 ]; then
echo "Wrong Number of Arguments (expected 2, got $#)" 1>&2
exit 1
fi
USERNAME=$1
PASSWORD=$2
#since we use expect inside a bash-script, we have to escape tcl-$.
expect << EOF
spawn su $USERNAME -c "exit"
expect "Password:"
send "$PASSWORDr"
#expect eof
set wait_result [wait]
# check if it is an OS error or a return code from our command
# index 2 should be -1 for OS erro, 0 for command return code
if [lindex $wait_result 2] == 0
exit [lindex $wait_result 3]
else
exit 1
EOF
edited May 23 '17 at 12:40
Community♦
1
1
answered Sep 29 '11 at 16:44
mr.Shumr.Shu
11111
11111
add a comment |
add a comment |
Bear in mind that, assuming the system is properly configured, the program will need to be run as root.
A better solution than reading the shadow file directly and writing your own code around crypt would be to just use the pam bindings.
The squid tarball used to come with a simple CLI tool for verifying usernames/passwords using stdio - so simple to adapt to using arguments - although the version I hacked previously was hardly a pin-up poster for structured programming. A quick google and it looks like the more recent versions have been cleaned up significantly but still a few 'goto's in there.
add a comment |
Bear in mind that, assuming the system is properly configured, the program will need to be run as root.
A better solution than reading the shadow file directly and writing your own code around crypt would be to just use the pam bindings.
The squid tarball used to come with a simple CLI tool for verifying usernames/passwords using stdio - so simple to adapt to using arguments - although the version I hacked previously was hardly a pin-up poster for structured programming. A quick google and it looks like the more recent versions have been cleaned up significantly but still a few 'goto's in there.
add a comment |
Bear in mind that, assuming the system is properly configured, the program will need to be run as root.
A better solution than reading the shadow file directly and writing your own code around crypt would be to just use the pam bindings.
The squid tarball used to come with a simple CLI tool for verifying usernames/passwords using stdio - so simple to adapt to using arguments - although the version I hacked previously was hardly a pin-up poster for structured programming. A quick google and it looks like the more recent versions have been cleaned up significantly but still a few 'goto's in there.
Bear in mind that, assuming the system is properly configured, the program will need to be run as root.
A better solution than reading the shadow file directly and writing your own code around crypt would be to just use the pam bindings.
The squid tarball used to come with a simple CLI tool for verifying usernames/passwords using stdio - so simple to adapt to using arguments - although the version I hacked previously was hardly a pin-up poster for structured programming. A quick google and it looks like the more recent versions have been cleaned up significantly but still a few 'goto's in there.
answered Sep 30 '11 at 16:02
symcbeansymcbean
2,28711221
2,28711221
add a comment |
add a comment |
#! /bin/bash
# (GPL3+) Alberto Salvia Novella (es20490446e)
passwordHash ()
password=$1
salt=$2
encryption=$3
hashes=$(echo $password
passwordIsValid ()
user=$1
password=$2
encryption=$(secret "encryption" $user)
salt=$(secret "salt" $user)
salted=$(secret "salted" $user)
hash=$(passwordHash $password $salt $encryption)
[ $salted = $hash ] && echo "true"
secret ()
secret=$1
user=$2
shadow=$(shadow $user)
if [ $secret = "encryption" ]; then
position=1
elif [ $secret = "salt" ]; then
position=2
elif [ $secret = "salted" ]; then
position=3
fi
echo $(substring $shadow "$" $position)
shadow ()
user=$1
shadow=$(cat /etc/shadow
substring ()
string=$1
separator=$2
position=$3
substring=$string//"$separator"/$'2'
IFS=$'2' read -a substring <<< "$substring"
echo $substring[$position]
passwordIsValid $@
add a comment |
#! /bin/bash
# (GPL3+) Alberto Salvia Novella (es20490446e)
passwordHash ()
password=$1
salt=$2
encryption=$3
hashes=$(echo $password
passwordIsValid ()
user=$1
password=$2
encryption=$(secret "encryption" $user)
salt=$(secret "salt" $user)
salted=$(secret "salted" $user)
hash=$(passwordHash $password $salt $encryption)
[ $salted = $hash ] && echo "true"
secret ()
secret=$1
user=$2
shadow=$(shadow $user)
if [ $secret = "encryption" ]; then
position=1
elif [ $secret = "salt" ]; then
position=2
elif [ $secret = "salted" ]; then
position=3
fi
echo $(substring $shadow "$" $position)
shadow ()
user=$1
shadow=$(cat /etc/shadow
substring ()
string=$1
separator=$2
position=$3
substring=$string//"$separator"/$'2'
IFS=$'2' read -a substring <<< "$substring"
echo $substring[$position]
passwordIsValid $@
add a comment |
#! /bin/bash
# (GPL3+) Alberto Salvia Novella (es20490446e)
passwordHash ()
password=$1
salt=$2
encryption=$3
hashes=$(echo $password
passwordIsValid ()
user=$1
password=$2
encryption=$(secret "encryption" $user)
salt=$(secret "salt" $user)
salted=$(secret "salted" $user)
hash=$(passwordHash $password $salt $encryption)
[ $salted = $hash ] && echo "true"
secret ()
secret=$1
user=$2
shadow=$(shadow $user)
if [ $secret = "encryption" ]; then
position=1
elif [ $secret = "salt" ]; then
position=2
elif [ $secret = "salted" ]; then
position=3
fi
echo $(substring $shadow "$" $position)
shadow ()
user=$1
shadow=$(cat /etc/shadow
substring ()
string=$1
separator=$2
position=$3
substring=$string//"$separator"/$'2'
IFS=$'2' read -a substring <<< "$substring"
echo $substring[$position]
passwordIsValid $@
#! /bin/bash
# (GPL3+) Alberto Salvia Novella (es20490446e)
passwordHash ()
password=$1
salt=$2
encryption=$3
hashes=$(echo $password
passwordIsValid ()
user=$1
password=$2
encryption=$(secret "encryption" $user)
salt=$(secret "salt" $user)
salted=$(secret "salted" $user)
hash=$(passwordHash $password $salt $encryption)
[ $salted = $hash ] && echo "true"
secret ()
secret=$1
user=$2
shadow=$(shadow $user)
if [ $secret = "encryption" ]; then
position=1
elif [ $secret = "salt" ]; then
position=2
elif [ $secret = "salted" ]; then
position=3
fi
echo $(substring $shadow "$" $position)
shadow ()
user=$1
shadow=$(cat /etc/shadow
substring ()
string=$1
separator=$2
position=$3
substring=$string//"$separator"/$'2'
IFS=$'2' read -a substring <<< "$substring"
echo $substring[$position]
passwordIsValid $@
answered Jan 17 at 11:38
Alberto Salvia NovellaAlberto Salvia Novella
1185
1185
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f21705%2fhow-to-check-password-with-linux%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Log in as the user with the password?
– Kusalananda
Sep 29 '11 at 14:39
The test must be done automatically, I can't manually type the password from the web server
– michelemarcon
Sep 29 '11 at 14:43