def add_file_owner_windows(): try: import win32api import win32con import win32security except ImportError: return from sources_types import Win32_UserAccount from sources_types import Win32_Group def SID_CodeToName(typeSID): mapSIDList = { win32security.SidTypeUser: "******", win32security.SidTypeGroup: "Group SID", win32security.SidTypeDomain: "Domain SID", win32security.SidTypeAlias: "Alias SID", win32security.SidTypeWellKnownGroup: "Well-known group", win32security.SidTypeDeletedAccount: "Deleted account", win32security.SidTypeInvalid: "Invalid SID", win32security.SidTypeUnknown: "Unknown type SID", win32security.SidTypeComputer: "Computer SID", # win32security.SidTypeLabel: "Mandatory integrity label SID" # NOT DEFINED } try: return mapSIDList[typeSID] except: return "Unknown SID" try: sd = win32security.GetFileSecurity (file_name, win32security.OWNER_SECURITY_INFORMATION) except Exception as exc: msg = str(exc) grph.add((file_node, pc.property_owner, lib_util.NodeLiteral(msg))) return owner_sid = sd.GetSecurityDescriptorOwner () account_name, domain_name, typeCode = win32security.LookupAccountSid(None, owner_sid) typ_nam = SID_CodeToName(typeCode) logging.debug("Domain=%s Name=%s Type=%s", domain_name, account_name, typ_nam) if typeCode == win32security.SidTypeUser: account_node = Win32_UserAccount.MakeUri(account_name, domain_name) elif typeCode == win32security.SidTypeGroup: account_node = Win32_Group.MakeUri(account_name, domain_name) elif typeCode == win32security.SidTypeWellKnownGroup: account_node = Win32_Group.MakeUri(account_name, domain_name) else: # What else can we do ? account_node = Win32_UserAccount.MakeUri(account_name, domain_name) # TODO: What can we do with the domain ? grph.add((account_node, lib_common.MakeProp("Domain"), lib_util.NodeLiteral(domain_name))) grph.add((account_node, lib_common.MakeProp("SID"), lib_util.NodeLiteral(typ_nam))) grph.add((file_node, pc.property_owner, account_node))
def AddFileOwnerWindows(grph, filNode, filNam): import win32api import win32con import win32security from sources_types import Win32_UserAccount from sources_types import Win32_Group def SID_CodeToName(typeSID): mapSIDList = { win32security.SidTypeUser: "******", win32security.SidTypeGroup: "Group SID", win32security.SidTypeDomain: "Domain SID", win32security.SidTypeAlias: "Alias SID", win32security.SidTypeWellKnownGroup: "Well-known group", win32security.SidTypeDeletedAccount: "Deleted account", win32security.SidTypeInvalid: "Invalid SID", win32security.SidTypeUnknown: "Unknown type SID", win32security.SidTypeComputer: "Computer SID", # win32security.SidTypeLabel: "Mandatory integrity label SID" # NOT DEFINED } try: return mapSIDList[typeSID] except: return "Unknown SID" #print "I am", win32api.GetUserNameEx (win32con.NameSamCompatible) try: sd = win32security.GetFileSecurity (filNam, win32security.OWNER_SECURITY_INFORMATION) except: exc = sys.exc_info()[1] msg = str(exc) grph.add( ( filNode, pc.property_owner, lib_common.NodeLiteral(msg) ) ) return owner_sid = sd.GetSecurityDescriptorOwner () accountName, domainName, typeCode = win32security.LookupAccountSid (None, owner_sid) typNam = SID_CodeToName(typeCode) sys.stderr.write("Domain=%s Name=%s Type=%s\n" % (domainName, accountName,typNam) ) if typeCode == win32security.SidTypeUser: accountNode = Win32_UserAccount.MakeUri(accountName,domainName) elif typeCode == win32security.SidTypeGroup: accountNode = Win32_Group.MakeUri(accountName,domainName) elif typeCode == win32security.SidTypeWellKnownGroup: accountNode = Win32_Group.MakeUri(accountName,domainName) else: # What else can we do ? accountNode = Win32_UserAccount.MakeUri(accountName,domainName) # TODO: What can we do with the domain ? grph.add( ( accountNode, lib_common.MakeProp("Domain"), lib_common.NodeLiteral(domainName) ) ) grph.add( ( accountNode, lib_common.MakeProp("SID"), lib_common.NodeLiteral(typNam) ) ) grph.add( ( filNode, pc.property_owner, accountNode ) )
def Main(): cgiEnv = lib_common.ScriptEnvironment() hostname = cgiEnv.GetId() node_host = lib_uris.gUriGen.HostnameUri(hostname) grph = cgiEnv.GetGraph() try: lib_win32.WNetAddConnect(hostname) except Exception as exc: lib_common.ErrorMessageHtml("NetUserEnum:" + str(exc)) # This could be a parameter. Hard-coded value for the moment. if lib_util.is_local_address(hostname): level = 2 # 1,2 else: level = 2 # 1,2 logging.debug("hostname=%s level=%d", hostname, level) resume_handle = 0 while True: try: # Connects a computer to or disconnects a computer from a shared resource, or displays information about computer connections. # The command also controls persistent net connections. Used without parameters, net use retrieves a list of network connections. # net use [{DeviceName | *}] [\\ComputerName\ShareName[\volume]] [{Password | *}]] [/user:[DomainName\]UserName] # [/user:[DottedDomainName\]UserName] [/user: [UserName@DottedDomainName] [/savecred] [/smartcard] [{/delete | /persistent:{yes | no}}] # https://mail.python.org/pipermail/python-win32/2003-April/000961.html lst_users, total, resume_handle = win32net.NetUserEnum( hostname, level, win32netcon.FILTER_NORMAL_ACCOUNT, resume_handle) except Exception as exc: lib_common.ErrorMessageHtml("NetUserEnum:" + str(exc)) for usr_elt in lst_users: # {'comment': u'Built-in account for administering the computer/domain', 'workstations': u'', 'country_code': 0L, 'last_logon': 1426 # 729970L, 'full_name': u'', 'parms': u'', 'code_page': 0L, 'priv': 2L, 'auth_flags': 0L, 'logon_server': u'\\\\*', 'home_dir': u'', ' # usr_comment': u'', 'acct_expires': 4294967295L, 'bad_pw_count': 0L, 'logon_hours': '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff # \xff\xff\xff\xff\xff\xff\xff\xff\xff', 'password': None, 'units_per_week': 168L, 'last_logoff': 0L, 'name': u'Administrator', 'max_s # torage': 4294967295L, 'num_logons': 11L, 'password_age': 191184801L, 'flags': 66083L, 'script_path': u''}, user_name = usr_elt['name'] node_user = survol_Win32_UserAccount.MakeUri(user_name, hostname) grph.add((node_host, pc.property_user, node_user)) try: txt_comment = usr_elt['comment'] grph.add((node_user, pc.property_information, lib_util.NodeLiteral(txt_comment))) except KeyError: pass if resume_handle == 0: break cgiEnv.OutCgiRdf()
def MemberNameToNode(sidUsage, memberName, servName): if sidUsage == 1 or sidUsage == 6: memberNode = survol_Win32_UserAccount.MakeUri(memberName, servName) elif sidUsage == 5 or sidUsage == 2: memberNode = survol_Win32_Group.MakeUri(memberName, servName) else: serverNode = lib_common.gUriGen.HostnameUri(servName) return memberNode
def Main(): cgiEnv = lib_common.CgiEnv(can_process_remote=True) try: # Exception if local machine. hostName = cgiEnv.m_entity_id_dict["Domain"] except KeyError: hostName = None if not hostName or lib_util.IsLocalAddress(hostName): serverBox = lib_common.gUriGen serverNode = lib_common.nodeMachine servName_or_None = None else: serverBox = lib_common.RemoteBox(hostName) serverNode = lib_common.gUriGen.HostnameUri(hostName) servName_or_None = hostName # hostname = "Titi" for example try: lib_win32.WNetAddConnect(hostName) except: lib_common.ErrorMessageHtml("Error WNetAddConnect %s:%s" % (hostName, str(sys.exc_info()))) userName = cgiEnv.m_entity_id_dict["Name"] DEBUG("hostName=%s userName=%s", hostName, userName) grph = cgiEnv.GetGraph() nodeUser = survol_Win32_UserAccount.MakeUri(userName, hostName) # TODO: Quid de NetUserGetGroups ?? # [(groupName, attribute), ...] = NetUserGetGroups(serverName, userName ) try: resuList = win32net.NetUserGetLocalGroups(servName_or_None, userName) except: lib_common.ErrorMessageHtml("Error:userName="******":servName_or_None=" + str(servName_or_None) + ":" + str(sys.exc_info())) for groupName in resuList: nodeGroup = survol_Win32_Group.MakeUri(groupName, hostName) grph.add((nodeUser, pc.property_group, nodeGroup)) if hostName: nodeGroupRemote = serverBox.UriMakeFromDict( "Win32_Group", { "Name": groupName, "Domain": hostName }) # TODO: Instead, both object must have the same universal alias grph.add((nodeGroup, pc.property_alias, nodeGroupRemote)) cgiEnv.OutCgiRdf()
def Main(): cgiEnv = lib_common.ScriptEnvironment(can_process_remote=True) try: # Exception if local machine. host_name = cgiEnv.m_entity_id_dict["Domain"] except KeyError: host_name = None if lib_util.is_local_address(host_name): server_box = lib_uris.gUriGen serv_name_or_none = None else: server_box = lib_common.RemoteBox(host_name) serv_name_or_none = host_name try: lib_win32.WNetAddConnect(host_name) except Exception as exc: lib_common.ErrorMessageHtml("Error WNetAddConnect %s:%s" % (host_name, str(exc))) user_name = cgiEnv.m_entity_id_dict["Name"] logging.debug("host_name=%s user_name=%s", host_name, user_name) grph = cgiEnv.GetGraph() node_user = survol_Win32_UserAccount.MakeUri(user_name, host_name) # TODO: And NetUserGetGroups ?? # [(group_name, attribute), ...] = NetUserGetGroups(serverName, user_name ) try: resu_list = win32net.NetUserGetLocalGroups(serv_name_or_none, user_name) except Exception as exc: lib_common.ErrorMessageHtml("Error:user_name=" + user_name + ":serv_name_or_none=" + str(serv_name_or_none) + ":" + str(exc)) for group_name in resu_list: node_group = survol_Win32_Group.MakeUri(group_name, host_name) grph.add((node_user, pc.property_group, node_group)) if host_name: node_group_remote = server_box.node_from_dict( "Win32_Group", { "Name": group_name, "Domain": host_name }) # TODO: Instead, both object must have the same universal alias grph.add((node_group, pc.property_alias, node_group_remote)) cgiEnv.OutCgiRdf()
def Main(): cgiEnv = lib_common.CgiEnv(can_process_remote=True) try: # Exception if local machine. hostName = cgiEnv.m_entity_id_dict["Domain"] except KeyError: hostName = None if not hostName or lib_util.IsLocalAddress(hostName): serverBox = lib_common.gUriGen serverNode = lib_common.nodeMachine servName_or_None = None else: serverBox = lib_common.RemoteBox(hostName) serverNode = lib_common.gUriGen.HostnameUri(hostName) servName_or_None = hostName # hostname = "Titi" for example # lib_win32.WNetAddConnect(hostName) userName = cgiEnv.m_entity_id_dict["Name"] grph = cgiEnv.GetGraph() nodeUser = survol_Win32_UserAccount.MakeUri(userName, hostName) try: infoList = win32net.NetUserGetInfo(servName_or_None, userName, 2) except: lib_common.ErrorMessageHtml("Error:" + str(sys.exc_info())) for infoKey in infoList: try: infoVal = infoList[infoKey] grph.add((nodeUser, lib_common.MakeProp(infoKey), lib_common.NodeLiteral(infoVal))) except: txtDisp = str(sys.exc_info()[1]) grph.add((nodeUser, lib_common.MakeProp(infoKey), lib_common.NodeLiteral(txtDisp))) cgiEnv.OutCgiRdf()
def Main(): cgiEnv = lib_common.ScriptEnvironment(can_process_remote=True) try: # Exception if local machine. host_name = cgiEnv.m_entity_id_dict["Domain"] except KeyError: host_name = None if lib_util.is_local_address(host_name): serv_name_or_none = None else: serv_name_or_none = host_name user_name = cgiEnv.m_entity_id_dict["Name"] grph = cgiEnv.GetGraph() node_user = survol_Win32_UserAccount.MakeUri(user_name, host_name) try: info_list = win32net.NetUserGetInfo(serv_name_or_none, user_name, 2) except Exception as exc: lib_common.ErrorMessageHtml("Error:" + str(exc)) for info_key in info_list: try: info_val = info_list[info_key] grph.add((node_user, lib_common.MakeProp(info_key), lib_util.NodeLiteral(info_val))) except Exception as exc: txt_disp = str(exc) grph.add((node_user, lib_common.MakeProp(info_key), lib_util.NodeLiteral(txt_disp))) cgiEnv.OutCgiRdf()
def Main(): cgiEnv = lib_common.CgiEnv() grph = cgiEnv.GetGraph() # TODO: Try this on a remote machine. server = None # Run on local machine for the moment. # servName_or_None is for Windows functions where the local host must be None. # servNameNotNone is for our URLs where the hostname must be explicit. if not server or lib_util.IsLocalAddress(server): servName_or_None = None # So it is compatible with WMI. servNameNotNone = lib_uris.TruncateHostname(lib_util.currentHostname) # .home serverNode = lib_common.nodeMachine serverBox = lib_common.gUriGen else: servName_or_None = server servNameNotNone = server serverNode = lib_common.gUriGen.HostnameUri(server) serverBox = lib_common.RemoteBox(server) resume = 0 numMembers = 0 while True: level = 1 data, total, resume = win32net.NetLocalGroupEnum( servName_or_None, level, resume) for group in data: # sys.stderr.write("Group %(name)s:%(comment)s\n" % group) # TODO: Not sure about the groupname syntax. groupName = group['name'] # nodeGroup = lib_common.gUriGen.GroupUri( groupName ) nodeGroup = survol_Win32_Group.MakeUri(groupName, servNameNotNone) grph.add((nodeGroup, pc.property_host, lib_common.nodeMachine)) groupComment = group['comment'] if groupComment != "": groupCommentMaxWidth = max(80, len(groupName)) if len(groupComment) > groupCommentMaxWidth: groupComment = groupComment[:groupCommentMaxWidth] + "..." grph.add((nodeGroup, pc.property_information, lib_common.NodeLiteral(groupComment))) memberresume = 0 while True: levelMember = 2 memberData, total, memberResume = win32net.NetLocalGroupGetMembers( server, group['name'], levelMember, memberresume) for member in memberData: # Converts Sid to username userName, domain, type = win32security.LookupAccountSid( servName_or_None, member['sid']) numMembers = numMembers + 1 # sys.stderr.write(" Member: %s: %s\n" % (userName, member['domainandname'])) # nodeUser = lib_common.gUriGen.UserUri( userName ) nodeUser = survol_Win32_UserAccount.MakeUri( userName, servNameNotNone) # TODO: Not sure about the property. # TODO: Not sure about the username syntax. grph.add((nodeUser, pc.property_group, nodeGroup)) if memberResume == 0: break if not resume: break cgiEnv.OutCgiRdf("LAYOUT_SPLINE")
def Main(): cgiEnv = lib_common.ScriptEnvironment(can_process_remote=True) server = cgiEnv.GetId() grph = cgiEnv.GetGraph() if lib_util.is_local_address(server): serv_name_or_none = None server_node = lib_common.nodeMachine else: serv_name_or_none = server server_node = lib_uris.gUriGen.HostnameUri(server) try: lib_win32.WNetAddConnect(serv_name_or_none) except Exception as exc: # Maybe the machine is not online. lib_common.ErrorMessageHtml(str(exc)) resume = 0 num_members = 0 try: while True: data, total, resume = win32net.NetLocalGroupEnum( serv_name_or_none, 1, resume) for group in data: logging.debug("Group %(name)s:%(comment)s", group) # TODO: Not sure about the groupname syntax. group_name = group['name'] logging.debug("group_name=%s", group_name) node_group = survol_Win32_Group.MakeUri(group_name, server) grph.add((node_group, pc.property_host, server_node)) group_comment = group['comment'] logging.debug("group_comment=%s", group_comment) if group_comment != "": group_comment_max_width = max(80, len(group_name)) if len(group_comment) > group_comment_max_width: group_comment = group_comment[: group_comment_max_width] + "..." grph.add((node_group, pc.property_information, lib_util.NodeLiteral(group_comment))) memberresume = 0 while True: member_data, total, member_resume = win32net.NetLocalGroupGetMembers( serv_name_or_none, group_name, 2, memberresume) for member in member_data: # Converts Sid to username num_members = num_members + 1 try: user_name, domain, the_type = win32security.LookupAccountSid( server, member['sid']) except Exception as exc: logging.warning( "Server=%s LookupAccountSid Caught:%s", server, str(exc)) continue logging.debug("Member: %s: %s server=%s", user_name, member['domainandname'], server) # node_user = serverBox.UserUri( user_name ) node_user = survol_Win32_UserAccount.MakeUri( user_name, server) # TODO: Not sure about the property. # TODO: Not sure about the username syntax. grph.add((node_user, pc.property_group, node_group)) if memberresume == 0: break if not resume: break except Exception as exc: lib_common.ErrorMessageHtml("win32 local groups:" + str(exc)) cgiEnv.OutCgiRdf("LAYOUT_SPLINE")
def Main(): cgiEnv = lib_common.CgiEnv() hostname = cgiEnv.GetId() nodeHost = lib_common.gUriGen.HostnameUri(hostname) grph = cgiEnv.GetGraph() try: # hostname = "Titi" for example lib_win32.WNetAddConnect(hostname) except: exc = sys.exc_info()[1] lib_common.ErrorMessageHtml("NetUserEnum:" + str(exc)) if lib_util.IsLocalAddress(hostname): level = 2 # 1,2 else: level = 2 # 1,2 sys.stderr.write("hostname=%s level=%d\n" % (hostname, level)) resumeHandle = 0 while True: try: # Maintenant, comme tous les autres appels remote a Windows, ca retourne "access denied" vers Titi. # Comme s'il y avait avant une connection implicite. # Ou bien un Impersonate() ? On en a vire un qui ne marchait quand machine locale. # Peut-etre que le process du serveur en beneficiait tout le temps ? # Et meme WMI fonctionnait sans meme entrer le mot de passe. # # Connects a computer to or disconnects a computer from a shared resource, or displays information about computer connections. # The command also controls persistent net connections. Used without parameters, net use retrieves a list of network connections. # net use [{DeviceName | *}] [\\ComputerName\ShareName[\volume]] [{Password | *}]] [/user:[DomainName\]UserName] # [/user:[DottedDomainName\]UserName] [/user: [UserName@DottedDomainName] [/savecred] [/smartcard] [{/delete | /persistent:{yes | no}}] # Ca marche si on fait ca avant: # net use \\Titi tXXXXXXa /user:Titi\[email protected] # https://mail.python.org/pipermail/python-win32/2003-April/000961.html lstUsers, total, resumeHandle = win32net.NetUserEnum( hostname, level, win32netcon.FILTER_NORMAL_ACCOUNT, resumeHandle) except: exc = sys.exc_info()[1] lib_common.ErrorMessageHtml("NetUserEnum:" + str(exc)) for usrElt in lstUsers: # {'comment': u'Built-in account for administering the computer/domain', 'workstations': u'', 'country_code': 0L, 'last_logon': 1426 # 729970L, 'full_name': u'', 'parms': u'', 'code_page': 0L, 'priv': 2L, 'auth_flags': 0L, 'logon_server': u'\\\\*', 'home_dir': u'', ' # usr_comment': u'', 'acct_expires': 4294967295L, 'bad_pw_count': 0L, 'logon_hours': '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff # \xff\xff\xff\xff\xff\xff\xff\xff\xff', 'password': None, 'units_per_week': 168L, 'last_logoff': 0L, 'name': u'Administrator', 'max_s # torage': 4294967295L, 'num_logons': 11L, 'password_age': 191184801L, 'flags': 66083L, 'script_path': u''}, userName = usrElt['name'] nodeUser = survol_Win32_UserAccount.MakeUri(userName, hostname) grph.add((nodeHost, pc.property_user, nodeUser)) try: txtComment = usrElt['comment'] grph.add((nodeUser, pc.property_information, lib_common.NodeLiteral(txtComment))) except KeyError: pass if resumeHandle == 0: break cgiEnv.OutCgiRdf()
def Main(): cgiEnv = lib_common.CgiEnv(can_process_remote=True) server = cgiEnv.GetId() grph = cgiEnv.GetGraph() if lib_util.IsLocalAddress(server): servName_or_None = None serverNode = lib_common.nodeMachine else: servName_or_None = server serverNode = lib_common.gUriGen.HostnameUri(server) # http://www.math.uiuc.edu/~gfrancis/illimath/windows/aszgard_mini/movpy-2.0.0-py2.4.4/movpy/lib/win32/Demos/win32netdemo.py # servName_or_None, imper = lib_win32.MakeImpersonate(server) # hostname = "Titi" for example try: lib_win32.WNetAddConnect(servName_or_None) except: # Maybe the machine is not online. exc = sys.exc_info()[1] lib_common.ErrorMessageHtml(str(exc)) resume = 0 numMembers = 0 try: while True: # data, total, resume = win32net.NetLocalGroupEnum(server, 1, resume) data, total, resume = win32net.NetLocalGroupEnum( servName_or_None, 1, resume) for group in data: DEBUG("Group %(name)s:%(comment)s", group) # TODO: Not sure about the groupname syntax. groupName = group['name'] DEBUG("groupName=%s", groupName) # nodeGroup = serverBox.GroupUri( groupName ) nodeGroup = survol_Win32_Group.MakeUri(groupName, server) grph.add((nodeGroup, pc.property_host, serverNode)) groupComment = group['comment'] DEBUG("groupComment=%s", groupComment) if groupComment != "": groupCommentMaxWidth = max(80, len(groupName)) if len(groupComment) > groupCommentMaxWidth: groupComment = groupComment[:groupCommentMaxWidth] + "..." grph.add((nodeGroup, pc.property_information, lib_common.NodeLiteral(groupComment))) memberresume = 0 while True: # memberData, total, memberResume = win32net.NetLocalGroupGetMembers(server, group['name'], 2, resume) memberData, total, memberResume = win32net.NetLocalGroupGetMembers( servName_or_None, groupName, 2, memberresume) for member in memberData: # Converts Sid to username numMembers = numMembers + 1 try: userName, domain, type = win32security.LookupAccountSid( server, member['sid']) except Exception: exc = sys.exc_info()[1] WARNING("Server=%s Caught:%s", server, str(exc)) continue DEBUG("Member: %s: %s server=%s", userName, member['domainandname'], server) # nodeUser = serverBox.UserUri( userName ) nodeUser = survol_Win32_UserAccount.MakeUri( userName, server) # TODO: Not sure about the property. # TODO: Not sure about the username syntax. grph.add((nodeUser, pc.property_group, nodeGroup)) if memberResume == 0: break if not resume: break except Exception: exc = sys.exc_info()[1] lib_common.ErrorMessageHtml("win32 local groups:" + str(exc)) cgiEnv.OutCgiRdf("LAYOUT_SPLINE")
def _member_name_to_node(sid_usage, member_name, serv_name): if sid_usage == 1 or sid_usage == 6: member_node = survol_Win32_UserAccount.MakeUri(member_name, serv_name) elif sid_usage == 5 or sid_usage == 2: member_node = survol_Win32_Group.MakeUri(member_name, serv_name) return member_node