chore: add encoder fuzzing
ofc nothing more :wink
This commit is contained in:
1 parent
e2089531ee
commit
e9423dcb9c
22 files changed
+532
-558
No files matched your search
@@ -0,0 +1,43 @@
|
||||
#![no_main]
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
use pumpkin_protocol::bedrock::packet_encoder::UDPNetworkEncoder;
|
||||
use pumpkin_protocol::bedrock::SubClient;
|
||||
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
if data.len() < 10 {
|
||||
return;
|
||||
}
|
||||
|
||||
let compression_threshold = data[0] as usize;
|
||||
let compression_level = (data[1] % 10) as u32;
|
||||
let packet_id = u16::from_be_bytes([data[2], data[3]]);
|
||||
let sender = match data[4] % 4 {
|
||||
0 => SubClient::Main,
|
||||
1 => SubClient::SubClient0,
|
||||
2 => SubClient::SubClient1,
|
||||
_ => SubClient::SubClietn2,
|
||||
};
|
||||
let target = match data[5] % 4 {
|
||||
0 => SubClient::Main,
|
||||
1 => SubClient::SubClient0,
|
||||
2 => SubClient::SubClient1,
|
||||
_ => SubClient::SubClietn2,
|
||||
};
|
||||
let use_compression = data[6] % 2 == 0;
|
||||
let packet_payload = &data[7..];
|
||||
|
||||
let mut encoder = UDPNetworkEncoder::new();
|
||||
|
||||
if use_compression {
|
||||
encoder.set_compression((compression_threshold, compression_level));
|
||||
}
|
||||
|
||||
let mut out = Vec::new();
|
||||
let _ = encoder.write_game_packet(
|
||||
packet_id,
|
||||
sender,
|
||||
target,
|
||||
packet_payload,
|
||||
&mut out
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
#![no_main]
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
use pumpkin_protocol::java::packet_encoder::TCPNetworkEncoder;
|
||||
use pumpkin_protocol::codec::var_int::VarInt;
|
||||
use pumpkin_protocol::ser::NetworkWriteExt;
|
||||
use pumpkin_protocol::ServerPacket;
|
||||
use pumpkin_protocol::java::client::play::CPlayerPosition;
|
||||
use pumpkin_protocol::packet::MultiVersionJavaPacket;
|
||||
use pumpkin_protocol::ClientPacket;
|
||||
use pumpkin_util::version::MinecraftVersion;
|
||||
use std::io::Cursor;
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
const TARGET_VERSION: MinecraftVersion = MinecraftVersion::V_1_21_4;
|
||||
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
if data.len() < 20 {
|
||||
return;
|
||||
}
|
||||
|
||||
let compression_threshold = data[0] as usize;
|
||||
let compression_level = (data[1] % 10) as u32;
|
||||
let encryption_key: [u8; 16] = data[2..18].try_into().unwrap();
|
||||
let use_compression = data[18] % 2 == 0;
|
||||
let use_encryption = data[19] % 2 == 0;
|
||||
let packet_data = &data[20..];
|
||||
|
||||
let rt = Runtime::new().unwrap();
|
||||
rt.block_on(async {
|
||||
let mut out = Vec::new();
|
||||
let mut encoder = TCPNetworkEncoder::new(&mut out);
|
||||
|
||||
if use_compression {
|
||||
encoder.set_compression((compression_threshold, compression_level));
|
||||
}
|
||||
if use_encryption {
|
||||
encoder.set_encryption(&encryption_key);
|
||||
}
|
||||
|
||||
// 1. Fuzz with raw bytes (simulating various packet payloads)
|
||||
let mut buf = Vec::new();
|
||||
let packet_id = if packet_data.is_empty() { 0 } else { packet_data[0] as i32 };
|
||||
let _ = buf.write_var_int(&VarInt(packet_id));
|
||||
buf.extend_from_slice(packet_data);
|
||||
let _ = encoder.write_packet(buf.into()).await;
|
||||
|
||||
// 2. Fuzz with actual packets if they can be partially read
|
||||
let mut cursor = Cursor::new(packet_data);
|
||||
if let Ok(packet) = CPlayerPosition::read(&mut cursor, &TARGET_VERSION) {
|
||||
let mut packet_buf = Vec::new();
|
||||
let id = CPlayerPosition::to_id(TARGET_VERSION);
|
||||
let _ = packet_buf.write_var_int(&VarInt(id));
|
||||
let _ = packet.write_packet_data(&mut packet_buf, &TARGET_VERSION);
|
||||
let _ = encoder.write_packet(packet_buf.into()).await;
|
||||
}
|
||||
|
||||
let _ = encoder.flush().await;
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user