Last active
January 31, 2025 11:35
-
-
Save VadimGush/37d04563bb67b97e07a862507ad89bc4 to your computer and use it in GitHub Desktop.
Decode AmneziaWG client key in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Decoder { | |
public void test() throws Exception { | |
// AmneziaWG client key which starts with "vpn://" is just a Base64 encoded and compressed JSON configuration file | |
final var encoded = "vpn://<insert your client key>".replace("vpn://", ""); | |
// They use Base64 URL encoder. Inside of the Base64 encoded string is a compressed data. | |
final var compressed = ByteBuffer.wrap(Base64.getUrlDecoder().decode(encoded)); | |
// Data is compressed using QT qCompress function, which uses zlib and adds a 4 byte header to the compressed data | |
final var header = new byte[4]; // The header is 32-bit unsigned integer representing the size of uncompressed data | |
compressed.get(header); // This will remove 4 byte header from the buffer | |
final var inflater = new Inflater(); | |
inflater.setInput(compressed); | |
// 10 KB should be enough to fit uncompressed data because configs are quite small. Increase if needed. | |
final var uncompressed = ByteBuffer.allocate(1024 * 10); | |
inflater.inflate(uncompressed); | |
uncompressed.flip(); // After data is written to the buffer, flip the buffer so we can start reading from it | |
final var data = new byte[uncompressed.remaining()]; | |
uncompressed.get(data); | |
final var text = new String(data); | |
System.out.println(text); // This will print the entire AmneziaWG client configuration | |
// JsonObject is from the Vertx.io library (you can use any other JSON library) | |
final var lastConfig = new JsonObject(text) | |
.getJsonArray("containers") // Different connection protocols they call "containers" | |
.getJsonObject(0) | |
.getJsonObject("awg") // "awg" is for the AmneziaWG connection protocol | |
.getString("last_config"); | |
System.out.println(lastConfig); // This will be the specific configuration for awg protocol | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment