Windows core programming code analysis based on Visual C ++ (19) enumeration process and process loading module information

Source: Internet
Author: User

During Windows security programming, we often need to check the process. Let's take a look at the enumeration process and process loading module. See Code implementation and annotation analysis.

 

 

/* Header file */# include <windows. h> # include <psapi. h> # include <tlhelp32.h> # include <stdio. h>/* pre-statement */# pragma comment (Lib, "psapi. lib ")/* function declaration */void winapi enumprocess1 (); void winapi enumprocess2 (); void listprocessmodules1 (DWORD dwpid); void listprocessmodules2 (DWORD dwpid ); void printmemoryinfo (DWORD dwpid); void showprocessmemoryinfo (DWORD dwpid); void listheapinfo (DWORD dwpid); void listprocess Threads (DWORD dwpid); void printerror (lptstr MSG ); /************************************ void winapi enumprocess1 () * The function calls enumprocesses to traverse the process, * and calls the listprocessmodules1 function and the * listprocessthreads function to list modules and threads ** without parameters, no return value ************************************** /void winapi enumprocess1 () {// assume no more than 1024 processes DWORD aprocesses [1024], cbneeded, cprocesses; unsigned int I; // call enumprocessesif (! Enumprocesses (aprocesses, sizeof (aprocesses), & cbneeded) return; // number of processes cprocesses = cbneeded/sizeof (DWORD); for (I = 0; I <cprocesses; I ++) {// display the process information printf ("\ n *************************** ***********************"); printf ("\ nprocess: % u \ n", aprocesses [I]); printf ("\ n *********************************** *****************"); // list the module information and thread information listprocessmodules1 (aprocesses [I]); listprocessthreads (Aprocesses [I]);} /************************************ void winapi enumprocess2 () * The function calls process32first and process32next to traverse the process. * The listprocessmodules2 function is called to list the module. * The showprocessmemoryinfo function is called to Display memory usage. ** No parameter is set, no return value ************************************** /void winapi enumprocess2 () {handle hprocesssnap; handle hprocess; processentry32 pe32; DWORD dwpriorityclass; // snapshothprocesssnap = createconlhelp32snapshot (th32 Cs_snapprocess, 0); If (hprocesssnap = invalid_handle_value) {printerror ("createconlhelp32snapshot (of processes)"); Return ;}// set the input parameter, structure size pe32.dwsize = sizeof (processentry32); // start to list the process if (! Process32first (hprocesssnap, & pe32) {printerror ("process32first"); // error message closehandle (hprocesssnap); return ;} do {// print the process name printf ("\ n ============================ ============================= "); printf ("\ nprocess name: % s", pe32.szexefile); printf ("\ n accept"); // obtain the priority dwpriorityclass = 0; hprocess = OpenProcess (process_all_access, false, pe32.th32p Rocessid); If (hprocess = NULL) printerror ("OpenProcess"); else {dwpriorityclass = getpriorityclass (hprocess); If (! Dwpriorityclass) printerror ("getpriorityclass"); closehandle (hprocess);} // print process-related information printf ("\ n process id = 0x % 08x", pe32.th32processid ); printf ("\ n thread count = % d", pe32.cntthreads); printf ("\ n parent process id = 0x % 08x", pe32.th32parentprocessid ); printf ("\ n priority base = % d", pe32.pcpriclassbase); If (dwpriorityclass) printf ("\ n priority class = % d", dwpriorityclass); // obtain module information, display memory Usage listprocessmodules2 (bytes); printmemoryinfo (pe32.th32processid); listheapinfo (pe32.th32processid);} while (process32next (hprocesssnap, & pe32); closehandle (hprocesssnap ); // close the handle return ;} /************************************ void listprocessmodules1 (DWORD dwpid) * function call the enumprocessmodules function * to list and display the module loaded by a process. The ** parameter DWORD dwpid process PID ******************** * *****************/void listprocess Modules1 (DWORD dwpid) {hmodule hmod [1024]; handle hprocess; DWORD cbneeded; unsigned int I; printf ("\ nlistprocessmodules1 process ID % u \ n", dwpid ); // open the process and obtain the handle hprocess = OpenProcess (process_query_information | process_vm_read, false, dwpid); If (null = hprocess) return; // call enumprocessmodulesif (enumprocessmodules (hprocess, hmod, sizeof (hmod), & cbneeded) {for (I = 0; I <(cbneeded/sizeof (hmodu Le); I ++) {tchar szmodname [max_path]; // obtain the obtained path if (getmodulefilenameex (hprocess, hmod [I], szmodname, sizeof (szmodname) /sizeof (tchar) {// print printf (text ("\ t % s (0x % 08x) \ n"), szmodname, hmod [I]) ;}} closehandle (hprocess ); // close Process Handle }/********************************* * *** void listprocessmodules2 (DWORD dwpid) * The module32first and module32next functions * are called to list and display the module loaded by a process. * The DWORD dwpid process PID ****************** * ******************/Void listprocessmodules2 (DWORD dwpid) {handle hmodulesnap = invalid_handle_value; moduleentry32 me32; printf ("\ nlistprocessmodules2 process ID % u \ n", dwpid); // snapshothmodulesnap = creatw.lhelp32snapshot (th32cs_snapmodule, dwpid); If (hmodulesnap = invalid_handle_value) {printerror ("createconlhelp32snapshot (of modules)"); return;} // set the input parameter. The structure size is me32.dwsize = sizeof (Mo Duleentry32); // get the module information if (! Module32first (hmodulesnap, & me32) {printerror ("module32first"); // show cause of failureclosehandle (hmodulesnap); // must clean up the snapshot object! Return;} do {printf ("\ n Module name: % s", me32.szmodule); printf ("\ n executable = % s", me32.szexepath ); printf ("\ n process id = 0x % 08x", me32.th32processid); printf ("\ n ref count (G) = 0x % 04x", me32.glblcntusage ); printf ("\ n ref count (p) = 0x % 04x", me32.proccntusage); printf ("\ n base address = 0x % 08x", (DWORD) me32.modbaseaddr ); printf ("\ n base size = % d", me32.modbasesize);} while (modul E32next (hmodulesnap, & me32); closehandle (hmodulesnap); // close the handle return ;} /************************************ void printmemoryinfo (DWORD dwpid) * The function shows the memory usage of processes. * The DWORD dwpid process PID ************************* * ************/void printmemoryinfo (DWORD dwpid) {handle hprocess; process_memory_counters PMC; printf ("\ nprocess ID: % u \ n", dwpid); hprocess = OpenProcess (process_query_information | proces S_vm_read, false, dwpid); If (null = hprocess) return; If (getprocessmemoryinfo (hprocess, & PMC, sizeof (PMC) {printf ("\ tpagefaultcount: 0x % 08x \ n ", PMC. pagefaultcount); printf ("\ tpeakworkingsetsize: 0x % 08x \ n", PMC. peakworkingsetsize); printf ("\ tworkingsetsize: 0x % 08x \ n", PMC. workingsetsize); printf ("\ tquotapeakpagedpoolusage: 0x % 08x \ n", PMC. quotapeakpagedpoolusage); printf ("\ tquotapagedpoolus Age: 0x % 08x \ n ", PMC. quotapagedpoolusage); printf ("\ tquotapeaknonpagedpoolusage: 0x % 08x \ n", PMC. quotapeaknonpagedpoolusage); printf ("\ tquotanonpagedpoolusage: 0x % 08x \ n", PMC. quotanonpagedpoolusage); printf ("\ tpagefileusage: 0x % 08x \ n", PMC. pagefileusage); printf ("\ tpeakpagefileusage: 0x % 08x \ n", PMC. peakpagefileusage);} closehandle (hprocess );} /************************************ void listhe Apinfo (DWORD dwpid) * The function shows the heap distribution of processes. * The DWORD dwpid process PID ************************* * ************/void listheapinfo (DWORD dwpid) {heaplist32 HL; heapentry32 he; handle hsnapshot = success; printf ("\ listheapinfo process ID % u \ n", dwpid); // snapshothsnapshot = createconlhelp32snapshot (success, dwpid ); if (hsnapshot = invalid_handle_value) {printerror ("createconlhelp32sna Pshot (of heaplist) "); return;} // set the input parameter, structure size hl. dwsize = sizeof (heaplist32); // get the information if (! Heap32listfirst (hsnapshot, & HL) {printerror ("heap32listfirst"); closehandle (hsnapshot); Return ;}do {printf ("\ n \ theap id = % u ", hl. th32heapid); printf ("\ theap flags = % u", HL. dwflags); he. dwsize = sizeof (heapentry32); If (! Heap32first (& he, dwpid, HL. th32heapid) {printerror ("heap32first"); // error closehandle (hsnapshot); return ;} do {// display information printf ("\ n \ t heap address \ t = % u", he. dwaddress); printf ("\ t heap size \ t = % u", he. dwblocksize); printf ("\ t heap flags \ t = % u", he. dwflags); printf ("\ t heap handle \ t = % u", he. hhandle);} while (heap32next (& he);} while (heap32listnext (hsnapshot, & HL); closehandle (hsnapshot ); // Close the handle return ;} /************************************ void listprocessthreads (DWORD dwpid) * function call thread32first and thread32next * display the thread of a process. The ** parameter DWORD dwpid process PID ********************* * ****************/void listprocessthreads (DWORD dwpid) {handle hthreadsnap = invalid_handle_value; threadentry32 te32; printf ("\ listprocessthreads process ID % u \ n", dwpid); // snapshoththreadsnap = creat#lhelp32sn Apshot (th32cs_snapthread, 0); If (hthreadsnap = invalid_handle_value) return; // set the input parameter, the structure size te32.dwsize = sizeof (threadentry32); // get the information if (! Thread32first (hthreadsnap, & te32) {printerror ("thread32first"); // show cause of failureclosehandle (hthreadsnap); // must clean up the snapshot object! Return;} do {If (te32.th32ownerprocessid = dwpid) {// display the relevant information printf ("\ n thread id = 0x % 08x", te32.th32threadid ); printf ("\ t base priority = % d", te32.tpbasepri); printf ("\ t Delta priority = % d", te32.tpdeltapri);} while (thread32next (hthreadsnap, & te32); closehandle (hthreadsnap); Return ;}// print the error message void printerror (lptstr MSG) {DWORD Enum; tchar sysmsg [256]; tchar * P; enum = getlasterror (); formatmessage (format_message_from_system | values, null, Enum, makelangid (lang_neutral, sublang_default), sysmsg, 256, null); P = sysmsg; while (* P> 31) | (* P = 9) + + P; do {* p -- = 0;} while (P> = sysmsg) & (* P = '. ') | (* P <33); printf ("\ n warning: % s failed with error % d (% s)", MSG, Enum, sysmsg);} void main () {printf ("enumprocess1 \ n"); enumprocess1 (); printf ("\ n \ nenumprocess2 \ n "); enumprocess2 ();}

 

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.