首页
社区
课程
招聘
6
[原创]在内核驱动中获取进程终端ID(SessionId)[新手发个贴]
发表于: 2013-3-14 10:52 7819

[原创]在内核驱动中获取进程终端ID(SessionId)[新手发个贴]

2013-3-14 10:52
7819

在内核驱动中获取进程终端ID(SessionId)

在驱动开发中为了支持RDP模式,经常在内核驱动中要获取一个进程的终端ID(SessionId),即任务管理器中的"会话ID"列。
在应用层获取SessionId的方法很多比如WTSEnumerateProcesses函数,但是在内核中的方法目前我只知道一方法,现在分享给大家,
关键技术是:通过ZwQueryInformationProcess或NtQueryInformationProcess方法,使用ProcessSessionInformation类型获取SessionId,废话少说帖上代码

typedef NTSTATUS (*QUERY_INFO_PROCESS) (
        __in HANDLE ProcessHandle,
        __in PROCESSINFOCLASS ProcessInformationClass,
        __out PVOID ProcessInformation,
        __in ULONG ProcessInformationLength,
        __out PULONG ReturnLength
        );

QUERY_INFO_PROCESS ZwQueryInformationProcess = NULL;

/************************************************************************
@method:GetProcessSessionId
                获取进程SessionId

@param:        __out PULONG pSessionId : 返回的进程SessionId
        __in DWORD dwProcessId : 进程Pid,0代表当前进程
               
@return:NTSTATUS
                  
@date:        [03/13/2013 zhansq]
************************************************************************/
NTSTATUS GetProcessSessionId(__out PULONG pSessionId,__in DWORD dwProcessId)
{
        NTSTATUS status = STATUS_SUCCESS;
        ULONG returnedLength = 0;
        PROCESS_SESSION_INFORMATION SessionInfo;
        HANDLE  hProcess = NULL;

        PAGED_CODE();
       
        //get ZwQueryInformationProcess address
        if (NULL == ZwQueryInformationProcess)
        {
                UNICODE_STRING routineName;
               
                RtlInitUnicodeString(&routineName,L"ZwQueryInformationProcess");

                if (KeGetCurrentIrql() != PASSIVE_LEVEL) return STATUS_UNSUCCESSFUL;

                ZwQueryInformationProcess = (QUERY_INFO_PROCESS)MmGetSystemRoutineAddress(&routineName);
                               
                if (NULL == ZwQueryInformationProcess)
                {
                        KdPrint(("MmGetSystemRoutineAddress : Cannot get ZwQueryInformationProcess\n"));
                        return STATUS_UNSUCCESSFUL;
                }
        }

        if (KeGetCurrentIrql() > APC_LEVEL) return STATUS_UNSUCCESSFUL;

        if (NULL == ZwQueryInformationProcess) return STATUS_UNSUCCESSFUL;

        if (dwProcessId != 0)
        {
                //ZwOpenProcess
                OBJECT_ATTRIBUTES ObjectAttributes;
                CLIENT_ID clientid;
                InitializeObjectAttributes(&ObjectAttributes, 0 ,OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, 0, 0);
                clientid.UniqueProcess = (HANDLE)dwProcessId;
                clientid.UniqueThread=0;

                status = ZwOpenProcess(&hProcess, PROCESS_ALL_ACCESS, &ObjectAttributes, &clientid);
                if (!NT_SUCCESS(status)) return status;
        }
        else
        {
                hProcess = ZwCurrentProcess();
        }

        returnedLength = sizeof(SessionInfo);
        status = ZwQueryInformationProcess(hProcess,
                ProcessSessionInformation,
                &SessionInfo,
                returnedLength,
                &returnedLength);
       
        if (NT_SUCCESS(status) && pSessionId != NULL)
        {
                //we got what we needed
                *pSessionId = SessionInfo.SessionId;
        }

        return status;       
}


[注意]看雪招聘,专注安全领域的专业人才平台!

上传的附件:
收藏
免费 6
支持
分享
赞赏记录
参与人
雪币
留言
时间
伟叔叔
为你点赞~
2024-5-31 07:18
心游尘世外
为你点赞~
2024-5-31 04:14
QinBeast
为你点赞~
2024-5-31 04:04
飘零丶
为你点赞~
2024-4-3 00:06
shinratensei
为你点赞~
2024-2-4 05:46
PLEBFE
为你点赞~
2023-3-7 00:35
最新回复 (1)
雪    币: 1140
活跃值: (3426)
能力值: ( LV12,RANK:385 )
在线值:
发帖
回帖
粉丝
2
1.导出但没有公开的函数:PsGetProcessSessionId
2.FltGetRequestorSessionId (Available in Microsoft Windows 7 and later versions of the Windows operating system.)
没有试验,不知可以不?
2013-11-14 17:39
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册