L2ClientDat decoder.

This commit is contained in:
MobiusDev
2018-05-13 13:35:03 +00:00
parent 574f600afa
commit 836beafd0f
211 changed files with 26666 additions and 0 deletions

View File

@ -0,0 +1,140 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.clientcryptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import com.l2jmobius.clientcryptor.crypt.DatCrypter;
import com.l2jmobius.config.ConfigDebug;
public class DatFile extends File
{
private ByteBuffer _buff;
public DatFile(String pathname)
{
super(pathname);
}
public ByteBuffer getBuff()
{
return _buff;
}
public void decrypt(DatCrypter crypter) throws IOException
{
loadInfo(crypter);
try
{
try (FileInputStream fis = new FileInputStream(this);)
{
crypter.unlock();
crypter.aquire();
fis.skip(28L);
byte[] buff = new byte[crypter.getChunkSize(fis.available())];
for (int len = fis.available() - crypter.getSkipSize(); len > 0; len -= fis.read(buff))
{
crypter.update(buff);
if (!crypter.isLock())
{
continue;
}
}
if (crypter.isLock())
{
_buff = null;
return;
}
_buff = crypter.decryptResult();
crypter.release();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void encrypt(byte[] buff, String file, DatCrypter crypter) throws IOException
{
crypter.unlock();
crypter.aquire();
FileOutputStream os = new FileOutputStream(file, false);
String header = "Lineage2Ver" + crypter.getCode();
os.write(header.getBytes("UTF-16LE"));
crypter.update(buff);
byte[] res = crypter.encryptResult().array();
os.write(res);
if (ConfigDebug.DAT_ADD_END_BYTES)
{
byte[] endBytes = new byte[20];
endBytes[19] = 100;
os.write(endBytes);
}
os.close();
crypter.release();
}
@SuppressWarnings("resource")
private void loadInfo(DatCrypter crypter) throws IOException
{
if (!exists() || !canRead())
{
throw new IOException("Can not read the dat file");
}
FileInputStream fis = new FileInputStream(this);
if (fis.available() < 28)
{
throw new IOException("Can not read the dat file : too small");
}
byte[] head = new byte[28];
fis.read(head);
String header = new String(head, "UTF-16LE");
if (!header.startsWith("Lineage2Ver"))
{
throw new IOException("Can not read the dat file : wrong header");
}
if (header.endsWith("111") || header.endsWith("120"))
{
return;
}
if (header.endsWith("211") || header.endsWith("212"))
{
return;
}
if (header.endsWith("311"))
{
return;
}
if (header.endsWith("411") || header.endsWith("412") || header.endsWith("413") || header.endsWith("414"))
{
if (fis.available() < 20)
{
throw new IOException("Can not read the dat file : too small");
}
}
else
{
throw new IOException("Can not read the dat file : unknown header : '" + header + "'");
}
fis.close();
}
}

View File

@ -0,0 +1,78 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.clientcryptor.crypt;
import java.nio.ByteBuffer;
public class BlowFishDatCrypter extends DatCrypter
{
private boolean encrypt = false;
private final BlowfishEngine blowfish = new BlowfishEngine();
public BlowFishDatCrypter(int code, String key, boolean deCrypt)
{
super(code);
encrypt = !deCrypt;
blowfish.init(encrypt, key.getBytes());
}
@Override
public ByteBuffer decryptResult()
{
return null;
}
@Override
public ByteBuffer encryptResult()
{
return null;
}
@Override
public void update(byte[] b)
{
}
@Override
public int getChunkSize(int available)
{
return available;
}
@Override
public int getSkipSize()
{
return 0;
}
@Override
public boolean isLock()
{
return false;
}
@Override
public boolean isEncrypt()
{
return encrypt;
}
@Override
public void unlock()
{
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,122 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.clientcryptor.crypt;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DESDatCrypter extends DatCrypter
{
private ByteArrayOutputStream _result;
private final boolean encrypt;
private final Cipher _cipher;
public DESDatCrypter(int code, String sKey, boolean deCrypt) throws Exception
{
super(code);
encrypt = !deCrypt;
byte[] key = sKey.getBytes();
byte[] keyXor = new byte[key.length];
for (int i = 0; i < key.length; ++i)
{
byte[] arrby = keyXor;
int n = i % 8;
arrby[n] = (byte) (arrby[n] ^ key[i]);
}
DESKeySpec dks = new DESKeySpec(keyXor);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
_cipher = Cipher.getInstance("DES/ECB/NoPadding");
_cipher.init(deCrypt ? 2 : 1, desKey);
}
@Override
public ByteBuffer decryptResult()
{
return ByteBuffer.wrap(_result.toByteArray());
}
@Override
public ByteBuffer encryptResult()
{
return ByteBuffer.wrap(_result.toByteArray());
}
@Override
public void update(byte[] bArray)
{
try
{
if (!encrypt)
{
int size;
_result = new ByteArrayOutputStream(bArray.length);
byte[] bytes = new byte[8];
for (int position = 0; position < bArray.length; position += size)
{
size = Math.min(8, bArray.length - position);
System.arraycopy(bArray, position, bytes, 0, size);
_result.write(size == 8 ? _cipher.doFinal(bytes) : bytes, 0, size);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public int getChunkSize(int available)
{
return available;
}
@Override
public int getSkipSize()
{
return 0;
}
@Override
public boolean isLock()
{
return false;
}
@Override
public boolean isEncrypt()
{
return encrypt;
}
@Override
public void unlock()
{
}
@Override
public void aquire()
{
super.aquire();
}
}

View File

@ -0,0 +1,92 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.clientcryptor.crypt;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
public abstract class DatCrypter
{
private final ReentrantLock _lock = new ReentrantLock(true);
private final int code;
private boolean useStructure;
private final List<String> fileEndNames = new ArrayList<>();
public DatCrypter(int code)
{
this.code = code;
}
public abstract void update(byte[] var1);
public abstract ByteBuffer decryptResult();
public abstract ByteBuffer encryptResult();
public abstract int getChunkSize(int var1);
public abstract int getSkipSize();
public abstract boolean isLock();
public boolean checkAquired()
{
return _lock.isHeldByCurrentThread();
}
public void aquire()
{
_lock.lock();
}
public void release()
{
_lock.unlock();
}
public abstract boolean isEncrypt();
public abstract void unlock();
public int getCode()
{
return code;
}
public void addFileExtension(String n)
{
fileEndNames.addAll(Arrays.asList(n.split(";")));
}
public boolean checkFileExtension(String n)
{
return n.contains(".") && fileEndNames.contains(n.split("\\.")[1]);
}
public boolean isUseStructure()
{
return useStructure;
}
public void setUseStructure(boolean useStructure)
{
this.useStructure = useStructure;
}
}

View File

@ -0,0 +1,231 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.clientcryptor.crypt;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Arrays;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import javax.crypto.Cipher;
import com.l2jmobius.util.DebugUtil;
import com.l2jmobius.util.Util;
public class RSADatCrypter extends DatCrypter
{
private Cipher _cipher;
private ByteArrayOutputStream _result;
private boolean encrypt = false;
private boolean _errorLock = false;
public RSADatCrypter(int code, String modulus, String exp, boolean deCrypt)
{
super(code);
try
{
_cipher = Cipher.getInstance("RSA/ECB/nopadding");
if (deCrypt)
{
RSAPublicKeySpec keyspec = new RSAPublicKeySpec(new BigInteger(modulus, 16), new BigInteger(exp, 16));
RSAPublicKey rsaKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(keyspec);
_cipher.init(2, rsaKey);
}
else
{
encrypt = true;
RSAPrivateKeySpec keyspec = new RSAPrivateKeySpec(new BigInteger(modulus, 16), new BigInteger(exp, 16));
RSAPrivateKey rsaKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(keyspec);
_cipher.init(1, rsaKey);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public ByteBuffer decryptResult()
{
if (!checkAquired())
{
throw new IllegalStateException("Dont even think about using a DatCrypter that you didnt aquired");
}
byte[] compressed = _result.toByteArray();
int inflatedSize = compressed[0] & 255;
inflatedSize += (compressed[1] << 8) & 65280;
inflatedSize += (compressed[2] << 16) & 16711680;
inflatedSize += (compressed[3] << 24) & -16777216;
ByteArrayInputStream bais = new ByteArrayInputStream(compressed, 4, compressed.length - 4);
InflaterInputStream iis = new InflaterInputStream(bais, new Inflater());
ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
byte[] inflatedResult = new byte[128];
try
{
int len;
while ((len = iis.read(inflatedResult)) > 0)
{
baos.write(inflatedResult, 0, len);
}
}
catch (IOException e)
{
e.printStackTrace();
}
if (baos.size() != inflatedSize)
{
DebugUtil.getLogger().error("[RSADatCrypter] Hum inflated result doesnt have the expected length..(" + baos.size() + "!=" + inflatedSize + ")");
}
return ByteBuffer.wrap(baos.toByteArray());
}
@Override
public ByteBuffer encryptResult()
{
if (!checkAquired())
{
throw new IllegalStateException("Dont even think about using a DatCrypter that you didnt aquired");
}
ByteArrayOutputStream result = new ByteArrayOutputStream();
try
{
int len;
ByteArrayInputStream input = new ByteArrayInputStream(_result.toByteArray());
byte[] buffer = new byte[124];
byte[] block = new byte[128];
while ((len = input.read(buffer)) > 0)
{
Arrays.fill(block, (byte) 0);
block[0] = (byte) ((len >> 24) & 255);
block[1] = (byte) ((len >> 16) & 255);
block[2] = (byte) ((len >> 8) & 255);
block[3] = (byte) (len & 255);
System.arraycopy(buffer, 0, block, 128 - len - ((124 - len) % 4), len);
result.write(_cipher.doFinal(block));
}
}
catch (Exception e)
{
e.printStackTrace();
}
return ByteBuffer.wrap(result.toByteArray());
}
@Override
public void update(byte[] b)
{
if (!checkAquired())
{
throw new IllegalStateException("Dont even think about using a DatCrypter that you didnt aquired");
}
try
{
if (!encrypt)
{
byte[] chunk = _cipher.doFinal(b);
int size = chunk[3];
size += (chunk[2] << 8) & 65280;
size += (chunk[1] << 16) & 16711680;
int pad = (-size & 1) + (-(size += (chunk[0] << 24) & -16777216) & 2);
DebugUtil.debug("Size:" + size + " pad:" + pad);
if (size > 128)
{
_errorLock = true;
return;
}
_result.write(chunk, 128 - size - pad, size);
DebugUtil.debug("--- BLOCK:\n" + Util.printData(chunk) + "-----");
}
else
{
try
{
ByteArrayOutputStream s = new ByteArrayOutputStream(b.length);
DeflaterOutputStream dos = new DeflaterOutputStream(s, new Deflater());
dos.write(b);
dos.finish();
dos.close();
int l = b.length;
_result = new ByteArrayOutputStream(10 + s.toByteArray().length);
_result.write(l & 255);
_result.write((l & 65280) >> 8);
_result.write((l & 16711680) >> 16);
_result.write((l & -16777216) >> 24);
_result.write(s.toByteArray());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
catch (Exception e)
{
e.printStackTrace();
_errorLock = true;
}
}
@Override
public void aquire()
{
super.aquire();
_result = new ByteArrayOutputStream(128);
}
@Override
public boolean isEncrypt()
{
return encrypt;
}
@Override
public int getChunkSize(int available)
{
return 128;
}
@Override
public int getSkipSize()
{
return 20;
}
@Override
public boolean isLock()
{
return _errorLock;
}
@Override
public void unlock()
{
_errorLock = false;
}
}

View File

@ -0,0 +1,91 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.clientcryptor.crypt;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
public class XorDatCrypter extends DatCrypter
{
private ByteArrayOutputStream _result;
private final boolean encrypt;
private final int xorKey;
public XorDatCrypter(int code, int key, boolean deCrypt)
{
super(code);
encrypt = !deCrypt;
xorKey = key;
}
@Override
public ByteBuffer decryptResult()
{
return ByteBuffer.wrap(_result.toByteArray());
}
@Override
public ByteBuffer encryptResult()
{
return ByteBuffer.wrap(_result.toByteArray());
}
@Override
public void update(byte[] bArray)
{
for (byte b : bArray)
{
_result.write(b ^ xorKey);
}
}
@Override
public int getChunkSize(int available)
{
return 1;
}
@Override
public int getSkipSize()
{
return 0;
}
@Override
public boolean isLock()
{
return false;
}
@Override
public boolean isEncrypt()
{
return encrypt;
}
@Override
public void unlock()
{
}
@Override
public void aquire()
{
super.aquire();
_result = new ByteArrayOutputStream(128);
}
}