首页
社区
课程
招聘
5
[原创]APK自我保护方法
发表于: 2013-12-28 21:41 110180

[原创]APK自我保护方法

2013-12-28 21:41
110180
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
1. struct DexHeader {
 
2. u1 magic[8]; /* includes version number */
 
3. u4 checksum; /* adler32 checksum */
 
4. u1 signature[kSHA1DigestLen]; /* SHA-1 hash */
 
5. u4 fileSize; /* length of entire file */
 
6. u4 headerSize; /* offset to start of next section */
 
7. u4 endianTag;
 
8. u4 linkSize;
 
9. u4 linkOff;
 
10. u4 mapOff;
 
11. u4 stringIdsSize;
 
12. u4 stringIdsOff;
 
13. u4 typeIdsSize;
 
14. u4 typeIdsOff;
 
15. u4 protoIdsSize;
 
16. u4 protoIdsOff;
 
17. u4 fieldIdsSize;
 
18. u4 fieldIdsOff;
 
19. u4 methodIdsSize;
 
20. u4 methodIdsOff;
 
21. u4 classDefsSize;
 
22. u4 classDefsOff;
 
23. u4 dataSize;
 
24. u4 dataOff;
 
25. };
1
2
3
4
5
1. struct DexStringId {
  
2. u4 stringDataOff; /* file offset to string_data_item */
 
3. };
1
2
3
4
5
1. struct DexTypeId {
 
2. u4 descriptorIdx; /* index into stringIds list for type descriptor */
 
3. };
1
2
3
4
5
6
7
8
9
1. struct DexProtoId {
  
2. u2 classIdx; /* index into typeIds list for defining class */
 
3. u2 typeIdx; /* index into typeIds for field type */
 
4. u4 nameIdx; /* index into stringIds for field name */
 
5. };
1
2
3
4
5
6
7
8
9
1. struct DexFieldId {
  
2. u2 classIdx; /* index into typeIds list for defining class */
 
3. u2 typeIdx; /* index into typeIds for field type */
 
4. u4 nameIdx; /* index into stringIds for field name */
 
5. };
1
2
3
4
5
6
7
8
9
1. struct DexMethodId {
 
2. u2 classIdx; /* index into typeIds list for defining class */
 
3. u2 protoIdx; /* index into protoIds for method prototype */
 
4. u4 nameIdx; /* index into stringIds for method name */
 
5. };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1. struct DexClassDef {
 
2. u4 classIdx; /* index into typeIds for this class */
 
3. u4 accessFlags;
 
4. u4 superclassIdx; /* index into typeIds for superclass */
 
5. u4 interfacesOff; /* file offset to DexTypeList */
 
6. u4 sourceFileIdx; /* index into stringIds for source file name */
 
7. u4 annotationsOff; /* file offset to annotations_directory_item */
 
8. u4 classDataOff; /* file offset to class_data_item */
 
9. u4 staticValuesOff; /* file offset to DexEncodedArray */
 
10. };
1
2
3
4
5
6
7
8
9
10
11
12
13
1. struct DexClassData {
 
2. DexClassDataHeader header;
 
3. DexField* staticFields;
 
4. DexField* instanceFields;
 
5. DexMethod* directMethods;
 
6. DexMethod* virtualMethods;
 
7. };
1
2
3
4
5
6
7
1. struct DexField {
 
2. u4 fieldIdx; /* index to a field_id_item */
 
3. u4 accessFlags;
 
4. };
1
2
3
4
5
6
7
8
9
1. struct DexMethod {
 
2. u4 methodIdx; /* index to a method_id_item */
 
3. u4 accessFlags;
 
4. u4 codeOff; /* file offset to a code_item */
 
5. };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1. struct DexCode {
 
2. u2 registersSize;
 
3. u2 insSize;
 
4. u2 outsSize;
 
5. u2 triesSize;
 
6. u4 debugInfoOff; /* file offset to debug info stream */
 
7. u4 insnsSize; /* size of the insns array, in u2 units */
 
8. u2 insns[1];
 
9. /* followed by optional u2 padding */
 
10. /* followed by try_item[triesSize] */
 
11. /* followed by uleb128 handlersSize */
 
12. /* followed by catch_handler_item[handlersSize] */
 
13. };
1
2
3
4
5
6
7
1. String apkPath = this.getPackageCodePath();
 
2. ZipFile apkfile = new ZipFile(apkPath);
 
3. ZipEntry dexentry = zipfile.getEntry("classes.dex");
 
4. InputStream dexstream = zipfile.getInputStream(dexentry);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1. String apkPath = this.getPackageCodePath();
 
2. Long dexCrc = Long.parseLong(this.getString(R.string.dex_crc));
 
3. try {
 
4. ZipFile zipfile = new ZipFile(apkPath);
 
5. ZipEntry dexentry = zipfile.getEntry("classes.dex");
 
6. if(dexentry.getCrc() != dexCrc){
 
7. System.out.println("Dex has been *modified!");
 
8. }else{
 
9. System.out.println("Dex hasn't been modified!");
 
10. }
 
11. } catch (IOException e) {
 
12. // TODO Auto-generated catch block
 
13. e.printStackTrace();
 
14. }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1. MessageDigest msgDigest = null;
 
2. try {
 
3. msgDigest = MessageDigest.getInstance("MD5")
 
4. byte[] bytes = new byte[8192];
 
5. int byteCount;
 
6. FileInputStream fis = null;
 
7. fis = new FileInputStream(new File(apkPath));
 
8. while ((byteCount = fis.read(bytes)) > 0)
 
9. msgDigest.update(bytes, 0, byteCount);
 
10. BigInteger bi = new BigInteger(1, msgDigest.digest());
 
11. String md5 = bi.toString(16);
 
12. fis.close();
 
13. /*
 
14. 从服务器获取存储的 Hash 值,并进行比较
 
15. */
     
16. } catch (Exception e) {
 
17. e.printStackTrace();
 
18. }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1. public class Reflection {
 
2. public void methodA(){
 
3. System.out.println("Invoke methodA");
 
4. }
 
5. public void methodB(){
 
6. System.out.println("Invoke methodB");
 
7. }
 
8. }

[注意]看雪招聘,专注安全领域的专业人才平台!

上传的附件:
收藏
免费 5
支持
分享
赞赏记录
参与人
雪币
留言
时间
心游尘世外
为你点赞~
2024-5-31 07:20
QinBeast
为你点赞~
2024-5-31 07:10
飘零丶
为你点赞~
2024-5-31 01:43
shinratensei
为你点赞~
2024-5-31 01:28
PLEBFE
为你点赞~
2023-3-5 04:15
最新回复 (81)
雪    币: 33
活跃值: (145)
能力值: ( LV7,RANK:100 )
在线值:
发帖
回帖
粉丝
2
great!
2013-12-28 21:58
0
雪    币: 7020
活跃值: (5014)
能力值: ( LV7,RANK:110 )
在线值:
发帖
回帖
粉丝
3
收藏备用
2013-12-28 22:11
0
雪    币: 168
活跃值: (70)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
4
支持一个
2013-12-28 22:21
0
雪    币: 7
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
其实还有个方向,就是防注入,比如卸载被注入的so,ptrace自己等等
2013-12-28 23:24
0
雪    币: 208
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
6
支持加收藏加下载!
多谢楼主分享!
2013-12-29 12:22
0
雪    币: 1413
活跃值: (401)
能力值: (RANK:270 )
在线值:
发帖
回帖
粉丝
7
最近给MindMac加精华加得手都酸了……
2013-12-29 14:38
0
雪    币: 293
活跃值: (225)
能力值: (RANK:250 )
在线值:
发帖
回帖
粉丝
8
你确定不是因为其他事情手酸?
你是版主,我在积极支持你的工作:P
2013-12-29 18:10
0
雪    币: 2562
活跃值: (4293)
能力值: ( LV13,RANK:540 )
在线值:
发帖
回帖
粉丝
9
是哪只手酸,这个要搞清楚,嘿嘿!!学习,学习,多谢分享!
2013-12-29 18:15
0
雪    币: 1413
活跃值: (401)
能力值: (RANK:270 )
在线值:
发帖
回帖
粉丝
10
我错了,您加油……
2013-12-29 19:20
0
雪    币: 427
活跃值: (64)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
收藏备用,版主右手都酸了
2013-12-29 20:14
0
雪    币: 1008
活跃值: (3247)
能力值: ( LV11,RANK:190 )
在线值:
发帖
回帖
粉丝
12
mark
2013-12-29 20:29
0
雪    币: 219
活跃值: (878)
能力值: (RANK:290 )
在线值:
发帖
回帖
粉丝
13
好东西great
2013-12-30 01:27
0
雪    币: 210
活跃值: (20)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
14
收藏稍后慢慢看
2013-12-30 08:12
0
雪    币: 108
活跃值: (54)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
15
very nice
2013-12-30 09:03
0
雪    币: 100
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
16
收藏以备日后使用
2013-12-30 09:16
0
雪    币: 190
活跃值: (40)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
17
nice
2013-12-30 09:42
0
雪    币: 5
活跃值: (409)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
18
好东西  很全面   但是好多方法虽然猥琐   却完全抛弃了效率问题。。。。。。。反射的消耗还是很大的。。。。所有东西都用上的话,我很怀疑apk是否能跑起来。。。。
2013-12-30 12:28
0
雪    币: 293
活跃值: (225)
能力值: (RANK:250 )
在线值:
发帖
回帖
粉丝
19
恩,是的,保护的话也是主要针对部分关键代码,肯定不能全都用上,全都用上开发人员估计要疯了~~
2013-12-30 14:15
0
雪    币: 6362
活跃值: (707)
能力值: ( LV5,RANK:70 )
在线值:
发帖
回帖
粉丝
20
学习了  mark
2013-12-31 13:04
0
雪    币: 19
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
21
学习了
2014-1-1 08:36
0
雪    币: 132
活跃值: (19)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
22
道高一尺 魔高一丈 而已
2014-1-1 12:40
0
雪    币: 16
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
23
好东西,留位
2014-1-2 09:33
0
雪    币: 216
活跃值: (75)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
24
如果每个方法都有配套的实现代码就更好了!
2014-1-3 14:47
0
雪    币: 197
活跃值: (970)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
25
好帖,值得慢慢揣摩,先收藏了 :)
2014-1-3 17:29
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册