LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2010, 05:19 AM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
What is the easiest way to replace a hash in a shadow file, not using passwd?


What is the easiest way to replace a hash in a shadow file for one particular user, not using passwd, and when the current password is unknown?

TIA
 
Old 08-05-2010, 06:46 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Um, you have to be root to be able to do anything with /etc/shadow, so why not simply use passwd?
Code:
passwd user_name               < this lets root change it
passwd -l user_name            < this lock it (sticks an '!' in the password field)
passwd -d user_name            < this delete the password
There are other options, see man passwd (as root).

If you really want to fiddle around with /etc/shadow, carefully, carefully, you have to be root and you can simply edit it with vi; thus, if you can generate a legit hash and you're root, you can simply edit that into the password field.

Here's a little C program that will generate a hash:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <time.h>
#define _XOPEN_SOURCE
#include <unistd.h>

extern  char    *crypt  (const char *, const char *);

void    main    (int argc, char *argv [])
{
        char    salt [3];
        char    *passwd, *encryptedpw;
        char    *user;
        int     i;

        /*      seed the random number generator        */
        srand ((int) time ((unsigned int) NULL));
        /*
         *      we need two random numbers in the range
         *      >= 65 <= 90 or >= 97 <= 122 (that's A - Z
         *      or a - z inclusive) for the salt characters
        */
        while ((i = rand()) < 65 ||
                i > 90 && i < 97 ||
                i > 122)
                ;
        salt [0] = i;
        while ((i = rand()) < 65 ||
                i > 90 && i < 97 ||
                i > 122)
                ;
        salt [1] = i;
        salt [2] = '\0';
        /*      find out who we are                     */
        if ((user = getenv ("USER")) == (char *) NULL) {
                (void) fprintf (stderr,
                    "%s:\tunable to determine user id\n",
                    argv [0]);
                exit (EXIT_FAILURE);
        }
        /*      ask for the password                    */
        passwd = getpass ("Password to encrypt: ");
        /*      crypt() only looks at the first two characters of salt  */
        encryptedpw = crypt (passwd, salt);
        (void) fprintf (stdout, "%s:%s\n", user, encryptedpw);
        exit (EXIT_SUCCESS);
}
Save the above in some "file.c" then
Code:
cc -o file file.c -lcrypt
Use at your own risk.

Hope this helps some.
 
Old 08-05-2010, 06:48 AM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Thanks! This was for an email password though, which has a separate user owned shadow file.
 
Old 08-05-2010, 06:56 AM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
perl -e '$var = crypt("password","\$1\$rAnd8chr\$"); print $var\n;'

then vim the shadow file and paste in the output from that previous command, if you aren't using md5 passwords change the $1$...$ to just two characters but not $1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shadow and passwd idlehands Linux - Security 2 07-28-2010 03:04 PM
What is used to create the shadow password hash?? helptonewbie Linux - General 11 08-17-2009 02:02 AM
/etc/shadow hash changes? Oxagast Linux - Security 1 12-28-2008 12:07 PM
changing the /etc/shadow hash algo. zerg4141 Linux - Security 2 08-07-2006 08:32 PM
/etc/passwd or /etc/shadow? tiger7007 Linux - Security 2 03-21-2004 04:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration