[FIX] Encrypted Runtime

This commit is contained in:
Gravita 2021-05-11 16:23:55 +07:00
parent 92a6947ab8
commit 30cabd25fd
2 changed files with 45 additions and 8 deletions

View file

@ -1,5 +1,6 @@
package pro.gravit.launchserver.binary; package pro.gravit.launchserver.binary;
import org.jetbrains.annotations.NotNull;
import pro.gravit.launcher.Launcher; import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.serialize.HOutput; import pro.gravit.launcher.serialize.HOutput;
import pro.gravit.launcher.serialize.stream.StreamObject; import pro.gravit.launcher.serialize.stream.StreamObject;
@ -11,15 +12,18 @@
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream; import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.URL; import java.net.URL;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.HashSet; import java.util.HashSet;
@ -195,6 +199,7 @@ private final static class EncryptedRuntimeDirVisitor extends SimpleFileVisitor<
private final Path sourceDir; private final Path sourceDir;
private final String targetDir; private final String targetDir;
private final SecretKeySpec sKeySpec; private final SecretKeySpec sKeySpec;
private final IvParameterSpec iKeySpec;
private EncryptedRuntimeDirVisitor(ZipOutputStream output, String aesKey, Map<String, byte[]> hashs, Path sourceDir, String targetDir) { private EncryptedRuntimeDirVisitor(ZipOutputStream output, String aesKey, Map<String, byte[]> hashs, Path sourceDir, String targetDir) {
this.output = output; this.output = output;
@ -204,7 +209,8 @@ private EncryptedRuntimeDirVisitor(ZipOutputStream output, String aesKey, Map<St
try { try {
byte[] key = SecurityHelper.fromHex(aesKey); byte[] key = SecurityHelper.fromHex(aesKey);
byte[] compatKey = SecurityHelper.getAESKey(key); byte[] compatKey = SecurityHelper.getAESKey(key);
sKeySpec = new SecretKeySpec(compatKey, "AES"); sKeySpec = new SecretKeySpec(compatKey, "AES/CBC/PKCS5Padding");
iKeySpec = new IvParameterSpec("8u3d90ikr7o67lsq".getBytes());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -223,13 +229,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
Cipher cipher = null; Cipher cipher = null;
try { try {
cipher = Cipher.getInstance("AES"); cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec); cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iKeySpec);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
try(OutputStream stream = new CipherOutputStream(new NoCloseOutputStream(output), cipher)) {
IOHelper.transfer(file, new CipherOutputStream(output, cipher)); IOHelper.transfer(file, stream);
}
// Return result // Return result
return super.visitFile(file, attrs); return super.visitFile(file, attrs);
@ -238,5 +245,33 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
private ZipEntry newEntry(String fileName) { private ZipEntry newEntry(String fileName) {
return newZipEntry(targetDir + IOHelper.CROSS_SEPARATOR + fileName); return newZipEntry(targetDir + IOHelper.CROSS_SEPARATOR + fileName);
} }
private static class NoCloseOutputStream extends OutputStream {
private final OutputStream stream;
private NoCloseOutputStream(OutputStream stream) {
this.stream = stream;
}
@Override
public void write(int i) throws IOException {
stream.write(i);
}
@Override
public void write(byte[] b) throws IOException {
stream.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
stream.write(b, off, len);
}
@Override
public void flush() throws IOException {
stream.flush();
}
}
} }
} }

View file

@ -113,8 +113,10 @@ protected void initProps() {
properties.put("launcher.guardType", server.config.launcher.guardType); properties.put("launcher.guardType", server.config.launcher.guardType);
properties.put("launchercore.env", server.config.env); properties.put("launchercore.env", server.config.env);
properties.put("launcher.memory", server.config.launcher.memoryLimit); properties.put("launcher.memory", server.config.launcher.memoryLimit);
if (server.runtime.runtimeEncryptKey == null) server.runtime.runtimeEncryptKey= SecurityHelper.randomStringAESKey(); if (server.config.launcher.encryptRuntime) {
properties.put("launcher.runtimeEncryptKey", server.runtime.runtimeEncryptKey); if (server.runtime.runtimeEncryptKey == null) server.runtime.runtimeEncryptKey = SecurityHelper.randomStringToken();
properties.put("runtimeconfig.runtimeEncryptKey", server.runtime.runtimeEncryptKey);
}
properties.put("launcher.certificatePinning", server.config.launcher.certificatePinning); properties.put("launcher.certificatePinning", server.config.launcher.certificatePinning);
properties.put("runtimeconfig.passwordEncryptKey", server.runtime.passwordEncryptKey); properties.put("runtimeconfig.passwordEncryptKey", server.runtime.passwordEncryptKey);
String launcherSalt = SecurityHelper.randomStringToken(); String launcherSalt = SecurityHelper.randomStringToken();