找回密码
 立即注册
查看: 768|回复: 0

[笔记] Unity AssetBundle高效加密案例分享

[复制链接]
发表于 2020-12-30 09:58 | 显示全部楼层 |阅读模式
这是侑虎科技第585篇文章,感谢作者江鱼供稿。欢迎转发分享,未经作者授权请勿转载。如果您有任何独到的见解或者发现也欢迎联系我们,一起探讨。(QQ群:793972859)
作者主页:https://www.zhihu.com/people/yu-jiang-3-65/,作者也是U Sparkle活动参与者,UWA欢迎更多开发朋友加入U Sparkle开发者计划,这个舞台有你更精彩!
随着AssetStudio的普及,Unity项目中使用的AssetBundle资源可以被各种小白用户解包提取,由此AssetBundle资源包的安全问题不得不引起我们重视。
过去,通过官方文档的了解,我们主要可以通过,AssetBundle.LoadFromMemory(Async)的方案来实现资源包加密,官方文档对这个方法是这样描述的:
Use this method to create an AssetBundle from an array of bytes. This is useful when you have downloaded the data with encryption and need to create the AssetBundle from the unencrypted bytes.
Compared to LoadFromMemoryAsync, this version is synchronous and will not return until it is done creating the AssetBundle object.
下面是官方文档中的示例代码:
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. using System.Collections;
  4. public class ExampleClass : MonoBehaviour
  5. {
  6.     byte[] MyDecription(byte[] binary)
  7.     {
  8.         byte[] decrypted = new byte[1024];
  9.         return decrypted;
  10.     }
  11.     IEnumerator Start()
  12.     {
  13.         var uwr = UnityWebRequest.Get("http://myserver/myBundle.unity3d");
  14.         yield return uwr.SendWebRequest();
  15.         byte[] decryptedBytes = MyDecription(uwr.downloadHandler.data);
  16.         AssetBundle.LoadFromMemory(decryptedBytes);
  17.     }
  18. }
