首页
社区
课程
招聘
[求助]只要是EtwRegistration类型的句柄都复制失败
发表于: 2016-1-11 21:35 4419

[求助]只要是EtwRegistration类型的句柄都复制失败

2016-1-11 21:35
4419
通过DuplicateHandle复制..
不知道哪出事了..显示感觉少了几个句柄 后面检查的时候发现这些少了的句柄都是EtwRegistration...
code
BOOL EnumHandle(DWORD pid)
{
	HMODULE hNtdll = 0;
	ULONG hNum = 0,ret = 0;//hNum ZwQueryInformationProcess return
	NTSTATUS state = 0;
	HANDLE TageHandle = 0;
	HANDLE hTest = 0;
	ULONG i = 0;
	POBJECT_NAME_INFORMATION ObjName;
	HANDLE hProcess = 0;
	ULONG RetVal = 0;
	POBJECT_TYPE_INFORMATION lpIn;
	ULONG  DupFaNum = 0; //复制句柄失败次数
	ZWQUERYINFORMATIONPROCESS ZwQueryInformationProcess;
	ZWQUERYOBJECT ZwQueryObject;

	hNtdll = GetModuleHandle("ntdll.dll");
	if(hNtdll == NULL)
	{
		printf("hNtdll is null\n");
		return FALSE;
	}

	ZwQueryInformationProcess = (ZWQUERYINFORMATIONPROCESS)GetProcAddress(hNtdll,"ZwQueryInformationProcess");
	ZwQueryObject = (ZWQUERYOBJECT)GetProcAddress(hNtdll,"ZwQueryObject");
	RtlAdjustPrivilege = (RTLADJUSTPRIVILEGE)GetProcAddress(hNtdll,"RtlAdjustPrivilege");

	if(ZwQueryInformationProcess == NULL || ZwQueryObject == NULL || RtlAdjustPrivilege == NULL)
	{
		printf("ZwAPI is null\n");
		return FALSE;
	}

	RtlAdjustPrivilege(20,1,0,&RetVal);//debug
	RtlAdjustPrivilege(19,1,0,&RetVal);

	hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
                            PROCESS_DUP_HANDLE, FALSE, pid);
	if(hProcess==0)
	{
		printf("OpenProcess is null;\n");
		return FALSE;
	}
	
	ObjName = (POBJECT_NAME_INFORMATION)malloc(0x2000);
	if(ObjName == 0)
	{
		printf("malloc is null\n");
		CloseHandle(hProcess);
		return FALSE;
	}
	state = ZwQueryInformationProcess(hProcess,ProcessHandleCount,&hNum,sizeof(hNum),&ret);
	if(!NT_SUCCESS(state))
	{
		printf("state is null\n");
		free(ObjName);
		CloseHandle(hProcess);
		return FALSE;
	}
	
	lpIn = (POBJECT_TYPE_INFORMATION)malloc(0x1000);
	if(lpIn==0)
	{
		free(ObjName);
		CloseHandle(hProcess);
		return FALSE;
	}

	while (i != hNum)
	{
		TageHandle = (HANDLE)((ULONG)TageHandle + 4);
		if(DuplicateHandle(hProcess,TageHandle,GetCurrentProcess(),&hTest,0,FALSE,DUPLICATE_SAME_ACCESS))
		{
			state = ZwQueryObject(hTest,ObjectTypeInformation,lpIn,0x1000,NULL);
			if(!NT_SUCCESS(state))
			{
				CloseHandle(hProcess);
				free(ObjName);
				free(lpIn);
				return FALSE;
			}
			wprintf(L"ObjectType:%wZ---",lpIn->TypeName);
			if(!wcscmp((wchar_t*)lpIn->TypeName.Buffer,L"Key"))
			{
				//printf("Find Key\n");
			}
			state = ZwQueryObject(hTest,ObjectNameInformation,ObjName,0x2000,NULL);
			if(!NT_SUCCESS(state))
			{
				i++;
				continue;
			}
			printf("Handle:0x%x---",(ULONG)TageHandle);
			wprintf(L"HandleName:%wZ\n",ObjName->Name);
			memset(ObjName,0,0x2000);
			i++;
		}else
		{
			DupFaNum++;
		}
		if(DupFaNum>50)//Can only judge the number of failures to avoid the death cycle
			break;

	}

	//EtwRegistration这个类型复制不过来 不知道怎么回事
	printf("\nReal HandleCount:%d---Enum HandleCount:%d\n",hNum,i);
	printf("EtwRegistration Handle Num:%d\n",hNum - i);
	free(ObjName);
	free(lpIn);
	CloseHandle(hProcess);
	return TRUE;
}

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 4
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
我是新手
不是很懂这些
帮楼主搜索了一下
找到了一些有用的资料:
ZwDuplicateObject也是存在一些弊端,例如,对于实现了OpenProcedure方法,且在方法中禁止打开的对象类型(例如ALPC Port对象、EtwRegistration对象),就无法进行duplicate,因为duplicate也会触发OpenProcedure和句柄增加

来自:http://blog.shajincheng.com/post/66.html
2016-1-28 11:47
0
雪    币: 256
活跃值: (48)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
3
原因是Etw是不支持读写的
2016-1-28 14:47
0
游客
登录 | 注册 方可回帖
返回
//