Line data Source code
1 : //! Module for the PE file format support in LIEF.
2 : //!
3 : //! The [`Binary`] structure exposes the main API to inspect a PE file. It can be instantiated,
4 : //! using either: [`crate::pe::parse`], [`crate::pe::Binary::parse`] or [`crate::Binary::parse`]
5 : //!
6 : //! ```
7 : //! let pe = lief::pe::parse("demo.exe").unwrap();
8 : //! for section in pe.sections() {
9 : //! println!("section: {}", section.name());
10 : //! }
11 : //! ```
12 :
13 : pub mod binary;
14 : pub mod data_directory;
15 : pub mod debug;
16 : pub mod delay_import;
17 : pub mod export;
18 : pub mod headers;
19 : pub mod import;
20 : pub mod load_configuration;
21 : pub mod relocation;
22 : pub mod resources;
23 : pub mod rich_header;
24 : pub mod section;
25 : pub mod signature;
26 : pub mod tls;
27 : pub mod code_integrity;
28 : pub mod builder;
29 : pub mod exception;
30 : pub mod exception_x64;
31 : pub mod exception_aarch64;
32 : pub mod chpe_metadata_arm64;
33 : pub mod chpe_metadata_x86;
34 : pub mod dynamic_relocation;
35 : pub mod dynamic_fixups;
36 : pub mod enclave_configuration;
37 : pub mod volatile_metadata;
38 : pub mod parser_config;
39 :
40 : #[doc(inline)]
41 : pub use binary::Binary;
42 : #[doc(inline)]
43 : pub use data_directory::DataDirectory;
44 : #[doc(inline)]
45 : pub use delay_import::DelayImport;
46 : #[doc(inline)]
47 : pub use export::Export;
48 : #[doc(inline)]
49 : pub use headers::{DosHeader, Header, OptionalHeader};
50 : #[doc(inline)]
51 : pub use relocation::Relocation;
52 : #[doc(inline)]
53 : pub use resources::Manager as ResourcesManager;
54 : #[doc(inline)]
55 : pub use resources::Node as ResourceNode;
56 : #[doc(inline)]
57 : pub use rich_header::{RichEntry, RichHeader};
58 : #[doc(inline)]
59 : pub use section::Section;
60 : #[doc(inline)]
61 : pub use tls::TLS;
62 : #[doc(inline)]
63 : pub use import::Import;
64 : #[doc(inline)]
65 : pub use signature::Signature;
66 : #[doc(inline)]
67 : pub use exception::{RuntimeExceptionFunction, ExceptionInfo};
68 : #[doc(inline)]
69 : pub use load_configuration::{LoadConfiguration, CHPEMetadata};
70 : #[doc(inline)]
71 : pub use dynamic_relocation::DynamicRelocation;
72 : #[doc(inline)]
73 : pub use dynamic_fixups::DynamicFixup;
74 : #[doc(inline)]
75 : pub use enclave_configuration::{EnclaveConfiguration, EnclaveImport};
76 : #[doc(inline)]
77 : pub use volatile_metadata::VolatileMetadata;
78 : #[doc(inline)]
79 : pub use parser_config::Config as ParserConfig;
80 :
81 : #[allow(non_camel_case_types)]
82 984 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
83 : pub enum Algorithms {
84 : SHA_512,
85 : SHA_384,
86 : SHA_256,
87 : SHA_1,
88 : MD5,
89 : MD4,
90 : MD2,
91 : RSA,
92 : EC,
93 : MD5_RSA,
94 : SHA1_DSA,
95 : SHA1_RSA,
96 : SHA_256_RSA,
97 : SHA_384_RSA,
98 : SHA_512_RSA,
99 : SHA1_ECDSA,
100 : SHA_256_ECDSA,
101 : SHA_384_ECDSA,
102 : SHA_512_ECDSA,
103 : UNKNOWN(u32),
104 : }
105 :
106 :
107 : impl From<u32> for Algorithms {
108 984 : fn from(value: u32) -> Self {
109 984 : match value {
110 0 : 0x00000001 => Algorithms::SHA_512,
111 0 : 0x00000002 => Algorithms::SHA_384,
112 456 : 0x00000003 => Algorithms::SHA_256,
113 192 : 0x00000004 => Algorithms::SHA_1,
114 36 : 0x00000005 => Algorithms::MD5,
115 0 : 0x00000006 => Algorithms::MD4,
116 0 : 0x00000007 => Algorithms::MD2,
117 240 : 0x00000008 => Algorithms::RSA,
118 0 : 0x00000009 => Algorithms::EC,
119 0 : 0x0000000a => Algorithms::MD5_RSA,
120 0 : 0x0000000b => Algorithms::SHA1_DSA,
121 0 : 0x0000000c => Algorithms::SHA1_RSA,
122 60 : 0x0000000d => Algorithms::SHA_256_RSA,
123 0 : 0x0000000e => Algorithms::SHA_384_RSA,
124 0 : 0x0000000f => Algorithms::SHA_512_RSA,
125 0 : 0x00000010 => Algorithms::SHA1_ECDSA,
126 0 : 0x00000011 => Algorithms::SHA_256_ECDSA,
127 0 : 0x00000012 => Algorithms::SHA_384_ECDSA,
128 0 : 0x00000013 => Algorithms::SHA_512_ECDSA,
129 0 : _ => Algorithms::UNKNOWN(value),
130 :
131 : }
132 984 : }
133 : }
134 :
135 : impl From<Algorithms> for u32 {
136 156 : fn from(value: Algorithms) -> u32 {
137 156 : match value {
138 0 : Algorithms::SHA_512 => 0x00000001,
139 0 : Algorithms::SHA_384 => 0x00000002,
140 156 : Algorithms::SHA_256 => 0x00000003,
141 0 : Algorithms::SHA_1 => 0x00000004,
142 0 : Algorithms::MD5 => 0x00000005,
143 0 : Algorithms::MD4 => 0x00000006,
144 0 : Algorithms::MD2 => 0x00000007,
145 0 : Algorithms::RSA => 0x00000008,
146 0 : Algorithms::EC => 0x00000009,
147 0 : Algorithms::MD5_RSA => 0x0000000a,
148 0 : Algorithms::SHA1_DSA => 0x0000000b,
149 0 : Algorithms::SHA1_RSA => 0x0000000c,
150 0 : Algorithms::SHA_256_RSA => 0x0000000d,
151 0 : Algorithms::SHA_384_RSA => 0x0000000e,
152 0 : Algorithms::SHA_512_RSA => 0x0000000f,
153 0 : Algorithms::SHA1_ECDSA => 0x00000010,
154 0 : Algorithms::SHA_256_ECDSA => 0x00000011,
155 0 : Algorithms::SHA_384_ECDSA => 0x00000012,
156 0 : Algorithms::SHA_512_ECDSA => 0x00000013,
157 0 : Algorithms::UNKNOWN(_) => 0,
158 :
159 : }
160 156 : }
161 : }
162 :
163 : /// Parse a PE file from the given file path
164 0 : pub fn parse(path: &str) -> Option<Binary> {
165 0 : Binary::parse(path)
166 0 : }
167 :
168 : /// Parse a PE file from the given file path and configuration
169 0 : pub fn parse_with_config(path: &str, config: &ParserConfig) -> Option<Binary> {
170 0 : Binary::parse_with_config(path, config)
171 0 : }
|