找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 8942|回复: 8

[开源] 以系统权限执行Regedit

 火.. [复制链接]

96

主题

158

回帖

4

精华

核心会员

积分
6513
发表于 2012-5-15 04:53:19 | 显示全部楼层 |阅读模式
本帖最后由 diddom 于 2012-5-31 07:33 编辑

虽然这是老掉牙的东西,但是我想对一些新手可能有点帮助

一般我们执行程式的权限大都在管理员

但是当我们需要执行Regedit,想看 SAM

你却发现看不到, 这时我们可以靠正统的方法去实现

执行後就可以看SAM了

[HKEY_LOCAL_MACHINE\SAM\SAM]

叫出工作管理员,你会发现regedit的权限是"SYSTEM"

希望此文能带领你进入权限的入门

SystemPrivilege_20120517.rar (27 KB, 下载次数: 11)

  1. // SystemPrivilege.cpp : Defines the entry point for the console application.
  2. //

  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <psapi.h>
  7. #include <Accctrl.h>
  8. #include <Aclapi.h>

  9. #pragma comment(lib, "PSAPI.LIB")

  10. char strUserName[260];


  11. // [HKEY_LOCAL_MACHINE\SAM\SAM]

  12. BOOL CreateSystemProcess( LPTSTR szProcessName);

  13. BOOL EnablePrivilege()
  14. {
  15.         HANDLE hToken;
  16.         TOKEN_PRIVILEGES tkp;

  17.         if (OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
  18.         {
  19.                 LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid );
  20.                 tkp.PrivilegeCount=1;
  21.                 tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
  22.                 AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL );       
  23.                 return( (GetLastError()==ERROR_SUCCESS) );
  24.         }
  25.         return TRUE;
  26. }                               


  27. DWORD GetPidFromProcName(LPCSTR ProcName)
  28. {
  29.         DWORD processid[1024];
  30.         DWORD needed;
  31.         DWORD processcount;
  32.         char path[MAX_PATH] = "";
  33.         HANDLE hProcess;
  34.         HMODULE hModule;

  35.         // getting need buffer size
  36.         EnumProcesses(processid, sizeof(processid), &needed);
  37.         processcount = needed / sizeof(DWORD);
  38.                        
  39.         for (DWORD i=0; i<processcount; i++)
  40.         {       
  41.                 //hProcess = NULL;

  42.                 hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processid[i]);

  43.                 DWORD pp = processid[i];

  44.                 if (hProcess)
  45.                 {
  46.                         EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
  47.                         GetModuleFileNameEx(hProcess, hModule, path, sizeof(path));
  48.                         //GetShortPathName(path,path,256);

  49.                         char *strLwr=strlwr(path);
  50.                         char *pos = strstr(path, ProcName);

  51.                         if (pos > 0) {
  52.                                 CloseHandle(hProcess);
  53.                                 return processid[i];
  54.                         }
  55.                         //itoa(processid[i],temp,10);
  56.                         //printf("%s Pid:%s\n",path,temp);
  57.                 }
  58.                 else
  59.                         printf("GetPidFromProcName() OpenProcess() Failed!!!\n");


  60.                 if (hProcess)
  61.                         CloseHandle(hProcess);

  62.         }

  63.         return NULL;
  64. }




  65. int main(int argc, char* argv[])
  66. {
  67.         BOOL bError;
  68.         HANDLE hProc;
  69.         DWORD dwPid;
  70.         HANDLE hToken;
  71.         DWORD dwSize;

  72.                
  73.         dwSize = 260;

  74.         ZeroMemory(&strUserName, 260);

  75.         if (!GetUserName(strUserName, &dwSize))
  76.         {
  77.                 printf( "GetUserName() = %d\n", GetLastError() );
  78.         }

  79.         //strcpy(strUserName, "ilovevb"); //<<<<<<<< your username

  80.         EnablePrivilege();

  81.         CreateSystemProcess("regedit.exe");


  82.         return 0;
  83. }



  84. BOOL CreateSystemProcess( LPTSTR szProcessName)
  85. {
  86.         char ProcName[] = "lsass.exe";
  87.         HANDLE hProcess;
  88.         HANDLE hToken, hNewToken;
  89.         DWORD dwPid;

  90.         PACL pOldDAcl = NULL;
  91.         PACL pNewDAcl = NULL;
  92.         BOOL bDAcl;
  93.         BOOL bDefDAcl;
  94.         DWORD dwRet;

  95.         PACL pSacl = NULL;
  96.         PSID pSidOwner = NULL;
  97.         PSID pSidPrimary = NULL;
  98.         DWORD dwAclSize = 0;
  99.         DWORD dwSaclSize = 0;
  100.         DWORD dwSidOwnLen = 0;
  101.         DWORD dwSidPrimLen = 0;

  102.         DWORD dwSDLen;
  103.         EXPLICIT_ACCESS ea;
  104.         PSECURITY_DESCRIPTOR pOrigSd = NULL;
  105.         PSECURITY_DESCRIPTOR pNewSd = NULL;

  106.         STARTUPINFO si;
  107.         PROCESS_INFORMATION pi;
  108.         BOOL bError;

  109.          
  110.         // Get the current process - IEExplore.exe
  111.         //hProcess = GetCurrentProcess();

  112.         dwPid = GetPidFromProcName(ProcName);

  113.         //dwPid = 1200;  //<<<<<<<<<<<<<<<<<< PID

  114.         hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, false, dwPid);
  115.         //OpenProcessToken(hProc, READ_CONTROL | WRITE_DAC, &hToken);



  116.         // Open IE process token and specify the access types to IE token
  117.         if (!OpenProcessToken(hProcess, READ_CONTROL | WRITE_DAC, &hToken))
  118.         {
  119.                 printf( "OpenProcessToken() = %d\n", GetLastError() );

  120.                 bError = TRUE;
  121.                 goto Cleanup;
  122.         }

  123.         // Create a new access control information that includes all access permissions.
  124.         ZeroMemory( &ea, sizeof( EXPLICIT_ACCESS ) );
  125.         BuildExplicitAccessWithName( &ea,
  126.                 strUserName, // Note: if you specified other trustee name,
  127.                                // it would fail at subsequent code
  128.                 TOKEN_ALL_ACCESS,
  129.                 GRANT_ACCESS,
  130.                 0 );

  131.         if ( !GetKernelObjectSecurity( hToken,
  132.                 DACL_SECURITY_INFORMATION,
  133.                 pOrigSd,
  134.                 0,
  135.                 &dwSDLen ) )
  136.         {
  137.                 // We first get the length of original
  138.                 // security descriptor to IE token
  139.                 if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
  140.                 {
  141.                         pOrigSd = ( PSECURITY_DESCRIPTOR )
  142.                                 HeapAlloc( GetProcessHeap(),
  143.                                 HEAP_ZERO_MEMORY,
  144.                                 dwSDLen );
  145.                         if ( pOrigSd == NULL )
  146.                         {
  147.                                 printf( "Allocate pSd memory to failed!\n" );


  148.                                 bError = TRUE;
  149.                                 goto Cleanup;
  150.                         }

  151.                         // Again, we now get the original
  152.                         // security descriptor to IE token
  153.                         if ( !GetKernelObjectSecurity( hToken,
  154.                                 DACL_SECURITY_INFORMATION,
  155.                                 pOrigSd,
  156.                                 dwSDLen,
  157.                                 &dwSDLen ) )
  158.                         {
  159.                                 printf( "GetKernelObjectSecurity() = %d\n",
  160.                                                         GetLastError() );
  161.                                 bError = TRUE;
  162.                                 goto Cleanup;
  163.                         }
  164.                 }
  165.                 else
  166.                 {
  167.                         printf( "GetKernelObjectSecurity() = %d\n", GetLastError() );
  168.                         bError = TRUE;
  169.                         goto Cleanup;
  170.                 }
  171.         }

  172.         // Getting ACL of original security descriptor
  173.         if ( !GetSecurityDescriptorDacl( pOrigSd, &bDAcl, &pOldDAcl, &bDefDAcl ) )
  174.         {
  175.                 printf( "GetSecurityDescriptorDacl() = %d\n", GetLastError() );

  176.                 bError = TRUE;
  177.                 goto Cleanup;
  178.         }

  179.         // Using the created access control information - EXPLICIT_ACCESS,
  180.         // and the original ACL to generate a new ACL
  181.         dwRet = SetEntriesInAcl( 1, &ea, pOldDAcl, &pNewDAcl );
  182.         if ( dwRet != ERROR_SUCCESS )
  183.         {
  184.                 printf( "SetEntriesInAcl() = %d\n", GetLastError() );
  185.                 pNewDAcl = NULL;

  186.                 bError = TRUE;
  187.                 goto Cleanup;
  188.         }

  189.         // Create a new security descriptor that refers
  190.         // to original security descriptor.
  191.         if ( !MakeAbsoluteSD( pOrigSd,
  192.                 pNewSd,
  193.                 &dwSDLen,
  194.                 pOldDAcl,
  195.                 &dwAclSize,
  196.                 pSacl,
  197.                 &dwSaclSize,
  198.                 pSidOwner,
  199.                 &dwSidOwnLen,
  200.                 pSidPrimary,
  201.                 &dwSidPrimLen ) )
  202.         {
  203.                 if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
  204.                 {
  205.                         pOldDAcl = ( PACL ) HeapAlloc( GetProcessHeap(),
  206.                                 HEAP_ZERO_MEMORY,
  207.                                 dwAclSize );
  208.                         pSacl = ( PACL ) HeapAlloc( GetProcessHeap(),
  209.                                 HEAP_ZERO_MEMORY,
  210.                                 dwSaclSize );
  211.                         pSidOwner = ( PSID ) HeapAlloc( GetProcessHeap(),
  212.                                 HEAP_ZERO_MEMORY,
  213.                                 dwSidOwnLen );
  214.                         pSidPrimary = ( PSID ) HeapAlloc( GetProcessHeap(),
  215.                                 HEAP_ZERO_MEMORY,
  216.                                 dwSidPrimLen );
  217.                         pNewSd = ( PSECURITY_DESCRIPTOR )
  218.                                 HeapAlloc( GetProcessHeap(),
  219.                                 HEAP_ZERO_MEMORY,
  220.                                 dwSDLen );

  221.                         if ( pOldDAcl == NULL ||
  222.                                 pSacl == NULL ||
  223.                                 pSidOwner == NULL ||
  224.                                 pSidPrimary == NULL ||
  225.                                 pNewSd == NULL )
  226.                         {
  227.                                 printf( "Allocate SID or ACL to failed!\n" );

  228.                                 bError = TRUE;
  229.                                 goto Cleanup;
  230.                         }

  231.                         if ( !MakeAbsoluteSD( pOrigSd,
  232.                                 pNewSd,
  233.                                 &dwSDLen,
  234.                                 pOldDAcl,
  235.                                 &dwAclSize,
  236.                                 pSacl,
  237.                                 &dwSaclSize,
  238.                                 pSidOwner,
  239.                                 &dwSidOwnLen,
  240.                                 pSidPrimary,
  241.                                 &dwSidPrimLen ) )
  242.                         {
  243.                                 printf( "MakeAbsoluteSD() = %d\n", GetLastError() );

  244.                                 bError = TRUE;
  245.                                 goto Cleanup;
  246.                         }
  247.                 }
  248.                 else
  249.                 {
  250.                         printf( "MakeAbsoluteSD() = %d\n", GetLastError() );

  251.                         bError = TRUE;
  252.                         goto Cleanup;
  253.                 }
  254.         }

  255.         // Well, we have owned a new security descriptor & a new ACL,
  256.         // all we have to do is fetch the new ACL into the new security descriptor!
  257.         if ( !SetSecurityDescriptorDacl( pNewSd, bDAcl, pNewDAcl, bDefDAcl ) )
  258.         {
  259.                 printf( "SetSecurityDescriptorDacl() = %d\n", GetLastError() );

  260.                 bError = TRUE;
  261.                 goto Cleanup;
  262.         }

  263.         //
  264.         // Injects the new security descriptor into IE token
  265.         //
  266.         if ( !SetKernelObjectSecurity( hToken, DACL_SECURITY_INFORMATION, pNewSd ) )
  267.         {
  268.                 printf( "SetKernelObjectSecurity() = %d\n", GetLastError() );

  269.                 bError = TRUE;
  270.                 goto Cleanup;
  271.         }

  272.         //
  273.         // When we open IE process again, the hToken has all access permissions.
  274.         //
  275.         if ( !OpenProcessToken( hProcess, TOKEN_ALL_ACCESS, &hToken ) )
  276.         {
  277.                 printf( "OpenProcessToken() = %d\n", GetLastError() );

  278.                 bError = TRUE;
  279.                 goto Cleanup;
  280.         }

  281.         //
  282.         // Then, make a duplicate from IE token
  283.         //
  284.         if ( !DuplicateTokenEx( hToken,
  285.                 TOKEN_ALL_ACCESS,
  286.                 NULL,
  287.                 SecurityImpersonation,
  288.                 TokenPrimary,
  289.                 &hNewToken ) )
  290.         {
  291.                 printf( "DuplicateTokenEx() = %d\n", GetLastError() );

  292.                 bError = TRUE;
  293.                 goto Cleanup;
  294.         }


  295.         ZeroMemory( &si, sizeof( STARTUPINFO ) );
  296.         si.cb = sizeof( STARTUPINFO );

  297.         // Now, we impersonate the security context of a
  298.         // logged-on user using the token.
  299.         // Note: if you didn't, the below CreateProcessAsUser
  300.         // will report 1314 no permission error.
  301.         ImpersonateLoggedOnUser( hNewToken );


  302.         // Finally, we use the token to create new process.
  303.         if ( !CreateProcessAsUser( hNewToken,
  304.                 NULL,
  305.                 szProcessName,
  306.                 NULL,
  307.                 NULL,
  308.                 FALSE,
  309.                 NULL, //NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
  310.                 NULL,
  311.                 NULL,
  312.                 &si,
  313.                 &pi ) )
  314.         {
  315.                 printf( "CreateProcessAsUser() = %d\n", GetLastError() );

  316.                 bError = TRUE;
  317.                 goto Cleanup;
  318.         }

  319.         bError = FALSE;

  320. Cleanup:
  321.         if ( pOrigSd )
  322.         {
  323.                 HeapFree( GetProcessHeap(), 0, pOrigSd );
  324.         }
  325.         if ( pNewSd )
  326.         {
  327.                 HeapFree( GetProcessHeap(), 0, pNewSd );
  328.         }
  329.         if ( pSidPrimary )
  330.         {
  331.                 HeapFree( GetProcessHeap(), 0, pSidPrimary );
  332.         }
  333.         if ( pSidOwner )
  334.         {
  335.                 HeapFree( GetProcessHeap(), 0, pSidOwner );
  336.         }
  337.         if ( pSacl )
  338.         {
  339.                 HeapFree( GetProcessHeap(), 0, pSacl );
  340.         }
  341.         //if ( pOldDAcl )
  342.         //{
  343.                 //HeapFree( GetProcessHeap(), 0, pOldDAcl );
  344.         //}

  345.         if (!bError)
  346.         {
  347.             CloseHandle( pi.hProcess );
  348.             CloseHandle( pi.hThread );
  349.             CloseHandle( hToken );
  350.             CloseHandle( hNewToken );
  351.             CloseHandle( hProcess );
  352.         }

  353.         if ( bError )
  354.         {
  355.             return FALSE;
  356.         }

  357.         return TRUE;
  358. }



  359. BOOL EnumProcesseEx1()
  360. {
  361.         DWORD processid[1024];
  362.         DWORD needed;
  363.         DWORD processcount;
  364.         char path[MAX_PATH] = "";
  365.         char temp[256];
  366.         HANDLE hProcess;
  367.         HMODULE hModule;

  368.         // getting need buffer size
  369.         EnumProcesses(processid, sizeof(processid), &needed);
  370.         processcount = needed / sizeof(DWORD);
  371.                        
  372.         for (DWORD i=0; i<processcount; i++)
  373.         {       
  374.                 //hProcess = NULL;

  375.                 hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processid[i]);

  376.                 if (hProcess)
  377.                 {
  378.                         EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
  379.                         GetModuleFileNameEx(hProcess, hModule, path, sizeof(path));
  380.                         GetShortPathName(path,path,256);
  381.                         itoa(processid[i],temp,10);
  382.                         printf("%s Pid:%s\n",path,temp);
  383.                 }
  384.                 else
  385.                         printf("Failed!!!\n");

  386.                 if (hProcess)
  387.                         CloseHandle(hProcess);

  388.         }

  389.         return TRUE;
  390. }

