渗透测试中的 Application Compatibility Shims

发布于 2024-12-18 14:29:56 字数 10777 浏览 34 评论 0

0x00 前言

Application Compatibility 是一个框架,主要用来解决应用程序在新版 Windows 系统上的兼容性问题。然而在渗透测试中它却有着更多的用处,本文将对公开资料进行整理,介绍在渗透测试中的具体利用技术,帮助大家更好的认识它,防御它。

0x01 简介

Shim:

相当于是在应用程序和 Windows API 之间的逻辑层。

当应用程序创建进程的时候,WindowsLoader 首先会检查 sysmain.sdb(位于%windir%\AppPatch\),如果存在已注册的 sdb 文件,IAT 将被重定向到 Shim,实现功能替换。

本文将介绍以下内容:

  • 创建 Shim 文件
  • 实际利用方法
  • 相关开源工具
  • 检测和防御

0x02 创建 Shim 文件

1.Microsoft Application Compatibility Toolkit(ACT)

下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=7352

默认修复方式种类个数为 365

Alt text

启动时加入 /x 参数可获得更多修复方式,总数 807

如图

Alt text

根据提示创建后生成.sdb 文件,需要安装使其生效

可在 Microsoft Application Compatibility Toolkit 中直接选择安装,如图

Alt text

0x03 实际利用方法

1.Hiding in the Registry

选择 VirtualRegistry

Command line 填入:

ADDREDIRECT(HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run^HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunHidden)

安装 shim

启动 regedit

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 下键值无法查看,如图

Alt text

但在 cmd 下执行如下命令可以查看:

reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

如图

Alt text

2.Hiding in the File System

选择 CorrectFilePaths

Command line 填入:

c:\test\;c:\users

Module name 填入 *.exe ,点击 add

安装 shim

启动 cmd.exe,无法查看 c:\test 下的文件

如图

Alt text

注:类似的还有 VirtualizeDeleteFileRedirectShortcut

3.Persistence

可供选择的 Fix 有:

  • InjectDll
  • LoadLibraryRedirect
  • RedirectShortcut
  • RedirectEXE
  • ShimViaEAT
  • LoadLibraryfromCWD
  • Hijacking DLL

4.Disable Security Features of the OS

可供选择的 Fix 有:

  • Disable NX
  • Disable ASLR
  • DisableSEH
  • Prevent the Loading of DLLs
  • Disable Windows Resource Protection
  • Elevate to Administrator
  • DisableWindowsDefender
  • DisableAdvancedRPCClientHardening

注:以上思路参考自:

0x04 安装和卸载 Shim

1.sdbinst.exe

用来安装和卸载.sdb 文件

微软官方提供,默认位于 c:\windows\system32 下,运行需要管理员权限

usage:

 -? - print this help text.
 -p - Allow SDBs containing patches.
 -q - Quiet mode: prompts are auto-accepted.
 -u - Uninstall.
 -g {guid} - GUID of file (uninstall only).
 -n "name" - Internal name of file (uninstall only).

卸载:

sdbinst.exe -u -n "name"

安装过程中 sdbinst.exe 做了如下操作:

在如下注册表位置创建键值保存 Shim 信息:

  • HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Custom
  • HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\InstalledSDB

将 sdb 文件复制到如下文件路径:

  • C:\Windows\AppPatch\Custom\
  • C:\Windows\AppPatch\Custom\Custom64\

添加到已安装的应用程序列表中

依次打开控制面板-程序-程序和功能-卸载程序,可看到安装的 Shim 名称

2.sdb-explorer

下载地址:https://github.com/evil-e/sdb-explorer

同样可用来安装.sdb 文件,相比于 sdbinst.exe 多了如下特征:

  • 源代码开源
  • 支持 In-Memory patch
  • 安装过程不将 sdb 文件复制到 C:\Windows\AppPatch\Custom\下
  • 安装过程不在已安装的应用程序列表中显示安装的 Shim 名称

usage:

Print full sdb tree
  sdb-explorer.exe -t filename.sdb 

