Last active
July 10, 2022 23:35
-
-
Save beer-psi/38a1344d4c761dd81008b3e7d04e0929 to your computer and use it in GitHub Desktop.
batojs thingy
This file contains 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
#![no_std] | |
#[cfg(test)] | |
mod tests; | |
extern crate alloc; | |
use alloc::{format, string::String, vec, vec::Vec}; | |
use aes::{ | |
cipher::{BlockDecryptMut, KeyIvInit}, | |
Aes256, | |
}; | |
use block_padding::Pkcs7; | |
use cbc::Decryptor; | |
use evpkdf::evpkdf; | |
use md5::Md5; | |
type Aes256CbcDec = Decryptor<Aes256>; | |
const AES_128_LENGTH: usize = 16; | |
const AES_192_LENGTH: usize = 28; | |
const AES_256_LENGTH: usize = 32; | |
#[derive(Debug)] | |
pub enum DecryptError { | |
DecodeError, | |
InvalidLength, | |
} | |
fn base64_decode<T: AsRef<[u8]>>(binary: T) -> Result<Vec<u8>, base64::DecodeError> { | |
let binary = binary.as_ref(); | |
let mut buf = vec![0; binary.len() * 4 / 3 + 4]; | |
let bytes_written = base64::decode_config_slice(binary, base64::STANDARD, &mut buf)?; | |
buf.resize(bytes_written, 0); | |
Ok(buf) | |
} | |
fn batojs_parse(batojs: String) -> String { | |
let preprocess = batojs | |
.replace( | |
"+(+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+", | |
".", | |
) | |
.replace("+[]", "!") | |
.chars() | |
.collect::<Vec<char>>(); | |
let mut ret = String::new(); | |
let mut tmp = 0; | |
let mut i: usize = 0; | |
while i < preprocess.len() { | |
if preprocess[i] == '!' && preprocess[i + 1] == '!' { | |
tmp += 1; | |
i += 1; | |
} else if preprocess[i] == ']' { | |
ret.push_str(format!("{tmp}").as_str()); | |
tmp = 0; | |
} else if preprocess[i] == '.' { | |
ret.push('.'); | |
} | |
i += 1; | |
} | |
ret | |
} | |
/// A function that imitates basic usage of CryptoJS.AES.decrypt(message, key, { iv: iv }). | |
pub fn cryptojs_aes_decrypt( | |
message: &[u8], | |
key: &[u8], | |
iv: Option<&[u8; 16]>, | |
) -> Result<Vec<u8>, DecryptError> { | |
let mut key_iv = [0; 48]; | |
let iv = iv.unwrap_or(&[0; 16]); | |
// Check for salt | |
let (salt, ciphertext) = if &message[0..=7] == b"Salted__" { | |
(&message[8..=15], &message[16..]) | |
} else { | |
("".as_bytes(), message) | |
}; | |
let (actual_key, actual_iv) = if key.len() != AES_128_LENGTH | |
&& key.len() != AES_192_LENGTH | |
&& key.len() != AES_256_LENGTH | |
{ | |
evpkdf::<Md5>(key, salt, 1, &mut key_iv); | |
key_iv.split_at(32) | |
} else { | |
(key as &[u8], iv as &[u8]) | |
}; | |
if let Ok(cipher) = Aes256CbcDec::new_from_slices(actual_key, actual_iv) { | |
let mut buf: Vec<u8> = vec![0; ciphertext.len()]; | |
buf[..ciphertext.len()].copy_from_slice(ciphertext); | |
let pt = cipher.decrypt_padded_mut::<Pkcs7>(&mut buf).unwrap(); | |
Ok(Vec::from(pt)) | |
} else { | |
Err(DecryptError::InvalidLength) | |
} | |
} | |
pub fn batojs_decrypt(server: String, batojs: String) -> String { | |
let temp = batojs_parse(batojs); | |
if let Ok(server_decoded) = base64_decode(server) { | |
let pt = cryptojs_aes_decrypt(&server_decoded, temp.as_bytes(), None).unwrap(); | |
String::from_utf8_lossy(&pt).replace('"', "") | |
} else { | |
String::new() | |
} | |
} |
This file contains 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
[package] | |
name = "fuck-batojs" | |
version = "0.1.0" | |
edition = "2021" | |
[lib] | |
crate-type = ["cdylib"] | |
[dependencies] | |
aes = { version = "0.8", default-features = false } | |
base64 = { version = "0.13.0", default-features = false } | |
block-modes = { version = "0.9.0", default-features = false } | |
block-padding = { version = "0.3.2", default-features = false } | |
cbc = { version = "0.1.2", features = ["block-padding"] } | |
evpkdf = { git = "https://github.com/EnoughTea/evpkdf", branch = "to_digest_0.10" } | |
md-5 = { version = "0.10.1", default-features = false } |
This file contains 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
use alloc::string::String; | |
use crate::batojs_decrypt; | |
#[test] | |
fn it_works() { | |
assert_eq!( | |
batojs_decrypt( | |
String::from("U2FsdGVkX1/BpAhFJuKeE/VbYYRotc0xWX9FUBwoCexRgKrh8bJJl+utGzUgEQDTO289qBaz/cEVrvNtHc6r0Uu99cd3zFHJG8E4XrJtPzw="), | |
String::from("[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[+[]]+(+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+[!+[]+!+[]+!+[]]+[+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]"), | |
).unwrap(), | |
String::from("https://xfs-002.batcg.com/7002/e7a/62864218adbf8b94db9d6a7e/") | |
); | |
assert_eq!( | |
batojs_decrypt( | |
String::from("U2FsdGVkX1+QAISmCttqCCStjfQ11VNW1zhaBEzTWjdlR1SAICBcQEskiLuSVzy8inGOepPP+EZrT+FqY9Ysiuk8Jnoij93eAvgQxyQ4SPYXjw5avs63USpyrpsO22Ig"), | |
String::from("[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[+!+[]]+[+!+[]]+[!+[]+!+[]]+(+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]]"), | |
).unwrap(), | |
String::from("https://file-bato-temp.batcg.com/f4b/629009703bfa84814e8f3b4f/") | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment