Impersonating a Different Logon

Tim Golden mail at timgolden.me.uk
Tue Apr 6 22:11:12 CEST 2010


On 06/04/2010 20:26, Kevin Holleran wrote:
> Hello,
>
> I am sweeping some of our networks to find devices.  When I find a
> device I try to connect to the registry using _winreg and then query a
> specific key that I am interested in.  This works great for machines
> that are on our domain, but there are left over machines that are
> stand alone and the credentials fail.  I understand you cannot pass in
> credentials with _winreg but is there a way to simulate a logon of
> another user (the machine's local admin) to query the registry?

The simplest may well be to use WMI (example from here):

http://timgolden.me.uk/python/wmi/cookbook.html#list-registry-keys

<code - untested>
import wmi

reg = wmi.WMI (
   "machine",
   user="machine\admin",
   password="Secret",
   namespace="DEFAULT"
).StdRegProv

result, names = reg.EnumKey (
   hDefKey=_winreg.HKEY_LOCAL_MACHINE,
   sSubKeyName="Software"
)
for name in names:
   print name

</code>

I can't try it out at the moment but in principle it should work.

TJG



More information about the Python-list mailing list