{"id":118,"date":"2012-03-07T23:54:42","date_gmt":"2012-03-07T23:54:42","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=118"},"modified":"2012-06-04T19:49:26","modified_gmt":"2012-06-04T19:49:26","slug":"mimicking-task-manager","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2012\/03\/mimicking-task-manager\/","title":{"rendered":"Mimicking task manager"},"content":{"rendered":"<p>Have you ever wondered how to make your own task manager? That thing that pops up when you press control+shift+escape and shows all the process names, ids and files associated with them? Well now you can!<\/p>\n<p>&nbsp;<\/p>\n<p>What you need is a C compiler, the dll, and the library. You can download them here: <a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2012\/03\/PROCS.zip\">PROCS<\/a><\/p>\n<p>The code below creates a window, and a listbox control then proceeds to populate the contents of said listbox with a for loop going through each process.<\/p>\n<p>The meat and potatoes are in the &#8216;procs&#8217; library which make the calls easy as pie. The definitions in procs.h are as such:<br \/>\n<pre><code class=\"C++\">\nDWORD __stdcall GetNumberOfProcesses();\nBOOL __stdcall GetProcessIDList(DWORD dwIDArray, DWORD dwArraySize);\nBOOL __stdcall GetProcessPath(DWORD dwPID, char *szBuff, DWORD dwBuffSize);\nBOOL __stdcall GetProcessBaseSize(DWORD dwPID, DWORD *dwImageBase, DWORD *dwImageSize);\nDWORD __stdcall GetNumberOfModules(DWORD dwPID);\nBOOL __stdcall GetModuleHandleList(DWORD dwPID,DWORD *dwHandleArray, DWORD dwArraySize);\nBOOL __stdcall GetModulePath(DWORD dwPID, DWORD dwModh, char *szBuff, DWORD dwBuffSize);\nBOOL __stdcall GetModuleSize(DWORD dwPID, DWORD dwModh, DWORD *dwImageSize);\nDWORD __stdcall GetProcessPathID(char szPath);\nHANDLE __stdcall GetModuleHandleEx(DWORD dwPID, char szModule);\n<\/code><\/pre><\/p>\n<p>Internally the library makes use of the PSAPI functions supported from the win32 lib for creating a working set from the process list. This library saves me a ton of time and was written by a rockstar of the reverse engineering community &#8211; Yoda. I found the library within his tool LordPE available here:<\/p>\n<p>http:\/\/www.woodmann.com\/collaborative\/tools\/index.php\/LordPE<\/p>\n<p>Here&#8217;s what it looks like compiled:<\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2012\/03\/taskmanforme.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-122\" title=\"taskmanforme\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2012\/03\/taskmanforme.png\" alt=\"\" width=\"800\" height=\"600\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2012\/03\/taskmanforme.png 800w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2012\/03\/taskmanforme-300x225.png 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre><code class=\"C++\"><\/code><\/pre>\n<p>#include <windows.h><\/p>\n<p>#include <psapi.h>\n<p>#include \u201cprocs.h\u201d<\/p>\n<p>#include <stdio.h><\/p>\n<p>DWORD ProcList(void);<\/p>\n<p>LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);<\/p>\n<p>char szClassName[ ] = \u201cOh god not the bees!\u201d;<\/p>\n<p>int WINAPI WinMain (HINSTANCE hThisInstance,<\/p>\n<p>HINSTANCE hPrevInstance,<\/p>\n<p>LPSTR lpszArgument,<\/p>\n<p>int nCmdShow)<\/p>\n<p>{<\/p>\n<p>HWND hwnd;<\/p>\n<p>MSG messages;<\/p>\n<p>WNDCLASSEX wincl;<\/p>\n<p>wincl.hInstance = hThisInstance;<\/p>\n<p>wincl.lpszClassName = szClassName;<\/p>\n<p>wincl.lpfnWndProc = WindowProcedure;<\/p>\n<p>wincl.style = CS_DBLCLKS;<\/p>\n<p>wincl.cbSize = sizeof (WNDCLASSEX);<\/p>\n<p>wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);<\/p>\n<p>wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);<\/p>\n<p>wincl.hCursor = LoadCursor (NULL, IDC_ARROW);<\/p>\n<p>wincl.lpszMenuName = NULL;<\/p>\n<p>wincl.cbClsExtra = 0;<\/p>\n<p>wincl.cbWndExtra = 0;<\/p>\n<p>wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;<\/p>\n<p>if (!RegisterClassEx (&#038;wincl))<\/p>\n<p>return 0;<\/p>\n<p>hwnd = CreateWindowEx (<\/p>\n<p>0,<\/p>\n<p>szClassName,<\/p>\n<p>\u201cProcess Listing\u201d,<\/p>\n<p>WS_OVERLAPPEDWINDOW,<\/p>\n<p>CW_USEDEFAULT,<\/p>\n<p>CW_USEDEFAULT,<\/p>\n<p>800,<\/p>\n<p>600,<\/p>\n<p>HWND_DESKTOP,<\/p>\n<p>NULL,<\/p>\n<p>hThisInstance,<\/p>\n<p>NULL<\/p>\n<p>);<\/p>\n<p>HWND hListBox = CreateWindowEx(WS_EX_CLIENTEDGE, \u201cLISTBOX\u201d, NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, 35, 15, 700, 500, hwnd, 666, hThisInstance, NULL);<\/p>\n<p>DWORD psz,pid;<\/p>\n<p>HANDLE process;<\/p>\n<p>char process_name[1024];<\/p>\n<p>char pidmsg1[512];<\/p>\n<p>char pidmsg2[512];<\/p>\n<p>DWORD list[1024];<\/p>\n<p>DWORD numoprocs = GetNumberOfProcesses();<\/p>\n<p>if(!GetProcessIDList(list,psz))<\/p>\n<p>{<\/p>\n<p>MessageBox(NULL,\u201dDamnit!\u201d,\u201dGetProcessIDList failed for some reason.\u201d,MB_OK);<\/p>\n<p>ExitProcess(0);<\/p>\n<p>}<\/p>\n<p>int cnt;<\/p>\n<p>for(cnt = 0;cnt<numoprocs;cnt++)\n\n{\n\nif(list[cnt] == 4)\n\n{\n\nsprintf(pidmsg1,\u201dProcess ID: %d\\t Process Name: This is a null process, dont bother =\\\\\\r\\n\u201d,list[cnt],process_name);\n\nSendDlgItemMessage(hwnd, 666, LB_ADDSTRING, 0, (LPARAM)pidmsg1);\n\n \n\n}\n\nprocess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, list[cnt]);\n\nif(process != NULL &#038;&#038; list[cnt] != 4)\n\n{\n\n\/\/GetProcessImageFileName(process, process_name, 1024);\n\nGetModuleFileNameEx(process,NULL, process_name, 1024);\n\nsprintf(pidmsg2,\u201dProcess ID: %d \\tProcess Name:%s\u201d,list[cnt],process_name);\n\nSendDlgItemMessage(hwnd, 666, LB_ADDSTRING, 0, (LPARAM)pidmsg2);\n\nCloseHandle(process);\n\n}\n\n}\n\n \n\n \n\nShowWindow (hwnd, nCmdShow);\n\n \n\n \n\nwhile (GetMessage (&#038;messages, NULL, 0, 0))\n\n{\n\n \n\nTranslateMessage(&#038;messages);\n\n \n\nDispatchMessage(&#038;messages);\n\n}\n\n \n\n \n\nreturn messages.wParam;\n\n}\n\n \n\nLRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)\n\n{\n\nswitch (message)\n\n{\n\ncase WM_DESTROY:\n\nPostQuitMessage (0);\n\nbreak;\n\ncase WM_CREATE:\n\n{\n\n \n\nbreak;\n\n}\n\ndefault:\n\nreturn DefWindowProc (hwnd, message, wParam, lParam);\n\n}\n\n return 0;\n\n}\n[\/prettify]\n\nI'm utilizing this library for my debugger centrally for showing off a list of running processes and modules loaded by each. its minimalist simplicity is what I like most about it.\n\nAn alternative to this approach would be to call FindWindowEx() to find all desktop windows and then call GetWindowThreadProcessId to grab the process ID from the returned handle HWND. This approach however only enumerates processes that have a window to show, thus is ineffective for my needs, but feel free to mess around with it. Have some sample codes:\n\n\n[prettify class=\"C++\"]\nvoid KillShit(void)\n\n{\n\nHWND mywind;\n\nDWORD exitcode = -1;\n\nHANDLE myproc;\n\nDWORD procid;\n\nmywind = FindWindow(NULL, \"Title Of window such as \\'explorer\\'\");\n\nGetWindowThreadProcessId(mywind,procid);\n\nmyproc = OpenProcess(PROCESS_ALL_ACCESS,true,procid);\n\nif(!TerminateProcess( myproc, exitcode))\n\n{\n\nprintf(\"nope!\");\n\n}\n\nprintf(\"killing process %d\", procid);\n\n}\n\n[\/prettify]\n\nHappy cracking\n\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2012\/03\/1330018702816.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-123\" title=\"1330018702816\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2012\/03\/1330018702816.jpg\" alt=\"\" width=\"236\" height=\"251\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wondered how to make your own task manager? That thing that pops up when you press control+shift+escape and shows all the process names, ids and files associated with them? Well now you can! &nbsp; What you need is a C compiler, the dll, and the library. You can download them here: PROCS [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/118"}],"collection":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":7,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/118\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}