Skip to content

Commit

Permalink
[HaRepacker] improve keygen compatibility with other ver. of maplesto…
Browse files Browse the repository at this point in the history
…ry (AOB scanning)
  • Loading branch information
lastbattle committed Jul 14, 2024
1 parent 2964e2c commit 158bdd0
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 13 deletions.
69 changes: 62 additions & 7 deletions HaRepacker/GUI/ZLZPacketEncryptionKeyForm.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
using HaSharedLibrary.SystemInterop;
/*Copyright(c) 2024, LastBattle https://github.com/lastbattle/Harepacker-resurrected
Permission 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:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE 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.
*/


using HaSharedLibrary.SystemInterop;
using HaSharedLibrary.Util;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
Expand All @@ -23,8 +47,10 @@ public ZLZPacketEncryptionKeyForm() {
}

#region 64Bit ZLZ

/// <summary>
/// Opens the ZLZ64.DLL file
/// https://forum.ragezone.com/threads/release-packet-encryption-key-for-64-bit-maplestory-in-disassembly.1230765/
/// </summary>
/// <returns></returns>
public bool OpenZLZDllFile_64Bit(string filePath) {
Expand All @@ -43,30 +69,59 @@ public bool OpenZLZDllFile_64Bit(string filePath) {
MessageBox.Show($"Unable to load DLL Library. Kernel32 GetLastError() : {lastError}", "Error");
}
else {
bool bError = true;
IntPtr func_setEncryptionKey = (IntPtr)0;
IntPtr encryptionKeyLoc = (IntPtr) 0;

try {
System.IntPtr functionAddress = (System.IntPtr)(module.ToInt64() + 0x3A40); // sub_0000000180003A40
var currentProcess = Process.GetCurrentProcess().Handle;

var Method = Marshal.GetDelegateForFunctionPointer(functionAddress, typeof(ZLZGenerateKey.GenerateKey)) as ZLZGenerateKey.GenerateKey;
Method();
// The function where the keys are set
// 83 3D AD 26 07 00 00 0F 85 4A 01 00 00 C7 05 D9 // MSEA 234
// 83 3D ED 45 0D 00 00 0F 85 4A 01 00 00 C7 05 39 // KMS
const string SEARCH_PATTERN_SET_ENC = "83 3D ?? ?? ?? 00 00 0F 85 ?? 01 00 00 C7 ?? ?? ?? ?? ?? ??";
func_setEncryptionKey = MemoryScannerHelper.ScanCurrentProcessMemory(currentProcess, module.ToInt64(), module.ToInt64() + 0x10000, SEARCH_PATTERN_SET_ENC);

ShowKey_64Bit(module, 0x74030);
return true;
}
// The location where the keys are set
const string SEARCH_PATTERN_ENC_KEY = "13 00 00 00 52 00 00 00 2A 00 00 00";
encryptionKeyLoc = MemoryScannerHelper.ScanCurrentProcessMemory(currentProcess, module.ToInt64(), module.ToInt64() + 0x100000, SEARCH_PATTERN_ENC_KEY);

//MessageBox.Show(func_setEncryptionKey.ToString("X8"));

if ((long)func_setEncryptionKey != 0) { // function for setting the encKey
//System.IntPtr functionAddress = (System.IntPtr)(module.ToInt64() + 0x3A40); // sub_0000000180003A40

var Method = Marshal.GetDelegateForFunctionPointer((IntPtr)func_setEncryptionKey, typeof(ZLZGenerateKey.GenerateKey)) as ZLZGenerateKey.GenerateKey;
Method();

ShowKey_64Bit(module, (int)(encryptionKeyLoc.ToInt64() - module.ToInt64()));//ShowKey_64Bit(module, 0x74030);
bError = false;
} }
catch (Exception exp) {
MessageBox.Show($"Invalid KeyGen position. This version of MapleStory may be unsupported.\r\n{exp}", "Error");
}
finally {
kernel32.FreeLibrary(module);
}

if (bError) {
MessageBox.Show(String.Format("Invalid KeyGen position. This version of MapleStory may be unsupported.\r\nfunc_setEncryptionKey = {0}, encryptionKeyLoc = {1}", func_setEncryptionKey.ToString("X8"), encryptionKeyLoc.ToString("X8")), "Error");
}
else
return true;
}
return false;
}

