Skip to content

Commit

Permalink
blog
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanxuan committed Jun 8, 2023
1 parent 20669d8 commit d83a3c4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions _posts/2023-05-29-p9lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ categories:
tags:
---

更新中...是议题 [Unearthing the TrustedCore: A Critical Review on Huawei’s Trusted Execution Environment](https://www.usenix.org/conference/woot20/presentation/busch)的子部分,其分析的TEE方案是华为自研的TrustedCore,解密TA的大概流程主要有三步:(1)模拟运行TEE中的白盒密码算法解出RSA私钥prikeyx。(2)使用RSA的私钥prikeyx解密TA头部的manifest。(3)使用解密后manifest中的AES key 解密 TA 正文。但作者没有给出解密过程中的一些细节,例如RSA和AES密钥的组织方法。所以我尝试复现了这个解密,并给出解密过程中的所有细节。
议题 [Unearthing the TrustedCore: A Critical Review on Huawei’s Trusted Execution Environment](https://www.usenix.org/conference/woot20/presentation/busch) 的子部分,其分析的TEE方案是华为自研的TrustedCore,解密TA的大概流程主要有三步:(1)模拟运行TEE中的白盒密码算法解出RSA私钥prikeyx。(2)使用RSA的私钥prikeyx解密TA头部的manifest。(3)使用解密后manifest中的AES key 解密 TA 正文。但作者没有给出解密过程中的一些细节,例如RSA和AES密钥的组织方法。所以我尝试复现了这个解密,并给出解密过程中的所有细节。
![image](https://xuanxuanblingbling.github.io/assets/pic/p9lite/two_key.png)

- 议题:[Unearthing the TrustedCore: A Critical Review on Huawei’s Trusted Execution Environment](https://www.usenix.org/conference/woot20/presentation/busch)
Expand Down Expand Up @@ -1056,8 +1056,51 @@ uint32_t kaddr_to_uref(void *kaddr)
## 全局变量的逆向
- dword_156764
- dword_156758
通过之前的逆向我们认为:
- AES的KEY:内容为dword_156764指向的数据,大小为dword_156758
- AES 的 IV:内容为dword_156764指向的数据,大小为dword_156758除2
IDA里改个名继续逆向:
- dword_156764,改名为key_iv
- dword_156758,改名为key_iv_size
关注key_iv,其还在parse_manifest中被使用:
![image](https://xuanxuanblingbling.github.io/assets/pic/p9lite/re_1.png)
继续分析parse_manifest,也就是RSA解密manifest并解析的处理函数:
- 44行:key_iv也即v14,是TEE_Malloc出的一个堆块地址,堆块大小为key_iv_size
- 51行:key_iv也即v14,拷贝自v13,大小为key_iv_size
- 43行:v13,指向AES KEY,是v2 + dword_156754 + 52 加出来的
![image](https://xuanxuanblingbling.github.io/assets/pic/p9lite/re_2.png)
所以只要把v2和dword_156754分析明白即可,根据v2是manifest_rsa_decry函数的参数可以推测,v2就指向解密完的manifest的开头。按照这个推测,变量img_info就是解密后manifest的前四个字节:
![image](https://xuanxuanblingbling.github.io/assets/pic/p9lite/re_5.png)
仔细分析代码可以确定,img_info以及其之后的数据就是由parse_manifest中的while循环解析出来的,因此这部分内存很有可能就是解密后的manifest:
![image](https://xuanxuanblingbling.github.io/assets/pic/p9lite/re_3.png)
尝试计算dword_156754和img_info偏移差为0x28:
```python
>>> hex(0x0156754 - 0x015672C)
'0x28'
```

因此按照此偏移解析manifest:

- dword_156754: 0x20
- dword_156758: 0x20 (key_iv_size)

![image](https://xuanxuanblingbling.github.io/assets/pic/p9lite/re_4.png)

因此拷贝到v14的v13,推测为AES KEY,其值为 v2 + dword_156754 + 52,即manifest开头处偏移 0x20 + 52 字节,即0x54(84)字节,因此按照此位置解析明文manifest中AES的KEY和IV,大小分别为32字节和16字节:

```python
from Crypto.Cipher import AES
Expand All @@ -1071,7 +1114,9 @@ msg = a.decrypt(cipher)
print(msg)
```

```
成功解密出ELF,证明了以上我们连蒙带猜的逆向结果是正确的:

```python
➜ python3 exp.py
b'\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00'
```
Expand Down Expand Up @@ -1152,7 +1197,7 @@ P9 Lite是在国外上市的手机,不过闲鱼上也可以买到,我买了

![image](https://xuanxuanblingbling.github.io/assets/pic/p9lite/p9lite.png)

其他相关:
另外其实作者恢复globaltask和TEEOS的ELF结构的过程也还值得研究原理,其他相关:

- [EL3 Tour: Get The Ultimate Privilege of Android Phone](https://downloads.immunityinc.com/infiltrate2019-slidepacks/guanxing-wen-el3-tour-get-the-ultimate-privilege-of-android-phone/infiltrate.pdf)
- [On the Security of ARM TrustZone-Based Trusted Execution Environments](https://opus4.kobv.de/opus4-fau/files/16008/diss-mbusch.pdf)
Expand Down
Binary file added assets/pic/p9lite/re_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pic/p9lite/re_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pic/p9lite/re_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pic/p9lite/re_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pic/p9lite/re_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d83a3c4

Please sign in to comment.