[Samba] copy the password hash from one user to another user

Simon FONTENEAU sfonteneau at tranquil.it
Wed Aug 17 08:44:36 UTC 2022


python :

from samba.netcmd.user import GetPasswordCommand
from samba.samdb import SamDB
from samba.param import LoadParm
from samba.credentials import Credentials
from samba.param import LoadParm

lp = LoadParm()
creds = Credentials()
creds.guess(lp)

samdb = SamDB(url='/var/lib/samba/private/sam.ldb', 
session_info=system_session(),credentials=creds, lp=lp)

testpawd = GetPasswordCommand()
testpawd.lp = lp
for user in samdb.search(base=basedn, 
expression="(&(objectClass=user)(sAMAccountName=usernametest))", 
attrs=["sAMAccountName"]):
        passwordhash = 
testpawd.get_account_attributes(samdb,None,basedn,filter="(sAMAccountName=%s)" 
% 
(str(user["sAMAccountName"])),scope=ldb.SCOPE_SUBTREE,attrs=['unicodePwd'],decrypt=True)
        print(passwordhash)
        print(user["sAMAccountName"][0])


For assign a password hash to a user (bash) :

pdbedit --set-nt-hash=username "hash"

If you want to do it in python you will have to use the samr library


Simon



Le 09/08/2022 à 14:08, Marcos Ariel Negrini via samba a écrit :
> any idea?
>
> El 5/8/22 a las 10:49, Marcos Ariel Negrini via samba escribió:
>> Hello everyone:
>> for the integration of a system with samba4AD I need that when 
>> creating a user takes by default the password of another (for example 
>> that when creating the user B, is generated with the password of A). 
>> I was looking how to obtain the hash value of the password of 
>> A(ldbsearch -H /path/sam.ldb '(cn=A)' unicodepwd), but I did not find 
>> how to assign this hash to B. Is it possible to do this? I was trying 
>> to do all this with python, I managed without difficulty to change 
>> the password of B from a string, but not from the corresponding hash.
>> greetings
>>
>



More information about the samba mailing list