显示任何进程加载的DLL文件的代码

时间:2021-11-16 01:40:14

复制代码 代码如下:


#include <windows.h>
#include <iostream.h>
#include <tlhelp32.h>
#include <stdio.h>
int main(int argc, char* argv[])

char exename[500];
printf("[ENTER EXE FILE NAME]\t");
scanf("%s",exename);
printf("\n[YOU ENTER]\t%s\n",exename);
Sleep(3000);
//提升当前进程的权限使其有权限对其他进程进行操作
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
if ( ! OpenProcessToken( GetCurrentProcess(),
     TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ){
      //WriteToLog("openprocesstoken error");
      return 1;
}
if ( ! LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue ) ){
     CloseHandle( hToken );
     //WriteToLog("lookuprivilegevalue error");
     return 1;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ){
     CloseHandle( hToken );
     return 1;
}
//取得目标进程的PID
DWORD pid;
HANDLE snapshot;
snapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
struct tagPROCESSENTRY32 processsnap;
processsnap.dwSize=sizeof(tagPROCESSENTRY32);
for(Process32First(snapshot,&processsnap);Process32Next(snapshot,&processsnap);){
     if(!stricmp(processsnap.szExeFile,exename))
     {
      pid=processsnap.th32ProcessID;
      break;
     }
}
CloseHandle(snapshot);
////////////////////////////////////////
      MODULEENTRY32 pe32;

             // 在使用这个结构之前,先设置它的大小
             pe32.dwSize = sizeof(pe32);

             // 给进程内所有模块拍一个快照
             //276为某进程的ID
             HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,pid);

             if(hProcessSnap == INVALID_HANDLE_VALUE)
             {      
              //建立快照失败
                       return -1; 
        printf("[createtoolhelp32snapshot error]\n");
      }

             // 遍历进程快照,轮流显示每个进程的信息
             BOOL bMore = Module32First(hProcessSnap, &pe32);
             while(bMore)
             {      
       printf("\n[DLL NAME]\t%s\n",pe32.szModule);
       printf("[DLL PATH]\t%s\n",pe32.szExePath);

   
       bMore = Module32Next(hProcessSnap, &pe32);
             }
             // 不要忘记清除掉snapshot对象
             CloseHandle(hProcessSnap);

  
             return 0;
}