简体   繁体   English

检查Android资产完整性

[英]Check Android Assets Integrity

In my folder assets/data , there are a lot of XML files containing static data for my app. 在我的文件夹assets/data ,有很多XML文件包含我的应用程序的静态数据。

It's really easy for someone to retrieve an APK, modify a part of it and install on a device. 有人可以很容易地检索APK,修改它的一部分并安装在设备上。

I would like to prevent users to alter my static data by checking the integrity of my assets/data folder. 我想通过检查assets/data文件夹的完整性来阻止用户更改我的静态数据。

Initially I was considering to use MD5 checksum , but it will probably be too slow for the amount of files I gonna have (50-100). 最初我考虑使用MD5校验和 ,但对于我将拥有的文件数量(50-100)可能太慢了。

Do you have any suggestion? 你有什么建议吗?

Edit: 编辑:

This app is a game with an XML file describing each level. 这个应用程序是一个带有描述每个级别的XML文件的游戏。

I'll describe how you can effectively protect against modification and repackaging, not how you can protect the assets on their own, although you could ultimately apply the same technique to encrypting them. 我将描述如何有效地防止修改和重新打包,而不是如何自己保护资产,尽管最终可以应用相同的技术来加密它们。 It's imperfect, but you can make modification significantly more difficult. 它不完美,但你可以使修改变得更加困难。

You sign the application with a certificate. 您使用证书对应用程序进行签名。 Although they can remove yours, noone else can produce the same certificate when putting it back together. 虽然他们可以删除你的,但没有其他人可以在重新组合时生成相同的证书。 You can therefore check the signature of the application at runtime, to make sure it's what you expect. 因此,您可以在运行时检查应用程序的签名,以确保它符合您的期望。

Here's some cheap and nasty code to do this: 这是一些廉价而讨厌的代码:

   PackageManager pm = context.getPackageManager();

   PackageInfo info = pm.getPackageInfo( context.getPackageName(), PackageManager.GET_SIGNATURES );

   if ( info.signatures[ 0 ].toCharsString().equals( YOUR_SIGNATURE ) )
   {
     //signature is OK
   }

where YOUR_SIGNATURE is a constant, obtained from running this code on the signed app. 其中YOUR_SIGNATURE是一个常量,通过在签名的应用程序上运行此代码获得。

Now, there are two remaining problems that you have already hinted at: 现在,您已经暗示过两个问题:

  1. how can you stop someone just modifying the constant in the source code to match their certificate, then repackaging and re-signing the app? 如何阻止某人修改源代码中的常量以匹配他们的证书,然后重新打包并重新签名应用程序?

  2. how can you stop someone finding the check method and removing it? 你怎么能阻止某人找到检查方法并将其删除?

Answer to both: you can't, not absolutely, but you can do a pretty good job through obfuscation. 回答两者:你不能,不是绝对,但你可以通过混淆做得很好。 The free Proguard, but more usefully the commercial Dexguard, are tools for doing this. 免费的Proguard,但更有用的商业Dexguard,是这样做的工具。 You may baulk at the current €350 cost of the latter; 你可以考虑当前的350欧元成本; on the other hand, I have tried to reverse engineer apps that are protected like this, and unless the stakes were very high, it isn't worth the trouble. 另一方面,我试图对这样受保护的应用程序进行逆向工程,除非赌注非常高,否则不值得麻烦。

To an extent, you could also do the obfuscation for (1) yourself; 在某种程度上,你也可以自己进行混淆(1); have the signature 'constant' assembled at runtime through some complicated programmatic method that makes it difficult to find and replace. 通过一些复杂的编程方法在运行时组装签名“常量”,这使得难以找到和替换。

(2) is really a software design issue; (2)真的是一个软件设计问题; making it sufficiently complicated or annoying to remove the check. 使检查变得足够复杂或烦人。 Obfuscation just makes it more difficult to find in the first place. 混淆只会使首先找到它更加困难。

As a further note, you might want to look at whether stuff like Google Licensing gives you any protection in this area. 另外请注意,您可能希望了解Google Licensing等内容是否为您提供此领域的任何保护。 I don't have any experience of it though, so you're on your own there. 我没有任何经验,所以你在那里自己。

Sort of an answer although it is in the negative. 虽然它是否定的,但是答案的排序。

If the person has your apk and has decoded it, then even if you used a checksum, they can just update the code portion with the new checksum. 如果此人拥有您的apk并已对其进行解码,那么即使您使用了校验和,他们也可以使用新的校验和更新代码部分。 I don't think you can win this one. 我认为你不能赢得这个。 You can put a great deal of effort into protecting it but if you assume somebody can obtain and modify the apk, then they can also undo the protection. 您可以花费大量精力来保护它,但如果您假设某人可以获取并修改apk,那么他们也可以撤消保护。 On my commercial stuff, I just try to make the decoding non-obvious but not bullet proof. 在我的商业广告中,我只是尝试使解码不明显,但不是防弹。 I know anything more is not worth the effort or even possible. 我知道更多的东西不值得努力,甚至不可能。

Perhaps you could zip up the xml files and put it in the assets/data folder; 也许您可以压缩xml文件并将其放在assets / data文件夹中; and then do a checksum on that .zip. 然后对.zip进行校验和。 On the first run, you could unzip the files to get the .xml layouts. 在第一次运行时,您可以解压缩文件以获取.xml布局。 See Unzip file from zip archive of multiple files using ZipFile class for unzipping an archive. 请参阅使用ZipFile类从多个文件的zip存档解压缩文件以解压缩存档。

Probably the most reliable way would be for the level XML data to be downloaded from a server when the app is started with a check of the time stamp and sizes of the level files. 可能最可靠的方法是在启动应用程序时通过检查级别文件的时间戳和大小来从服务器下载级别XML数据。 That also lets you provide updates to level data over time. 这也允许您随时间提供级别数据的更新。 Of course this means you have the added expense of a server to host which may be another problem. 当然,这意味着您需要增加服务器的主机费用,这可能是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM