首页
社区
课程
招聘
[原创]Youpk: 又一款基于ART的主动调用的脱壳机
2020-5-31 00:04 55835

[原创]Youpk: 又一款基于ART的主动调用的脱壳机

2020-5-31 00:04
55835

Youpk: 又一款基于ART的主动调用的脱壳机

原理

Youpk是一款针对Dex整体加固+各式各样的Dex抽取的脱壳机

 

基本流程如下:

  1. 从内存中dump DEX
  2. 构造完整调用链, 主动调用所有方法并dump CodeItem
  3. 合并 DEX, CodeItem

从内存中dump DEX

DEX文件在art虚拟机中使用DexFile对象表示, 而ClassLinker中引用了这些对象, 因此可以采用从ClassLinker中遍历DexFile对象并dump的方式来获取.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//unpacker.cc
std::list<const DexFile*> Unpacker::getDexFiles() {
  std::list<const DexFile*> dex_files;
  Thread* const self = Thread::Current();
  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
  ReaderMutexLock mu(self, *class_linker->DexLock());
  const std::list<ClassLinker::DexCacheData>& dex_caches = class_linker->GetDexCachesData();
  for (auto it = dex_caches.begin(); it != dex_caches.end(); ++it) {
    ClassLinker::DexCacheData data = *it;
    const DexFile* dex_file = data.dex_file;
    dex_files.push_back(dex_file);
  }
  return dex_files;
}

另外, 为了避免dex做任何形式的优化影响dump下来的dex文件, 在dex2oat中设置 CompilerFilter 为仅验证

1
2
//dex2oat.cc
compiler_options_->SetCompilerFilter(CompilerFilter::kVerifyAtRuntime);

构造完整调用链, 主动调用所有方法

  1. 创建脱壳线程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    //unpacker.java
    public static void unpack() {
        if (Unpacker.unpackerThread != null) {
            return;
        }
     
        //开启线程调用
        Unpacker.unpackerThread = new Thread() {
            @Override public void run() {
                while (true) {
                    try {
                        Thread.sleep(UNPACK_INTERVAL);
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (shouldUnpack()) {
                        Unpacker.unpackNative();
                    }  
                }
            }
        };
        Unpacker.unpackerThread.start();
    }
  2. 在脱壳线程中遍历DexFile的所有ClassDef

    1
    2
    //unpacker.cc
    for (; class_idx < dex_file->NumClassDefs(); class_idx++) {
  3. 解析并初始化Class

    1
    2
    3
    4
    5
    //unpacker.cc
    mirror::Class* klass = class_linker->ResolveType(*dex_file, dex_file->GetClassDef(class_idx).class_idx_, h_dex_cache, h_class_loader);
    StackHandleScope<1> hs2(self);
    Handle<mirror::Class> h_class(hs2.NewHandle(klass));
    bool suc = class_linker->EnsureInitialized(self, h_class, true, true);
  4. 主动调用Class的所有Method, 并修改ArtMethod::Invoke使其强制走switch型解释器

    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
    //unpacker.cc
    uint32_t args_size = (uint32_t)ArtMethod::NumArgRegisters(method->GetShorty());
    if (!method->IsStatic()) {
        args_size += 1;
    }
     
    JValue result;
    std::vector<uint32_t> args(args_size, 0);
    if (!method->IsStatic()) {
        mirror::Object* thiz = klass->AllocObject(self);
        args[0] = StackReference<mirror::Object>::FromMirrorPtr(thiz).AsVRegValue(); 
    }
    method->Invoke(self, args.data(), args_size, &result, method->GetShorty());
     
    //art_method.cc
    if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this)
    || (Unpacker::isFakeInvoke(self, this) && !this->IsNative()))) {
    if (IsStatic()) {
    art::interpreter::EnterInterpreterFromInvoke(
    self, this, nullptr, args, result, /*stay_in_interpreter*/ true);
    } else {
    mirror::Object* receiver =
    reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
    art::interpreter::EnterInterpreterFromInvoke(
    self, this, receiver, args + 1, result, /*stay_in_interpreter*/ true);
    }
    }
     
    //interpreter.cc
    static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImplKind;
  5. 在解释器中插桩, 在每条指令执行前设置回调

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    //interpreter_switch_impl.cc
    // Code to run before each dex instruction.
      #define PREAMBLE()                                                                 \
      do {                                                                               \
        inst_count++;                                                                    \
        bool dumped = Unpacker::beforeInstructionExecute(self, shadow_frame.GetMethod(), \
                                                         dex_pc, inst_count);            \
        if (dumped) {                                                                    \
          return JValue();                                                               \
        }                                                                                \
        if (UNLIKELY(instrumentation->HasDexPcListeners())) {                            \
          instrumentation->DexPcMovedEvent(self, shadow_frame.GetThisObject(code_item->ins_size_),  shadow_frame.GetMethod(), dex_pc);                                                                                  \
        }                                                                                \
      } while (false)
  6. 在回调中做针对性的CodeItem的dump, 这里仅仅是简单的示例了直接dump, 实际上, 针对某些厂商的抽取, 可以真正的执行几条指令等待CodeItem解密后再dump

    1
    2
    3
    4
    5
    6
    7
    8
    //unpacker.cc
    bool Unpacker::beforeInstructionExecute(Thread *self, ArtMethod *method, uint32_t dex_pc, int inst_count) {
      if (Unpacker::isFakeInvoke(self, method)) {
          Unpacker::dumpMethod(method);
        return true;
      }
      return false;
    }

合并 DEX, CodeItem

将dump下来的CodeItem填充到DEX的相应位置中即可. 主要是基于google dx工具修改.

参考链接

FUPK3: https://bbs.pediy.com/thread-246117.htm

 

FART: https://bbs.pediy.com/thread-252630.htm

刷机

  1. 仅支持pixel 1代
  2. 重启至bootloader: adb reboot bootloader
  3. 解压 Youpk_sailfish.zip 并双击 flash-all.bat
    百度云:https://pan.baidu.com/s/1ySSy2vNW5TyFjH1LNAcd5w
    提取码:vseh

使用方法

  1. 该工具仅仅用来学习交流, 请勿用于非法用途, 否则后果自付!

  2. 配置待脱壳的app包名, 准确来讲是进程名称

    1
    adb shell "echo cn.youlor.mydemo >> /data/local/tmp/unpacker.config"
  3. 启动apk等待脱壳
    每隔10秒将自动重新脱壳(已完全dump的dex将被忽略), 当日志打印unpack end时脱壳完成

  4. pull出dump文件, dump文件路径为 /data/data/包名/unpacker

    1
    adb pull /data/data/cn.youlor.mydemo/unpacker
  5. 调用修复工具 dexfixer.jar, 两个参数, 第一个为dump文件目录(必须为有效路径), 第二个为重组后的DEX目录(不存在将会创建)

    1
    java -jar dexfixer.jar /path/to/unpacker /path/to/output

适用场景

  1. 整体加固
  2. 抽取:
    • nop占坑型(类似某加密)
    • naitve化, 在<clinit>中解密(类似早期阿里)
    • goto解密型(类似新版某加密?najia): https://bbs.pediy.com/thread-259448.htm

常见问题

  1. dump中途退出或卡死,重新启动进程,再次等待脱壳即可
  2. 当前仅支持被壳保护的dex, 不支持App动态加载的dex/jar

开源

仓库地址: https://github.com/youlor/unpacker


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2021-5-31 13:41 被Youlor编辑 ,原因: 开源
收藏
点赞28
打赏
分享
最新回复 (62)
雪    币: 997
活跃值: (415)
能力值: ( LV7,RANK:100 )
在线值:
发帖
回帖
粉丝
上海刘一刀 2 2020-5-31 00:06
2
0
膜  大佬666666666666666666666666666666666
雪    币: 348
活跃值: (157564)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
伟叔叔 2020-5-31 00:07
3
0
学习了
雪    币: 12052
活跃值: (15379)
能力值: ( LV12,RANK:240 )
在线值:
发帖
回帖
粉丝
pureGavin 2 2020-5-31 00:26
4
0
mark,楼主辛苦了
雪    币: 226
活跃值: (1284)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
hpphpp 2020-5-31 00:37
5
0
插眼,学习
雪    币: 3907
活跃值: (5712)
能力值: ( LV12,RANK:200 )
在线值:
发帖
回帖
粉丝
roysue 3 2020-5-31 09:59
6
2

试了下,案例来自此文中的aipao.apk,FART最终的效果是可以看Smali,这个Youpk可以直接回填到dex,用jadx直接打开就行:
见图片:

雪    币: 4057
活跃值: (312)
能力值: ( LV4,RANK:45 )
在线值:
发帖
回帖
粉丝
Buu 1 2020-5-31 12:37
7
0

大佬牛逼

最后于 2020-5-31 12:37 被Buu编辑 ,原因:
雪    币: 156
活跃值: (953)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
bluegatar 2020-5-31 13:54
8
0
刷机包没必要刷bootlloader和radio啊,可以把包再精简下
雪    币: 1634
活跃值: (108)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
TopDbg 2020-5-31 15:51
9
0
思路清晰,感谢分享!
雪    币: 0
活跃值: (304)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
黑色刺客 2020-5-31 20:08
10
0
请问下模拟器能刷么
雪    币: 8250
活跃值: (4575)
能力值: ( LV8,RANK:134 )
在线值:
发帖
回帖
粉丝
hanbingle 2 2020-5-31 20:21
11
1
思路清晰,给你点赞,对于一些自解密的抽取壳确实需要构造更深的调用链甚至是执行几条smali指令再dump,比如我在看雪高研班上给的那个自解密的样本
雪    币: 343
活跃值: (115062)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Youlor 2020-5-31 20:58
12
0
黑色刺客 请问下模拟器能刷么
不能, 目前只有pixel 1代的刷机包
雪    币: 343
活跃值: (115062)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Youlor 2020-5-31 21:00
13
0
hanbingle 思路清晰[em_63],给你点赞,对于一些自解密的抽取壳确实需要构造更深的调用链甚至是执行几条smali指令再dump,比如我在看雪高研班上给的那个自解密的样本
嗯嗯, 我看到那个特殊抽取壳的帖子, 发现和之前分析过的najia的一个样本比较像, 都是goto到解密再goto到解密后的原指令, 所以就一并处理了下
雪    币: 2964
活跃值: (2082)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
一颗金柚子 1 2020-6-1 10:00
14
0
思路值得学习,目前已经将楼主的方法复现成功,确实可以脱下来大部分壳,不过我想问一下“执行几条指令之后再dump”部分是根据ins_count这个传参来判断还是在Opcode中dump呢?
雪    币: 283
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Czhiqiang 2020-6-1 10:28
15
0
一颗金柚子 思路值得学习,目前已经将楼主的方法复现成功,确实可以脱下来大部分壳,不过我想问一下“执行几条指令之后再dump”部分是根据ins_count这个传参来判断还是在Opcode中dump呢?


最后于 2020-6-1 10:29 被Czhiqiang编辑 ,原因:
雪    币: 343
活跃值: (115062)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Youlor 2020-6-1 10:35
16
0
一颗金柚子 思路值得学习,目前已经将楼主的方法复现成功,确实可以脱下来大部分壳,不过我想问一下“执行几条指令之后再dump”部分是根据ins_count这个传参来判断还是在Opcode中dump呢?
当前是根据ins_count, opcode一起作为特征的
雪    币: 13432
活跃值: (4763)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
tDasm 2020-6-1 11:19
17
0
修改的源码都贴出来了?让大家完整学习一下。
雪    币: 5233
活跃值: (3255)
能力值: ( LV10,RANK:175 )
在线值:
发帖
回帖
粉丝
挤蹭菌衣 1 2020-6-1 19:17
18
0
tql
雪    币: 866
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
jack.zhang 2020-6-2 09:26
19
0
虽然,思路有点不清晰 对art主动调用;但是还是任务 
雪    币: 1892
活跃值: (1750)
能力值: (RANK:400 )
在线值:
发帖
回帖
粉丝
莫灰灰 9 2020-6-2 10:43
20
0
学习了~
雪    币: 969
活跃值: (838)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
lrzhao 2020-6-2 15:27
21
0
OnCreate 抽取的可以吗?试了一下 还是 public native void onCreate(Bundle bundle);
雪    币: 1759
活跃值: (2309)
能力值: ( LV2,RANK:15 )
在线值:
发帖
回帖
粉丝
又见飞刀z 2020-6-2 17:21
22
0
bluegatar 刷机包没必要刷bootlloader和radio啊,可以把包再精简下
我感觉直接一个整体挺好的,一键刷入多方便
雪    币: 3313
活跃值: (2886)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
D-t 2020-6-2 19:44
23
0
lrzhao OnCreate 抽取的可以吗?试了一下 还是 public native void onCreate(Bundle bundle);

你这是vmp吧 这个帖子说的很清楚 针对整体和抽取填抗的类型 不支持被v掉的代码

最后于 2020-6-2 19:45 被D-t编辑 ,原因: 1
雪    币: 969
活跃值: (838)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
lrzhao 2020-6-2 21:21
24
0
那是我跨服聊天了
雪    币: 386
活跃值: (834)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
笑对VS人生 2020-6-4 08:26
25
0
厉害啦
游客
登录 | 注册 方可回帖
返回