forked from felix-pb/kfd
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcs_blobs.m
188 lines (156 loc) · 7.78 KB
/
cs_blobs.m
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// cs_blobs.c
// kfd
//
// Created by Seo Hyun-gyu on 2023/08/05.
//
#include "cs_blobs.h"
#include "krw.h"
#include "offsets.h"
#include "vnode.h"
#include "utils.h"
#include "thanks_opa334dev_htrowii.h"
extern const uint8_t *der_decode_plist(CFAllocatorRef allocator, CFTypeRef* output, CFErrorRef *error, const uint8_t *der_start, const uint8_t *der_end);
typedef struct __SC_GenericBlob {
uint32_t magic; /* magic number */
uint32_t length; /* total length of blob */
char data[];
} CS_GenericBlob
__attribute__ ((aligned(1)));
uint32_t convertToLittleEndian(uint32_t num) {
return ((num & 0x000000FF) << 24) |
((num & 0x0000FF00) << 8) |
((num & 0x00FF0000) >> 8) |
((num & 0xFF000000) >> 24);
}
//https://github.com/opa334/Dopamine/blob/master/BaseBin/libjailbreak/src/util.m#L656
NSMutableDictionary *DEREntitlementsDecode(uint8_t *start, uint8_t *end)
{
if (!start || !end) return nil;
if (start == end) return nil;
CFTypeRef plist = NULL;
CFErrorRef err;
der_decode_plist(NULL, &plist, &err, start, end);
if (plist) {
if (CFGetTypeID(plist) == CFDictionaryGetTypeID()) {
NSMutableDictionary *plistDict = (__bridge_transfer id)plist;
return plistDict;
}
else if (CFGetTypeID(plist) == CFDataGetTypeID()) {
// This code path is probably never used, but I decided to implement it anyways
// Because I saw in disassembly that there is a possibility for this to return data
NSData *plistData = (__bridge_transfer id)plist;
NSPropertyListFormat format;
NSError *decodeError;
NSMutableDictionary *result = ((NSDictionary *)[NSPropertyListSerialization propertyListWithData:plistData options:0 format:&format error:&decodeError]).mutableCopy;
if (!result) {
printf("[-] Error decoding DER: %s\n", decodeError.description.UTF8String);
}
return result;
}
}
return nil;
}
uint64_t fun_cs_blobs(char *execPath) {
uint64_t ubc_info = unsign_kptr(kread64(getVnodeAtPath(execPath) + off_vnode_vu_ubcinfo));
uint32_t cs_add_gen = kread32(ubc_info + off_ubc_info_cs_add_gen);
// cs_add_gen += 1;
printf("cs_add_gen: 0x%x\n", cs_add_gen);
kwrite32(ubc_info + off_ubc_info_cs_add_gen, cs_add_gen);
uint64_t csblobs = kread64(ubc_info + off_ubc_info_cs_blobs);
printf("csblobs: 0x%llx\n", csblobs);
uint32_t csb_flags = kread32(csblobs + off_cs_blob_csb_flags);
printf("csb_flags: 0x%x\n", csb_flags);
uint64_t csb_teamid = kread64(csblobs + off_cs_blob_csb_teamid);
printf("csb_teamid: 0x%llx\n", csb_teamid);
printf("csb_cdhash\n");
HexDump(csblobs + off_cs_blob_csb_cdhash, 20);
return 0;
}
uint64_t fun_proc_dump_entitlements(uint64_t proc) {
uint64_t proc_ro = kread64(proc + off_p_proc_ro);
uint64_t ucreds = kread64(proc_ro + off_p_ro_p_ucred);
uint64_t cr_label_pac = kread64(ucreds + off_u_cr_label);
uint64_t cr_label = unsign_kptr(cr_label_pac);
printf("[i] ucred->cr_label: 0x%llx\n", cr_label);
//https://github.com/apple-oss-distributions/xnu/blob/xnu-8792.41.9/osfmk/kern/cs_blobs.h#L283
//Thanks @jmpews, https://twitter.com/jmpews/status/1623669186894659584/photo/3
uint64_t osents = kread64(cr_label + 8);//+8, 16, 32, 64, 104, 112, 128, 168, 176, 232, 240 has kernel pointer values...
printf("[i] osents: 0x%llx\n", osents);
uint64_t osentitlements = kread64(osents + 0x10);
printf("[i] osentitlements: 0x%llx\n", osentitlements);
uint64_t dict = kread64(osentitlements + 0x70);
printf("[i] dict: 0x%llx\n", dict);
uint64_t query_ctx = osentitlements + 0x20;
printf("[i] query_ctx: 0x%llx\n", query_ctx);
uint64_t der_start = kread64(query_ctx + 0x40);
printf("[i] der_start: 0x%llx\n", der_start);
uint64_t der_end = kread64(query_ctx + 0x20);
printf("[i] der_end: 0x%llx\n", der_end);
uint64_t der_len = der_end - der_start;
printf("[i] der_len: 0x%llx\n", der_len);
uint8_t has_no_der_ents = kread8(osentitlements + 0x50);
printf("[i] has_no_der_ents: 0x%x\n", has_no_der_ents);
uint64_t csb_der_entitlements_blob = kread64(osentitlements + 0x60);
printf("[i] csb_der_entitlements_blob: 0x%llx\n", csb_der_entitlements_blob);
if(!has_no_der_ents) {
CS_GenericBlob der_ents_blob = {0};
kreadbuf(csb_der_entitlements_blob, (uint8_t *)&der_ents_blob, sizeof(der_ents_blob));
uint32_t der_ents_data_len = der_ents_blob.length;
printf("[i] der_ents_blob.length: 0x%x\n", convertToLittleEndian(der_ents_data_len));
uint8_t *der_ents_data = malloc(der_len);
kreadbuf(csb_der_entitlements_blob + 8, der_ents_data, der_len);
uint8_t *us_der_end = der_ents_data + der_len;
NSMutableDictionary *entitlements = DEREntitlementsDecode(der_ents_data, us_der_end);
if(entitlements != nil) {
NSLog(@"[+] Got decoded entitlements!\n%@", entitlements);
} else {
HexDump(csb_der_entitlements_blob, der_len);
}
free(der_ents_data);
}
return 0;
}
uint64_t fun_vnode_dump_entitlements(const char* path) {
uint64_t vnode = getVnodeAtPath(path);
uint64_t ubc_info_pac = kread64(vnode + off_vnode_vu_ubcinfo);
uint64_t ubc_info = unsign_kptr(ubc_info_pac);
uint64_t csblobs = kread64(ubc_info + off_ubc_info_cs_blobs);
if(csblobs == 0) {
printf("[-] Couldn't get vnode->ubc_info->cs_blobs.\n");
return -1;
}
uint64_t csb_pmap_cs_entry = kread64(csblobs + off_cs_blob_csb_pmap_cs_entry);
printf("[i] vnode->ubc_info->cs_blobs->csb_pmap_cs_entry: 0x%llx\n", csb_pmap_cs_entry);
uint32_t csb_validation_category = kread32(csblobs + off_cs_blob_csb_validation_category);
printf("[i] vnode->ubc_info->cs_blobs->csb_validation_category: 0x%x\n", csb_validation_category);
uint64_t ce_ctx = kread64(csb_pmap_cs_entry + off_pmap_cs_code_directory_ce_ctx);
printf("[i] vnode->ubc_info->cs_blobs->csb_pmap_cs_entry->ce_ctx: 0x%llx\n", ce_ctx);
uint32_t der_entitlements_size = kread32(csb_pmap_cs_entry + off_pmap_cs_code_directory_der_entitlements_size);
printf("[i] vnode->ubc_info->cs_blobs->csb_pmap_cs_entry->der_entitlements_size: 0x%x\n", der_entitlements_size);
uint32_t trust_level = kread32(csb_pmap_cs_entry + off_pmap_cs_code_directory_trust);
printf("[i] vnode->ubc_info->cs_blobs->csb_pmap_cs_entry->trust: 0x%x\n", trust_level);
//https://github.com/apple-oss-distributions/xnu/blob/xnu-8792.41.9/EXTERNAL_HEADERS/CoreEntitlements/EntitlementsPriv.h#L21
//https://github.com/apple-oss-distributions/xnu/blob/xnu-8792.41.9/EXTERNAL_HEADERS/CoreEntitlements/der_vm.h#L33
uint64_t der_start = kread64(ce_ctx + 0x38);
printf("[i] der_start: 0x%llx\n", der_start);
uint64_t der_end = kread64(ce_ctx + 0x20);
printf("[i] der_end: 0x%llx\n", der_end);
uint64_t der_len = der_end - der_start;
printf("[i] der_len: 0x%llx\n", der_len);
CS_GenericBlob der_ents_blob = {0};
kreadbuf(der_start, (uint8_t *)&der_ents_blob, sizeof(der_ents_blob));
uint32_t der_ents_data_len = der_ents_blob.length;
printf("[i] der_ents_blob.length: 0x%x\n", convertToLittleEndian(der_ents_data_len));
uint8_t *der_ents_data = malloc(der_len);
kreadbuf(der_start + 8, der_ents_data, der_len);
uint8_t *us_der_end = der_ents_data + der_len;
NSMutableDictionary *entitlements = DEREntitlementsDecode(der_ents_data, us_der_end);
if(entitlements != nil) {
NSLog(@"[+] Got decoded entitlements!\n%@", entitlements);
} else {
HexDump(der_start, der_len);
}
free(der_ents_data);
return 0;
}