简体   繁体   English

防止android项目加载本机库

[英]Prevent android project to load native library

Reverse engineering is a great problem for android apk. 逆向工程对于android apk来说是个大问题。 Anyone can easily generate code from apk file using dex2jar and jd. 任何人都可以使用dex2jar和jd轻松地从apk文件生成代码。 Proguard can gives little bit security by changing the variable/method name. 通过更改变量/方法名称,Proguard可以提供一点点安全性。 Problem is .so file. 问题是.so文件。 As an example, i have a so file which has a encrypt and decrypt method in it. 例如,我有一个so文件,其中包含加密和解密方法。 If someone get my apk then he can easily get the so file and by creating a new project with same package name (not hidden in native loading class) he can use the functions without any problem. 如果有人获得了我的apk,那么他可以轻松获得so文件,并通过使用相同的包名称(未隐藏在本机加载类中)创建一个新项目,他可以毫无问题地使用这些函数。

Is there any solution to prevent this?? 有什么解决方案可以防止这种情况吗? Is there any way so that i can identify my project from native code and reject the request if it comes from invalid project? 有什么办法可以使我从本机代码中识别我的项目,如果请求来自无效项目,则拒绝该请求? Eclipse sign the apk with a key-store and password. Eclipse使用密钥库和密码对apk进行签名。 Can i check the sign information from native code?? 我可以从本机代码中查看标志信息吗?

My 2 cents are that Proguard is just a basic obfuscator(till a point). 我的2美分是, Proguard只是一个基本的混淆器(一点)。 There are many other services/tools(which might not be completely free) but do good obfuscation, app integrity protection, Tamper detection and Tamper defence etc. Check out Arxan and DashO Pro . 还有许多其他服务/工具(可能不是完全免费的),但是它们具有很好的混淆,应用程序完整性保护,篡改检测和篡改防御等功能。请查看ArxanDashO Pro You might try to use them for more protection. 您可以尝试使用它们提供更多保护。 They even work with rooted devices. 他们甚至可以与植根设备一起使用。 Hope this helps in someway. 希望这会有所帮助。

The problem 问题

This is a classic problem with any intermediary bytecode language: JVM languages (Java, Groovy, Akka, etc.), CLR languages (C#, ASP.NET, etc.) and more. 这是任何中间字节码语言的经典问题:JVM语言(Java,Groovy,Akka等),CLR语言(C#,ASP.NET等)等。 Since the procedures to compile the source code to its intermediary bytecode are well-documented and fairly straight-forward, it isn't all that difficult to reverse engineer the bytecode back into source. 由于将源代码编译为其中间字节码的过程已得到了充分的文档证明,并且相当简单,因此将字节码反向工程回源代码并不难。

As @Shobhit Puri has already pointed out, a good obfuscator will help deter most would-be attackers from expending the energy to decompile your bytecode. 正如@Shobhit Puri已经指出的那样,一个好的混淆器将帮助阻止大多数可能的攻击者花费精力来反编译您的字节码。 But any determined attacker with enough skills will be able to reverse engineer any obfuscated code (please note that there are many high quality Java de-obfuscators out there...) you can generate. 但是,任何具有足够技能的坚定的攻击者都可以对任何混淆的代码进行逆向工程(请注意,那里有许多高质量的Java反混淆器 ...)。

So how does one mitigate this? 那么如何减轻这种情况呢?

Your best bet is to store any sensititve resources or business logic on the server, and have the client (your Android app) access those resources or invoke that business logic in a defensive way . 最好的选择是在服务器上存储任何敏感资源或业务逻辑, 并让客户端(您的Android应用)访问这些资源或以防御方式调用该业务逻辑 By "defensive", I mean you code the client in such a way that any attacker who successfully obtained your full source code wouldn't be able to ascertain anything meaningful with it. 所谓“防御性”,是指您以一种方式对客户端进行编码,以使成功获得完整源代码的任何攻击者都无法使用它来确定任何有意义的东西。

For instance, instead of: 例如,代替:

// Get password from user input:
String password = getUserInput();

// Make sure password is correct.
if(password.equals("12345") {
    // Grant access to the system
} else {
    throw new SecurityException("Blah whatever");
}

You have this: 你有这个:

// Get password from user input:
String password = getUserInput();

// Make sure password is correct.
String correctPassword = passwordService.fetchFromServer();
if(password.equals(correctPassword) {
    // Grant access to the system
} else {
    throw new SecurityException("Blah whatever");
}

If an attacker were to get access to your source code, and you provide access to the password somewhere on the client-side (even if its encrypted), eventually they'll figure out how to break in. But if you fetch the magical password from the server, and never reference its value anywhere on the client, then they now need to break into your server to get access to it. 如果攻击者可以访问您的源代码,并且您在客户端的某个位置提供了密码访问权限(即使已加密),则最终他们会弄清楚如何破解。但是,如果您获取了神奇的密码从服务器上访问,并且永远不要在客户端上的任何位置引用其值,那么他们现在需要进入您的服务器才能访问它。

Then all you have to do is properly secure your app server, and for that you have excellent prospects like: 然后,您所要做的就是正确保护应用服务器的安全,为此,您具有良好的前景:

Note that in my code example above, ultimately there is no perfect defense against a skilled-enough attacker. 请注意,在我上面的代码示例中,最终没有针对熟练的攻击者的完美防御。 For instance, a skilled attacker could sniff the network traffic between the client and the server during the time that the passwordService#fetchFromServer() call was made. 例如, passwordService#fetchFromServer()调用期间,熟练的攻击者可能会嗅探客户端与服务器之间的网络流量。 If the password is returned as plaintext, voila - they have it. 如果密码以明文形式返回,瞧,他们知道了。 Even if the password is returned using SSL or some other strong encryption, your client will need to store or access the private key for that encryption somewhere locally. 即使使用SSL或其他某种高度加密的密码返回了密码,您的客户端也需要在本地某处存储或访问该加密的私钥。 Once they have the private key, they'll be able to access the password. 拥有私钥后,他们将能够访问密码。 All this to say: 这一切都说:

There is no such thing as perfect security. 没有完美的安全性之类的东西。 There is no such thing as an unhackable system. 没有不可破解的系统。 The purpose of your security should be to make the cost of obtaining the resource far greater than the value of the resource itself. 安全性的目的应是使获取资源的成本远远大于资源本身的价值。

Bottom Line 底线

As a smart developer, you need to assume that the attackers will exploit your client application every which way. 作为一个聪明的开发人员,您需要假设攻击者将以各种方式利用您的客户端应用程序。 You need to assume that they will eventually gain access to your client's source code, and even understand how it communicates with your server. 您需要假设他们最终将获得对客户端源代码的访问权,甚至需要了解它们如何与服务器通信。 And on the server-side, you need to assume that every request coming from a client is an attack, and treat it as such. 在服务器端,您需要假设来自客户端的每个请求都是攻击,因此将其视为攻击。 This will put you in the right state of mind to code defensively. 这将使您处于正确的防御状态,可以进行防御性编码。

What you need to beef up on is server-side security, and that is not trivial by any means. 您需要增强的是服务器端安全性,无论如何这都不是小事。 But if you just remember to keep all sensitive code and data on the server, and access it defensively on the client-side, you'll do more to protect your app than any obfuscator can offer you. 但是,如果您只记得将所有敏感的代码和数据保存在服务器上,并在客户端进行防御性访问,那么您将在保护应用程序方面做得比任何混淆器所能提供的更多。 HTH and happy coding. HTH和快乐的编码。

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

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