Ejemplo n.º 1
0
def BuildSrvNetwork(machineName):
    sys.stderr.write("BuildSrvNetwork machineName=%s localhost=%s\n" %
                     (machineName, lib_util.currentHostname))

    machName_or_None, imper = lib_win32.MakeImpersonate(machineName)

    # SC_MANAGER_ENUMERATE_SERVICE
    hscm = win32service.OpenSCManager(machName_or_None, None, accessSCM)

    dictServiceToNode = BuildSrvDict(hscm, machineName)

    # Now links the services together.
    for serviceName in dictServiceToNode:
        # nodeService = dictServiceToNode[ serviceName ]
        # sys.stderr.write("BuildSrvNetwork serviceName=%s\n" % (serviceName))

        try:
            hdnSrv = win32service.OpenService(
                hscm, serviceName, win32service.SERVICE_ENUMERATE_DEPENDENTS)
            depSrvLst = win32service.EnumDependentServices(
                hdnSrv, win32service.SERVICE_STATE_ALL)

            for depSrv in depSrvLst:
                # sys.stderr.write("depSrv=%s\n" % ( depSrv[0] ) )
                subServiceName = depSrv[0]
                try:
                    nodeSubService = dictServiceToNode[subServiceName]
                except KeyError:
                    sys.stderr.write("Main=%s Sub=%s NOT CREATED\n" %
                                     (serviceName, subServiceName))
                    continue

                dictServiceToNode[subServiceName]["depends_in"].append(
                    serviceName)
                dictServiceToNode[serviceName]["depends_out"].append(
                    subServiceName)

            # NOT SURE ABOUT THIS, NEVER TESTED BUT SEEMS NECESSARY.
            win32service.CloseServiceHandle(hdnSrv)
        except:
            exc = sys.exc_info()
            # With wsgi and maybe cgi, many dependencies not seen. OK with Apache.
            # Why especially these ones which have a lot of dependencies ?
            # BuildSrvNetwork serviceName=RpcSs:
            # BuildSrvNetwork serviceName=RpcEptMapper
            # BuildSrvNetwork serviceName=DcomLaunch:
            # BuildSrvNetwork serviceName=pla:
            sys.stderr.write("BuildSrvNetwork serviceName=%s: Caught: %s\n" %
                             (serviceName, str(exc)))
            # pywintypes.error: (5, 'OpenService', 'Access is denied.')

            pass

    return dictServiceToNode
Ejemplo n.º 2
0
def BuildSrvNetwork(machine_name):
    logging.debug("BuildSrvNetwork machineName=%s localhost=%s", machine_name, lib_util.currentHostname)

    mach_name_or_none, imper = lib_win32.MakeImpersonate(machine_name)

    # SC_MANAGER_ENUMERATE_SERVICE
    hscm = win32service.OpenSCManager(mach_name_or_none, None, accessSCM)

    dict_service_to_node = _build_srv_dict(hscm, machine_name)

    # Now links the services together.
    for service_name in dict_service_to_node:
        # nodeService = dict_service_to_node[ service_name ]

        try:
            hdn_srv = win32service.OpenService(hscm, service_name, win32service.SERVICE_ENUMERATE_DEPENDENTS)
            dep_srv_lst = win32service.EnumDependentServices(hdn_srv, win32service.SERVICE_STATE_ALL)

            for dep_srv in dep_srv_lst:
                sub_service_name = dep_srv[0]
                try:
                    nodeSubService = dict_service_to_node[sub_service_name]
                except KeyError:
                    logging.warning("Main=%s Sub=%s NOT CREATED", service_name, sub_service_name)
                    continue

                dict_service_to_node[sub_service_name]["depends_in"].append(service_name)
                dict_service_to_node[service_name]["depends_out"].append(sub_service_name)
            win32service.CloseServiceHandle(hdn_srv)
        except Exception as exc:
            # With wsgi and maybe cgi, many dependencies not seen. OK with Apache.
            # Why especially these ones which have a lot of dependencies ?
            # BuildSrvNetwork service_name=RpcSs:
            # BuildSrvNetwork service_name=RpcEptMapper
            # BuildSrvNetwork service_name=DcomLaunch:
            # BuildSrvNetwork service_name=pla:
            logging.warning("BuildSrvNetwork service_name=%s: Caught: %s", service_name, str(exc) )
            # pywintypes.error: (5, 'OpenService', 'Access is denied.')

            pass

    return dict_service_to_node
