吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 9087|回复: 13
收起左侧

[Android 原创] 保护Android resources文件不被反编译原理分析

[复制链接]
pwelyn 发表于 2016-2-1 21:22
保护Android resources文件不被反编译原理分析
0x00前言
本人新手,文章写的如果有不正确的地方烦请各路大神指点  
0x01工具
010 Editor
apktool
aapt
文本编辑器

0x02原理
主要是通过HEX修改resources.arsc DataType数据类型来促使apktool无法直接反编译资源文件,从而保护资源文件。
首先我们需要了解一下appt dump resources的基本格式。
resource<Resource ID> <Package Name>:<Type>/<Name>: t=<DataType> d=<Data> (s=<Size> r=<Res0>)
Resource ID R.java中的资源ID
Package Name 资源所在的的包
Type 资源的类型
Name 资源名称
DataType 数据类型,按照以下枚举类型取值
Data 资源的值,根据dataType进行解释
Size 一直为0x0008
Res0 固定为0x00

通过aapt dump查看apk的资源
[Bash shell] 纯文本查看 复制代码
aapt d --values resources test.apk


[AppleScript] 纯文本查看 复制代码
Package Groups (1)
Package Group 0 id=0x7f packageCount=1 name=com.example.myapp
  Package 0 id=0x7f name=com.example.myapp
    type 1 configCount=4 entryCount=1
      spec resource 0x7f020000 com.example.myapp:drawable/ic_launcher: flags=0x40000100
      config ldpi-v4:
        resource 0x7f020000 com.example.myapp:drawable/ic_launcher: t=0x03 d=0x00000001 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/drawable-ldpi-v4/ic_launcher.png"
      config mdpi-v4:
        resource 0x7f020000 com.example.myapp:drawable/ic_launcher: t=0x03 d=0x00000002 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/drawable-mdpi-v4/ic_launcher.png"
      config hdpi-v4:
        resource 0x7f020000 com.example.myapp:drawable/ic_launcher: t=0x03 d=0x00000003 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/drawable-hdpi-v4/ic_launcher.png"
      config xhdpi-v4:
        resource 0x7f020000 com.example.myapp:drawable/ic_launcher: t=0x03 d=0x00000004 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/drawable-xhdpi-v4/ic_launcher.png"
    type 2 configCount=1 entryCount=1
      spec resource 0x7f030000 com.example.myapp:layout/main: flags=0x40000000
      config (default):
        resource 0x7f030000 com.example.myapp:layout/main: t=0x03 d=0x00000000 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/layout/main.xml"
    type 3 configCount=1 entryCount=2
      spec resource 0x7f040000 com.example.myapp:string/app_name: flags=0x40000000
      spec resource 0x7f040001 com.example.myapp:string/hex_test: flags=0x40000000
      config (default):
        resource 0x7f040000 com.example.myapp:string/app_name: t=0x03 d=0x00000005 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "myapp"
        resource 0x7f040001 com.example.myapp:string/hex_test: t=0x03 d=0x00000006 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "hex"


string/hex_test这个值在APP里面不起任何作用,专门用于修改保护资源使用,string值不要使用APP内部调用的资源,如果用正常使用的值修改后会无法正常使用
resource 0x7f040001 即 string/hex_test 的ID t=0x03 DataType数据类型 d=0x00000006 Data 资源值 s=0x0008 r=0x00 Size和Res0
下面就使用010Editor修改t=0x03 0x03修改为0x02或0x01都可以,我修改0x02
用010Editor打开resources.arsc
直接搜索0306000000(为什么这样,因为十六进制搜索需要反过来)
1.png
DataType数据类型
直接修改03为02保存,直接替换resources.arsc到apk中

0x03演示
使用apktool反编译进行测试
[Bash shell] 纯文本查看 复制代码
apktool d -f test.apk 