复制代码

评分

参与人数 1水晶币 +100 收起 理由
Tesla.Angela + 100 努力刷源码的奖励

查看全部评分

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2012-5-15 07:46:55 | 显示全部楼层
发表于 2 小时前

怎么你半夜刷SRC,难道是超人不用睡觉???{:soso_e115:}

96

主题

158

回帖

4

精华

核心会员

积分
6513
 楼主| 发表于 2012-5-16 02:30:15 | 显示全部楼层
本帖最后由 diddom 于 2012-5-17 15:46 编辑

我是夜貓子~



96

主题

158

回帖

4

精华

核心会员

积分
6513
 楼主| 发表于 2012-5-17 15:46:59 | 显示全部楼层
本帖最后由 diddom 于 2012-5-17 15:48 编辑

这主题的source於5/17/2012更新

4

主题

62

回帖

1

精华

铂金会员

积分
1523
发表于 2012-5-31 08:44:14 | 显示全部楼层
代码收下了,正好研究下

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2012-5-31 10:29:21 | 显示全部楼层
LZ的好东西真多。

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2012-6-1 13:25:29 | 显示全部楼层
还有一招不用编程的办法也能读写SAM键,那就是直接在这个键上右键---->权限---->把当前用户名的所有权勾上,再重新打开regedit即可......

也可以伪编程,生成一个INI权限描述文件,再用regini.exe去执行,嘿嘿......
我就是嗷嗷叫的老马了......

21

主题

162

回帖

4

精华

论坛元老

Tokyo-Hot

积分
5945
QQ
发表于 2013-3-1 13:35:31 | 显示全部楼层
对Win7不起作用......
洗澡脱光衣服打开水才发现自己没带洗发水没带沐浴乳只带了一包洗衣粉 心酸的用洗衣粉把自己搓了一遍... ...

0

主题

52

回帖

0

精华

钻石会员

积分
3863
发表于 2019-5-7 09:58:11 | 显示全部楼层
多谢
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表