Ejemplo n.º 3
0
def AddInfo(grph, node, entity_ids_arr):
    serviceNam = entity_ids_arr[0]
    sys.stderr.write("AddInfo serviceNam=%s\n" % serviceNam)

    machName_or_None, imper = lib_win32.MakeImpersonate("")
    hscm = win32service.OpenSCManager(machName_or_None, None, accessSCM)

    try:
        status = win32service.SERVICE_QUERY_CONFIG | win32service.SERVICE_QUERY_STATUS | win32service.SERVICE_INTERROGATE | win32service.SERVICE_ENUMERATE_DEPENDENTS
        hdnSrv = win32service.OpenService(hscm, serviceNam, status)
        lstSrvPairs = win32service.QueryServiceStatusEx(hdnSrv)
        win32service.CloseServiceHandle(hdnSrv)
    except Exception:
        exc = sys.exc_info()[1]
        # Probably "Access is denied"
        sys.stderr.write("AddInfo Caught:%s\n" % str(exc))
        lstSrvPairs = dict()
        try:
            lstSrvPairs["Status"] = str(exc[2])
        except:
            lstSrvPairs["Status"] = str(exc)

    # CheckPoint                0
    # ControlsAccepted          1
    # CurrentState              4
    # ProcessId              3176
    # ServiceFlags              0
    # ServiceSpecificExitCode	0
    # ServiceType              16
    # WaitHint                  0
    # Win32ExitCode             0
    for keySrv in lstSrvPairs:
        sys.stderr.write("AddInfo keySrv:%s\n" % keySrv)
        valSrv = lstSrvPairs[keySrv]
        if keySrv == "ProcessId":
            if int(valSrv) != 0:
                nodeProc = lib_common.gUriGen.PidUri(valSrv)
                grph.add((nodeProc, pc.property_pid,
                          lib_common.NodeLiteral(valSrv)))
                grph.add((node, lib_common.MakeProp(keySrv), nodeProc))
        elif keySrv == "ServiceType":
            svcTypSrc = ""
            svcTypInt = int(valSrv)
            if svcTypInt & win32service.SERVICE_KERNEL_DRIVER:
                svcTypSrc += "KERNEL_DRIVER "
            if svcTypInt & win32service.SERVICE_FILE_SYSTEM_DRIVER:
                svcTypSrc += "FILE_SYSTEM_DRIVER "
            #if svcTypInt & win32service.SERVICE_ADAPTER: svcTypSrc += "ADAPTER "
            #if svcTypInt & win32service.SERVICE_RECOGNIZER_DRIVER: svcTypSrc += "RECOGNIZER_DRIVER "
            if svcTypInt & win32service.SERVICE_WIN32_OWN_PROCESS:
                svcTypSrc += "WIN32_OWN_PROCESS "
            if svcTypInt & win32service.SERVICE_WIN32_SHARE_PROCESS:
                svcTypSrc += "WIN32_SHARE_PROCESS "
            if svcTypInt & win32service.SERVICE_WIN32: svcTypSrc += "WIN32 "
            if svcTypInt & win32service.SERVICE_INTERACTIVE_PROCESS:
                svcTypSrc += "INTERACTIVE_PROCESS "

            grph.add((node, lib_common.MakeProp(keySrv),
                      lib_common.NodeLiteral(svcTypSrc)))

        elif keySrv == "CurrentState":
            statesArray = ("SERVICE_STOPPED", "SERVICE_START_PENDING",
                           "SERVICE_STOP_PENDING", "SERVICE_RUNNING",
                           "SERVICE_CONTINUE_PENDING", "SERVICE_PAUSE_PENDING",
                           "SERVICE_PAUSED")

            # Fetches from the module a constant with this value.
            srcStatSrc = valSrv
            for srvStatVar in statesArray:
                if valSrv == getattr(win32service, srvStatVar):
                    srcStatSrc = srvStatVar
                    break
            grph.add((node, lib_common.MakeProp(keySrv),
                      lib_common.NodeLiteral(srcStatSrc)))

        else:
            grph.add((node, lib_common.MakeProp(keySrv),
                      lib_common.NodeLiteral(valSrv)))

    return