@zsa233/frida-analykit-agent 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,271 @@
1
+
2
+ import {
3
+ readByteArray,
4
+ binaryReadU8, binaryReadU16,
5
+ binaryReadU32, binaryReadS32,
6
+ binaryReadU64, binaryReadS64,
7
+ } from "../utils/utils.js"
8
+
9
+
10
+
11
+ export type Ehdr = {
12
+ ei_class: number,
13
+ e_type: number,
14
+ e_phoff: number,
15
+ e_shoff: number,
16
+ e_phnum: number,
17
+ e_shnum: number,
18
+ e_shstrndx: number,
19
+ }
20
+
21
+ export type Phdr = {
22
+ p_type: number,
23
+ p_offset: number,
24
+ p_vaddr: number,
25
+ p_paddr: number,
26
+ p_filesz: number,
27
+ p_memsz: number,
28
+ p_align: number,
29
+ }
30
+
31
+ export type Shdr = {
32
+ name: string | null,
33
+ base: NativePointer
34
+ size: number
35
+
36
+ sh_name: number,
37
+ sh_type: number,
38
+ sh_addr: number,
39
+ sh_offset: number,
40
+ sh_size: number,
41
+ sh_link: number,
42
+ sh_info: number,
43
+ sh_addralign: number,
44
+ sh_entsize: number,
45
+ }
46
+
47
+
48
+ export type Dyn = {
49
+ d_tag: number,
50
+ d_un: number,
51
+ }
52
+
53
+
54
+ export class Soinfo {
55
+ strtab: NativePointer = NULL
56
+ strtab_size: number = 0
57
+ symtab: NativePointer = NULL
58
+ plt_rela: NativePointer = NULL
59
+ plt_rela_count: number = 0
60
+ rela: NativePointer = NULL
61
+ rela_count: number = 0
62
+ relr: NativePointer = NULL
63
+ relr_count: number = 0
64
+ init_func: NativePointer = NULL
65
+ init_array: NativePointer = NULL
66
+ init_array_count: number = 0
67
+ fini_array: NativePointer = NULL
68
+ fini_array_count: number = 0
69
+ plt_got: NativePointer = NULL
70
+ }
71
+
72
+ export type Rela = {
73
+ r_offset: number,
74
+ r_info: number,
75
+ r_addend: number,
76
+ }
77
+
78
+
79
+ export type Sym = {
80
+ name: string
81
+ relocPtr: NativePointer | null
82
+ hook: NativePointer | null
83
+ implPtr: NativePointer | null
84
+ linked: boolean
85
+
86
+ st_name: number
87
+ st_info: number
88
+ st_other: number
89
+ st_shndx: number
90
+ st_value: NativePointer | null
91
+ st_size: number
92
+ }
93
+
94
+
95
+ export const Elf_Ehdr = {
96
+ EI_Magic: readByteArray(0, 4),
97
+ EI_CLASS: binaryReadU8(4),
98
+ B64: {
99
+ E_Type: binaryReadU16(16),
100
+ E_Phoff: binaryReadU64(32),
101
+ E_Shoff: binaryReadU64(40),
102
+ E_Phnum: binaryReadU16(56),
103
+ E_Shnum: binaryReadU16(60),
104
+ E_Shstrndx: binaryReadU16(62),
105
+ SIZE: 64,
106
+ },
107
+ B32: {
108
+ E_Type: binaryReadU16(16),
109
+ E_Phoff: binaryReadU32(28),
110
+ E_Shoff: binaryReadU32(32),
111
+ E_Phnum: binaryReadU16(44),
112
+ E_Shnum: binaryReadU16(48),
113
+ E_Shstrndx: binaryReadU16(50),
114
+ SIZE: 52,
115
+ },
116
+ }
117
+
118
+
119
+ export const Elf_Phdr = {
120
+ B64: {
121
+ P_Type: binaryReadU32(0),
122
+ E_Flags: binaryReadU32(4),
123
+ P_Offset: binaryReadU64(8),
124
+ P_Vaddr: binaryReadU64(16),
125
+ P_Paddr: binaryReadU64(24),
126
+ P_Filesz: binaryReadU64(32),
127
+ P_Memsz: binaryReadU64(40),
128
+ P_Align: binaryReadU64(48),
129
+ SIZE: 56,
130
+ },
131
+ B32: {
132
+ P_Type: binaryReadU32(0),
133
+ E_Flags: binaryReadU32(4),
134
+ P_Offset: binaryReadU32(8),
135
+ P_Vaddr: binaryReadU32(12),
136
+ P_Paddr: binaryReadU32(16),
137
+ P_Filesz: binaryReadU32(20),
138
+ P_Memsz: binaryReadU32(24),
139
+ P_Align: binaryReadU32(28),
140
+ SIZE: 32,
141
+ },
142
+ }
143
+
144
+
145
+ export const Elf_Shdr = {
146
+ B64: {
147
+ Sh_Name: binaryReadU32(0),
148
+ Sh_Type: binaryReadU32(4),
149
+ Sh_Flags: binaryReadU64(8),
150
+ Sh_Addr: binaryReadU64(16),
151
+ Sh_Offset: binaryReadU64(24),
152
+ Sh_Size: binaryReadU64(32),
153
+ Sh_Link: binaryReadU32(40),
154
+ Sh_Info: binaryReadU32(44),
155
+ Sh_Addralign: binaryReadU64(48),
156
+ Sh_Entsize: binaryReadU64(56),
157
+ SIZE: 64,
158
+ },
159
+ B32: {
160
+ Sh_Name: binaryReadU32(0),
161
+ Sh_Type: binaryReadU32(4),
162
+ Sh_Flags: binaryReadU32(8),
163
+ Sh_Addr: binaryReadU32(12),
164
+ Sh_Offset: binaryReadU32(16),
165
+ Sh_Size: binaryReadU32(20),
166
+ Sh_Link: binaryReadU32(24),
167
+ Sh_Info: binaryReadU32(28),
168
+ Sh_Addralign: binaryReadU32(32),
169
+ Sh_Entsize: binaryReadU32(36),
170
+ SIZE: 40,
171
+ },
172
+ }
173
+
174
+ export const Elf_Dyn = {
175
+ B64: {
176
+ D_Tag: binaryReadU64(0),
177
+ D_Un: binaryReadU64(8),
178
+ SIZE: 16,
179
+ },
180
+ B32: {
181
+ D_Tag: binaryReadU32(0),
182
+ D_Un: binaryReadU32(4),
183
+ SIZE: 8,
184
+ },
185
+ }
186
+
187
+
188
+ export const Elf_Sym = {
189
+ B64: {
190
+ St_Name: binaryReadU32(0),
191
+ St_Info: binaryReadU8(4),
192
+ St_Other: binaryReadU8(5),
193
+ St_Shndx: binaryReadU16(6),
194
+ St_Value: binaryReadU64(8),
195
+ St_Size: binaryReadU64(16),
196
+ SIZE: 24,
197
+ },
198
+ B32: {
199
+ St_Name: binaryReadU32(0),
200
+ St_Info: binaryReadU8(4),
201
+ St_Other: binaryReadU8(5),
202
+ St_Shndx: binaryReadU16(6),
203
+ St_Value: binaryReadU32(8),
204
+ St_Size: binaryReadU32(12),
205
+ SIZE: 16,
206
+ }
207
+ }
208
+
209
+ export const Elf_Rela = {
210
+ B64: {
211
+ R_Offset: binaryReadU64(0),
212
+ R_Info: binaryReadU64(8),
213
+ R_Addend: binaryReadS64(16),
214
+ SIZE: 24,
215
+ INFO_SYM: 32n,
216
+ INFO_TYPE: 0xffffffffn,
217
+ Reloc: binaryReadU64(0),
218
+ },
219
+ B32: {
220
+ R_Offset: binaryReadU32(0),
221
+ R_Info: binaryReadU32(4),
222
+ R_Addend: binaryReadS32(8),
223
+ SIZE: 12,
224
+ INFO_SYM: 16n,
225
+ INFO_TYPE: 0xffffn,
226
+ Reloc: binaryReadU32(0),
227
+ },
228
+ }
229
+
230
+
231
+ export enum DyntabTag {
232
+ DT_NULL = 0,
233
+ DT_NEEDED = 1,
234
+ DT_PLTRELSZ = 2,
235
+ DT_PLTGOT = 3,
236
+ DT_HASH = 4,
237
+ DT_STRTAB = 5,
238
+ DT_SYMTAB = 6,
239
+ DT_RELA = 7,
240
+ DT_RELASZ = 8,
241
+ DT_RELAENT = 9,
242
+ DT_STRSZ = 10,
243
+ DT_SYMENT = 11,
244
+ DT_INIT = 12,
245
+ DT_FINI = 13,
246
+ DT_SONAME = 14,
247
+ DT_RPATH = 15,
248
+ DT_SYMBOLIC = 16,
249
+ DT_REL = 17,
250
+ DT_RELSZ = 18,
251
+ DT_RELENT = 19,
252
+ DT_PLTREL = 20,
253
+ DT_DEBUG = 21,
254
+ DT_TEXTREL = 22,
255
+ DT_JMPREL = 23,
256
+ DT_ENCODING = 32,
257
+
258
+ DT_BIND_NOW = 24,
259
+ DT_INIT_ARRAY = 25,
260
+ DT_FINI_ARRAY = 26,
261
+ DT_INIT_ARRAYSZ = 27,
262
+ DT_FINI_ARRAYSZ = 28,
263
+ DT_RUNPATH = 29,
264
+ DT_FLAGS = 30,
265
+
266
+ DT_RELR = 0x6fffe000,
267
+ DT_RELRSZ = 0x6fffe001,
268
+ DT_RELRENT = 0x6fffe003,
269
+ DT_RELRCOUNT = 0x6fffe005,
270
+ }
271
+
@@ -0,0 +1,33 @@
1
+ import { setGlobalProperties } from "../config.js"
2
+ import { ElfFileFixer, ElfModuleX } from "./module.js"
3
+
4
+
5
+
6
+ export class ElfTools {
7
+
8
+ static findModuleByName(name: string, tryFix: boolean = false): ElfModuleX | null {
9
+ const mod = Process.findModuleByName(name)
10
+ if (mod === null) {
11
+ return null
12
+ }
13
+ return this.loadFromModule(mod, tryFix)
14
+ }
15
+
16
+ static getModuleByName(name: string, tryFix: boolean = false): ElfModuleX {
17
+ const modx = this.findModuleByName(name, tryFix)
18
+ if (modx === null) {
19
+ throw new Error(`[getModuleByName] ${name} module not found.`)
20
+ }
21
+ return modx
22
+ }
23
+
24
+ static loadFromModule(mod: Module, tryFix: boolean = false): ElfModuleX {
25
+ const fixers = tryFix ? [new ElfFileFixer(mod.path)] : undefined
26
+ return new ElfModuleX(mod, fixers)
27
+ }
28
+ }
29
+
30
+
31
+ setGlobalProperties({
32
+ ElfTools,
33
+ })
@@ -0,0 +1,74 @@
1
+ import { setGlobalProperties } from "../config.js"
2
+ import { InstructionSequence } from "./insn.js"
3
+
4
+
5
+
6
+ type ScoreResult = {
7
+ instructions: Arm64Instruction[],
8
+ eoi?: Arm64Instruction,
9
+ score: number
10
+ }
11
+
12
+
13
+ export class Subroutine extends InstructionSequence {
14
+
15
+ constructor(entry: Arm64Instruction) {
16
+ super(entry)
17
+ }
18
+
19
+ scoreThunk(): ScoreResult {
20
+ const MAX_INSTR = 20
21
+ let score: number = 0
22
+
23
+ let i: number = 0
24
+ const insns: Arm64Instruction[] = []
25
+ loop: for (const insn of this) {
26
+ insns.push(insn)
27
+ const ops = insn.operands
28
+ switch (insn.mnemonic) {
29
+ case 'br':
30
+ case 'b':
31
+ this.eoi = insn
32
+ score += 100
33
+ break loop
34
+ case 'stp':
35
+ case 'ldp':
36
+ if (ops[2].type === 'mem' && ops[2].value.base === 'sp') {
37
+ score -= 20
38
+ break
39
+ }
40
+ case 'ret':
41
+ score = 0
42
+ this.eoi = insn
43
+ break
44
+ case 'sub':
45
+ case 'add':
46
+ if (ops[0].value as string === 'sp') {
47
+ score -= 20
48
+ break
49
+ }
50
+ break
51
+ }
52
+ if (i >= 5) {
53
+ // 指令越多分数越低
54
+ score -= 5
55
+ }
56
+ i++
57
+ if (i >= MAX_INSTR) {
58
+ break
59
+ }
60
+ }
61
+
62
+ return {
63
+ instructions: insns,
64
+ eoi: this.eoi,
65
+ score: score,
66
+ }
67
+ }
68
+
69
+ }
70
+
71
+
72
+ setGlobalProperties({
73
+ Subroutine,
74
+ })