Example #1
0
    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))
Example #2
0
	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 ) )
Example #3
0
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
Example #4
0
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()
Example #6
0
def Main():
    cgiEnv = lib_common.CgiEnv(can_process_remote=True)

    server = cgiEnv.m_entity_id_dict["Domain"]
    groupName = cgiEnv.m_entity_id_dict["Name"]

    grph = cgiEnv.GetGraph()

    # http://www.math.uiuc.edu/~gfrancis/illimath/windows/aszgard_mini/movpy-2.0.0-py2.4.4/movpy/lib/win32/Demos/win32netdemo.py

    # hostname = "Titi" for example
    try:
        lib_win32.WNetAddConnect(server)
    except:
        exc = sys.exc_info()[1]
        lib_common.ErrorMessageHtml("Server=%s Caught:%s" % (server, str(exc)))

    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)

    # nodeGroup = serverBox.GroupUri( groupName )
    # nodeGroup = survol_Win32_Group.MakeUri( groupName, servName_or_None )
    nodeGroup = survol_Win32_Group.MakeUri(groupName, servNameNotNone)

    try:
        memberresume = 0
        while True:
            memberData, total, memberResume = win32net.NetLocalGroupGetMembers(
                servName_or_None, groupName, 2, memberresume)
            for member in memberData:
                sidUsage = member['sidusage']
                # Converts Sid to username
                try:
                    memberName, domain, type = win32security.LookupAccountSid(
                        server, member['sid'])
                except Exception:
                    exc = sys.exc_info()[1]
                    ERROR("Server=%s Caught:%s", server, str(exc))
                    continue

                DEBUG("Member: %s:", str(member))
                DEBUG("Lookup: %s: %s", memberName, member['domainandname'])
                # nodeUser = serverBox.UserUri( userName )

                DEBUG("servNameNotNone=%s", servNameNotNone)
                memberNode = MemberNameToNode(sidUsage, memberName,
                                              servNameNotNone)

                grph.add((memberNode, pc.property_group, nodeGroup))
                grph.add((memberNode, lib_common.MakeProp("SID Usage"),
                          lib_common.NodeLiteral(SidUsageToString(sidUsage))))
                grph.add(
                    (memberNode, lib_common.MakeProp("Security Identifier"),
                     lib_common.NodeLiteral(member['sid'])))

                if servName_or_None:
                    nodeMemberRemote = MemberNameToNodeRemote(
                        sidUsage, memberName, servName_or_None, serverBox)
                    # TODO: Instead, both object must have the same universal alias
                    grph.add((memberNode, pc.property_alias, nodeMemberRemote))

            if memberResume == 0:
                break
    except Exception:
        exc = sys.exc_info()[1]
        lib_common.ErrorMessageHtml("win32 local groups:" + str(exc))

    cgiEnv.OutCgiRdf("LAYOUT_SPLINE")
Example #7
0
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")
Example #8
0
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")
Example #9
0
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")
Example #10
0
def Main():
    cgiEnv = lib_common.ScriptEnvironment(can_process_remote = True)

    server = cgiEnv.m_entity_id_dict["Domain"]
    group_name = cgiEnv.m_entity_id_dict["Name"]

    grph = cgiEnv.GetGraph()

    try:
        lib_win32.WNetAddConnect(server)
    except Exception as exc:
        lib_common.ErrorMessageHtml("Server=%s Caught:%s" % (server, str(exc)))

    if lib_util.is_local_address(server):
        serv_name_or_none = None

        # So it is compatible with WMI: ".home" removal.
        serv_name_not_none = lib_uris.TruncateHostname(lib_util.currentHostname)
    else:
        serv_name_or_none = server
        serv_name_not_none = server
    server_box = lib_uris.MachineBox(server)

    node_group = survol_Win32_Group.MakeUri(group_name, serv_name_not_none)

    try:
        memberresume = 0
        while True:
            member_data, total, member_resume = win32net.NetLocalGroupGetMembers(
                serv_name_or_none, group_name, 2, memberresume)

            prop_sid_usage = lib_common.MakeProp("SID Usage")
            prop_security_identifier = lib_common.MakeProp("Security Identifier")

            for member in member_data:
                sid_usage = member['sidusage']
                # Converts Sid to username
                try:
                    member_name, domain, type = win32security.LookupAccountSid(server, member['sid'])
                except Exception as exc:
                    logging.error("Server=%s Caught:%s", server, str(exc))
                    continue

                logging.debug("Member: %s:", str(member))
                logging.debug("Lookup: %s: %s", member_name, member['domainandname'])

                member_node = _member_name_to_node(sid_usage, member_name, serv_name_not_none)

                grph.add((member_node, pc.property_group, node_group))
                grph.add((member_node, prop_sid_usage, lib_util.NodeLiteral(_sid_usage_to_string(sid_usage))))
                grph.add((member_node, prop_security_identifier, lib_util.NodeLiteral(member['sid'])))

                if serv_name_or_none:
                    node_member_remote = serv_name_or_none(sid_usage, member_name, serv_name_or_none, server_box)
                    # TODO: Instead, both object must have the same universal alias
                    grph.add((member_node, pc.property_alias, node_member_remote))

            if member_resume == 0:
                break
    except Exception as exc:
        lib_common.ErrorMessageHtml("win32 local groups:" + str(exc))

    cgiEnv.OutCgiRdf("LAYOUT_SPLINE")
Example #11
0
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