Access to the Shadow Password Database in Python


To access the UNIX shadow password database, we should use the spwd module. We need enough privileges to access this file. The shadow password database entries are like tuple like object.

To use the spwd module, we should import it using −

import spwd

The attributes of the shadow password database are −

Index Attribute & Description
0

sp_nam

The Login Name or the username of the user

1

sp_pwd

The Encrypted password

2

sp_lstchg

Date of last change

3

sp_min

Minimal number of days between changes of the password

4

sp_max

Maximum number of days between changes of the password

5

sp_warn

Warn about number of days to change the password

6

sp_inact

Number of days after the password expires until block of the account

7

p_expire

Number of days from 01/01/1997 until account is disabled

8

sp_flag

Reserved

Some methods of this module are −

Method spwd.getspnam(name)

This method will return the shadow password database entry for the given user name.

Method spwd.getspall()

This method will return the all available shadow password database entry.

Example Code

import spwd
print("Root: " + str(spwd.getspnam('root')) + '\n') #Password detail for root
for entry in spwd.getspall():
    print("Name: " + entry[0] + "\t\tPassword: " + entry.sp_pwdp)

Output

(To get the desired output, we should run the script in sudo mode)

$ sudo python3 example.py
Root: spwd.struct_spwd(sp_namp='root', sp_pwdp='!', sp_lstchg=17778, sp_min=0, sp_max=99999, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)

……..
……..
……..
Name: geoclue        Password: *
Name: gnome-initial-setup        Password: *
Name: gdm        Password: *
Name: unix_user        Password: $6$47n9s0Ep$znWkgNtywebHGKq2o6kZKhGOM8ryp8z4/6P6PUE1m.NQ5Erg9aWncNUAGbuNLFNWUO9M9xzKLxRpFGB5md/nu1
Name: mongodb        Password: !

Updated on: 30-Jul-2019

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements