diff --git a/.gitignore b/.gitignore index b253564..9a9b355 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea/ config.json packet.bin +outpacket.bin diff --git a/Cargo.lock b/Cargo.lock index ac014dc..f1de0a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -162,6 +162,16 @@ version = "1.0.97" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4" +[[package]] +name = "cfg-expr" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d4ba6e40bd1184518716a6e1a781bf9160e286d219ccdb8ab2612e74cfe4789" +dependencies = [ + "smallvec", + "target-lexicon", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -177,6 +187,12 @@ dependencies = [ "libc", ] +[[package]] +name = "crunchy" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" + [[package]] name = "crypto-common" version = "0.1.6" @@ -203,6 +219,12 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "fnv" version = "1.0.7" @@ -278,6 +300,22 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "half" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" +dependencies = [ + "cfg-if", + "crunchy", +] + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + [[package]] name = "headers" version = "0.3.9" @@ -302,6 +340,12 @@ dependencies = [ "http", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -365,6 +409,16 @@ dependencies = [ "want", ] +[[package]] +name = "indexmap" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "itoa" version = "1.0.11" @@ -377,7 +431,9 @@ version = "0.1.0" dependencies = [ "axum", "binrw", + "md5", "minijinja", + "physis", "rand", "serde", "serde_json", @@ -398,12 +454,30 @@ version = "0.2.154" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" +[[package]] +name = "libz-sys" +version = "1.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "matchit" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" +[[package]] +name = "md5" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" + [[package]] name = "memchr" version = "2.7.2" @@ -476,12 +550,32 @@ version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "percent-encoding" version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "physis" +version = "0.4.0" +source = "git+https://github.com/redstrate/Physis#af0d34c011f49f7c0cabaf174ca30d123c0a0859" +dependencies = [ + "binrw", + "bitflags", + "half", + "libz-sys", + "system-deps", + "texture2ddecoder", + "tracing", +] + [[package]] name = "pin-project" version = "1.1.5" @@ -514,6 +608,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -627,6 +727,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -659,6 +768,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "smallvec" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" + [[package]] name = "socket2" version = "0.5.7" @@ -697,6 +812,34 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "system-deps" +version = "7.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d23aaf9f331227789a99e8de4c91bf46703add012bdfd45fdecdfb2975a005" +dependencies = [ + "cfg-expr", + "heck", + "pkg-config", + "toml", + "version-compare", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + +[[package]] +name = "texture2ddecoder" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d82f195862d86c369d24dfcaaad28334b374be46590375113ad74aeb4e1184c" +dependencies = [ + "paste", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -735,6 +878,40 @@ dependencies = [ "syn 2.0.62", ] +[[package]] +name = "toml" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tower" version = "0.4.13" @@ -810,6 +987,18 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version-compare" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b" + [[package]] name = "version_check" version = "0.9.4" @@ -969,3 +1158,12 @@ name = "windows_x86_64_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" + +[[package]] +name = "winnow" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7f4ea97f6f78012141bcdb6a216b2609f0979ada50b20ca5b52dde2eac2bb1" +dependencies = [ + "memchr", +] diff --git a/Cargo.toml b/Cargo.toml index aeeb333..fecaf90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,3 +35,5 @@ tracing-subscriber = { version = "0.3", features = ["fmt"], default-features = f rand = "0.8" minijinja = "2.0" binrw = { version = "0.14" } +physis = { git = "https://github.com/redstrate/Physis" } +md5 = "0.7.0" diff --git a/src/bin/kawari-lobby.rs b/src/bin/kawari-lobby.rs index 34033a9..90aa57c 100644 --- a/src/bin/kawari-lobby.rs +++ b/src/bin/kawari-lobby.rs @@ -12,7 +12,7 @@ async fn main() { loop { let (socket, _) = listener.accept().await.unwrap(); - let (mut read, _) = tokio::io::split(socket); + let (mut read, mut write) = tokio::io::split(socket); tokio::spawn(async move { let mut buf = [0; 2056]; @@ -20,7 +20,7 @@ async fn main() { let n = read.read(&mut buf).await.expect("Failed to read data!"); if n != 0 { - parse_packet(&buf[..n]); + parse_packet(&mut write, &buf[..n]).await; } } }); diff --git a/src/packet.rs b/src/packet.rs index 0acf63b..e0ce5a3 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,6 +1,8 @@ -use std::{fs::write, io::Cursor}; +use std::{fs::write, io::Cursor, time::{SystemTime, UNIX_EPOCH}}; -use binrw::{BinRead, binrw, helpers::until_eof}; +use binrw::{binrw, helpers::until_eof, BinRead, BinWrite}; +use physis::blowfish::Blowfish; +use tokio::{io::{AsyncWriteExt, WriteHalf}, net::TcpStream}; pub(crate) fn read_bool_from + std::cmp::PartialEq>(x: T) -> bool { x == T::from(1u8) @@ -23,19 +25,24 @@ enum ConnectionType { } #[binrw] -#[derive(Debug)] +#[derive(Debug, Clone)] enum SegmentType { #[brw(magic = 0x9u32)] InitializeEncryption { - #[br(pad_before = 36)] // empty + #[brw(pad_before = 36)] // empty #[br(count = 64)] #[br(map = read_string)] #[bw(ignore)] phrase: String, - #[br(pad_after = 512)] // empty - key: u32, + #[brw(pad_after = 512)] // empty + key: [u8; 4], }, + #[brw(magic = 0x0Au32)] + InitializationEncryptionResponse { + #[br(count = 0x280)] + data: Vec + } } #[binrw] @@ -56,14 +63,25 @@ struct PacketHeader { } #[binrw] -#[derive(Debug)] +#[derive(Debug, Clone)] struct PacketSegment { + #[bw(calc = self.calc_size())] size: u32, source_actor: u32, target_actor: u32, segment_type: SegmentType, } +impl PacketSegment { + fn calc_size(&self) -> u32 { + let header = std::mem::size_of::() * 4; + return header as u32 + match &self.segment_type { + SegmentType::InitializeEncryption { .. } => 616, + SegmentType::InitializationEncryptionResponse { .. } => 640, + }; + } +} + #[binrw] #[derive(Debug)] struct Packet { @@ -77,7 +95,52 @@ fn dump(msg: &str, data: &[u8]) { panic!("{msg} Dumped to packet.bin."); } -pub fn parse_packet(data: &[u8]) { +async fn send_packet(socket: &mut WriteHalf, segments: &[PacketSegment]) { + let timestamp: u64 = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Failed to get UNIX timestamp!") + .as_millis() + .try_into() + .unwrap(); + + let mut total_segment_size = 0; + for segment in segments { + total_segment_size += segment.calc_size(); + } + + let header = PacketHeader { + unk1: 0, + unk2: 0, + timestamp, + size: std::mem::size_of::() as u32 + total_segment_size, + connection_type: ConnectionType::Lobby, + segment_count: segments.len() as u16, + unk3: 0, + compressed: false, + unk4: 0, + unk5: 0, + }; + + let packet = Packet { + header, + segments: segments.to_vec(), + }; + + let mut cursor = Cursor::new(Vec::new()); + packet.write_le(&mut cursor); + + let buffer = cursor.into_inner(); + + tracing::info!("Wrote response packet to outpacket.bin"); + write("outpacket.bin", &buffer); + + socket + .write(&buffer) + .await + .expect("Failed to write packet!"); +} + +pub async fn parse_packet(socket: &mut WriteHalf, data: &[u8]) { let mut cursor = Cursor::new(data); if let Ok(packet) = Packet::read_le(&mut cursor) { @@ -90,8 +153,54 @@ pub fn parse_packet(data: &[u8]) { ); } - dump("nothing", data); + for segment in &packet.segments { + match &segment.segment_type { + SegmentType::InitializeEncryption { phrase, key } => { + // Generate an encryption key for this client + let client_key = generate_encryption_key(key, phrase); + + let blowfish = Blowfish::new(&client_key); + let mut data = blowfish.encrypt(&0xE0003C2Au32.to_le_bytes()).unwrap(); + data.resize(0x280, 0); + + let response_packet = PacketSegment { + source_actor: 0, + target_actor: 0, + segment_type: SegmentType::InitializationEncryptionResponse { + data + }, + }; + send_packet(socket, &[response_packet]).await; + }, + SegmentType::InitializationEncryptionResponse { .. } => panic!("The server is recieving a response packet!"), + } + } + + //dump("nothing", data); } else { dump("Failed to parse packet!", data); } } + +const GAME_VERSION: u16 = 7000; + +pub fn generate_encryption_key(key: &[u8], phrase: &str) -> [u8; 16] { + let mut base_key = vec![0x78, 0x56, 0x34, 0x12]; + base_key.extend_from_slice(&key); + base_key.extend_from_slice(&GAME_VERSION.to_le_bytes()); + base_key.extend_from_slice(&[0; 2]); // padding (possibly for game version?) + base_key.extend_from_slice(&phrase.as_bytes()); + + md5::compute(&base_key).0 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_encryption_key() { + let key = generate_encryption_key([0x00, 0x00, 0x00, 0x00], "foobar"); + assert_eq!(key, [169, 78, 235, 31, 57, 151, 26, 74, 250, 196, 1, 120, 206, 173, 202, 48]); + } +}