Skip to content

Commit

Permalink
Implement bruteforce wzIv AES key
Browse files Browse the repository at this point in the history
  • Loading branch information
lastbattle committed Apr 23, 2020
1 parent aa01960 commit 744a6ae
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 145 deletions.
418 changes: 275 additions & 143 deletions HaRepacker/GUI/MainForm.cs

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions HaRepacker/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions HaRepacker/Properties/Resources.ja.resx
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,7 @@
<data name="EncTypeCustom" xml:space="preserve">
<value>カスタム暗号化を使用する</value>
</data>
<data name="EncTypeGenerate" xml:space="preserve">
<value>Bruteforce AESキー(生成)</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HaRepacker/Properties/Resources.ko.resx
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,7 @@
<data name="EncTypeCustom" xml:space="preserve">
<value>Use custom encryption key</value>
</data>
<data name="EncTypeGenerate" xml:space="preserve">
<value>Bruteforce AES key (Generate)</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HaRepacker/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,7 @@ Click "Yes" to download the new version.</value>
<data name="EncTypeCustom" xml:space="preserve">
<value>Use custom encryption key</value>
</data>
<data name="EncTypeGenerate" xml:space="preserve">
<value>Bruteforce AES key (Generate)</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HaRepacker/Properties/Resources.zh-CHS.resx
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,7 @@
<data name="EncTypeCustom" xml:space="preserve">
<value>使用自定义编码</value>
</data>
<data name="EncTypeGenerate" xml:space="preserve">
<value>Bruteforce AES密钥(生成)</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HaRepacker/Properties/Resources.zh-CHT.resx
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,7 @@
<data name="EncTypeCustom" xml:space="preserve">
<value>使用自定義編碼</value>
</data>
<data name="EncTypeGenerate" xml:space="preserve">
<value>Bruteforce AES密鑰(生成)</value>
</data>
</root>
1 change: 1 addition & 0 deletions HaRepacker/WzFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public WzFile LoadWzFile(string path)
{
short fileVersion = -1;
bool isList = WzTool.IsListFile(path);

return LoadWzFile(path, WzTool.DetectMapleVersion(path, out fileVersion), fileVersion);
}

Expand Down
1 change: 1 addition & 0 deletions MapleLib/MapleLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Compile Include="Configuration\UserSettings.cs" />
<Compile Include="Helpers\ByteUtils.cs" />
<Compile Include="Helpers\ErrorLogger.cs" />
<Compile Include="PacketLib\HexTool.cs" />
<Compile Include="WzDataReader.cs" />
<Compile Include="WzLib\Util\WzMutableKey.cs" />
<Compile Include="WzLib\WzExtended.cs" />
Expand Down
80 changes: 80 additions & 0 deletions MapleLib/PacketLib/HexTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MapleLib.PacketLib
{
public static class HexTool
{
private static char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

/// <summary>
/// Converts a byte value to readable hex representation
/// </summary>
/// <param name="byteValue"></param>
/// <returns></returns>
public static String ToString(byte byteValue)
{
int tmp = byteValue << 8;
char[] retstr = new char[] { HEX[(tmp >> 12) & 0x0F], HEX[(tmp >> 8) & 0x0F] };
return new string(retstr);
}

/// <summary>
/// Converts an array of bytes to readable hex representation
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static String ToString(byte[] bytes)
{
StringBuilder hexed = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
hexed.Append(ToString(bytes[i]));
hexed.Append(' ');
}
return hexed.ToString();
}


/// <summary>
/// Converts an array of bytes to readable hex representation
/// Extension method for PacketWriter
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static String ToString(this PacketReader reader)
{
byte[] bytes = reader.ToArray();

StringBuilder hexed = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
hexed.Append(ToString(bytes[i]));
hexed.Append(' ');
}
return hexed.ToString();
}

/// <summary>
/// Converts an array of bytes to readable hex representation
/// Extension method for PacketWriter
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static String ToString(this PacketWriter writer)
{
byte[] bytes = writer.ToArray();

StringBuilder hexed = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
hexed.Append(ToString(bytes[i]));
hexed.Append(' ');
}
return hexed.ToString();
}
}
}
33 changes: 33 additions & 0 deletions MapleLib/WzLib/Util/WzTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using HaRepacker.Configuration;
using MapleLib.MapleCryptoLib;
Expand Down Expand Up @@ -128,6 +129,9 @@ public static byte[] GetIvByMapleVersion(WzMapleVersion ver)
}
return new byte[4]; // fallback with BMS
}
case WzMapleVersion.GENERATE: // dont fill anything with GENERATE, it is not supposed to load anything
return new byte[4];

case WzMapleVersion.BMS:
case WzMapleVersion.CLASSIC:
default:
Expand All @@ -144,6 +148,35 @@ private static int GetRecognizedCharacters(string source)
return result;
}


/// <summary>
/// Attempts to bruteforce the WzKey with a given WZ file
/// </summary>
/// <param name="wzPath"></param>
/// <param name="wzIvKey"></param>
/// <returns>The probability. Normalized to 100</returns>
public static bool TryBruteforcingWzIVKey(string wzPath, byte[] wzIvKey)
{
using (WzFile wzf = new WzFile(wzPath, wzIvKey))
{
string parseErrorMessage = string.Empty;
bool parsedSuccessfully = wzf.ParseWzFile(out parseErrorMessage);
if (!parsedSuccessfully)
{
wzf.Dispose();
return false;
}
if (wzf.WzDirectory.WzImages.Count > 0 && wzf.WzDirectory.WzImages[0].Name.EndsWith(".img"))
{
wzf.Dispose();
return true;
}

wzf.Dispose();
}
return false;
}

private static double GetDecryptionSuccessRate(string wzPath, WzMapleVersion encVersion, ref short? version)
{
WzFile wzf;
Expand Down
19 changes: 17 additions & 2 deletions MapleLib/WzLib/WzFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,25 @@ public WzFile(string filePath, short gameVersion, WzMapleVersion version)
if (version == WzMapleVersion.GETFROMZLZ)
{
FileStream zlzStream = File.OpenRead(Path.Combine(Path.GetDirectoryName(filePath), "ZLZ.dll"));
WzIv = Util.WzKeyGenerator.GetIvFromZlz(zlzStream);
this.WzIv = Util.WzKeyGenerator.GetIvFromZlz(zlzStream);
zlzStream.Close();
}
else WzIv = WzTool.GetIvByMapleVersion(version);
else
this.WzIv = WzTool.GetIvByMapleVersion(version);
}

/// <summary>
/// Open a wz file from a file on the disk with a custom WzIv key
/// </summary>
/// <param name="filePath">Path to the wz file</param>
public WzFile(string filePath, byte[] wzIv)
{
name = Path.GetFileName(filePath);
path = filePath;
mapleStoryPatchVersion = -1;
maplepLocalVersion = WzMapleVersion.CUSTOM;

this.WzIv = wzIv;
}

/// <summary>
Expand Down

0 comments on commit 744a6ae

Please sign in to comment.