Print patch details
  sdb-explorer.exe [-i] -p filename.sdb (patch | patchid | patchref | patchbin)
     -i - create IDAPython Script (optional)

Print patch details for checksum
  sdb-explorer.exe [-i] -s filename.sdb

Create file containing the leaked memory
  sdb-explorer.exe -l filename.sdb

Print Match Entries
  sdb-explorer.exe -d filename.sdb

Create Patch From file
  sdb-explorer.exe -C config.dat [-o filename.sdb]

Register sdb file
  sdb-explorer.exe -r filename.sdb [-a application.exe]

Display usage
  sdb-explorer.exe -h

演示如图

Alt text

执行以下命令注册 sdb 文件:

sdb-explorer.exe -r C:\Users\a\Desktop\test1.sdb -a putty.exe

注:-a 的参数指定程序的名称,不能填入程序的绝对路径

通过 sdb-explorer.exe 注册的 sdb 文件无法通过 sdbinst.exe 来删除,会显示 sbd 文件不存在,如图

Alt text

卸载可通过删除注册表键值的方式实现

注:通过 sdb-explorer 可以创建一个 In-Memory patch 的 shim,接着编译成 sdb 文件,进而安装使用,关于 In-Memory patch 的学习心得将在以后分享

In-Memory patch:

  • 可以替换或写入内存中的某个区域的任意字节
  • 可用来绕过应用程序白名单

0x05 查看 Shim 信息

1.sdb2xml

从.sdb 文件提取出 xml 格式的数据,可用来分析 sdb 文件

作者:Heath Stewart

下载地址:https://blogs.msdn.microsoft.com/heaths/2007/11/03/shim-database-to-xml/

usage:

sdb2xml sdb [-out report] [-base64 | -extract] [-?]

  sdb          Path to the shim database to process.
  -base64      Base-64 encode data in the XML report.
  -extract     Extract binary data to current or report directory.
  -out report  Path to the XML file to generate; otherwise, output to console.

如图,使用 sdb2xml 查看 test1.sdb 文件中的数据

Alt text

2.Compatibility Database Dumper (CDD)

作者:Alex Ionesceu

usage:

cdd.exe [-s][-e][-l][-f][-p][-d kernel-mode database file][-a usermode database file]

 -s Show shims
 -e Show executables
 -l Show layers
 -f Show flags
 -p Show patches
 -d Use Blocked Driver Database from this path
 -a Use Application Compatibility Database from this path

参考地址:

http://www.alex-ionescu.com/?p=40

但作者 Alex Ionescu 目前尚未将其开源

3.Shim Database Tool (sdb)

Usage:

sdb.exe [-noids] [-match] [PathToShimDatabse] [PathToFileName]

 -noids  Will prevent the output of the TagIds
 -match  Will match the provided file with the installed databases
         and displays the activated shims
         In this case 'PathToFileName' is required

NOTE: If no shim database path is provided,
      the default database will be used.

从.sdb 文件提取出 xml 格式的数据,演示如图

Alt text

显示指定程序是否被添加 Shim,如图,找到 putty.exe 已被添加了一个 Shim,guid 为 8F9DA6E2-5A7C-41E1-B89F8B72D63DEBA8

Alt text

0x06 检测和防御

禁用 Shim 的方法:

  • 英文系统: 打开 gpedit.msc,选择 Administrative Templates-Windows Components-Application Compatibility-Turn off Application Compatibility Engine
  • 中文系统: 打开 gpedit.msc,选择计算机配置-管理模板-Windows 组件-应用程序兼容性-关闭应用程序兼容性引擎

但不建议关闭 Shim,原因如下:

  • 导致 EMET 无法使用
  • 无法更新补丁

检测和防御:

0x07 小结

本文对 Application Compatibility Shims 在渗透测试中的相关技巧做了整理,希望对大家有所帮助。对于 In-Memory patch,值得研究的还有很多,学习心得将在以后分享。

更多关于 Shim 的研究资料可访问:http://sdb.tools/index.html

本文参考链接:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

不爱素颜

暂无简介

文章
评论
26 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文