From e75f16537b1626e9f8168b36438df7dc933adcd2 Mon Sep 17 00:00:00 2001 From: lapla-cogito Date: Sat, 24 Aug 2024 09:53:42 +0900 Subject: [PATCH] use sha256 as function encrypt key --- Cargo.lock | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 6 ++-- src/obfus.rs | 14 +++------- 4 files changed, 86 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed07d6e..190915b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,6 +66,15 @@ version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "cattleya" version = "0.1.0" @@ -75,9 +84,16 @@ dependencies = [ "clap", "memmap2", "rust-crypto", + "sha2", "thiserror", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.5.16" @@ -124,6 +140,35 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -136,6 +181,16 @@ version = "0.3.55" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "heck" version = "0.5.0" @@ -241,6 +296,17 @@ version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe834bc780604f4674073badbad26d7219cadfb4a2275802db12cbae17498401" +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "strsim" version = "0.11.0" @@ -289,6 +355,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -301,6 +373,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 64ae24d..af92312 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ anyhow = "1.0.86" clap = { version = "4.5.3", features = ["derive"] } memmap2 = "0.9.4" rust-crypto = "0.2.36" +sha2 = "0.10.8" thiserror = "1.0.63" diff --git a/README.md b/README.md index 50d1e10..3d158c3 100644 --- a/README.md +++ b/README.md @@ -171,15 +171,15 @@ $ objdump -d bin/res_enc 1218: 89 7d fc mov %edi,-0x4(%rbp) 121b: 48 89 75 f0 mov %rsi,-0x10(%rbp) 121f: bf 01 00 00 00 mov $0x1,%edi - 1224: e8 20 ff ff ff call 1149 <�0,> + 1224: e8 20 ff ff ff call 1149 <ӵ�> 1229: bf 01 00 00 00 mov $0x1,%edi 122e: e8 6a ff ff ff call 119d 1233: bf 05 00 00 00 mov $0x5,%edi - 1238: e8 0c ff ff ff call 1149 <�0,> + 1238: e8 0c ff ff ff call 1149 <ӵ�> 123d: bf 05 00 00 00 mov $0x5,%edi 1242: e8 56 ff ff ff call 119d 1247: bf 0a 00 00 00 mov $0xa,%edi - 124c: e8 f8 fe ff ff call 1149 <�0,> + 124c: e8 f8 fe ff ff call 1149 <ӵ�> 1251: bf 0a 00 00 00 mov $0xa,%edi 1256: e8 42 ff ff ff call 119d 125b: b8 00 00 00 00 mov $0x0,%eax diff --git a/src/obfus.rs b/src/obfus.rs index 170f9a7..979b489 100644 --- a/src/obfus.rs +++ b/src/obfus.rs @@ -323,16 +323,10 @@ impl Obfuscator { } pub fn encrypt_function_name(&mut self, function: &str, key: &str) -> crate::error::Result<()> { - let mut key_bytes = [0; 32]; - if key.len() > 32 { - return Err(crate::error::Error::InvalidOption( - "key length must be less than 32", - )); - } - for (i, byte) in key.bytes().enumerate() { - key_bytes[i] = byte; - } - let encryptor = crypto::aessafe::AesSafe256Encryptor::new(&key_bytes); + use sha2::digest::Digest as _; + + let hash = sha2::Sha256::digest(key.as_bytes()); + let encryptor = crypto::aessafe::AesSafe256Encryptor::new(&hash); let tmp_file = std::fs::OpenOptions::new() .read(true)