Skip to content

Commit 8f745f7

Browse files
committed
delete cmod
1 parent 6cdc9bb commit 8f745f7

File tree

4 files changed

+39
-112
lines changed

4 files changed

+39
-112
lines changed

agent/cmod/.clang-format

-1
This file was deleted.

agent/cmod/.gitignore

-2
This file was deleted.

agent/cmod/source.c

-97
This file was deleted.

agent/src/dump.ts

+39-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { memcpy, download } from './transfer';
22
import { normalize } from './path';
33
import { freeze, wakeup } from './threads';
4-
import c from '../cmod';
54

6-
type EncryptInfoTuple = [NativePointer, number, number, number, number];
5+
const MH_MAGIC_64 = 0xfeedfacf;
6+
const LC_ENCRYPTION_INFO = 0x21;
7+
const LC_ENCRYPTION_INFO_64 = 0x2c;
78

8-
interface ISet {
9-
[key: string]: boolean;
10-
}
9+
type EncryptInfoTuple = [NativePointer, number, number, number, number];
1110

1211
interface Option {
1312
executableOnly?: boolean
@@ -27,6 +26,36 @@ export function base() {
2726
return normalize(ObjC.classes.NSBundle.mainBundle().bundlePath().toString());
2827
}
2928

29+
function findCryptInfo(header: NativePointer) {
30+
const magic = header.readU32();
31+
if (magic !== MH_MAGIC_64) {
32+
throw new Error(`Unsupported magic ${magic.toString(16)}`);
33+
}
34+
35+
const ncmds = header.add(16).readU32();
36+
const cmds = header.add(32);
37+
38+
let offsetOfCmd = 0;
39+
let sizeOfCmd = 0;
40+
let offset = 0;
41+
let size = 0;
42+
43+
for (let i = 0; i < ncmds; i++) {
44+
const cmd = cmds.add(offsetOfCmd).readU32();
45+
sizeOfCmd = cmds.add(offsetOfCmd + 4).readU32();
46+
47+
if (cmd === LC_ENCRYPTION_INFO || cmd === LC_ENCRYPTION_INFO_64) {
48+
offset = cmds.add(offsetOfCmd + 8).readU32();
49+
size = cmds.add(offsetOfCmd + 12).readU32();
50+
return [cmds.add(offsetOfCmd), offset, size, offsetOfCmd, sizeOfCmd] as EncryptInfoTuple;
51+
}
52+
53+
offsetOfCmd += sizeOfCmd;
54+
}
55+
56+
throw new Error('Cannot find crypt info');
57+
}
58+
3059
export async function dump(opt: Option = {}) {
3160
// load all frameworks
3261
warmup();
@@ -35,20 +64,20 @@ export async function dump(opt: Option = {}) {
3564
freeze();
3665

3766
const bundle = base();
38-
const downloaded: ISet = {};
67+
const downloaded = new Set<string>();
3968
for (let mod of Process.enumerateModules()) {
4069
const filename = normalize(mod.path);
4170
if (!filename.startsWith(bundle))
4271
continue;
4372

44-
const info = findEncyptInfo!(mod.base) as EncryptInfoTuple;
73+
const info = findCryptInfo(mod.base) as EncryptInfoTuple;
4574
const [ptr, offset, size, offsetOfCmd, sizeOfCmd] = info;
4675

4776
if (ptr.isNull())
4877
continue;
4978

5079
await download(filename);
51-
downloaded[filename] = true;
80+
downloaded.add(filename);
5281

5382
// skip fat header
5483
const fatOffset = Process.findRangeByAddress(mod.base)!.file!.offset;
@@ -71,7 +100,7 @@ export async function dump(opt: Option = {}) {
71100

72101
}
73102

74-
async function pull(bundle: string, downloaded: ISet) {
103+
async function pull(bundle: string, downloaded: Set<string>) {
75104
const manager = ObjC.classes.NSFileManager.defaultManager();
76105
const enumerator = manager.enumeratorAtPath_(bundle);
77106
const pIsDir = Memory.alloc(Process.pointerSize);
@@ -85,7 +114,7 @@ async function pull(bundle: string, downloaded: ISet) {
85114
continue;
86115

87116
const fullname = normalize(base.stringByAppendingPathComponent_(path));
88-
if (downloaded[fullname])
117+
if (downloaded.has(fullname))
89118
continue;
90119

91120
pIsDir.writePointer(NULL);
@@ -96,8 +125,6 @@ async function pull(bundle: string, downloaded: ISet) {
96125
}
97126
}
98127

99-
const cm = new CModule(c);
100-
const findEncyptInfo = new NativeFunction(cm['find_encryption_info'], ['pointer', 'uint32', 'uint32', 'uint32', 'uint32'], ['pointer']);
101128

102129
export function warmup(): void {
103130
const { NSFileManager, NSBundle } = ObjC.classes

0 commit comments

Comments
 (0)