본문 바로가기
프로그래밍/C·C++

프로세스 리스트 얻기

by ITPro 2011. 7. 4.

-프로세스 리스트를 얻어와 프로세스 리스트를 구조체에 저장한다.
-PLIST : 프로세스 경로, PID 값을 가지는 간단한 이중 연결리스트 구조체
-DeleteProcessList : 프로세스 리스트를 제거한다.
-GetProcessList_TI : TlHelp32를 이용한 프로세스 리스트 획득 함수
                               (히든 프로세스 탐지 불가능)
-GetProcessList_BF : Psapi를 이용한 프로세스 리스트 획득 함수
                               (Brute Force식 PID 탐색을 통한 히득 프로세스 탐지 가능)

#include <windows.h>
#include <Psapi.h>
#include <TlHelp32.h>

#define BUFSIZE 512

/** 프로세스 리스트 저장 구조체 **/
struct _PLIST{
        char path[BUFSIZE];
        int pid;
        struct _PLIST* next;
};
typedef struct _PLIST PLIST;


//프로세스 리스트 제거
void DeleteProcessList(PLIST* pList){
        PLIST* tempList;

        if(pList == NULL){
                return;
        }

        while(pList->next != NULL){
                tempList=pList->next->next;
                free(pList->next);
                pList->next=tempList;
        }
        free(pList);

}

// 히든 프로세스를 찾지 못하는 검색 방법 (TlHelp32.h include 필요)
PLIST* GetProcessList_TI(){
        PLIST* pList=NULL;
        PLIST* tempList;
        PLIST* nextList;
        PROCESSENTRY32 procEntry={0};
        HANDLE hSnapshot;
        int pid=0;

        hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

        procEntry.dwSize=sizeof(procEntry);
        Process32First(hSnapshot,&procEntry);

        do{
                //구조체 저장
                tempList=(PLIST*)malloc(sizeof(PLIST));

                strncpy(tempList->path,procEntry.szExeFile,BUFSIZE-1);
                tempList->pid=procEntry.th32ProcessID;
                tempList->next=NULL;

                if(pList == NULL){
                        pList=tempList;
                        nextList=pList;
                }else{
                        nextList->next=tempList;
                        nextList=nextList->next;
                }
        }while(Process32Next(hSnapshot,&procEntry));

        CloseHandle(hSnapshot);

        return pList;
}


//BruteForce 기법을 사용한 히든 프로세스 검색 방법 (Psapi.h include 필요)
PLIST* GetProcessList_BF(){
        PLIST* pList=NULL;
        PLIST* tempList;
        PLIST* nextList;
        HANDLE hProc;
        HMODULE hMod;
        DWORD cbNeeded;
        char buffer[BUFSIZE];
        int pid=0;

        for(pid=0;pid <= 32768;pid++){

                hProc=OpenProcess(PROCESS_QUERY_INFORMATION |
                                                 PROCESS_VM_READ,FALSE,pid);

                if(hProc == NULL){
                        continue;
                }

                EnumProcessModules(hProc,&hMod,sizeof(hMod),&cbNeeded);

                GetModuleFileNameExA(hProc,hMod,buffer,BUFSIZE-1);

                //구조체 저장
                tempList=(PLIST*)malloc(sizeof(PLIST));

                strncpy(tempList->path,buffer,BUFSIZE-1);
                tempList->pid=pid;
                tempList->next=NULL;

                if(pList == NULL){
                        pList=tempList;
                        nextList=pList;
                }else{
                        nextList->next=tempList;
                        nextList=nextList->next;
                }

                CloseHandle(hProc);
        }

        return pList;
}


반응형

'프로그래밍 > C·C++' 카테고리의 다른 글

SEH(Structured Exception Handling)  (0) 2011.08.15
strrstr 함수 구현 (문자열 역순 탐색 함수)  (0) 2011.07.20
프로세스 제거하기  (0) 2011.07.04
프로세스 생성하기  (0) 2011.07.04
레지스트리 삭제하기  (0) 2011.07.04