首页 > 编程语言 >c++ x64 读取指定线程TEB地址

c++ x64 读取指定线程TEB地址

时间:2022-09-02 15:46:21浏览次数:130  
标签:INFORMATION THREAD ZwQueryInformationThread TEB hModule x64 c++ bi BASIC

调用微软未公开函数ZwQueryInformationThread
网上挺多帖子说得到的地址值为全c,查阅部分资料后发现64位系统与32位有一些区别,主要是_THREAD_BASIC_INFORMATION结构体的长度不同,各部分的大小有出入。
经更改后的代码如下:

点击查看代码
typedef enum _THREADINFOCLASS {
     ThreadBasicInformation = 0,
     ThreadTimes = 1,
     ThreadPriority = 2,
     ThreadBasePriority = 3,
     ThreadAffinityMask = 4,
     ThreadImpersonationToken = 5,
     ThreadDescriptorTableEntry = 6,
     ThreadEnableAlignmentFaultFixup = 7,
     ThreadEventPair_Reusable = 8,
     ThreadQuerySetWin32StartAddress = 9,
     ThreadZeroTlsCell = 10,
     ThreadPerformanceCount = 11,
     ThreadAmILastThread = 12,
     ThreadIdealProcessor = 13,
     ThreadPriorityBoost = 14,
     ThreadSetTlsArrayAddress = 15,   // Obsolete
     ThreadIsIoPending = 16,
     ThreadHideFromDebugger = 17,
     ThreadBreakOnTermination = 18,
     ThreadSwitchLegacyState = 19,
     ThreadIsTerminated = 20,
     ThreadLastSystemCall = 21,
     ThreadIoPriority = 22,
     ThreadCycleTime = 23,
     ThreadPagePriority = 24,
     ThreadActualBasePriority = 25,
     ThreadTebInformation = 26,
     ThreadCSwitchMon = 27,   // Obsolete
     ThreadCSwitchPmu = 28,
     ThreadWow64Context = 29,
     ThreadGroupInformation = 30,
     ThreadUmsInformation = 31,   // UMS
     ThreadCounterProfiling = 32,
     ThreadIdealProcessorEx = 33,
     ThreadCpuAccountingInformation = 34,
     ThreadSuspendCount = 35,
     ThreadActualGroupAffinity = 41,
     ThreadDynamicCodePolicyInfo = 42,
     MaxThreadInfoClass = 45,
} THREADINFOCLASS;

typedef NTSTATUS(WINAPI* ZWQUERYINFORMATIONTHREAD)(
     _In_      HANDLE          ThreadHandle,
     _In_      THREADINFOCLASS ThreadInformationClass,
     _In_      PVOID           ThreadInformation,
     _In_      ULONG           ThreadInformationLength,
     _Out_opt_ PULONG          ReturnLength
);

typedef struct _CLIENT_ID {
	QWORD UniqueProcess;
	QWORD UniqueThread;
} CLIENT_ID;


typedef struct _THREAD_BASIC_INFORMATION {
	LONG64                ExitStatus;
	PVOID                   TebBaseAddress;
	CLIENT_ID               ClientId;
	KAFFINITY               AffinityMask;
	LONG               Priority;
	LONG               BasePriority;
} THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION;
点击查看代码
ZWQUERYINFORMATIONTHREAD ZwQueryInformationThread;

HMODULE init(LPCSTR szFuncNmae)
{
	HMODULE hModule = LoadLibrary(_T("ntdll.dll"));
	if (hModule) {
		ZwQueryInformationThread = (ZWQUERYINFORMATIONTHREAD)GetProcAddress(hModule, szFuncNmae);
	}
	return hModule;
}

	HMODULE hModule = init("ZwQueryInformationThread");
	if (hModule && ZwQueryInformationThread)
	{
	THREAD_BASIC_INFORMATION bi;
	NT_TIB tib;
	DWORD lRet = NULL;
	ZeroMemory(&bi, sizeof(THREAD_BASIC_INFORMATION));

	NTSTATUS status = ZwQueryInformationThread(hThread, ThreadBasicInformation, &bi, sizeof(_THREAD_BASIC_INFORMATION), &lRet);
	if (status == STATUS_SUCCESS) {
	EnableDebugPrivilege(TRUE);
	HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, bi.ClientId.UniqueProcess);
	tebHandleString.Format(_T("0x%p"), bi.TebBaseAddress);
	}
}

参考:http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FThread%2FTHREAD_INFORMATION_CLASS.html
https://0xnope.top/2021/03/15/TEB/#Ntdll-NtCurrentTeb
https://www.geoffchappell.com/studies/windows/km/ntoskrnl/api/ps/psquery/class.htm?tx=133

标签:INFORMATION,THREAD,ZwQueryInformationThread,TEB,hModule,x64,c++,bi,BASIC
From: https://www.cnblogs.com/czyuQAQ/p/16638708.html

相关文章

  • 【C++】断言、likely等
    断言assert就是对表达式进行判断,如果条件不成立就会调用abort()中止程序运行,对于debug空指针有奇效,但是release版本不会用是一个宏而非函数五个要点:1.在函数开始时,监测......
  • SparkStreaming中的转换算子2--有状态的转换算子updateStateByKey
    将之前批次的状态保存,packageSparkStreaming.transimportorg.apache.spark.SparkConfimportorg.apache.spark.storage.StorageLevelimportorg.apache.spark.str......
  • 在 C# CLR 中学习 C++ 之了解 extern
    一:背景在CLR源码中有很多的extern和extern"C"这样的关键词,比如下面这些代码:externsize_tgc_global_mechanisms[MAX_GLOBAL_GC_MECHANISMS_COUNT];externDWO......
  • 【C++】智能指针
    这篇讲得很好https://blog.csdn.net/sjp11/article/details/123899141?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166201751616781790748003%2522%252C%2......
  • 35 | JAVA中的Collections(类似C++中的算法)
    CollectionsCollections是JDK提供的工具类,同样位于java.util包中。它提供了一系列静态方法,能更方便地操作各种集合。注意Collections结尾多了一个s,不是Collection!我们一......
  • 23 | JAVA集合简介(C++容器)
    集合简介C++中将容器分成三类:顺序、关联、hash。容器在JAVA中映射为集合。等号左边(接口)等号右边(实现)ListArrayList/LinkedListSetHashSet/TreeSet......
  • C++左值、右值引用
    概念左值与右值是相对于赋值运算符(=)与累加运算符(+=),以下内容统称为运算符。左值:存放于运算符左边的值,凡是能取地址(&)都是左值。右值:存放于运算符右边的值,不能取地址(&)都是......
  • C/C++中的自增自减运算符的前置后置问题
    前言在准备秋招的过程中,遇到双指针问题,发现自增自减运算符的前后置对于问题的解决有很大的影响,故写此文作为总结,方便后续查阅。正文一、前置后置的区别自增自减操作符......
  • c++常量引用容易踩的坑,修改数据不同步
    正常情况下被引用的对象改变,常量引用的值也跟着改变。i和j是同一个对象,所以是同步的:inti=42;constint&j=i;i=43;cout<<j<<endl;cout<<i<<endl;//......
  • C++ 打印杨辉三角/贾宪三角/帕斯卡三角
    #include<iostream>#include<iomanip>#include<windows.h>#include<fstream>#include<string>usingnamespacestd;#defineN10intmain(){inta[N][N......