Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AAEmu.Commons/Cryptography/ConnectionKeychain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class ConnectionKeychain
public byte[] AesKey { get; set; }
public byte[] IV { get; set; }
public uint XorKey { get; set; }
public uint XorKeyConstant1 { get; set; }
public uint XorKeyConstant2 { get; set; }
public byte SCMessageCount { get; set; }
public byte CSMessageCount { get; set; }
public byte CSOffsetSequence { get; set; }
Expand All @@ -27,6 +29,8 @@ public ConnectionKeychain(uint connId, RSACryptoServiceProvider kp)
AesKey = new byte[16];
IV = new byte[16];
XorKey = 0;
XorKeyConstant1 = 0;
XorKeyConstant2 = 0;
}
}
}
93 changes: 91 additions & 2 deletions AAEmu.Commons/Cryptography/EncryptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Security.Cryptography;
using System.Text;

using AAEmu.Commons.IO;
using AAEmu.Commons.Network;
using AAEmu.Commons.Utils;

Expand All @@ -19,8 +20,13 @@
public class EncryptionManager : Singleton<EncryptionManager>
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static readonly int DwKeySize = 1024;

Check warning on line 23 in AAEmu.Commons/Cryptography/EncryptionManager.cs

View workflow job for this annotation

GitHub Actions / build

The field 'EncryptionManager.DwKeySize' is assigned but its value is never used

Check warning on line 23 in AAEmu.Commons/Cryptography/EncryptionManager.cs

View workflow job for this annotation

GitHub Actions / build

The field 'EncryptionManager.DwKeySize' is assigned but its value is never used
private Dictionary<ulong, ConnectionKeychain> ConnectionKeys { get; set; } //Dictionary of valid keys bound to account Id and connection Id
public static bool needNewkey2;
public static bool needNewkey1;
private static string XorKeyValueFilePath;
private static Random rnd = new();


public void Load()
{
Expand Down Expand Up @@ -74,6 +80,33 @@
keys.AesKey = keys.RsaKeyPair.Decrypt(aesKeyEncrypted, false);
keys.RecievedKeys = true;
Logger.Warn("AES: {0} XOR: {1}", Helpers.ByteArrayToString(keys.AesKey), keys.XorKey);

// для автоматического подбора констант
LoadXorKeyConstant(keys);
}

private static void LoadXorKeyConstant(ConnectionKeychain keys)
{
var worldPath = Path.Combine(FileManager.AppPath, "Configurations");
XorKeyValueFilePath = Path.Combine(worldPath, "xorKeyValue.txt");
using var reader = new StreamReader(XorKeyValueFilePath);
while (!reader.EndOfStream)
{
var xorKeyValueLine1 = reader.ReadLine();
var xorKeyValue1 = reader.ReadLine();
if (xorKeyValueLine1 == "XorKeyConstant1:")
{
// сохраняем пакеты в список пакетов
keys.XorKeyConstant1 = Convert.ToUInt32(xorKeyValue1, 16);
}
var xorKeyValueLine2 = reader.ReadLine();
var xorKeyValue2 = reader.ReadLine();
if (xorKeyValueLine2 == "XorKeyConstant2:")
{
// сохраняем пакеты в список пакетов
keys.XorKeyConstant2 = Convert.ToUInt32(xorKeyValue2, 16);
}
}
}

public byte GetSCMessageCount(uint connectionId, ulong accountId)
Expand Down Expand Up @@ -225,6 +258,62 @@

