From 3a57d2d260527a52ac854103d2d5ff7e1ff94107 Mon Sep 17 00:00:00 2001 From: Alexander <1656007+0xbadjuju@users.noreply.github.com> Date: Mon, 23 Aug 2021 10:55:43 -0500 Subject: [PATCH 01/36] Migrating to D/Invoke --- Tokenvator/MainLoop.Modules.cs | 1 + Tokenvator/MainLoop.cs | 1 + .../Plugins/AccessTokens/AccessTokens.cs | 25 ++++++++ .../Plugins/AccessTokens/CreateTokens.cs | 7 ++- .../Plugins/AccessTokens/RestrictedToken.cs | 1 + .../Plugins/AccessTokens/TokenDriver.cs | 1 + .../Plugins/AccessTokens/TokenInformation.cs | 1 + .../Plugins/AccessTokens/TokenManipulation.cs | 1 + Tokenvator/Plugins/Enumeration/DesktopACL.cs | 1 + .../Plugins/Enumeration/UserSessions.cs | 1 + Tokenvator/Plugins/Execution/CreateProcess.cs | 7 +++ Tokenvator/Plugins/Execution/PSExec.cs | 1 + .../Plugins/MiniFilters/FilterInstance.cs | 1 + Tokenvator/Plugins/MiniFilters/Filters.cs | 1 + Tokenvator/Plugins/NamedPipes/NamedPipes.cs | 1 + Tokenvator/Resources/Misc.cs | 1 + Tokenvator/Tokenvator.csproj | 60 +++++++++++++++---- 17 files changed, 97 insertions(+), 15 deletions(-) diff --git a/Tokenvator/MainLoop.Modules.cs b/Tokenvator/MainLoop.Modules.cs index d670049..75dfde0 100644 --- a/Tokenvator/MainLoop.Modules.cs +++ b/Tokenvator/MainLoop.Modules.cs @@ -17,6 +17,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + using System.IO; namespace Tokenvator diff --git a/Tokenvator/MainLoop.cs b/Tokenvator/MainLoop.cs index 838c4eb..6c3f8c1 100644 --- a/Tokenvator/MainLoop.cs +++ b/Tokenvator/MainLoop.cs @@ -10,6 +10,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + namespace Tokenvator { partial class MainLoop diff --git a/Tokenvator/Plugins/AccessTokens/AccessTokens.cs b/Tokenvator/Plugins/AccessTokens/AccessTokens.cs index 3996e9a..e972c3f 100644 --- a/Tokenvator/Plugins/AccessTokens/AccessTokens.cs +++ b/Tokenvator/Plugins/AccessTokens/AccessTokens.cs @@ -7,11 +7,17 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; +//using MonkeyWorks.Unmanaged.Libraries.DInvoke; + +using DInvoke.DynamicInvoke; + using System.Runtime.InteropServices; using System.Diagnostics; namespace Tokenvator.Plugins.AccessTokens { + using MonkeyWorks = MonkeyWorks.Unmanaged.Libraries.DInvoke; + class AccessTokens : IDisposable { protected IntPtr phNewToken; @@ -97,6 +103,19 @@ public virtual bool OpenProcessToken(int processId) */ IntPtr hProcess = kernel32.OpenProcess(ProcessThreadsApi.ProcessSecurityRights.PROCESS_QUERY_INFORMATION, false, (uint)processId); + + Console.WriteLine("[D] Calling D/Invoke"); + + IntPtr pkernel32 = Generic.GetPebLdrModuleEntry("kernel32.dll"); + IntPtr pOpenProcess = Generic.GetExportAddress(pkernel32, "OpenProcess"); + object[] parameters = + { + Winnt.PROCESS_QUERY_INFORMATION, false, (uint)processId + }; + + IntPtr hProcess = (IntPtr)Generic.DynamicFunctionInvoke(pOpenProcess, typeof(MonkeyWorks.kernel32.OpenProcess), ref parameters); + //IntPtr hProcess = kernel32.OpenProcess(Winnt.PROCESS_QUERY_INFORMATION, false, (uint)processId); + if (IntPtr.Zero == hProcess) { Misc.GetWin32Error("OpenProcess"); @@ -104,6 +123,12 @@ public virtual bool OpenProcessToken(int processId) } Console.WriteLine("[*] Recieved Process Handle 0x{0}", hProcess.ToString("X4")); + IntPtr pOpenProcessToken = Generic.GetExportAddress(pkernel32, "OpenProcessToken"); + parameters = new object[] + { + hProcess, Winnt.TOKEN_ALL_ACCESS, hExistingToken + }; + if (!kernel32.OpenProcessToken(hProcess, Winnt.TOKEN_ALL_ACCESS, out hExistingToken)) { if (!kernel32.OpenProcessToken(hProcess, (uint)Winnt.ACCESS_MASK.MAXIMUM_ALLOWED, out hExistingToken)) diff --git a/Tokenvator/Plugins/AccessTokens/CreateTokens.cs b/Tokenvator/Plugins/AccessTokens/CreateTokens.cs index f40cd24..0412860 100644 --- a/Tokenvator/Plugins/AccessTokens/CreateTokens.cs +++ b/Tokenvator/Plugins/AccessTokens/CreateTokens.cs @@ -9,6 +9,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + using System.Security.Principal; namespace Tokenvator.Plugins.AccessTokens @@ -38,8 +39,8 @@ public void CreateToken(string[] groups, string command) return; } - uint LG_INCLUDE_INDIRECT = 0x0001; - uint MAX_PREFERRED_LENGTH = 0xFFFFFFFF; + //uint LG_INCLUDE_INDIRECT = 0x0001; + //uint MAX_PREFERRED_LENGTH = 0xFFFFFFFF; Console.WriteLine(); Console.WriteLine("_SECURITY_QUALITY_OF_SERVICE"); @@ -124,7 +125,7 @@ public void CreateToken(string userName, string[] groups, string command) return; } - uint MAX_PREFERRED_LENGTH = 0xFFFFFFFF; + //uint MAX_PREFERRED_LENGTH = 0xFFFFFFFF; #region _OBJECT_ATTRIBUTES Console.WriteLine(); diff --git a/Tokenvator/Plugins/AccessTokens/RestrictedToken.cs b/Tokenvator/Plugins/AccessTokens/RestrictedToken.cs index d85a04c..42733da 100644 --- a/Tokenvator/Plugins/AccessTokens/RestrictedToken.cs +++ b/Tokenvator/Plugins/AccessTokens/RestrictedToken.cs @@ -8,6 +8,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + namespace Tokenvator.Plugins.AccessTokens { class RestrictedToken : AccessTokens diff --git a/Tokenvator/Plugins/AccessTokens/TokenDriver.cs b/Tokenvator/Plugins/AccessTokens/TokenDriver.cs index 7eb0f8a..72ba29e 100644 --- a/Tokenvator/Plugins/AccessTokens/TokenDriver.cs +++ b/Tokenvator/Plugins/AccessTokens/TokenDriver.cs @@ -10,6 +10,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + namespace Tokenvator.Plugins.AccessTokens { class TokenDriver : IDisposable diff --git a/Tokenvator/Plugins/AccessTokens/TokenInformation.cs b/Tokenvator/Plugins/AccessTokens/TokenInformation.cs index 23f88fe..244ea3a 100644 --- a/Tokenvator/Plugins/AccessTokens/TokenInformation.cs +++ b/Tokenvator/Plugins/AccessTokens/TokenInformation.cs @@ -10,6 +10,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + namespace Tokenvator.Plugins.AccessTokens { class TokenInformation : AccessTokens diff --git a/Tokenvator/Plugins/AccessTokens/TokenManipulation.cs b/Tokenvator/Plugins/AccessTokens/TokenManipulation.cs index d84bcc5..c6f0b2d 100644 --- a/Tokenvator/Plugins/AccessTokens/TokenManipulation.cs +++ b/Tokenvator/Plugins/AccessTokens/TokenManipulation.cs @@ -13,6 +13,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + namespace Tokenvator.Plugins.AccessTokens { partial class TokenManipulation : AccessTokens diff --git a/Tokenvator/Plugins/Enumeration/DesktopACL.cs b/Tokenvator/Plugins/Enumeration/DesktopACL.cs index def27de..0587c2c 100644 --- a/Tokenvator/Plugins/Enumeration/DesktopACL.cs +++ b/Tokenvator/Plugins/Enumeration/DesktopACL.cs @@ -5,6 +5,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + using Tokenvator.Plugins.AccessTokens; using Tokenvator.Resources; diff --git a/Tokenvator/Plugins/Enumeration/UserSessions.cs b/Tokenvator/Plugins/Enumeration/UserSessions.cs index a0a8c61..bf031f5 100644 --- a/Tokenvator/Plugins/Enumeration/UserSessions.cs +++ b/Tokenvator/Plugins/Enumeration/UserSessions.cs @@ -12,6 +12,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + namespace Tokenvator.Plugins.Enumeration { class UserSessions diff --git a/Tokenvator/Plugins/Execution/CreateProcess.cs b/Tokenvator/Plugins/Execution/CreateProcess.cs index 06045e4..cb42428 100644 --- a/Tokenvator/Plugins/Execution/CreateProcess.cs +++ b/Tokenvator/Plugins/Execution/CreateProcess.cs @@ -4,6 +4,7 @@ using Tokenvator.Resources; +using DInvoke.DynamicInvoke; using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; @@ -16,6 +17,12 @@ static class CreateProcess //////////////////////////////////////////////////////////////////////////////// public static bool CreateProcessWithLogonW(IntPtr phNewToken, string name, string arguments) { + IntPtr padvapi32 = Generic.GetPebLdrModuleEntry("advapi32.dll"); + IntPtr pImpersonateLoggedOnUser = Generic.GetExportAddress(padvapi32, "ImpersonateLoggedOnUser"); + object[] paramaters = { phNewToken }; + bool retVal = (bool)Generic.DynamicFunctionInvoke(pImpersonateLoggedOnUser, typeof(Win32.Delegates.OpenProcess), ref paramaters); + + if (IntPtr.Zero != phNewToken && !advapi32.ImpersonateLoggedOnUser(phNewToken)) { Console.WriteLine("[-] Token Impersonation Failed"); diff --git a/Tokenvator/Plugins/Execution/PSExec.cs b/Tokenvator/Plugins/Execution/PSExec.cs index eae86d4..a15d6df 100644 --- a/Tokenvator/Plugins/Execution/PSExec.cs +++ b/Tokenvator/Plugins/Execution/PSExec.cs @@ -6,6 +6,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + using Tokenvator.Resources; namespace Tokenvator.Plugins.Execution diff --git a/Tokenvator/Plugins/MiniFilters/FilterInstance.cs b/Tokenvator/Plugins/MiniFilters/FilterInstance.cs index b081650..1d2f7ad 100644 --- a/Tokenvator/Plugins/MiniFilters/FilterInstance.cs +++ b/Tokenvator/Plugins/MiniFilters/FilterInstance.cs @@ -4,6 +4,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + namespace Tokenvator.Plugins.MiniFilters { class FilterInstance : Filters diff --git a/Tokenvator/Plugins/MiniFilters/Filters.cs b/Tokenvator/Plugins/MiniFilters/Filters.cs index 87685c2..a6d851c 100644 --- a/Tokenvator/Plugins/MiniFilters/Filters.cs +++ b/Tokenvator/Plugins/MiniFilters/Filters.cs @@ -4,6 +4,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + using Tokenvator.Resources; namespace Tokenvator.Plugins.MiniFilters diff --git a/Tokenvator/Plugins/NamedPipes/NamedPipes.cs b/Tokenvator/Plugins/NamedPipes/NamedPipes.cs index c0be358..2a247cd 100644 --- a/Tokenvator/Plugins/NamedPipes/NamedPipes.cs +++ b/Tokenvator/Plugins/NamedPipes/NamedPipes.cs @@ -11,6 +11,7 @@ using MonkeyWorks.Unmanaged.Headers; using MonkeyWorks.Unmanaged.Libraries; + namespace Tokenvator.Plugins.NamedPipes { class NamedPipes diff --git a/Tokenvator/Resources/Misc.cs b/Tokenvator/Resources/Misc.cs index be5de5f..00e96c8 100644 --- a/Tokenvator/Resources/Misc.cs +++ b/Tokenvator/Resources/Misc.cs @@ -3,6 +3,7 @@ using System.Linq; using MonkeyWorks.Unmanaged.Libraries; + using Tokenvator.Plugins.Execution; namespace Tokenvator.Resources diff --git a/Tokenvator/Tokenvator.csproj b/Tokenvator/Tokenvator.csproj index a3d4f18..4db4bef 100644 --- a/Tokenvator/Tokenvator.csproj +++ b/Tokenvator/Tokenvator.csproj @@ -142,6 +142,19 @@ + + + + + + + + + + + + + @@ -172,18 +185,6 @@ - - - - - - - - - - - - @@ -191,6 +192,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + @@ -198,9 +223,17 @@ .editorconfig + + + + + + + + @@ -209,6 +242,9 @@ false + + +