[Forensics-changes] [yara] 103/192: Fix endian issues with section name extraction (#607)

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:31:52 UTC 2017


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag v3.6.0
in repository yara.

commit 0e2c3a7cf35639a369e4e86d82227de67668bdae
Author: Jacob Baines <baines.jacob at gmail.com>
Date:   Wed Feb 15 03:36:45 2017 -0500

    Fix endian issues with section name extraction (#607)
---
 libyara/modules/elf.c |   3 +-
 tests/blob.h          | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-elf.c      |  21 +++++++
 3 files changed, 178 insertions(+), 1 deletion(-)

diff --git a/libyara/modules/elf.c b/libyara/modules/elf.c
index dff586e..c6ee384 100644
--- a/libyara/modules/elf.c
+++ b/libyara/modules/elf.c
@@ -164,7 +164,8 @@ void parse_elf_header_##bits##_##bo(                                           \
     section = (elf##bits##_section_header_t*)                                  \
       ((uint8_t*) elf + yr_##bo##bits##toh(elf->sh_offset));                   \
                                                                                \
-    if (section[yr_##bo##16toh(elf->sh_str_table_index)].offset < elf_size)    \
+    if (yr_##bo##bits##toh(                                                    \
+      section[yr_##bo##16toh(elf->sh_str_table_index)].offset) < elf_size)     \
     {                                                                          \
       str_table = (char*) elf + yr_##bo##bits##toh(                            \
           section[yr_##bo##16toh(elf->sh_str_table_index)].offset);            \
diff --git a/tests/blob.h b/tests/blob.h
index 08bf5b6..1659a91 100644
--- a/tests/blob.h
+++ b/tests/blob.h
@@ -178,3 +178,158 @@ uint8_t ELF64_FILE[] = {
   0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 };
+
+uint8_t ELF32_MIPS_FILE[] = {
+  0x7f, 0x45, 0x4c, 0x46, 0x01, 0x02, 0x01, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x02, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x40, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x34,
+  0x00, 0x00, 0x03, 0x04, 0x00, 0x00, 0x10, 0x07,
+  0x00, 0x34, 0x00, 0x20, 0x00, 0x03, 0x00, 0x28,
+  0x00, 0x0b, 0x00, 0x08, 0x70, 0x00, 0x00, 0x03,
+  0x00, 0x00, 0x00, 0x98, 0x00, 0x40, 0x00, 0x98,
+  0x00, 0x40, 0x00, 0x98, 0x00, 0x00, 0x00, 0x18,
+  0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x04,
+  0x00, 0x00, 0x00, 0x08, 0x70, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0xb0, 0x00, 0x40, 0x00, 0xb0,
+  0x00, 0x40, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x18,
+  0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x04,
+  0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
+  0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
+  0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x05,
+  0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x80, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x80, 0xd0,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x03, 0xe0, 0x00, 0x08, 0x24, 0x02, 0x00, 0x2a,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x47, 0x43, 0x43, 0x3a, 0x20, 0x28, 0x47, 0x4e,
+  0x55, 0x29, 0x20, 0x35, 0x2e, 0x33, 0x2e, 0x30,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0xd0,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d,
+  0x00, 0x00, 0x00, 0x1f, 0x41, 0x00, 0x00, 0x00,
+  0x0f, 0x67, 0x6e, 0x75, 0x00, 0x01, 0x00, 0x00,
+  0x00, 0x07, 0x04, 0x01, 0x00, 0x2e, 0x73, 0x79,
+  0x6d, 0x74, 0x61, 0x62, 0x00, 0x2e, 0x73, 0x74,
+  0x72, 0x74, 0x61, 0x62, 0x00, 0x2e, 0x73, 0x68,
+  0x73, 0x74, 0x72, 0x74, 0x61, 0x62, 0x00, 0x2e,
+  0x4d, 0x49, 0x50, 0x53, 0x2e, 0x61, 0x62, 0x69,
+  0x66, 0x6c, 0x61, 0x67, 0x73, 0x00, 0x2e, 0x72,
+  0x65, 0x67, 0x69, 0x6e, 0x66, 0x6f, 0x00, 0x2e,
+  0x74, 0x65, 0x78, 0x74, 0x00, 0x2e, 0x63, 0x6f,
+  0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x2e, 0x70,
+  0x64, 0x72, 0x00, 0x2e, 0x67, 0x6e, 0x75, 0x2e,
+  0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74,
+  0x65, 0x73, 0x00, 0x2e, 0x6d, 0x64, 0x65, 0x62,
+  0x75, 0x67, 0x2e, 0x61, 0x62, 0x69, 0x33, 0x32,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x40, 0x00, 0x98, 0x00, 0x00, 0x00, 0x00,
+  0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x40, 0x00, 0xb0, 0x00, 0x00, 0x00, 0x00,
+  0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x40, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00,
+  0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x03, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x03, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x03, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x03, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x04, 0x00, 0xff, 0xf1, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x04, 0x00, 0xff, 0xf1, 0x00, 0x00, 0x00, 0x08,
+  0x00, 0x41, 0x80, 0xd0, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0xff, 0xf1, 0x00, 0x00, 0x00, 0x0c,
+  0x00, 0x41, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
+  0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x13,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b,
+  0x00, 0x40, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00,
+  0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x22,
+  0x00, 0x41, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
+  0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x2e,
+  0x00, 0x40, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x08,
+  0x12, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x33,
+  0x00, 0x41, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
+  0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x3a,
+  0x00, 0x41, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
+  0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x3f,
+  0x00, 0x41, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
+  0x10, 0x00, 0x00, 0x03, 0x00, 0x6d, 0x61, 0x69,
+  0x6e, 0x2e, 0x63, 0x00, 0x5f, 0x67, 0x70, 0x00,
+  0x5f, 0x66, 0x64, 0x61, 0x74, 0x61, 0x00, 0x5f,
+  0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x5f,
+  0x66, 0x74, 0x65, 0x78, 0x74, 0x00, 0x5f, 0x5f,
+  0x62, 0x73, 0x73, 0x5f, 0x73, 0x74, 0x61, 0x72,
+  0x74, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x5f,
+  0x65, 0x64, 0x61, 0x74, 0x61, 0x00, 0x5f, 0x65,
+  0x6e, 0x64, 0x00, 0x5f, 0x66, 0x62, 0x73, 0x73,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b,
+  0x70, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x02,
+  0x00, 0x40, 0x00, 0x98, 0x00, 0x00, 0x00, 0x98,
+  0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
+  0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x2a,
+  0x70, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x02,
+  0x00, 0x40, 0x00, 0xb0, 0x00, 0x00, 0x00, 0xb0,
+  0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
+  0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x33,
+  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06,
+  0x00, 0x40, 0x00, 0xd0, 0x00, 0x00, 0x00, 0xd0,
+  0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39,
+  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x30,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
+  0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x42,
+  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4,
+  0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47,
+  0x6f, 0xff, 0xff, 0xf5, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x14,
+  0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57,
+  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x24,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
+  0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x24,
+  0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8c,
+  0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 0x0a,
+  0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x04,
+  0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x09,
+  0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xbc,
+  0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+  0x00, 0x00, 0x00, 0x00
+};
\ No newline at end of file
diff --git a/tests/test-elf.c b/tests/test-elf.c
index 8d13b47..6415933 100644
--- a/tests/test-elf.c
+++ b/tests/test-elf.c
@@ -24,6 +24,27 @@ int main(int argc, char** argv)
         condition: $a at elf.entry_point }",
       ELF64_FILE);
 
+  assert_true_rule_blob("import \"elf\" rule test { \
+    condition: elf.sections[2].name == \".comment\" }", ELF64_FILE);
+
+  assert_true_rule_blob("import \"elf\" rule test { \
+    condition: elf.machine == elf.EM_MIPS }", ELF32_MIPS_FILE);
+
+  assert_true_rule_blob("import \"elf\" rule test { \
+    condition: elf.number_of_sections == 11 and \
+    elf.number_of_segments == 3 }", ELF32_MIPS_FILE);
+
+  assert_true_rule_blob("import \"elf\" rule test { \
+    condition: for any i in (0..elf.number_of_sections): ( \
+    elf.sections[i].type == elf.SHT_PROGBITS and \
+    elf.sections[i].name == \".text\")}", ELF32_MIPS_FILE);
+
+  assert_true_rule_blob("import \"elf\" rule test { \
+    condition: for any i in (0..elf.number_of_segments): ( \
+    elf.segments[i].type == elf.PT_LOAD and \
+    elf.segments[i].virtual_address == 0x00400000 and \
+    elf.segments[i].file_size == 0xe0)}", ELF32_MIPS_FILE);
+
   yr_finalize();
   return 0;
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git



More information about the forensics-changes mailing list