#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tlhelp32.h>
#define USAGE "./%s [-l|-p <pid>|-n <nameprocess>|-kp <pidtokill>|-kn <nameprocess>| -kpp <pidtokillwithdebubprivilege]\n\n -p : Renvois le nom du processus d'un pid donne.\n -n : Renvois le pid d'un nom de processus donne.\n -kp : Kill le processus identifie par son pid.\n -kn : Kill le processus identifie par son nom.\n -kpp : Kill le processus identifie par son pid avec les privileges.\n\n"
int ProcessusList();
char* PidToNomProcessus(long pid);
long NomProcessusToPid(char* process);
int KillProcessus(long pid);
int SetDebugPrivileges();

int main(int argc,char* argv[])
{
    printf("View fucking process par 0vercl0k.\n\n");
    if(!argv[1]){printf(USAGE,argv[0]);return 0;}

    if(!strcmp("-l",argv[1]))
        ProcessusList();
    else if(!strcmp("-p",argv[1]))
        printf("Process ID : %ld    Nom du processus : %s\n",atoi(argv[2]),PidToNomProcessus(atoi(argv[2])) );
    else if(!strcmp("-n",argv[1]))
        printf("Nom du processus : %s    Process ID : %ld\n",argv[2],NomProcessusToPid(argv[2]));
    else if(!strcmp("-kp",argv[1]))
        KillProcessus(atoi(argv[2]));
    else if(!strcmp("-kn",argv[1]))
        KillProcessus(NomProcessusToPid(argv[2]));
    else if(!strcmp("-kpp",argv[1])){
        SetDebugPrivileges();
        KillProcessus(atoi(argv[2]));}
    else
        printf(USAGE,argv[0]);

    return 0;
}

int SetDebugPrivileges()
{
    TOKEN_PRIVILEGES privilege;
    HANDLE processCourant = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()) , jetonproc;

    OpenProcessToken(processCourant, TOKEN_ALL_ACCESS, &jetonproc);
    LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &privilege.Privileges[0].Luid);

    privilege.PrivilegeCount = 1;
    privilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(jetonproc, FALSE, &privilege, 0, NULL, NULL);

    CloseHandle(jetonproc);
    CloseHandle(processCourant);
}

int KillProcessus(long pid)
{
    HANDLE handleprocessus = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);//OpenProcess -> http://msdn2.microsoft.com/en-us/library/ms684320.aspx && http://msdn2.microsoft.com/en-us/library/ms684880.aspx
    if(handleprocessus == NULL){printf("[-] Votre processus n'existe pas ou vous n'avez aucun droit sur lui.\n");return 0;}

    if(TerminateProcess(handleprocessus,0)) //TerminateProcess -> http://msdn2.microsoft.com/en-us/library/ms686714.aspx
        printf("[+]  Processus kille avec succes.\n");
    else
        printf("[-] Erreur lors du kill.\n");
    CloseHandle(handleprocessus);
}


long NomProcessusToPid(char* process)
{
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    PROCESSENTRY32 structprocsnapshot = {0};

    structprocsnapshot.dwSize = sizeof(PROCESSENTRY32);

    if(snapshot == INVALID_HANDLE_VALUE)return 0;
    if(Process32First(snapshot,&structprocsnapshot) == FALSE)return 0;

    while(Process32Next(snapshot,&structprocsnapshot) )
    {
       if(!strcmp(structprocsnapshot.szExeFile,process))
       {
            CloseHandle(snapshot);
            return structprocsnapshot.th32ProcessID;
       }
    }
    CloseHandle(snapshot);
}

char* PidToNomProcessus(long pid)
{
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    PROCESSENTRY32 structprocsnapshot = {0};

    structprocsnapshot.dwSize = sizeof(PROCESSENTRY32);

    if(snapshot == INVALID_HANDLE_VALUE)return 0;
    if(Process32First(snapshot,&structprocsnapshot) == FALSE)return 0;

    while(Process32Next(snapshot,&structprocsnapshot) )
    {
       if(structprocsnapshot.th32ProcessID == pid)
       {
           CloseHandle(snapshot);
            return structprocsnapshot.szExeFile;
       }
    }
    CloseHandle(snapshot);
    return 0;
}


int ProcessusList()
{
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //CreateToolHelp32Snapshot() -> http://msdn2.microsoft.com/en-us/library/ms682489.aspx
    PROCESSENTRY32 structprocsnapshot = {0}; // tagPROCESSENTRY32 -> http://msdn2.microsoft.com/en-us/library/ms684839.aspx

    structprocsnapshot.dwSize = sizeof(PROCESSENTRY32); // On est obligé d'initialiser dwSize sinan process32first foire.

    if(snapshot == INVALID_HANDLE_VALUE)return 0;
    if(Process32First(snapshot,&structprocsnapshot) == FALSE)return 0;

    while(Process32Next(snapshot,&structprocsnapshot) ) //Process32Next -> http://msdn2.microsoft.com/en-us/library/ms684836.aspx
    {
       printf("-[ %s (PID : %ld)\n",structprocsnapshot.szExeFile,structprocsnapshot.th32ProcessID);
    }

    CloseHandle(snapshot);
    return 0;
}