This code was written by a friend of mine, IceDane, and posted online in 2005. Though it was released in 2005, it still works to list all running processes on a Windows XP machine. It is untested on Vista at the time of writing.
#include <windows.h>
#include <stdio.h>
#include <tlhelp32.h>
int main(int argc, char **argv)
{
// Handle for the process snap.
HANDLE hProcessSnap;
// A struct containing info on the process snape.
// This is where we get the name and PID of the process
PROCESSENTRY32 peStruct;
// Take a snapshot of all processes running.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
// Let's make sure that the snapshot is valid, and the function didn't fail.
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf("\n\nError creating snapshot of processes.\n\n");
return 0;
}
// We need to set the size of the struct before we use it.
peStruct.dwSize = sizeof(PROCESSENTRY32);
// Get info about the first process and make sure it won't F*** up.
if(Process32First(hProcessSnap, &peStruct) == FALSE)
{
printf("\n\nError calling Process32First.\n\n");
CloseHandle(hProcessSnap);
return 0;
}
// Open a file for writing.
FILE *out = fopen("processlisting.txt", "w+");
// Print the 'title'.
fprintf(out, "%17s - PID\n", "Process name");
fprintf(out, "--------------------------------\n");
// Loop through the process list, outputting name and PID of each process to the file each turn.
// We use do-while loop to ensure that the first process also gets printed.
do
{
fprintf(out, "%17s - %d\n", peStruct.szExeFile, peStruct.th32ProcessID);
}
while(Process32Next(hProcessSnap, &peStruct));
// Close file and the handle.
fclose(out);
CloseHandle(hProcessSnap);
printf("\n\nProcess list stored in processlisting.txt\n\n");
return 0;
}
Code: C To List All Processes
April 18, 2009 by Dr. David Davidson
Advertisement
Posted in Code, Local Exploitation | Tagged c++, Code, list, processes, windows xp | Leave a Comment
Leave a Reply Cancel reply
Finals Week
Hey dudes and dudettes. With finals week coming up, I'll only be posting new articles on Tuesdays and Fridays.- DavidNew Server
The new blog is at http://maldesign.netau.net. Update your bookmarks! I'll have a domain purchased soon. This blog (at this location on wordpress.com) will be discontinued to make way for the new server on April 25th.- DavidCategories
-
Blog Stats
- 2,416 hits