复制代码
需要注意的是,对于AssetBundle.LoadFromMemory(Async)这个方法,在官方的AssetBundle foudamentals一文中,官方又非常明确的指出:
Unity's recommendation is not to use this API.
AssetBundle.LoadFromMemoryAsync loads an AssetBundle from a managed-code byte array (byte[] in C#). It will always copy the source data from the managed-code byte array into a newly-allocated, contiguous block of native memory. If the AssetBundle is LZMA compressed, it will decompress the AssetBundle while copying. Uncompressed and LZ4-compressed AssetBundles will be copied verbatim.
The peak amount of memory consumed by this API will be at least twice the size of the AssetBundle: one copy in native memory created by the API, and one copy in the managed byte array passed to the API. Assets loaded from an AssetBundle created via this API will therefore be duplicated three times in memory: once in the managed-code byte array, once in the native-memory copy of the AssetBundle and a third time in GPU or system memory for the asset itself.
Prior to Unity 5.3.3, this API was known as AssetBundle.CreateFromMemory. Its functionality has not changed.
从官方的解释中,我们可以看到AssetBundle.LoadFromMemory(Async)的使用成本非常高昂,不被推荐是自然而然的事情。但是,有没有更高效便捷的方式去对AssetBundle进行加密处理,防止被小白用户利用AssetStudio之类的工具轻易地提取到AssetBundle的资源呢?
在查看Unity API的时候发现LoadFromFile末尾有一个offset参数,那么这个参数有什么用呢?是否可以起到防止AssetBundle资源直接被AssetStudio提取呢?先看官方文档的接口说明:
public static AssetBundle LoadFromFile(string path, uint crc, along offset);
Parameters
Returns
AssetBundle Loaded AssetBundle object or null if failed.
Description
Synchronously loads an AssetBundle from a file on disk.
The function supports bundles of any compression type. In case of lzma compression, the data will be decompressed to the memory. Uncompressed and chunk-compressed bundles can be read directly from disk.
Compared to LoadFromFileAsync, this version is synchronous and will not return until it is done creating the AssetBundle object.
This is the fastest way to load an AssetBundle.
官方文档的代码示例并没有,提供offest参数的演示,所以在这里就不搬运了,接下来我会用自己写的测试代码来做演示。
首先,我们需将XAsset生成好的AssetBundle文件内容进行偏移处理,待Unity打包完成后遍历所有AssetBundle文件,并对文件添加offset后进行覆盖,代码如下:
  1. foreach (string bundleName in bundleNames)
  2. {
  3.     string filepath = outputPath + "/" + bundleName;
  4.     // 利用 hashcode 做偏移
  5.     string hashcode = manifest.GetAssetBundleHash(bundleName).ToString();
  6.     ulong offset = Utility.GetOffset(hashcode);
  7.     if ( offset > 0)
  8.     {
  9.         byte[] filedata = File.ReadAllBytes(filepath);
  10.         int filelen = ((int)offset + filedata.Length);
  11.         byte[] buffer = new byte[filelen];
  12.         copyHead(filedata, buffer, (uint)offset);
  13.         copyTo(filedata, buffer, (uint)offset);
  14.         FileStream fs = File.OpenWrite(filepath);
  15.         fs.Write(buffer, 0, filelen);
  16.         fs.Close();
  17.         offsets  += filepath + " offset:" + offset + "\n";
  18.     }
  19.     WriteItem(stream, bundleName, filepath, hashcode);
  20. }
复制代码
然后,我们再进行加载测试,我们分别使用offset参数加载AssetBundle,和模拟解密文件后从内存中加载AssetBundle然后读取其中的一个Texture用于显示,可以参考以下代码:
  1. // 基于offset加载AssetBundle
  2. async void onLoadWithOffsetClicked()
  3. {
  4.     if (offsetBundle)
  5.         offsetBundle.Unload(true);
  6.     var current_memory = Profiler.GetTotalAllocatedMemoryLong();
  7.     display_image.texture = null;
  8.     var path = System.IO.Path.Combine(Application.streamingAssetsPath, "assets_previews_offset");
  9.     var assetBundleRequest = AssetBundle.LoadFromFileAsync(path, 0, 294);
  10.     await assetBundleRequest;
  11.     var texture = assetBundleRequest.assetBundle.LoadAsset<Texture2D>("download.jpg");
  12.     display_image.texture = texture;
  13.     offsetBundle = assetBundleRequest.assetBundle;
  14.     Debug.Log("Offset Load Complete:" + (Profiler.GetTotalAllocatedMemoryLong() - current_memory));
  15. }
  16. // 基于Menmory加载AssetBundle
  17. async void onLoadWithMemoryClicked()
  18. {
  19.     if (memoryBundle)
  20.         memoryBundle.Unload(true);
  21.     var current_memory = Profiler.GetTotalAllocatedMemoryLong();
  22.     display_image.texture = null;
  23.     var path = System.IO.Path.Combine(Application.streamingAssetsPath, "assets_previews");
  24.     WWW www = new WWW("file://" + path);
  25.     await www;
  26.     var request = AssetBundle.LoadFromMemoryAsync( www.bytes);
  27.     await request;
  28.     var texture = request.assetBundle.LoadAsset<Texture2D>("download.jpg");
  29.     display_image.texture = texture;
  30.     memoryBundle = request.assetBundle;
  31.     www.Dispose();
  32.     Debug.Log("Memory Load Complete:"+ (Profiler.GetTotalAllocatedMemoryLong() - current_memory));
  33. }
复制代码
接下来,我们再看看对以上两个函数执行的Profiler数据分析采样的结果:


测试场景截图


使用offset参数加载之前的内存情况


使用offset参数加载AssetBundle之后的内存情况


使用LoadFromMemory加载之前的内存情况


使用LoadFromMemory加载之后的内存情况




通过对比发现,使用LoadFromMemory内存明显发生了增长,并且在加载过程中还出现了一个内存高峰。
由于我们对AssetBundle的资源进行了偏移,势必在理论上,AssetStudio无法直接解析出我们Unity工程中的AssetBundle,接下来我们再来看下,我们的理论是否经得起实践的考验。
经测试,没加offset的时候可以轻易地用AssetStudio预览AssetBundle中的资源,请参考下图(因为用的是公司项目的资源所以需要打码处理):




带offset的资源,发现和我们的理论推测结果一致,请参考:




在测试过程中发现,有些老版本的AssetStudio,在解析带offest的资源的时候甚至会直接奔溃。其实,对于资源加密,我们大多数时候能做到的是防小白不防专家,不管你是采用简单的或者复杂的,在反编译高手手里都有点苍白,我亲眼所见一个大佬用IDA把人家的通信加密算法反出来了,所以这里就不做更深入的分析了。
文末,再次感谢江鱼的分享,如果您有任何独到的见解或者发现也欢迎联系我们,一起探讨。(QQ群:793972859)
也欢迎大家来积极参与U Sparkle开发者计划,简称“US”,代表你和我,代表UWA和开发者在一起!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-23 00:13 , Processed in 0.086333 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表