Skip to content

Commit

Permalink
Sync some updates from the main project
Browse files Browse the repository at this point in the history
  • Loading branch information
1582421598 committed Jun 16, 2022
1 parent 01ec5a6 commit 731c402
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 35 deletions.
9 changes: 9 additions & 0 deletions Il2CppDumper/Il2Cpp/Il2Cpp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ protected bool AutoPlusInit(ulong codeRegistration, ulong metadataRegistration)
if (Version >= 24.2)
{
pCodeRegistration = MapVATR<Il2CppCodeRegistration>(codeRegistration);
if (Version == 29)
{
if (pCodeRegistration.genericMethodPointersCount > 0x50000) //TODO
{
Version = 29.1;
codeRegistration -= PointerSize * 2;
Console.WriteLine($"Change il2cpp version to: {Version}");
}
}
if (Version == 27)
{
if (pCodeRegistration.reversePInvokeWrapperCount > 0x50000) //TODO
Expand Down
6 changes: 5 additions & 1 deletion Il2CppDumper/Il2Cpp/Il2CppClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ public class Il2CppCodeRegistration
public ulong invokerPointers;
public long customAttributeCount;
public ulong customAttributeGenerators;
public long unresolvedVirtualCallCount;
public long unresolvedVirtualCallCount; //29.1 unresolvedIndirectCallCount;
public ulong unresolvedVirtualCallPointers;
[Version(Min = 29.1)]
public ulong unresolvedInstanceCallPointers;
[Version(Min = 29.1)]
public ulong unresolvedStaticCallPointers;
public ulong interopDataCount;
public ulong interopData;
public ulong windowsRuntimeFactoryCount;
Expand Down
96 changes: 96 additions & 0 deletions Il2CppDumper/Il2CppBinaryNinja/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from binaryninja import *
from os.path import exists

def get_addr(bv: BinaryView, addr: int):
imageBase = bv.start
return imageBase + addr

class Il2CppProcessTask(BackgroundTaskThread):
def __init__(self, bv: BinaryView, script_path: str,
header_path: str):
BackgroundTaskThread.__init__(self, "Il2Cpp start", True)
self.bv = bv
self.script_path = script_path
self.header_path = header_path
self.has_types = False

def process_header(self):
self.progress = "Il2Cpp types (1/3)"
with open(self.header_path) as f:
result = self.bv.parse_types_from_string(f.read())
length = len(result.types)
i = 0
for name in result.types:
i += 1
if i % 100 == 0:
percent = i / length * 100
self.progress = f"Il2Cpp types: {percent:.2f}%"
if self.bv.get_type_by_name(name):
continue
self.bv.define_user_type(name, result.types[name])

def process_methods(self, data: dict):
self.progress = f"Il2Cpp methods (2/3)"
scriptMethods = data["ScriptMethod"]
length = len(scriptMethods)
i = 0
for scriptMethod in scriptMethods:
if self.cancelled:
self.progress = "Il2Cpp cancelled, aborting"
return
i += 1
if i % 100 == 0:
percent = i / length * 100
self.progress = f"Il2Cpp methods: {percent:.2f}%"
addr = get_addr(self.bv, scriptMethod["Address"])
name = scriptMethod["Name"].replace("$", "_").replace(".", "_")
signature = scriptMethod["Signature"]
func = self.bv.get_function_at(addr)
if func != None:
if func.name == name:
continue
if self.has_types:
func.function_type = signature
else:
func.name = scriptMethod["Name"]

def process_strings(self, data: dict):
self.progress = "Il2Cpp strings (3/3)"
scriptStrings = data["ScriptString"]
i = 0
for scriptString in scriptStrings:
i += 1
if self.cancelled:
self.progress = "Il2Cpp cancelled, aborting"
return
addr = get_addr(self.bv, scriptString["Address"])
value = scriptString["Value"]
var = self.bv.get_data_var_at(addr)
if var != None:
var.name = f"StringLiteral_{i}"
self.bv.set_comment_at(addr, value)

def run(self):
if exists(self.header_path):
self.process_header()
else:
log_warn("Header file not found")
if self.bv.get_type_by_name("Il2CppClass"):
self.has_types = True
data = json.loads(open(self.script_path, 'rb').read().decode('utf-8'))
if "ScriptMethod" in data:
self.process_methods(data)
if "ScriptString" in data:
self.process_strings(data)

def process(bv: BinaryView):
scriptDialog = OpenFileNameField("Select script.json", "script.json", "script.json")
headerDialog = OpenFileNameField("Select il2cpp_binja.h", "il2cpp_binja.h", "il2cpp_binja.h")
if not get_form_input([scriptDialog, headerDialog], "script.json from Il2CppDumper"):
return log_error("File not selected, try again!")
if not exists(scriptDialog.result):
return log_error("File not found, try again!")
task = Il2CppProcessTask(bv, scriptDialog.result, headerDialog.result)
task.start()

PluginCommand.register("Il2CppDumper", "Process file", process)
33 changes: 33 additions & 0 deletions Il2CppDumper/Il2CppBinaryNinja/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"pluginmetadataversion": 2,
"name": "Il2CppDumper",
"type": [
"core",
"ui",
"binaryview"
],
"api": [
"python3"
],
"description": "Add Il2Cpp structs and method signatures",
"longdescription": "",
"license": {
"name": "MIT",
"text": "Copyright (c) 2022 Il2CppDumper contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
},
"platforms": [
"Darwin",
"Linux",
"Windows"
],
"installinstructions": {
"Darwin": "Install Il2CppDumper",
"Linux": "Install Il2CppDumper",
"Windows": "Install Il2CppDumper"
},
"dependencies": {
},
"version": "1.0.0",
"author": "Il2CppDumper contributors",
"minimumbinaryninjaversion": 3164
}
1 change: 1 addition & 0 deletions Il2CppDumper/Outputs/StructGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ public void WriteScript(string outputDir)
sb.Append(HeaderConstants.HeaderV27);
break;
case 29:
case 29.1:
sb.Append(HeaderConstants.HeaderV29);
break;
default:
Expand Down
9 changes: 8 additions & 1 deletion Il2CppDumper/Utils/DummyAssemblyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,14 @@ private CustomAttributeArgument CreateCustomAttributeArgument(TypeReference type
var val = blobValue.Value;
if (typeReference.FullName == "System.Object")
{
val = new CustomAttributeArgument(GetBlobValueTypeReference(blobValue, memberReference), val);
if (blobValue.il2CppTypeEnum == Il2CppTypeEnum.IL2CPP_TYPE_IL2CPP_TYPE_INDEX)
{
val = new CustomAttributeArgument(memberReference.Module.ImportReference(typeof(Type)), GetTypeReference(memberReference, (Il2CppType)val));
}
else
{
val = new CustomAttributeArgument(GetBlobValueTypeReference(blobValue, memberReference), val);
}
}
else if (val == null)
{
Expand Down
6 changes: 4 additions & 2 deletions Il2CppDumper/Utils/SectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ private ulong FindMetadataRegistrationOld()
foreach (var section in data)
{
il2Cpp.Position = section.offset;
while (il2Cpp.Position < section.offsetEnd - il2Cpp.PointerSize)
var end = Math.Min(section.offsetEnd, il2Cpp.Length) - il2Cpp.PointerSize;
while (il2Cpp.Position < end)
{
var addr = il2Cpp.Position;
if (il2Cpp.ReadIntPtr() == typeDefinitionsCount)
Expand Down Expand Up @@ -284,7 +285,8 @@ private ulong FindMetadataRegistrationV21()
foreach (var section in data)
{
il2Cpp.Position = section.offset;
while (il2Cpp.Position < section.offsetEnd - il2Cpp.PointerSize)
var end = Math.Min(section.offsetEnd, il2Cpp.Length) - il2Cpp.PointerSize;
while (il2Cpp.Position < end)
{
var addr = il2Cpp.Position;
if (il2Cpp.ReadIntPtr() == typeDefinitionsCount)
Expand Down
27 changes: 0 additions & 27 deletions Il2CppDumper/binaryninja3_py3.py

This file was deleted.

41 changes: 41 additions & 0 deletions Il2CppDumper/il2cpp_header_to_binja.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import re

data = open("./il2cpp.h").read()

builtin = ["void", "intptr_t", "uint32_t", "uint16_t", "int32_t", "uint8_t", "bool",
"int64_t", "uint64_t", "double", "int16_t", "int8_t", "float", "uintptr_t",
"const", "union", "{", "};", "il2cpp_array_size_t", "il2cpp_array_lower_bound_t",
"struct", "Il2CppMethodPointer"]
structs = []
notfound = []
header = ""

for line in data.splitlines():
if line.startswith("struct") or line.startswith("union"):
struct = line.split()[1]
if struct.endswith(";"):
struct = struct[:-1]
structs.append(struct)
if line.startswith("\t"):
struct = line[1:].split()[0]
if struct == "struct":
struct = line[1:].split()[1]
if struct.endswith("*"):
struct = struct[:-1]
if struct.endswith("*"):
struct = struct[:-1]
if struct in builtin:
continue
if struct not in structs and struct not in notfound:
notfound.append(struct)
for struct in notfound:
header += f"struct {struct};" + "\n"
to_replace = re.findall("struct (.*) {\n};", data)
for item in to_replace:
data = data.replace("struct "+item+" {\n};", "")
data = data.replace("\t"+item.split()[0]+" ", "\tvoid *")
data = data.replace("\t struct "+item.split()[0]+" ", "\t void *")
data = re.sub(r": (\w+) {", r"{\n\t\1 super;", data)
with open("./il2cpp_binja.h", "w") as f:
f.write(header)
f.write(data)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Unity il2cpp reverse engineer
* Complete DLL restore (except code), can be used to extract `MonoBehaviour` and `MonoScript`
* Supports ELF, ELF64, Mach-O, PE, NSO and WASM format
* Supports Unity 5.3 - 2021.3
* Supports generate IDA and Ghidra scripts to help IDA and Ghidra better analyze il2cpp files
* Supports generate IDA, Ghidra and Binary Ninja scripts to help them better analyze il2cpp files
* Supports generate structures header file
* Supports Android memory dumped `libil2cpp.so` file to bypass protection
* Support bypassing simple PE protection
Expand Down Expand Up @@ -58,7 +58,7 @@ structure information header file

For Ghidra

#### binaryninja3_py3.py
#### Il2CppBinaryNinja

For BinaryNinja

Expand All @@ -68,7 +68,7 @@ For Ghidra, work with [ghidra-wasm-plugin](https://github.com/nneonneo/ghidra-wa

#### script.json

For ida.py and ghidra.py
For ida.py, ghidra.py and Il2CppBinaryNinja

#### stringliteral.json

Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Il2CppDumper.exe <executable-file> <global-metadata> <output-directory>

用于Ghidra

#### binaryninja3_py3.py
#### Il2CppBinaryNinja

用于BinaryNinja

Expand Down

0 comments on commit 731c402

Please sign in to comment.