-
Notifications
You must be signed in to change notification settings - Fork 42
/
il2cpp.lua
235 lines (206 loc) · 8.05 KB
/
il2cpp.lua
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
local Il2cppMemory = require("utils.il2cppmemory")
local VersionEngine = require("utils.version")
local AndroidInfo = require("utils.androidinfo")
local Searcher = require("utils.universalsearcher")
local PatchApi = require("utils.patchapi")
---@class Il2cpp
local Il2cppBase = {
il2cppStart = 0,
il2cppEnd = 0,
globalMetadataStart = 0,
globalMetadataEnd = 0,
globalMetadataHeader = 0,
MainType = AndroidInfo.platform and gg.TYPE_QWORD or gg.TYPE_DWORD,
pointSize = AndroidInfo.platform and 8 or 4,
---@type Il2CppTypeDefinitionApi
Il2CppTypeDefinitionApi = {},
MetadataRegistrationApi = require("il2cppstruct.metadataRegistration"),
TypeApi = require("il2cppstruct.type"),
MethodsApi = require("il2cppstruct.method"),
GlobalMetadataApi = require("il2cppstruct.globalmetadata"),
FieldApi = require("il2cppstruct.field"),
ClassApi = require("il2cppstruct.class"),
ObjectApi = require("il2cppstruct.object"),
ClassInfoApi = require("il2cppstruct.api.classinfo"),
FieldInfoApi = require("il2cppstruct.api.fieldinfo"),
---@type MyString
String = require("il2cppstruct.il2cppstring"),
MemoryManager = require("utils.malloc"),
--- Patch `Bytescodes` to `add`
---
--- Example:
--- arm64:
--- `mov w0,#0x1`
--- `ret`
---
--- `Il2cpp.PatchesAddress(0x100, "\x20\x00\x80\x52\xc0\x03\x5f\xd6")`
---@param add number
---@param Bytescodes string
---@return Patch
PatchesAddress = function(add, Bytescodes)
local patchCode = {}
for code in string.gmatch(Bytescodes, '.') do
patchCode[#patchCode + 1] = {
address = add + #patchCode,
value = string.byte(code),
flags = gg.TYPE_BYTE
}
end
---@type Patch
local patch = PatchApi:Create(patchCode)
patch:Patch()
return patch
end,
--- Searches for a method, or rather information on the method, by name or by offset, you can also send an address in memory to it.
---
--- Return table with information about methods.
---@generic TypeForSearch : number | string
---@param searchParams TypeForSearch[] @TypeForSearch = number | string
---@return table<number, MethodInfo[] | ErrorSearch>
FindMethods = function(searchParams)
Il2cppMemory:SaveResults()
for i = 1, #searchParams do
---@type number | string
searchParams[i] = Il2cpp.MethodsApi:Find(searchParams[i])
end
Il2cppMemory:ClearSavedResults()
return searchParams
end,
--- Searches for a class, by name, or by address in memory.
---
--- Return table with information about class.
---@param searchParams ClassConfig[]
---@return table<number, ClassInfo[] | ErrorSearch>
FindClass = function(searchParams)
Il2cppMemory:SaveResults()
for i = 1, #searchParams do
searchParams[i] = Il2cpp.ClassApi:Find(searchParams[i])
end
Il2cppMemory:ClearSavedResults()
return searchParams
end,
--- Searches for an object by name or by class address, in memory.
---
--- In some cases, the function may return an incorrect result for certain classes. For example, sometimes the garbage collector may not have time to remove an object from memory and then a `fake object` will appear or for a turnover, the object may still be `not implemented` or `not created`.
---
--- Returns a table of objects.
---@param searchParams table
---@return table
FindObject = function(searchParams)
Il2cppMemory:SaveResults()
for i = 1, #searchParams do
searchParams[i] = Il2cpp.ObjectApi:Find(Il2cpp.ClassApi:Find({Class = searchParams[i]}))
end
Il2cppMemory:ClearSavedResults()
return searchParams
end,
--- Searches for a field, or rather information about the field, by name or by address in memory.
---
--- Return table with information about fields.
---@generic TypeForSearch : number | string
---@param searchParams TypeForSearch[] @TypeForSearch = number | string
---@return table<number, FieldInfo[] | ErrorSearch>
FindFields = function(searchParams)
Il2cppMemory:SaveResults()
for i = 1, #searchParams do
---@type number | string
local searchParam = searchParams[i]
local searchResult = Il2cppMemory:GetInformationOfField(searchParam)
if not searchResult then
searchResult = Il2cpp.FieldApi:Find(searchParam)
Il2cppMemory:SetInformationOfField(searchParam, searchResult)
end
searchParams[i] = searchResult
end
Il2cppMemory:ClearSavedResults()
return searchParams
end,
---@param Address number
---@param length? number
---@return string
Utf8ToString = function(Address, length)
local chars, char = {}, {
address = Address,
flags = gg.TYPE_BYTE
}
if not length then
repeat
_char = string.char(gg.getValues({char})[1].value & 0xFF)
chars[#chars + 1] = _char
char.address = char.address + 0x1
until string.find(_char, "[%z%s]")
return table.concat(chars, "", 1, #chars - 1)
else
for i = 1, length do
local _char = gg.getValues({char})[1].value
chars[i] = string.char(_char & 0xFF)
char.address = char.address + 0x1
end
return table.concat(chars)
end
end,
---@param bytes string
ChangeBytesOrder = function(bytes)
local newBytes, index, lenBytes = {}, 0, #bytes / 2
for byte in string.gmatch(bytes, "..") do
newBytes[lenBytes - index] = byte
index = index + 1
end
return table.concat(newBytes)
end,
FixValue = function(val)
return AndroidInfo.platform and val & 0x00FFFFFFFFFFFFFF or val & 0xFFFFFFFF
end,
GetValidAddress = function(Address)
local lastByte = Address & 0x000000000000000F
local delta = 0
local checkTable = {[12] = true, [4] = true, [8] = true, [0] = true}
while not checkTable[lastByte - delta] do
delta = delta + 1
end
return Address - delta
end,
---@param self Il2cpp
---@param address number | string
SearchPointer = function(self, address)
address = self.ChangeBytesOrder(type(address) == 'number' and string.format('%X', address) or address)
gg.searchNumber('h ' .. address)
gg.refineNumber('h ' .. address:sub(1, 6))
gg.refineNumber('h ' .. address:sub(1, 2))
local FindsResult = gg.getResults(gg.getResultsCount())
gg.clearResults()
return FindsResult
end,
}
---@type Il2cpp
Il2cpp = setmetatable({}, {
---@param self Il2cpp
---@param config? Il2cppConfig
__call = function(self, config)
config = config or {}
getmetatable(self).__index = Il2cppBase
if config.libilcpp then
self.il2cppStart, self.il2cppEnd = config.libilcpp.start, config.libilcpp['end']
else
self.il2cppStart, self.il2cppEnd = Searcher.FindIl2cpp()
end
if config.globalMetadata then
self.globalMetadataStart, self.globalMetadataEnd = config.globalMetadata.start, config.globalMetadata['end']
else
self.globalMetadataStart, self.globalMetadataEnd = Searcher:FindGlobalMetaData()
end
if config.globalMetadataHeader then
self.globalMetadataHeader = config.globalMetadataHeader
else
self.globalMetadataHeader = self.globalMetadataStart
end
self.MetadataRegistrationApi.metadataRegistration = config.metadataRegistration
VersionEngine:ChooseVersion(config.il2cppVersion, self.globalMetadataHeader)
Il2cppMemory:ClearMemorize()
end,
__index = function(self, key)
assert(key == "PatchesAddress", "You didn't call 'Il2cpp'")
return Il2cppBase[key]
end
})
return Il2cpp