Stolas
Stolas

Reputation: 289

Change Windows password using Python

I am developing a little password manager tool using Python. It allows central password manager and single use passwords, therefor nobody will ever know the password of the server and we won't need to change all the passwords when an employee goes to an other employer.

Anyway, the design of the software was for it to Phone-Home (and thus elimiate a lot of Firewall/Natting issues) and run as a service. We can now sync the users of the system, verify if the passwords we got are correct. But the resetting of the password after the logging in is something we are struggling with at the moment.

win32.net NetUserchangePassword seemd to have what we want. As (I thought) it called "net user *". After testing I figured out it really called the netUserchangePassword feature of the msvc libs. The issue is that it doesnt work when I enter the old-password to be None.

The process runs as full-admin. And should be able to change user passwords without knowing the old (DISCLAIMER: I am from an unix background, thus root (superadmin) can do everything, ciorrect me if I am wrong with the Windows OS).

So, how can I as a super-admin change the password for localusers without knowing the old password using the Python Scripting Language?

Thanks!

Update:

def set_password(username, password):
    from win32com import adsi
    ads_obj = adsi.ADsGetObject("WinNT://localhost/%s,user" % username)
    ads_obj.Getinfo()
    ads_obj.Put('Password', password)
    ads_obj.SetInfo()


def verify_success(username, password):
    from win32security import LogonUser
    from win32con import LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT
    try:
        LogonUser(username, None, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT)
    except:
        return False
    return True

u = "test_usr"
p = "TheNewStrongP@S$w0rd!"
set_password(u, p)
if verify_success(u, p):
    print "W00t it workz"
else:
    print "Continue Googling"

Guess what, doesn't work.

Upvotes: 3

Views: 11515

Answers (2)

Liam
Liam

Reputation: 6429

that's going to work fine

import subprocess

subprocess.call("net users "+username+" "+password, shell = True)

Upvotes: 4

Stolas
Stolas

Reputation: 289

In the example. Change:

ads_obj.Put('Password', password)
ads_obj.SetInfo()

With:

ads_obj.SetPassword(password)

Issue solved :)

Upvotes: 2

Related Questions