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