From 854bbedfb4df86e1c2489ceaa49f3debd714bebf Mon Sep 17 00:00:00 2001 From: REinject Date: Wed, 11 Sep 2024 18:55:35 +0800 Subject: [PATCH] v0.1.3 - Fix some minor issues. --- Cargo.lock | 3 ++- Cargo.toml | 3 ++- README.md | 5 +++-- README_zh.md | 5 +++-- src/cert/certificate.rs | 43 ++++++++++++++++++++++++++++++----------- src/main.rs | 2 +- 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a72cf0b..9a0b1f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -457,7 +457,7 @@ checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] name = "pe-sign" -version = "0.1.2" +version = "0.1.3" dependencies = [ "chrono", "clap", @@ -465,6 +465,7 @@ dependencies = [ "der", "digest 0.10.7", "exe", + "num-traits", "pem-rfc7468 1.0.0-rc.1", "pretty-hex", "rsa", diff --git a/Cargo.toml b/Cargo.toml index 2e2cc1d..47ceb16 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pe-sign" -version = "0.1.2" +version = "0.1.3" edition = "2021" authors = ["REinject"] homepage = "https://github.com/0xlane/pe-sign" @@ -45,3 +45,4 @@ sha1 = { version = "0.10.6", features = ["oid"] } sha2 = { version = "0.10.8", features = ["oid"] } x509-cert = { version = "0.2.5", features = ["sct"] } chrono = "0.4.38" +num-traits = "0.2.19" diff --git a/README.md b/README.md index af93d0f..74fa8a9 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Alternatively, if you have `Cargo` installed, you can easily install it by runni ### Usage ```powershell -pe-sign (0.1.2) - REinject +pe-sign (0.1.3) - REinject A tool for parsing and verifing PE file signatures Repository: https://github.com/0xlane/pe-sign @@ -110,11 +110,12 @@ Certificate: Subject Public Key Info: Algorithm: RSA Public-Key: (2048 bit) - Modules: + Modulus: 00:cc:2e:a1:52:49:09:cc:22:ef:34:43:dc:41:a6:98:a0:1f:0f:69: 1a:33:b2:92:a5:73:26:4e:1d:b9:e2:ab:c4:46:e1:3e:f9:24:c2:f6: ... ... + Exponent: 65537 (0x10001) Extensions: Authority Key Identifier: 97:48:03:eb:15:08:6b:b9:b2:58:23:cc:94:2e:f1:c6:65:d2:64:8e diff --git a/README_zh.md b/README_zh.md index cc3fd60..63c854c 100644 --- a/README_zh.md +++ b/README_zh.md @@ -33,7 +33,7 @@ ### 使用说明 ```powershell -pe-sign (0.1.2) - REinject +pe-sign (0.1.3) - REinject A tool for parsing and verifing PE file signatures Repository: https://github.com/0xlane/pe-sign @@ -110,11 +110,12 @@ Certificate: Subject Public Key Info: Algorithm: RSA Public-Key: (2048 bit) - Modules: + Modulus: 00:cc:2e:a1:52:49:09:cc:22:ef:34:43:dc:41:a6:98:a0:1f:0f:69: 1a:33:b2:92:a5:73:26:4e:1d:b9:e2:ab:c4:46:e1:3e:f9:24:c2:f6: ... ... + Exponent: 65537 (0x10001) Extensions: Authority Key Identifier: 97:48:03:eb:15:08:6b:b9:b2:58:23:cc:94:2e:f1:c6:65:d2:64:8e diff --git a/src/cert/certificate.rs b/src/cert/certificate.rs index eab2a90..0a1c927 100644 --- a/src/cert/certificate.rs +++ b/src/cert/certificate.rs @@ -10,6 +10,7 @@ use der::{ Decode, Encode, }; use digest::{Digest, DynDigest}; +use num_traits::ToPrimitive; use rsa::{pkcs1::DecodeRsaPublicKey, traits::PublicKeyParts, Pkcs1v15Sign, RsaPublicKey}; use sha1::Sha1; use sha2::{Sha224, Sha256, Sha384, Sha512}; @@ -24,7 +25,6 @@ use super::{ name::RdnSequence, }; - /// Parse Certificate. #[derive(Clone, Debug, Eq, PartialEq)] pub struct Certificate { @@ -73,7 +73,11 @@ impl Display for Certificate { format!("Version: {} (0x{:x})", self.version + 1, self.version).indent(8) )?; writeln!(f, "{}", "Serial Number:".indent(8))?; - writeln!(f, "{}", self.serial_number.clone().to_bytes_string().indent(12))?; + writeln!( + f, + "{}", + self.serial_number.clone().to_bytes_string().indent(12) + )?; writeln!(f, "{}", format!("Issuer: {}", self.issuer).indent(8))?; writeln!(f, "{}", self.validity.to_string().indent(8))?; writeln!(f, "{}", format!("Subject: {}", self.subject).indent(8))?; @@ -91,7 +95,11 @@ impl Display for Certificate { format!("Signature Algorithm: {}", self.signature_algorithm).indent(4) )?; writeln!(f, "{}", "Signature Value:".indent(4))?; - write!(f, "{}", self.signature_value.clone().to_bytes_string().indent(12)) + write!( + f, + "{}", + self.signature_value.clone().to_bytes_string().indent(12) + ) } } @@ -188,7 +196,7 @@ impl Certificate { } } - /// Get the tbs_certificate binary data for validating its trustworthiness, + /// Get the tbs_certificate binary data for validating its trustworthiness, /// and the decrypted signature is the hash of tbs_certificate. pub fn get_tbs_certificate_bytes(self: &Self) -> Vec { self.__inner.tbs_certificate.to_der().unwrap() @@ -395,6 +403,9 @@ impl TryFrom for SubjectPublicKeyInf impl Display for SubjectPublicKeyInfo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mudulus = self.get_public_key_modulus(); + let exponent = self.get_public_key_exponent().unwrap_or(0); + writeln!(f, "Subject Public Key Info:")?; writeln!( f, @@ -404,18 +415,20 @@ impl Display for SubjectPublicKeyInfo { writeln!( f, "{}", - format!( - "Public-Key: ({} bit)\nModules:", - (self.get_public_key_modules().len() - 1) * 8 - ) - .indent(4) + format!("Public-Key: ({} bit)\nModulus:", (mudulus.len() - 1) * 8).indent(4) )?; - write!(f, "{}", self.get_public_key_modules().to_bytes_string().indent(8)) + writeln!(f, "{}", mudulus.to_bytes_string().indent(8))?; + write!( + f, + "{}", + format!("Exponent: {} (0x{:x})", exponent, exponent).indent(4) + ) } } impl SubjectPublicKeyInfo { - pub fn get_public_key_modules(self: &Self) -> Vec { + /// Returns the modulus of the key. + pub fn get_public_key_modulus(self: &Self) -> Vec { match &self.__inner_public_key { Some(rsa_public_key) => { let mut tmp = rsa_public_key.n().to_bytes_be(); @@ -425,4 +438,12 @@ impl SubjectPublicKeyInfo { None => self.subject_public_key.clone(), } } + + /// Returns the public exponent of the key. + pub fn get_public_key_exponent(self: &Self) -> Option { + match &self.__inner_public_key { + Some(rsa_public_key) => rsa_public_key.e().to_usize(), + None => None, + } + } } diff --git a/src/main.rs b/src/main.rs index 4b485eb..b45e4e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ fn cli() -> clap::Command { use clap::{arg, value_parser, Command}; Command::new("pe-sign") - .version("0.1.2") + .version("0.1.3") .about("A tool for parsing and verifing PE file signatures\n\nRepository: https://github.com/0xlane/pe-sign\n") .author("REinject") .help_template("{name} ({version}) - {author}\n{about}\n{all-args}")