/// <summary>
/// Display the Aes key used for the maplestory encryption.
/// https://forum.ragezone.com/threads/release-packet-encryption-key-for-64-bit-maplestory-in-disassembly.1230765/
/// </summary>
private void ShowKey_64Bit(IntPtr module, int baseKeyPosition) {
// OdinMS format
//MessageBox.Show("Module address: " + (module).ToString("X8"));
//MessageBox.Show("Show key at : " + (module + baseKeyPosition).ToString("X8"));

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 128; i += 4) {
IntPtr readAddress = (IntPtr)(module.ToInt64() + baseKeyPosition + i);
Expand Down
2 changes: 1 addition & 1 deletion HaRepacker/Properties/Resources.Designer.cs

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

2 changes: 1 addition & 1 deletion HaRepacker/Properties/Resources.ja.resx
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
<value>XMLの保存先を選択</value>
</data>
<data name="SelectWz" xml:space="preserve">
<value>WZファイルを選択</value>
<value>WZ/ZLZ ファイルを選択</value>
</data>
<data name="SelectWzImg" xml:space="preserve">
<value>IMGファイルを選択</value>
Expand Down
2 changes: 1 addition & 1 deletion HaRepacker/Properties/Resources.ko.resx
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
<value>XML을 저장할 위치 선택</value>
</data>
<data name="SelectWz" xml:space="preserve">
<value>WZ 파일 선택</value>
<value>WZ/ZLZ 파일 선택</value>
</data>
<data name="SelectWzImg" xml:space="preserve">
<value>IMG 파일 선택</value>
Expand Down
2 changes: 1 addition & 1 deletion HaRepacker/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ Click "Yes" to download the new version.</value>
<value>Select where to save the XML</value>
</data>
<data name="SelectWz" xml:space="preserve">
<value>Select the WZ File</value>
<value>Select WZ, or ZLZ.dll File</value>
</data>
<data name="SelectWzImg" xml:space="preserve">
<value>Select the IMG files</value>
Expand Down
2 changes: 1 addition & 1 deletion HaRepacker/Properties/Resources.zh-CHS.resx
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
<value>选择保存XML的位置</value>
</data>
<data name="SelectWz" xml:space="preserve">
<value>选择WZ文件</value>
<value>选择WZ/ZLZ文件</value>
</data>
<data name="SelectWzImg" xml:space="preserve">
<value>选择IMG文件</value>
Expand Down
2 changes: 1 addition & 1 deletion HaRepacker/Properties/Resources.zh-CHT.resx
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
<value>選擇儲存XML的位置</value>
</data>
<data name="SelectWz" xml:space="preserve">
<value>選擇WZ檔案</value>
<value>選擇WZ/ZLZ檔案</value>
</data>
<data name="SelectWzImg" xml:space="preserve">
<value>選擇IMG檔案</value>
Expand Down
1 change: 1 addition & 0 deletions HaSharedLibrary/HaSharedLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<Compile Include="SystemInterop\user32.cs" />
<Compile Include="SystemInterop\ZLZGenerateKey.cs" />
<Compile Include="Util\BitmapHelper.cs" />
<Compile Include="Util\MemoryScannerHelper.cs" />
<Compile Include="Util\ScreenDPIUtil.cs" />
<Compile Include="WzSoundResourceStreamer.cs" />
<Compile Include="Wz\WzInfoTools.cs" />
Expand Down
14 changes: 14 additions & 0 deletions HaSharedLibrary/SystemInterop/kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,19 @@ public class kernel32 {

[DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);


public const int PROCESS_VM_READ = 0x0010;

[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);

}
}
102 changes: 102 additions & 0 deletions HaSharedLibrary/Util/MemoryScannerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*Copyright(c) 2024, LastBattle https://github.com/lastbattle/Harepacker-resurrected
Permission 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:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE 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.
*/

using HaSharedLibrary.SystemInterop;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace HaSharedLibrary.Util {

public class MemoryScannerHelper {


/// <summary>
/// Searches the memory for byte array pattern
/// </summary>
/// <param name="processHandle">Process.GetCurrentProcess().Handle</param>
/// <param name="startAddress"></param>
/// <param name="endAddress"></param>
/// <param name="searchPattern">i.e [AC ?? 00 00 EE 5F ??]</param>
/// <returns></returns>
public static IntPtr ScanCurrentProcessMemory(IntPtr processHandle, long startAddress, long endAddress, string searchPattern) {
try {
int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];

for (long currentAddress = startAddress; currentAddress < endAddress; currentAddress += bufferSize) {
IntPtr bytesRead;
if (!kernel32.ReadProcessMemory(processHandle, (IntPtr)currentAddress, buffer, bufferSize, out bytesRead)) {
continue; // Skip if we can't read this memory region
}

int actualBytesRead = bytesRead.ToInt32();

byte?[] pattern = ParseSearchPattern(searchPattern);
int result = SearchPattern(buffer, actualBytesRead, pattern);

if (result != -1) {
return (IntPtr)(currentAddress + result);
}
}

return IntPtr.Zero; // Pattern not found
}
catch (Exception ex) {
Debug.WriteLine($"An error occurred: {ex.Message}");
return IntPtr.Zero;
}
}

/// <summary>
/// Parses an array-of-byte [AOB] search pattern
/// </summary>
/// <param name="pattern">i.e [AC ?? 00 00 EE 5F ??]</param>
/// <returns></returns>
private static byte?[] ParseSearchPattern(string pattern) {
return pattern.Split(' ')
.Select(b => b == "??" ? (byte?)null : Convert.ToByte(b, 16))
.ToArray();
}

private static int SearchPattern(byte[] haystack, int haystackLength, byte?[] needle) {
int needleLength = needle.Length;
for (int i = 0; i <= haystackLength - needleLength; i++) {
bool found = true;
for (int j = 0; j < needleLength; j++) {
if (needle[j].HasValue && haystack[i + j] != needle[j].Value) {
found = false;
break;
}
}
if (found) {
return i;
}
}
return -1;
}
}
}

0 comments on commit 158bdd0

Please sign in to comment.