1
0

feat: finish p16

This commit is contained in:
2025-09-08 17:25:30 +08:00
parent 26073ca3f3
commit b3ce1dfaef
3 changed files with 50 additions and 3 deletions

View File

@@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
common = { path = "../../common/" }
rand = { workspace = true }

View File

@@ -1,3 +1,48 @@
fn main() {
println!("Hello, world!");
use common::{aes_cbc_dec, aes_cbc_enc, pkcs7_padding, pkcs7_unpadding};
use rand::{Rng, rng};
fn append_and_enc(input: String, key: &[u8; 16]) -> Vec<u8> {
let input = input.replace(";", "\\;").replace("=", "\\=");
let prefix = "comment1=cooking%20MCs;userdata=".to_string();
let suffix = ";comment2=%20like%20a%20pound%20of%20bacon";
// println!("{}, {}", prefix.len(), suffix.len()); // 32, 42
let mut buffer = prefix + &input + suffix;
let input = unsafe { buffer.as_mut_vec() };
pkcs7_padding(input, 16);
let iv = [0u8; 16];
aes_cbc_enc(input, key, &iv).unwrap()
}
fn verify(cipher: &[u8], key: &[u8; 16]) -> bool {
let iv = [0u8; 16];
let plaintext = aes_cbc_dec(cipher, key, &iv).unwrap();
let plaintext = pkcs7_unpadding(&plaintext).unwrap();
let plaintext = String::from_utf8_lossy(&plaintext);
println!("{}", plaintext);
plaintext.contains(";admin=true;")
}
fn main() {
let mut rng = rng();
let key: [u8; 16] = rng.random();
// 构造输入,第一个块用于反转破坏
let input = "A".repeat(16) + &"A".repeat(5) + "XadminXtrue";
let mut cipher = append_and_enc(input, &key);
// 计算目标位置prefix(32) + padding(16) + target_start = 48 + 1 = 49
// 目标在第4块需要修改第3块
let block_to_modify = 2 * 16; // 第3块起始位置
// 修改密文来翻转目标位
cipher[block_to_modify + 5] ^= b'X' ^ b';'; // X -> ;
cipher[block_to_modify + 11] ^= b'X' ^ b'='; // X -> =
if verify(&cipher, &key) {
println!("Attack successful!");
} else {
println!("Attack failed");
}
}