{"id":81,"date":"2011-09-19T01:31:30","date_gmt":"2011-09-19T01:31:30","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=81"},"modified":"2011-09-19T01:31:43","modified_gmt":"2011-09-19T01:31:43","slug":"running-processes-and-openprocess-joys","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2011\/09\/running-processes-and-openprocess-joys\/","title":{"rendered":"Running processes and OpenProcess joys"},"content":{"rendered":"<p>For most windows users, this is as simple as control alt delete, or failing that, TASK.exe from the command prompt. nut how do you do it programmatically(it is so a word)?<\/p>\n<p>&nbsp;<\/p>\n<p>There are a few ways, but I like to use one particular library for its small size and speed. Procs.dll. The contents ofthe procs header are as follows:<\/p>\n<p>\/\/ PROCS.dll info<br \/>\ntypedef struct Procs<br \/>\n{<br \/>\nDWORD\u00a0 __stdcall GetNumberOfProcesses();<br \/>\nBOOL\u00a0\u00a0 __stdcall GetProcessIDList(DWORD *dwIDArray, DWORD dwArraySize);<br \/>\nBOOL\u00a0\u00a0 __stdcall GetProcessPath(DWORD dwPID, char *szBuff, DWORD dwBuffSize);<br \/>\nBOOL\u00a0\u00a0 __stdcall GetProcessBaseSize(DWORD dwPID, DWORD *dwImageBase, DWORD *dwImageSize);<\/p>\n<p>DWORD\u00a0 __stdcall GetNumberOfModules(DWORD dwPID);<br \/>\nBOOL\u00a0\u00a0 __stdcall GetModuleHandleList(DWORD dwPID,DWORD *dwHandleArray, DWORD dwArraySize);<br \/>\nBOOL\u00a0\u00a0 __stdcall GetModulePath(DWORD dwPID, DWORD dwModh, char *szBuff, DWORD dwBuffSize);<br \/>\nBOOL\u00a0\u00a0 __stdcall GetModuleSize(DWORD dwPID, DWORD dwModh, DWORD *dwImageSize);<\/p>\n<p>DWORD\u00a0 __stdcall GetProcessPathID(char* szPath);<br \/>\nHANDLE __stdcall GetModuleHandleEx(DWORD dwPID, char* szModule);<\/p>\n<p>};<\/p>\n<p>Short and sweet. The back end code was made by great cracker hacker. yoda\/FReAK2FReAK. This guy wrote LordPE, several custom debuggers, and a number of other things that put me to shame. I trust his code. So how do we invoke? Easily:<\/p>\n<p>unsigned int pid;<br \/>\npid =ProcList();<\/p>\n<p>\/\/ open the process with VM_READ so it dont crash:<br \/>\nHANDLE myproc = OpenProcess(PROCESS_VM_READ ,TRUE,pid); \/\/ open it<br \/>\nif(myproc == NULL)<br \/>\n{<br \/>\nMessageBox(NULL,&#8221;Invalid Process ID&#8221;,&#8221;U BROKE IT!&#8221;,MB_OK);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>DWORD ProcList()<br \/>\n{<\/p>\n<p>DWORD pid,psz;<br \/>\nDWORD list[512];<br \/>\nDWORD numoprocs = GetNumberOfProcesses();<br \/>\nif(!GetProcessIDList(list,psz))<br \/>\n{<br \/>\nMessageBox(NULL,&#8221;Damnit!&#8221;,&#8221;Double Damnit!&#8221;,MB_OK);<br \/>\nExitProcess(0);<br \/>\n}<br \/>\nint cnt = 0;<br \/>\nfor(;cnt&lt;numoprocs;cnt++)<br \/>\n{<br \/>\nprintf(&#8220;Process ID: %d\\r\\n&#8221;,list[cnt]);<\/p>\n<p>}<br \/>\nprintf(&#8220;There are %d live processes. Enter a Process ID to attach to: \\r\\n&#8221;,numoprocs);<br \/>\nscanf(&#8220;%d&#8221;,&amp;pid);<br \/>\nreturn pid;<br \/>\n}<\/p>\n<p>That just gets us the process ID&#8217;s. We can one up this with a call to GetModuleFileNameEx(). This function returns to us a string that contains where the process lives on the file system.<\/p>\n<p>&nbsp;<\/p>\n<p>What can we do with the process ID? OpenProcess() of course! And that opens up an avenue of awesome functions at our disposal. We can read process memory. Modify it if we want to. Here&#8217;s a little function for sifting through a live processes memory. The &#8216;proc&#8217; function arg is returned by each subsequent call to OpenProcess that&#8217;s what the function returns:<\/p>\n<p>int liveproc(HANDLE proc)<br \/>\n{<br \/>\nDWORD baseaddr = 0x0401000;<br \/>\nchar holdme[256];<br \/>\nint x =0;<br \/>\nprintf(&#8220;memory listing: \\r\\n&#8221;);<br \/>\nfor(;x&lt;sizeof(holdme);x++)<br \/>\n{<br \/>\nReadProcessMemory(proc,&amp;baseaddr,&amp;holdme,1,NULL); \/\/ read 1 byte at a type<br \/>\nprintf(&#8220;%02x &#8220;, holdme[x]);<br \/>\n}<\/p>\n<p>As for writing process memory, you would use the function WriteProcessMemory.<\/p>\n<p>I&#8217;ll go over WriteProcessMemory() in another blog post, specifically how to do it without crashing the application.<\/p>\n<p>&nbsp;<\/p>\n<p>Any other cool functions? GetThreadContext() of course! With this little number, we can read registers. A psuedo debugger as it were.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For most windows users, this is as simple as control alt delete, or failing that, TASK.exe from the command prompt. nut how do you do it programmatically(it is so a word)? &nbsp; There are a few ways, but I like to use one particular library for its small size and speed. Procs.dll. The contents ofthe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/81"}],"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=81"}],"version-history":[{"count":2,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/81\/revisions\/83"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}