Skip to content

Commit

Permalink
feat: implement is_64_bits, is_32_bits and is_dll functions in …
Browse files Browse the repository at this point in the history
…PE module
  • Loading branch information
plusvic committed Nov 21, 2023
1 parent 76f64aa commit f33dde9
Show file tree
Hide file tree
Showing 28 changed files with 200 additions and 56 deletions.
21 changes: 21 additions & 0 deletions yara-x/src/modules/pe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ fn main(input: &[u8]) -> PE {
}
}

/// Returns true if the file is a 32-bit PE.
#[module_export]
fn is_32_bits(ctx: &ScanContext) -> Option<bool> {
let magic = ctx.module_output::<PE>()?.opthdr_magic?;
Some(magic.value() == OptHdrMagic::IMAGE_NT_OPTIONAL_HDR32_MAGIC as i32)
}

/// Returns true if the file is a 64-bit PE.
#[module_export]
fn is_64_bits(ctx: &ScanContext) -> Option<bool> {
let magic = ctx.module_output::<PE>()?.opthdr_magic?;
Some(magic.value() == OptHdrMagic::IMAGE_NT_OPTIONAL_HDR64_MAGIC as i32)
}

/// Returns true if the file is dynamic link library (DLL)
#[module_export]
fn is_dll(ctx: &ScanContext) -> Option<bool> {
let characteristics = ctx.module_output::<PE>()?.characteristics?;
Some(characteristics & Characteristics::FILE_DLL as u32 != 0)
}

/// Returns the PE checksum, as calculated by YARA.
///
/// This is useful for comparing with the checksum appearing in the PE header
Expand Down
10 changes: 8 additions & 2 deletions yara-x/src/modules/pe/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,14 @@ impl From<PE<'_>> for pe::PE {
result.set_pointer_to_symbol_table(pe.pe_hdr.symbol_table_offset);
result.set_number_of_symbols(pe.pe_hdr.number_of_symbols);
result.set_size_of_optional_header(pe.pe_hdr.size_of_optional_header.into());


result.opthdr_magic = pe
.optional_hdr
.magic
.try_into()
.ok()
.map(EnumOrUnknown::<pe::OptHdrMagic>::from_i32);

result.subsystem = pe
.optional_hdr
.subsystem
Expand Down Expand Up @@ -1957,7 +1964,6 @@ impl From<PE<'_>> for pe::PE {

// TODO
// number_of_version_infos
// opthdr_magic

result.linker_version = MessageField::some(pe::Version {
major: Some(pe.optional_hdr.major_linker_version.into()),
Expand Down
54 changes: 54 additions & 0 deletions yara-x/src/modules/pe/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,57 @@ fn locale_and_language() {
&pe
);
}

#[test]
fn is_32bits() {
let pe = create_binary_from_zipped_ihex(
"src/modules/pe/tests/testdata/0ba6042247d90a187919dd88dc2d55cd882c80e5afc511c4f7b2e0e193968f7f.in.zip",
);

rule_true!(
r#"
import "pe"
rule test {
condition:
pe.is_32_bits()
}
"#,
&pe
);
}

#[test]
fn is_64bits() {
let pe = create_binary_from_zipped_ihex(
"src/modules/pe/tests/testdata/2e9c671b8a0411f2b397544b368c44d7f095eb395779de0ad1ac946914dfa34c.in.zip",
);

rule_true!(
r#"
import "pe"
rule test {
condition:
pe.is_64_bits()
}
"#,
&pe
);
}

#[test]
fn is_dll() {
let pe = create_binary_from_zipped_ihex(
"src/modules/pe/tests/testdata/079a472d22290a94ebb212aa8015cdc8dd28a968c6b4d3b88acdd58ce2d3b885.in.zip",
);

rule_true!(
r#"
import "pe"
rule test {
condition:
pe.is_dll()
}
"#,
&pe
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 14
minor: 29
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 258
dll_characteristics: 33088
timestamp: 1626863112
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 10
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 8450
dll_characteristics: 320
timestamp: 1528213185
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 48
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 8226
dll_characteristics: 34112
timestamp: 3665045795
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 7
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 271
dll_characteristics: 32768
timestamp: 998081829
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 14
minor: 13
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR64_MAGIC
characteristics: 8226
dll_characteristics: 16736
timestamp: 1827812126
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 6
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 271
dll_characteristics: 0
timestamp: 1524722207
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 8
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 258
dll_characteristics: 33088
timestamp: 1621233906
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 2
minor: 56
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 8974
dll_characteristics: 0
timestamp: 1274346651
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 2
minor: 51
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR64_MAGIC
characteristics: 47
dll_characteristics: 0
timestamp: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 8
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 259
dll_characteristics: 1024
timestamp: 1162198621
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 2
minor: 56
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR64_MAGIC
characteristics: 782
dll_characteristics: 0
timestamp: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 2
minor: 26
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 783
dll_characteristics: 0
timestamp: 1459377848
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 48
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 8226
dll_characteristics: 34112
timestamp: 3435013737
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 2
minor: 20
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR64_MAGIC
characteristics: 518
dll_characteristics: 0
timestamp: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 14
minor: 20
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR64_MAGIC
characteristics: 34
dll_characteristics: 49504
timestamp: 1776026023
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 8
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 259
dll_characteristics: 1024
timestamp: 1162198621
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 2
minor: 56
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR64_MAGIC
characteristics: 782
dll_characteristics: 0
timestamp: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 2
minor: 26
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 783
dll_characteristics: 0
timestamp: 1459377848
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 9
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 258
dll_characteristics: 1024
timestamp: 1301987779
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 14
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR64_MAGIC
characteristics: 34
dll_characteristics: 33056
timestamp: 1629390430
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 9
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 8450
dll_characteristics: 0
timestamp: 1299001425
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 10
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 8450
dll_characteristics: 1344
timestamp: 1314765018
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linker_version {
major: 0
minor: 0
}
opthdr_magic: IMAGE_NT_OPTIONAL_HDR32_MAGIC
characteristics: 270
dll_characteristics: 0
timestamp: 0
Expand Down
Loading

0 comments on commit f33dde9

Please sign in to comment.