自学内容网 自学内容网

​.NET一款反序列化执行命令的白名单工具

01阅读须知

此文所提供的信息只为网络安全人员对自己所负责的网站、服务器等(包括但不限于)进行检测或维护参考,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作。利用此文所提供的信息而造成的直接或间接后果和损失,均由使用者本人负责。本文所提供的工具仅用于学习,禁止用于其他方面

02基本介绍

Sharp4AddUtil.exe 是一款用于管理和配置 Microsoft Office 加载项的 Windows 系统实用工具,具备了微软签名,属于系统白名单文件。一般用于帮助用户和开发者进行加载项的安装、卸载和修复,提供了一种简便的方式来处理 Office 加载项相关的问题。尽管 Sharp4AddUtil.exe 提供了多种便利功能,但它也存在严重的反序列化漏洞。这一漏洞允许攻击者利用该工具加载恶意的载荷,可能导致系统受到威胁。

图片

03使用方法

首先,通过TextFormattingRunProperties链路,生成一个包含反序列化Poc的攻击载荷,载荷内容如下图所示。

图片

接着,运行如下命令即可完成反序列化漏洞,弹出winver.exe进程,具体命令如下所示。

Sharp4AddUtil.exe -AddinRoot:.

图片

04原理解析

在上文中,我们介绍了 Sharp4AddUtil.exe 的基本功能和反序列化漏洞。接下来,我们将进一步分析工具中的其他重要方法,以理解其在加载项管理中的具体实现。UpdateAddInsIfExist 方法检查加载项路径是否存在,且 AddIns.store 文件是否不存在或已过期。如果满足条件,调用 AddInStore.BuildAddInCache 方法构建或更新加载项缓存。

private static void UpdateAddInsIfExist(string addInsPath, Collection<string> warningsCollection)
{
    string path = Path.Combine(addInsPath, "AddIns.store");
    FileIOPermission fileIOPermission = new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, addInsPath);
    fileIOPermission.Assert();
    
    if (Directory.Exists(addInsPath) && (!File.Exists(path) || AddInStore.AddInStoreIsOutOfDate(addInsPath)))
    {
        AddInStore.BuildAddInCache(addInsPath, warningsCollection);
    }
}

随后,AddInStoreIsOutOfDate 方法通过 GetAddInDeploymentState 方法获取当前的加载项部署状态,并检查其文件计数是否与期望值一致,确定加载项是否过期。

private static bool AddInStoreIsOutOfDate(string addInPath)
{
    if (addInPath == null)
    {
        throw new ArgumentNullException("addInPath");
    }
    
    string path = Path.Combine(addInPath, "AddIns.store");
    DateTime lastWriteTime = File.GetLastWriteTime(path);
    int num = 0;
    
    if (Directory.Exists(addInPath))
    {
        foreach (string path2 in Directory.GetDirectories(addInPath))
        {
            if (AddInStore.DirectoryNeedsUpdating(path2, lastWriteTime, ref num))
            {
                return true;
            }
        }
    }
    
    AddInDeploymentState addInDeploymentState = AddInStore.GetAddInDeploymentState(addInPath);
    return addInDeploymentState == null || num != addInDeploymentState.FileCount;
}

由于使用了 BinaryFormatter 进行反序列化,该方法容易受到反序列化攻击。攻击者可以构造恶意的加载项文件,当 ReadCache 方法尝试反序列化这些文件时,将会导致执行任意代码。

[SecuritySafeCritical]
private static T ReadCache<T>(string storeFileName, bool mustExist)
{
    new SecurityPermission(SecurityPermissionFlag.SerializationFormatter).Demand();
    new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, storeFileName).Assert();
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    T result = default(T);
    if (File.Exists(storeFileName))
    {
       using (Stream stream = File.OpenRead(storeFileName))
        {
             if (stream.Length < 12L)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Res.DeployedAddInsFileCorrupted, new object[] { storeFileName }));
                    }
                    BinaryReader binaryReader = new BinaryReader(stream);
                    int num = binaryReader.ReadInt32();
                    long num2 = binaryReader.ReadInt64();
                    try
                    {
                        result = (T)((object)binaryFormatter.Deserialize(stream));
                    }
                    catch (Exception innerException)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Res.CantDeserializeData, new object[] { storeFileName }), innerException);
                    }
                   
        }
    }
}

综上,Sharp4AddUtil.exe 是一款功能丰富的 Office 加载项管理工具,能够高效地管理加载项的安装与更新。尽管提供了许多便利功能,但反序列化漏洞可以帮助红队利用白名单方式运行恶意代码。

05.NET安全知识库

星球汇聚了各行业安全攻防技术大咖,并且每日分享.NET安全技术干货以及交流解答各类技术等问题。

图片

图片

星球文化20+个专题栏目涵盖了点、线、面、体等知识面!其中主题包括.NET Tricks、漏洞分析、内存马、代码审计、预编译、反序列化、webshell免杀、命令执行、C#工具库等等。

    

图片

图片


原文地址:https://blog.csdn.net/ahhacker86/article/details/142515007

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!