<span id="Label3"></p> <blockquote> <blockquote> <p>@author ASCE1885 's Github book Weibo CSDN<br>Original link<br>It is often necessary to encrypt sensitive information in the development of android, so how to prevent the key from being reversed when the key is stored on the terminal device. This is a first chicken or the first egg paradox. Compared to writing the key in the Java layer, moving it down to the NDK layer is a better choice, This article describes how to confuse the NDK layer code to better protect our keys.</p> </blockquote> </blockquote><p><p>Confusion is a technique used to hide the intent of the program, the specific implementation technology may be different, the most effective technology can increase the difficulty of reverse engineering and cracking, to prevent intellectual property theft. There are already a lot of third-party software that can be used to confuse our Android apps, which are common:</p></p> <ul> <ul> <li>Proguard</li> <li>Dasho</li> <li>Dexguard</li> <li>Dexprotector</li> <li>Apkprotect</li> <li>Shield4j</li> <li>Stringer</li> <li>Allitori</li> </ul> </ul><p><p>These are different levels of function in the Code. The approximate process for Android compilation is as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs coffeescript">Java Code<span class="hljs-function"><span class="hljs-params">(.java)</span> -></span> Java Bytecode<span class="hljs-function"><span class="hljs-params">(.class)</span> -></span> Dalvik Bytecode(classes.dex)</code></pre></pre><p><p>Some of the obfuscation is directly before compiling the Java source code, and some of the Java bytecode, and some of the Dalvik byte Code.</p></p><p><p>The Android NDK allows developers to bypass virtual machines to further improve program performance, or to interact more directly with the kernel and hardware. Google's description of the NDK is that "the NDK is a set of toolsets that allow developers to develop apps using the native C + + language." This is beneficial for some types of apps to reuse the existing code base written by C + +, Although most apps do not need to use the Android NDK.</p></p><p><p>Compared to the confusion of Dalvik virtual machine hierarchy, There are not many code obfuscation options for native language (c + +) components, and OBFUSCATOR-LLVM Engineering is a notable Exception. This project focuses on the LLVM compiler, which makes it highly portable, compatible with all languages supported by LLVM (c,c++, objective-c, Ada and Fortran) and platforms (x86, x86-64, PowerPC, PowerPC-64, ARM, Thumb, SPARC, Alpha, cellspu, MIPS, MSP430, systemz,and XCore). 0vercl0k published a paper before o-llvm, explaining the advantages of using the LLVM compiler and the simple code Conversion.</p></p><p><p>I've been using the O-LLVM and NDK for a while. When I learned that Towelroot was also using o-llvm, I decided to write an article about it. Towelroot is an Android One-click root tool, which can be found in this article about how it uses Linux kernel bugs for root purposes. Towelroot uses O-LLVM primarily to prevent others from copying and using it for illegal purposes, while preventing it from being re-packaged and SOLD.</p></p><p><p>Let's explain how to get started using O-LLVM to confuse native code and achieve similar towelroot purposes.</p></p>Using the NDK O-LLVM binary overlay Package<p><p>I have packaged the obfuscation on the OSX and Linux platforms as binary overlay packages, and you can also compile from the source code by following the steps in the last section of this Article. Obfuscation binary Overlay package:</p></p> <ul> <ul> <li>osx-ndk-obfuscator.tar.bz2</li> <li>Android-ndk64-r10-linux-x86_64-obfuscator.tar</li> </ul> </ul><p><p>Download the correct binary overlay package and unzip it to your Computer's ndk directory.</p></p>Configuring the O-LLVM NDK Project<p><p>Now let's configure the NDK project to support the O-LLVM Obfuscation. Our engineering directory structure is as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">? AndroidObfuscation-NDK git:(master) tree ..├── jni│ ├── Android<span class="hljs-preprocessor">.mk</span>│ ├── Application<span class="hljs-preprocessor">.mk</span>│ └── obfuscationTest<span class="hljs-preprocessor">.c</span></code></pre></pre><p><p>The application.mk contents of the project are as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs ruby"><span class="hljs-constant">LOCAL_PATH</span><span class="hljs-symbol">:</span><span class="hljs-variable">$(</span>call my-dir)<span class="hljs-keyword">include</span><span class="hljs-variable">$(</span><span class="hljs-constant">CLEAR_VARS</span>)<span class="hljs-constant">APP_ABI</span><span class="hljs-symbol">:</span>= armeabi<span class="hljs-constant">NDK_TOOLCHAIN_VERSION</span><span class="hljs-symbol">:</span>= clang3.<span class="hljs-number">4</span>-obfuscator<span class="hljs-keyword">include</span><span class="hljs-variable">$(</span><span class="hljs-constant">BUILD_EXECUTABLE</span>)</code></pre></pre><p><p>The various code conversions for the obfuscation can be found in the Obfuscator Wiki. These conversion tokens can be set to the obfuscation via the Local_cflags tag. Remember that the conversion token of the obfuscation needs to start WITH-MLLVM so that the clang compiler can pass it On.</p></p><p><p>The configuration examples for android.mk are as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-subst">:=</span> $(call my<span class="hljs-attribute">-dir</span>)include $(CLEAR_VARS)LOCAL_MODULE <span class="hljs-subst">:=</span><span class="hljs-subst">:=</span> obfuscationTest<span class="hljs-built_in">.</span><span class="hljs-subst">:=</span><span class="hljs-attribute">-static</span><span class="hljs-subst">:=</span><span class="hljs-attribute">-mllvm</span><span class="hljs-attribute">-sub</span><span class="hljs-attribute">-mllvm</span><span class="hljs-attribute">-fla</span><span class="hljs-attribute">-mllvm</span><span class="hljs-attribute">-bcf</span>include $(BUILD_EXECUTABLE)</code></pre></pre><p><p>Now it's time to compile our project:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs mathematica">? <span class="hljs-keyword">Compile</span> thumb : obfuscated <= obfuscationTest.c[armeabi] Executable <span class="hljs-keyword">Install</span> : obfuscated => libs/armeabi/obfuscated</code></pre></pre><p><p>An example project using the above configuration and script structure can be found in Androidobfuscation-ndk.</p></p><p><p>The binary overlay Package I pre-built above contains the experimental string obfuscation techniques contributed by yag00. You can make string obfuscation by passing "-mllvm-xse" tokens to Local_cflags.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cpp">? AndroidObfuscation-NDK git:(master) cat jni/obfuscationTest.c<span class="hljs-preprocessor">#include <stdio.h></span><span class="hljs-keyword">int</span> main(<span class="hljs-keyword">void</span>){ <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello, world\n"</span>); <span class="hljs-keyword">return</span><span class="hljs-number">0</span>;}</code></pre></pre><p><p>In this example, the effect is as follows before using the string obfuscation feature:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs 1c">? <span class="hljs-string">| grep Hello</span>Hello, world</code></pre></pre><p><p>After using the string obfuscation feature:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs 1c">? <span class="hljs-string">| grep Hello</span></code></pre></pre>Build O-LLVM for NDK from source code<pre class="prettyprint"><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-attribute">-b</span> llvm<span class="hljs-subst">-</span><span class="hljs-number">3.4</span> https:<span class="hljs-comment">//github.com/obfuscator-llvm/obfuscator.git</span><span class="hljs-attribute">-DCMAKE_BUILD_TYPE</span>:<span class="hljs-built_in">String</span><span class="hljs-subst">=</span><span class="hljs-built_in">..</span>/obfuscator<span class="hljs-subst">/</span><span class="hljs-attribute">-j5</span></code></pre></pre><p><p>The complete guide to building O-LLVM is here, but the above explanation should suffice.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-attribute">-r</span><span class="hljs-variable">$NDK_PATH</span>/toolchains/arm<span class="hljs-attribute">-linux</span><span class="hljs-attribute">-androideabi</span><span class="hljs-attribute">-clang3</span><span class="hljs-number">.4</span><span class="hljs-variable">$NDK_PATH</span>/toolchains/arm<span class="hljs-attribute">-linux</span><span class="hljs-attribute">-androideabi</span><span class="hljs-attribute">-clang3</span><span class="hljs-number">.4</span><span class="hljs-attribute">-obfuscator</span></code></pre></pre><p><p>Open File</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-variable">$NDK_PATH</span>/toolchains/arm<span class="hljs-attribute">-linux</span><span class="hljs-attribute">-androideabi</span><span class="hljs-attribute">-clang3</span><span class="hljs-number">.4</span><span class="hljs-attribute">-obfuscator</span>/setup<span class="hljs-built_in">.</span>mk</code></pre></pre><p><p>Put the file inside:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs makefile"><span class="hljs-constant">TARGET_CC</span><span class="hljs-variable">$(LLVM_TOOLCHAIN_PREFIX)</span>clang<span class="hljs-variable">$(HOST_EXEEXT)</span><span class="hljs-constant">TARGET_CXX</span><span class="hljs-variable">$(LLVM_TOOLCHAIN_PREFIX)</span>clang++<span class="hljs-variable">$(HOST_EXEEXT)</span></code></pre></pre><p><p>Change to (remember to modify O-LLVM for your own computer above the Path)</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs makefile"><span class="hljs-constant">LLVM_TOOLCHAIN_PATH</span> := <PATH_TO_OBFUSCATOR_REPO>/build/bin/<span class="hljs-constant">TARGET_CC</span><span class="hljs-variable">$(LLVM_TOOLCHAIN_PATH)</span>clang<span class="hljs-variable">$(HOST_EXEEXT)</span><span class="hljs-constant">TARGET_CXX</span><span class="hljs-variable">$(LLVM_TOOLCHAIN_PATH)</span>clang++<span class="hljs-variable">$(HOST_EXEEXT)</span></code></pre></pre> <blockquote> <blockquote> <p>Appreciation of photography at the end of Wen</p> </blockquote> </blockquote><p><p></p></p> <p><p>Use the O-LLVM and NDK to confuse Android apps</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service