public static byte[] DecodeXor(byte[] bodyPacket, uint xorKey, ConnectionKeychain keys)
{
/*
* логика подбора такая:
* сначала подбираем первую константу для имеющейся второй
* если первая 0xFF, то меняем вторую на новую и начинаем подбор первой константы с 0x00
*/
var dirty = false;
// подбираем константы шифрации
if (keys.XorKeyConstant1 > 0x75A02480)
{
keys.XorKeyConstant1 = 0x75A02450;
dirty = true;
needNewkey2 = true;
}
if (keys.XorKeyConstant1 == 0 || keys.XorKeyConstant2 == 0)
{
LoadXorKeyConstant(keys);

//keys.XorKeyConstant2 = 0x00a3af00;
//dirty = true;
//needNewkey2 = true;
}

if (needNewkey1)
{
needNewkey1 = false;
// заменим первую константу
keys.XorKeyConstant1++;
if (keys.XorKeyConstant1 > 0x75A02480)
{
keys.XorKeyConstant1 = 0x75A02450;
needNewkey2 = true;
}
dirty = true;
}
if (needNewkey2)
{
needNewkey2 = false;
// заменим вторую константу
var tuneL = (byte)rnd.Next(0x01, 0xFF);
var tuneR = (byte)rnd.Next(0x01, 0xFF);
// Исходное uint число с заполнителями NN
var result = keys.XorKeyConstant2 & 0x00FFFF00;
result |= (uint)tuneL << 24; // Вставляем tuneL в старший байт
result |= tuneR; // Вставляем tuneR в младший байт
keys.XorKeyConstant2 = result; // Заменяем байты на указанные значения
dirty = true;
}
if (dirty)
{
using var writer = new StreamWriter(XorKeyValueFilePath, false);
writer.WriteLine("XorKeyConstant1:");
writer.WriteLine(keys.XorKeyConstant1.ToString("X8"));
writer.WriteLine("XorKeyConstant2:");
writer.WriteLine(keys.XorKeyConstant2.ToString("X8"));
}

// +-Hash начало блока для DecodeXOR, где второе число, в данном случае F(16 байт)-реальная длина данных в пакете, к примеру A(10 байт)-реальная длина данных в пакете
// | +-начало блока для DecodeAES
// V V
Expand All @@ -251,8 +340,8 @@
//var cry = mul ^ ((uint)MakeSeq(keys) + 0x75a024a4) ^ 0xc3903b6a; // 3.0.3.0 archerage.to
//var cry = mul ^ ((uint)MakeSeq(keys) + 0x75a024c4) ^ 0x2d3c9291; // 3.0.4.2 AAClassic
//var cry = mul ^ ((uint)MakeSeq(keys) + 0x75a02403) ^ 0x47a3afc6; // 5.0.7.0 AAFree - работает, но плохо
var cry = mul ^ ((uint)MakeSeq(keys) + 0x75a02476) ^ 0x45a3af75; // 5.0.7.0 AAFree - работает, довольно хорошо
var cry = mul ^ ((uint)MakeSeq(keys) + keys.XorKeyConstant1) ^ keys.XorKeyConstant2; // 5.0.7.0 AAFree - работает, довольно хорошо

var seq = keys.CSOffsetSequence;
var offset = 4;
if (seq != 0)
Expand Down
1 change: 0 additions & 1 deletion AAEmu.Commons/Network/PacketStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ public long[] ReadPiscW(int hcount)

return values;
}

public (float x, float y, float z) ReadPosition()
{
var position = ReadBytes(9);
Expand Down
4 changes: 4 additions & 0 deletions AAEmu.Game/AAEmu.Game.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
</ItemGroup>

<ItemGroup>
<Content Include="Configurations\xorKeyValue.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Configurations\AccessLevels.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -59,6 +62,7 @@
<Compile Remove="Models\Tasks\Telescopes\**" />
<EmbeddedResource Remove="Models\Tasks\Telescopes\**" />
<None Remove="Models\Tasks\Telescopes\**" />
<Compile Remove="Core\Packets\C2G\CSTakeAttachmentSequentially.cs" />
<Compile Update="Scripts\Commands\DeSpawnAll.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
Expand Down
4 changes: 4 additions & 0 deletions AAEmu.Game/Configurations/xorKeyValue.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
XorKeyConstant1:
75A0247F
XorKeyConstant2:
E7A3AF60
Loading
Loading