Skip to content

Commit

Permalink
增加了自定义key的功能(未完善),并且支持json配置
Browse files Browse the repository at this point in the history
  • Loading branch information
SummerofOrange committed Oct 29, 2022
1 parent 16f6d10 commit 926ee50
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 42 deletions.
8 changes: 4 additions & 4 deletions O&Z_IL2CPP_Security/Crypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class CryptHeader
public byte[] Crypted_Header; //加密后Header
public CryptHeader(object Header, IL2CPP_Version ver, long offset)
{
if (ver == IL2CPP_Version.V24_5)
if (ver == IL2CPP_Version.V24_4)
cryptHeader = new CryptedHeader_2019_4_32_f1((MetadataHeader_v24_5)Header);
else if (ver == IL2CPP_Version.V29)
cryptHeader = new CryptedHeader_2021_3_6_f1((MetadataHeader_v29)Header);
else if (ver == IL2CPP_Version.V28)
cryptHeader = new CryptedHeader_2021_3_6_f1((MetadataHeader_v28)Header);
else
throw new Exception("Error!");
o_Header = cryptHeader.GetType().GetMethod("cryptedHeader").Invoke(cryptHeader, null) as byte[];
Expand Down Expand Up @@ -343,7 +343,7 @@ public struct o_Header
public int genericParameterConstraintsSize;
}
public o_Header o_header;
public CryptedHeader_2021_3_6_f1(MetadataHeader_v29 metadataHeader)
public CryptedHeader_2021_3_6_f1(MetadataHeader_v28 metadataHeader)
{
o_header.sanity = 0x5A264F;
o_header.version = 0x00;
Expand Down
33 changes: 33 additions & 0 deletions O&Z_IL2CPP_Security/JsonManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;

namespace O_Z_IL2CPP_Security
{
public class JsonIndex
{
public int key;
public string Version;
}
public class JsonManager
{
public JsonIndex index;
public string path;
public JsonManager(string _path)
{
path = _path;
Read();
}
public void Read()
{
index = JsonMapper.ToObject<JsonIndex>(File.ReadAllText(path));
}
public void Set()
{
File.WriteAllText(path,JsonMapper.ToJson(index));
}
}
}
4 changes: 2 additions & 2 deletions O&Z_IL2CPP_Security/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Metadata(Stream i_metadatastream,Type HeaderType,object Header, IL2CPP_Ve
assembliesOffset = (uint)HeaderType.GetField("assembliesOffset").GetValue(Header);
windowsRuntimeTypeNamesOffset = (uint)HeaderType.GetField("windowsRuntimeTypeNamesOffset").GetValue(Header);
typeDefinitionsOffset = (uint)HeaderType.GetField("typeDefinitionsOffset").GetValue(Header);
if (Version == IL2CPP_Version.V24_5)
if (Version == IL2CPP_Version.V24_4)
{
stringCount = (int)HeaderType.GetField("stringCount").GetValue(Header);
stringLiteralCount = (int)HeaderType.GetField("stringLiteralCount").GetValue(Header);
Expand All @@ -191,7 +191,7 @@ public Metadata(Stream i_metadatastream,Type HeaderType,object Header, IL2CPP_Ve
windowsRuntimeTypeNamesSize = (int)HeaderType.GetField("windowsRuntimeTypeNamesSize").GetValue(Header);
typeDefinitionsCount = (int)HeaderType.GetField("typeDefinitionsCount").GetValue(Header);
}
else if(Version == IL2CPP_Version.V29)
else if(Version == IL2CPP_Version.V28)
{
stringCount = (int)HeaderType.GetField("stringSize").GetValue(Header);
stringLiteralCount = (int)HeaderType.GetField("stringLiteralSize").GetValue(Header);
Expand Down
2 changes: 1 addition & 1 deletion O&Z_IL2CPP_Security/O&Z_IL2CPP_Security.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>CheckVersion</ActiveDebugProfile>
<ActiveDebugProfile>O&amp;Z_IL2CPP_Security</ActiveDebugProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
Expand Down
64 changes: 39 additions & 25 deletions O&Z_IL2CPP_Security/Progarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@
List<byte[]> StringLiteraBytes = new List<byte[]>();
List<byte[]> StringLiteraBytes_Crypted = new List<byte[]>();
string OpenFilePath;
/*
if (!File.Exists("Config.json"))
byte[]? metadata_origin = null;
if(!File.Exists("Config.json"))
{
Json_Config json_Config = new Json_Config();
json_Config.LastOpenFolder = "";
json_Config.LastSaveFolder = "";
json_Config.LastOpenFileName = "";
json_Config.LastSaveFileName = "";
File.WriteAllText("Config.json", JsonMapper.ToJson(json_Config));
Console.WriteLine("Config.json not found!");
Console.WriteLine("正在生成默认配置文件...");
JsonIndex index = new JsonIndex()
{
key = 114514,
Version = "24.4"

};
File.WriteAllText("Config.json", JsonMapper.ToJson(index));
if (File.Exists("Config.json")) Console.WriteLine("已重新生成默认配置文件,..Done!");
}
return;
*/
JsonManager jsonManager = new JsonManager("Config.json");
if (args.Length == 0)
{

Help();
return;
}
else
Expand All @@ -30,7 +33,6 @@
}
Console.WriteLine("O&Z_IL2CPP_Security");
Console.WriteLine("Loading Meatadata:" + OpenFilePath);
byte[]? metadata_origin = null;

switch(args[1])
{
Expand All @@ -52,19 +54,19 @@ void _Crypt()
}
metadata_origin = File.ReadAllBytes(OpenFilePath);
if (!CheckMetadataFile()) return;
Console.WriteLine("Please input your il2cpp version(v24.5/v29):");
string _Read = Console.ReadLine();
switch(_Read)
jsonManager = new JsonManager("Config.json");

switch(jsonManager.index.Version)
{
case "v24.5": ver = IL2CPP_Version.V24_5; break;
case "v29": ver = IL2CPP_Version.V29; break;
case "24.4": ver = IL2CPP_Version.V24_4; break;
case "28": ver = IL2CPP_Version.V28; break;
default: Console.WriteLine("Error!"); return;
}
object Loader;
if (ver == IL2CPP_Version.V24_5)
if (ver == IL2CPP_Version.V24_4)
Loader = new LoadMetadata_v24_5(new MemoryStream(metadata_origin));
else if (ver == IL2CPP_Version.V29)
Loader = new LoadMetadata_v29(new MemoryStream(metadata_origin));
else if (ver == IL2CPP_Version.V28)
Loader = new LoadMetadata_v28(new MemoryStream(metadata_origin));
else
{
Console.WriteLine("Input version Error!");
Expand All @@ -74,7 +76,7 @@ void _Crypt()
StringLiteraBytes = metadata.GetBytesFromStringLiteral(metadata.stringLiterals);
StringLiteraBytes_Crypted = Crypt.Cryptstring(StringLiteraBytes);
byte[] allstring = metadata.GetAllStringFromMeta();
Stream stream = metadata.SetCryptedStreamToMetadata(StringLiteraBytes_Crypted, CryptB(allstring),ver);
Stream stream = metadata.SetCryptedStreamToMetadata(StringLiteraBytes_Crypted, CryptB(allstring,(byte)Tools.CheckNull(jsonManager.index.key), jsonManager.index.key),ver);
byte[] tmp = Tools.StreamToBytes(stream);
File.WriteAllBytes(args[2], tmp);

Expand All @@ -83,6 +85,7 @@ void _Crypt()
void _default()
{
Console.WriteLine("parameter ERROR!");
Help();
return;
}
void _Read()
Expand All @@ -100,7 +103,10 @@ bool CheckMetadataFile()
}
void _Test()
{

jsonManager = new JsonManager("Config.json");
Console.WriteLine("Your Password is: " + jsonManager.index.key);
Console.WriteLine("Your MetadataVersion is: " + jsonManager.index.Version);
Console.WriteLine((byte)Tools.CheckNull(114514));
}
void CheckVersion()
{
Expand All @@ -109,18 +115,19 @@ void CheckVersion()
MetadataCheck metadataCheck = new MetadataCheck(new MemoryStream(metadata_origin));
Console.WriteLine("Your Metadata Version:" + metadataCheck.Version);
}
byte[] decrypt(byte[] b)
byte[] CryptB(byte[] b,byte skip,int key)
{
byte[] result = new byte[b.Length];
for (int i = 0; i < b.Length; i++)
{
if (b[i] != 0x52)
result[i] = (byte)(b[i] ^ 114514);
if (b[i] != 0 && b[i] != skip)
result[i] = (byte)(b[i] ^ key);
else
result[i] = b[i];
}
return result;
}
/*
byte[] CryptB(byte[] b)
{
byte[] result = new byte[b.Length];
Expand All @@ -133,3 +140,10 @@ byte[] CryptB(byte[] b)
}
return result;
}
*/
void Help()
{
Console.WriteLine("O&Z_IL2CPP_Security使用方法:"+"\n");
Console.WriteLine("O&Z_IL2CPP_Security.exe [文件路径] [参数] *[输出路径]");
Console.WriteLine("参数: Crypt:加密 CheckVersion:检查版本");
}
8 changes: 4 additions & 4 deletions O&Z_IL2CPP_Security/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"commandName": "Project",
"commandLineArgs": "\"global-metadata.dat\" Crypt \"global-metadata.dat.crypted\""
},
"Asset": {
"commandName": "Project",
"commandLineArgs": "\"test.ab\" Test \"test.ab.encrypted\""
},
"CheckVersion": {
"commandName": "Project",
"commandLineArgs": "\"global-metadata.dat\" CheckVersion"
},
"Test": {
"commandName": "Project",
"commandLineArgs": "\"global-metadata.dat\" Test"
}
}
}
11 changes: 11 additions & 0 deletions O&Z_IL2CPP_Security/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,16 @@ public static byte[] StreamToBytes(Stream stream)
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
public static int CheckNull(int Checker)
{
for(byte i = byte.MinValue; i < byte.MaxValue;i++)
{
if ((byte)(i ^ Checker) == 0x00)
{
return i;
}
}
return 0x100;
}
}
}
12 changes: 6 additions & 6 deletions O&Z_IL2CPP_Security/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace O_Z_IL2CPP_Security
{
public enum IL2CPP_Version
{
V24_5,
V29
V24_4,
V28
};
public struct MetadataHeader_v24_5
{
Expand Down Expand Up @@ -82,7 +82,7 @@ public struct MetadataHeader_v24_5
public int exportedTypeDefinitionsOffset; // TypeDefinitionIndex
public int exportedTypeDefinitionsCount;
}
public struct MetadataHeader_v29
public struct MetadataHeader_v28
{
public uint sanity;
public int version;
Expand Down Expand Up @@ -227,11 +227,11 @@ public LoadMetadata_v24_5(Stream i_metadatastream)
metadatastream.Position = 0;
}
}
public class LoadMetadata_v29
public class LoadMetadata_v28
{
public Stream metadatastream;
public MetadataHeader_v29 Header;
public LoadMetadata_v29(Stream i_metadatastream)
public MetadataHeader_v28 Header;
public LoadMetadata_v28(Stream i_metadatastream)
{
metadatastream = i_metadatastream;
BinaryReader Reader = new BinaryReader(metadatastream);
Expand Down

0 comments on commit 926ee50

Please sign in to comment.