直接报错信息如下:
[JavaScript] 纯文本查看 复制代码
I: Using Apktool 2.0.3 on test.apk
I: Loading resource table...
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: /Users/pwelyn/Library/apktool/framework/1.apk
I: Regular manifest package...
I: Decoding file-resources...
I: Decoding values */* XMLs...
Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x7f000006
    at brut.androlib.res.data.ResPackage.getResSpec(ResPackage.java:59)
    at brut.androlib.res.data.ResTable.getResSpec(ResTable.java:65)
    at brut.androlib.res.data.ResTable.getResSpec(ResTable.java:61)
    at brut.androlib.res.data.value.ResReferenceValue.getReferent(ResReferenceValue.java:57)
    at brut.androlib.res.data.value.ResReferenceValue.encodeAsResXml(ResReferenceValue.java:47)
    at brut.androlib.res.data.value.ResScalarValue.encodeAsResXmlValue(ResScalarValue.java:58)
    at brut.androlib.res.data.value.ResScalarValue.serializeToResValuesXml(ResScalarValue.java:75)
    at brut.androlib.res.AndrolibResources.generateValuesFile(AndrolibResources.java:502)
    at brut.androlib.res.AndrolibResources.decode(AndrolibResources.java:252)
    at brut.androlib.Androlib.decodeResourcesFull(Androlib.java:136)
    at brut.androlib.ApkDecoder.decode(ApkDecoder.java:102)
    at brut.apktool.Main.cmdDecode(Main.java:165)
    at brut.apktool.Main.main(Main.java:81)


我使用的是最新版本apktool2.0.3版本,关键错误信息:
[AppleScript] 纯文本查看 复制代码
Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x7f000006


0x04修复
手动修复这个错误,也就是根据上面的原理进行反推,看一下错误信息:
[AppleScript] 纯文本查看 复制代码
Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x7f000006 

首先使用aapt dump查看apk的资源
[Bash shell] 纯文本查看 复制代码
aapt d --values resources test.apk 

资源:
[JavaScript] 纯文本查看 复制代码
Package Groups (1)
Package Group 0 id=0x7f packageCount=1 name=com.example.myapp
  Package 0 id=0x7f name=com.example.myapp
    type 1 configCount=4 entryCount=1
      spec resource 0x7f020000 com.example.myapp:drawable/ic_launcher: flags=0x40000100
      config ldpi-v4:
        resource 0x7f020000 com.example.myapp:drawable/ic_launcher: t=0x03 d=0x00000001 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/drawable-ldpi-v4/ic_launcher.png"
      config mdpi-v4:
        resource 0x7f020000 com.example.myapp:drawable/ic_launcher: t=0x03 d=0x00000002 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/drawable-mdpi-v4/ic_launcher.png"
      config hdpi-v4:
        resource 0x7f020000 com.example.myapp:drawable/ic_launcher: t=0x03 d=0x00000003 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/drawable-hdpi-v4/ic_launcher.png"
      config xhdpi-v4:
        resource 0x7f020000 com.example.myapp:drawable/ic_launcher: t=0x03 d=0x00000004 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/drawable-xhdpi-v4/ic_launcher.png"
    type 2 configCount=1 entryCount=1
      spec resource 0x7f030000 com.example.myapp:layout/main: flags=0x40000000
      config (default):
        resource 0x7f030000 com.example.myapp:layout/main: t=0x03 d=0x00000000 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "res/layout/main.xml"
    type 3 configCount=1 entryCount=2
      spec resource 0x7f040000 com.example.myapp:string/app_name: flags=0x40000000
      spec resource 0x7f040001 com.example.myapp:string/hex_test: flags=0x40000000
      config (default):
        resource 0x7f040000 com.example.myapp:string/app_name: t=0x03 d=0x00000005 (s=0x0008 r=0x00) (PUBLIC)
          (string8) "myapp"
        resource 0x7f040001 com.example.myapp:string/hex_test: t=0x02 d=0x00000006 (s=0x0008 r=0x00) (PUBLIC)
          (attribute) 0x00000006


关键信息:(attribute) 0x00000006 报错 resource spec: 0x7f000006
使用010Editor打开resources.arsc直接搜索06000000
2.jpg
图片描述
直接将DataType数据类型0x02修改为0x03 修改后保存,直接替换resources.arsc到apk中进行反编译测试
[Bash shell] 纯文本查看 复制代码
apktool d -f test.apk

测试通过:
[Bash shell] 纯文本查看 复制代码
I: Using Apktool 2.0.3 on test.apk
I: Loading resource table...
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: /Users/pwelyn/Library/apktool/framework/1.apk
I: Regular manifest package...
I: Decoding file-resources...
I: Decoding values */* XMLs...
I: Baksmaling classes.dex...
I: Copying assets and libs...
I: Copying unknown files...
I: Copying original files...


0x05结尾
本文只是和各位交流,谢谢。



免费评分

参与人数 6威望 +1 吾爱币 +1 热心值 +6 收起 理由
zhiyi1120 + 1 + 1 谢谢@Thanks!
独行风云 + 1 感谢楼主分享教程
dych1688 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩.
qtfreet00 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩.
茗清逸 + 1 热心回复!
william2568 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
hellokits 发表于 2016-2-1 21:55
提示: 作者被禁止或删除 内容自动屏蔽
caleb110 发表于 2016-2-2 12:08
gh2923 发表于 2016-2-2 22:59
与你相约 发表于 2016-2-3 19:42 来自手机
☆═━┈支持┈━═☆
uzbek_hit 发表于 2016-2-18 16:18
啊五爱,你比四爱多一爱,你比六爱少一爱
solea 发表于 2016-4-2 15:29 来自手机
长见识了。
Wa6600 发表于 2016-4-2 23:08
受教了。。。。。感谢。。
Lesita 发表于 2016-4-3 22:14
谢谢,这种非常有用,收藏。
貌似速递易快递机的 app 好像与这种方法类似
头像被屏蔽
刷机男孩 发表于 2016-12-29 11:14
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-5 03:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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