#include<Windows.h>
#include<tchar.h>
#include<TlHelp32.h>
#include "Header.h"
#include"DebugPrivs.h"


void ParseProcessDetails(LPPROCESSENTRY32 processDetails) {

	// Parsing the PROCESSENTRY32 structure 
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684839(v=vs.85).aspx


	_tprintf(_T("PID: %d	Threads: %d		EXE: %s\n"),
		processDetails->th32ProcessID,
		processDetails->cntThreads,
		processDetails->szExeFile);

}


int main(void) {

	// Are privileges required for basic information? :) 
	// Most malcode would prefer APIs which do not require elevated access
	
	/*
	if (EnableDebugAbilityWithChecks()) {
		_tprintf(_T("[+] SeDebugPriv Enabled\n"));
	}
	else {
		_tprintf(_T("[-] Not all information might be available\n"));
	}
	*/


	// Gotta Love MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686701(v=vs.85).aspx
	// Create a system snapshot and get a handle
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682489(v=vs.85).aspx
	// Use TH32CS_SNAPPROCESS for now - Students can improvise

	HANDLE	allProcesses;

	allProcesses = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	if (INVALID_HANDLE_VALUE == allProcesses ) {
		ErrorDetails(_T("CreateToolhelp32Snapshot"), true);
	}


	// Let us know get the process details from the system snapshot
	// Looks like we have the findfirst findnext style APIs for this
	// Process32First https://msdn.microsoft.com/en-us/library/windows/desktop/ms684834(v=vs.85).aspx
	// Process32Next https://msdn.microsoft.com/en-us/library/windows/desktop/ms684836(v=vs.85).aspx
	// ParseProcessDetails is defined upstairs

	PROCESSENTRY32 processEntry;
	processEntry.dwSize = sizeof(PROCESSENTRY32);  // Documentation requests to set size before use

	// Get the first entry
	if (!Process32First(allProcesses, &processEntry)) {
		
		// Unusual if we don't get anything? :) 
		ErrorDetails(_T("Process32First()"), true);
	}

	// do-while is the best for findfirst, findnext style APIs

	do {
		// Send for Parsing
		ParseProcessDetails(&processEntry);

	} while (Process32Next(allProcesses, &processEntry));


	CloseHandle(allProcesses);

	getchar();
	return	0;
}