Skip to content

Commit

Permalink
dev: add scripts to play with win32 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Apr 24, 2024
1 parent 0707e08 commit 0620f8a
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 14 deletions.
106 changes: 106 additions & 0 deletions tests/dev/dev_win32_user_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import win32api
import win32net
import win32netcon
import win32security

server = None


def ServerEnum():
"Enumerates all servers on the network"
resume = 0
server = None

while 1:
data, total, resume = win32net.NetServerEnum(
server, 100, win32netcon.SV_TYPE_ALL, None, resume
)
for s in data:
print("Found server %s" % s["name"])
# Now loop over the shares.
shareresume = 0
while 1:
sharedata, total, shareresume = win32net.NetShareEnum(
server, 2, shareresume
)
for share in sharedata:
print(
" %(netname)s (%(path)s):%(remark)s - in use by %(current_uses)d users"
% share
)
if not shareresume:
break
if not resume:
break
print("Enumerated all the servers on the network")


def GetInfo(userName=None):
"Dumps level 3 information about the current user"
if userName is None:
userName = win32api.GetUserName()
print(f"Dumping level 3 information about user: {userName}")
info = win32net.NetUserGetInfo(None, userName, 3)
for key, val in info.items():
print("%s=%s" % (key, val))


def LocalGroupEnum():
"Enumerates all the local groups"
resume = 0
nmembers = 0
server = None

data, total, resume = win32net.NetLocalGroupEnum(server, 1, resume)
for group in data:
print("\n\tFound group: %(name)s " % group)
memberresume = 0
print(group.keys())
# while 1:
# memberdata, total, memberresume = win32net.NetLocalGroupGetMembers(server, group['name'], 2, resume)
# for member in memberdata:
# # Just for the sake of it, we convert the SID to a username
# username, domain, type = win32security.LookupAccountSid(server, member['sid'])
# nmembers = nmembers + 1
# print(" Member %s (%s)" % (username, member['domainandname']))
# if memberresume==0:
# break
# if not resume:
# break
assert nmembers, "Couldnt find a single member in a single group!"
print("Enumerated all the local groups")


def GroupEnum():
"Enumerates all the domain groups"
nmembers = 0
resume = 0
server = win32api.GetDomainName()

while 1:
data, total, resume = win32net.NetGroupEnum(server, 1, resume)
# print "Call to NetGroupEnum obtained %d entries of %d total" % (len(data), total)
for group in data:
print("Found group %(name)s:%(comment)s " % group)
memberresume = 0
while 1:
memberdata, total, memberresume = win32net.NetGroupGetUsers(
server, group["name"], 0, resume
)
for member in memberdata:
# print(" Member %(name)s" % member)
nmembers = nmembers + 1
if memberresume == 0:
break
if not resume:
break
assert nmembers, "Couldnt find a single member in a single group!"
print("Enumerated all the groups")


print(win32api.GetDomainName())

# print(GetInfo())
# ServerEnum()
# LocalGroupEnum()
GroupEnum()
40 changes: 26 additions & 14 deletions tests/dev/dev_win_user_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
- https://mhammond.github.io/pywin32/html/com/help/active_directory.html
"""


# standard
import getpass
import os
Expand All @@ -18,9 +17,11 @@
# 3rd party
import win32api
import win32com
import win32com.client
import win32net
import win32security
import wmi

# import wmi

# user_info = win32net.NetUserGetInfo(
# win32net.NetGetAnyDCName(), win32api.GetUserName(), 2
Expand All @@ -36,8 +37,8 @@
print(domain)


wmi_os = wmi.WMI().Win32_ComputerSystem()[0]
print(wmi_os.Name, "PartOfDomain?", wmi_os.PartOfDomain)
# wmi_os = wmi.WMI().Win32_ComputerSystem()[0]
# print(wmi_os.Name, "PartOfDomain?", wmi_os.PartOfDomain)

# variables
srv_ldap_host = "svcldap"
Expand All @@ -50,14 +51,17 @@ def construct_moniker():
return "".join(moniker)


obj = win32com.client.GetObject("winmgmts:")
# obj = win32com.client.GetObject("winmgmts:")
# print("obj: ", obj.IsClass)

# -- pywin32 (COM objects) : local groups
local_groups: list[str] = sorted(
set(win32net.NetUserGetLocalGroups(uname()[1], getpass.getuser()))
)
print(f"User's local groups using pywin32: {'; '.join(local_groups)}")
try:
local_groups: list[str] = sorted(
set(win32net.NetUserGetLocalGroups(uname()[1], getpass.getuser()))
)
print(f"User's local groups using pywin32: {'; '.join(local_groups)}")
except:
print("no local groups retrieved")

# uid = os.getuid()
# user = pwd.getpwuid(uid)
Expand Down Expand Up @@ -93,12 +97,20 @@ def construct_moniker():
# -- PYAD


from pyad import aduser
import pyad

try:
user = aduser.ADUser.from_cn(getpass.getuser())
print(user)
print(user.get_attribute("memberOf"))
user = pyad.aduser.ADUser.from_cn(getpass.getuser())
print(user.get_attribute("name")[0], user, type(user))
print(user.get_domain())
user_groups = user.get_memberOfs()
for grp in user_groups:
if not isinstance(grp, pyad.ADGroup):
print("ARRRR")
print(grp.get_group_scope())
print(grp.get_attribute("name"))
# print(user.get_attribute("memberOf"))
print(len(user.get_memberOfs()) == len(user.get_attribute("memberOf")))
except Exception as err:
print("Computer is not attached to any domain")

Expand Down Expand Up @@ -141,7 +153,7 @@ def is_computer_in_domain():
# # for group in group_membership:
# # print(group)

# -- PURE WIN32COM
# # -- PURE WIN32COM
# import win32com.client

# objRootDSE = win32com.client.GetObject("LDAP://RootDSE")
Expand Down

0 comments on commit 0620f8a

Please sign in to comment.