How to get module information based on thread number

Source: Internet
Author: User

After you get the thread, you can get handle through openthread, and then get the thread information through Zwqueryinformationthread.


Part1 Preparation:

#include <psapi.h>

#include <locale.h>

#include <iostream>

#pragmacomment (lib, "PSAPI.lib")


typedef enum _THREADINFOCLASS {

Threadbasicinformation,

Threadtimes,

ThreadPriority,

Threadbasepriority,

Threadaffinitymask,

Threadimpersonationtoken,

Threaddescriptortableentry,

Threadenablealignmentfaultfixup,

Threadeventpair_reusable,

Threadquerysetwin32startaddress,

Threadzerotlscell,

Threadperformancecount,

Threadamilastthread,

Threadidealprocessor,

Threadpriorityboost,

Threadsettlsarrayaddress,

Threadisiopending,

Threadhidefromdebugger,

Threadbreakontermination,

Maxthreadinfoclass

} Threadinfoclass;

typedef struct _CLIENT_ID {

HANDLE uniqueprocess;

HANDLE Uniquethread;

} client_id;

typedef client_id *PCLIENT_ID;

typedef struct _THREAD_BASIC_INFORMATION {//information Class 0

LONG Exitstatus;

PVOID tebbaseaddress;

client_id ClientId;

LONG Affinitymask;

LONG priority;

LONG basepriority;

} thread_basic_information, *pthread_basic_information;

extern "C" LONG (__stdcall *zwqueryinformationthread) (

In HANDLE Threadhandle,

In Threadinfoclass Threadinformationclass,

Out PVOID Threadinformation,

In ULONG Threadinformationlength,

Out Pulong returnlength OPTIONAL

) = NULL;


The preparation to be done in the main function:

SetLocale (Lc_all, ". ACP ");

HInstance Hntdll =:: GetModuleHandle (TEXT ("Ntdll"));

(farproc&) Zwqueryinformationthread =:: GetProcAddress (Hntdll, "Zwqueryinformationthread");



Part 2 getting related information

Thread_basic_information TBI;

PVOID startaddr;

LONG status;

HANDLE thread, process;

Thread =:: Openthread (Thread_all_access, FALSE, dwThreadID);

if (thread = = NULL)

{

printf ("Cannot open thread handle\n");

return FALSE;

}

Status = Zwqueryinformationthread (thread,threadquerysetwin32startaddress, &startaddr, sizeof (STARTADDR), NULL);

if (Status < 0)

{

CloseHandle (thread);

printf ("Cannot get status1\n");

return FALSE;

};

printf ("Thread%08x start address is%p\n", dwThreadID, STARTADDR);

Status = Zwqueryinformationthread (thread,

Threadbasicinformation,

&TBI,

sizeof (TBI),

NULL);

if (Status < 0)

{

CloseHandle (thread);

printf ("Cannot get status2\n");

return FALSE;

};

printf ("Thread%08x process ID is%08x\n", dwThreadID, (DWORD) TBI. clientid.uniqueprocess);

Process =:: OpenProcess (Process_all_access,

FALSE,

(DWORD) TBI. clientid.uniqueprocess);

if (process = = NULL)

{

DWORD error =:: GetLastError ();

CloseHandle (thread);

SetLastError (Error);

return FALSE;

};

TCHAR ModName [0x100];

:: Getmodulefilenameex (Process, NULL, ModName, 0x100);

printf ("Thread%08x process image is%s\n", dwThreadID, ModName);

Getmappedfilename (Process,

STARTADDR,

ModName,

0x100);

std::string stname (pName);

std::string stmodname (ModName);

if (Stmodname.find (stname)! = Std::string::npos)

{

printf ("Thread%08x executable code module is%s\n", dwThreadID, ModName);

ret = TRUE;

}

CloseHandle (process);

CloseHandle (thread);




How to get module information based on thread number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.