1.下载源码

wget https://github.com/VirusTotal/yara/archive/refs/tags/v4.2.3.tar.gz

2.预编译

tar -zxf yara-4.2.0.tar.gz
cd yara-4.2.0
./bootstrap.sh

yum -y install automake libtool make gcc pkg-config
yum -y flex bison

3.编译与安装

./bootstrap.sh
./configure
make
sudo make install

4.跑测试用例

[root@localhost yara-4.2.3]# make check
Making check in libyara
make[1]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3/libyara'
make check-am
make[2]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3/libyara'
make[2]: Nothing to be done for `check-am'.
make[2]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3/libyara'
make[1]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3/libyara'
make[1]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3'
make test-arena test-alignment test-atoms test-api test-rules test-pe test-elf test-version test-bitmask test-math test-stack test-re-split test-async test-exception test-dotnet
make[2]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3'
make[2]: `test-arena' is up to date.
make[2]: `test-alignment' is up to date.
make[2]: `test-atoms' is up to date.
make[2]: `test-api' is up to date.
make[2]: `test-rules' is up to date.
make[2]: `test-pe' is up to date.
make[2]: `test-elf' is up to date.
make[2]: `test-version' is up to date.
make[2]: `test-bitmask' is up to date.
make[2]: `test-math' is up to date.
make[2]: `test-stack' is up to date.
make[2]: `test-re-split' is up to date.
make[2]: `test-async' is up to date.
make[2]: `test-exception' is up to date.
make[2]: `test-dotnet' is up to date.
make[2]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3'
make check-TESTS
make[2]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3'
make[3]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3'
PASS: test-arena
PASS: test-alignment
PASS: test-atoms
PASS: test-api
PASS: test-rules
PASS: test-pe
PASS: test-elf
PASS: test-version
PASS: test-bitmask
PASS: test-math
PASS: test-stack
PASS: test-re-split
PASS: test-async
PASS: test-exception
PASS: test-dotnet
make[4]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3'
Making all in libyara
make[5]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3/libyara'
make all-am
make[6]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3/libyara'
make[6]: Nothing to be done for `all-am'.
make[6]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3/libyara'
make[5]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3/libyara'
make[5]: Entering directory `/mnt/hgfs/GitHub/yara-4.2.3'
make[5]: Nothing to be done for `all-am'.
make[5]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3'
make[4]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3'
============================================================================
Testsuite summary for yara 4.2.3
============================================================================
# TOTAL: 15
# PASS: 15
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
make[3]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3'
make[2]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3'
make[1]: Leaving directory `/mnt/hgfs/GitHub/yara-4.2.3'

5.待分析文件,手动分析

查看文件详细信息

[root@localhost yara-4.2.3]# file tests/data/tiny-idata-5200 
tests/data/tiny-idata-5200: PE32 executable (console) Intel 80386 (stripped to external PDB), for MS Windows

以十六进制导出文件

[root@localhost yara-4.2.3]# hexdump tests/data/tiny-idata-5200 
0000000 5a4d 0090 0003 0000 0004 0000 ffff 0000
0000010 00b8 0000 0000 0000 0040 0000 0000 0000
0000020 0000 0000 0000 0000 0000 0000 0000 0000
0000030 0000 0000 0000 0000 0000 0000 0080 0000
0000040 1f0e 0eba b400 cd09 b821 4c01 21cd 6854
0000050 7369 7020 6f72 7267 6d61 6320 6e61 6f6e
0000060 2074 6562 7220 6e75 6920 206e 4f44 2053
0000070 6f6d 6564 0d2e 0a0d 0024 0000 0000 0000
0000080 4550 0000 014c 0007 56b8 56fc 0000 0000
0000090 0000 0000 00e0 030f 010b 1a02 2000 0000
00000a0 7000 0000 1000 0000 14e0 0000 1000 0000
00000b0 3000 0000 0000 0040 1000 0000 1000 0000
00000c0 0004 0000 0001 0000 0004 0000 0000 0000
00000d0 9000 0000 1000 0000 a8dc 0000 0003 0000
00000e0 0000 0020 1000 0000 0000 0010 1000 0000
00000f0 0000 0000 0010 0000 0000 0000 0000 0000
0000100 6000 0000 0590 0000 0000 0000 0000 0000
0000110 0000 0000 0000 0000 0000 0000 0000 0000
*
0000140 8004 0000 0018 0000 0000 0000 0000 0000
0000150 0000 0000 0000 0000 6104 0000 00c8 0000
0000160 0000 0000 0000 0000 0000 0000 0000 0000
0000170 0000 0000 0000 0000 742e 7865 0074 0000
0000180 1774 0000 1000 0000 2000 0000 1000 0000
0000190 0000 0000 0000 0000 0000 0000 0060 6050
00001a0 642e 7461 0061 0000 0030 0000 3000 0000
00001b0 1000 0000 3000 0000 0000 0000 0000 0000
00001c0 0000 0000 0040 c030 722e 6164 6174 0000
00001d0 0550 0000 4000 0000 1000 0000 4000 0000
00001e0 0000 0000 0000 0000 0000 0000 0040 4030
00001f0 622e 7373 0000 0000 0400 0000 5000 0000
0000200 0000 0000 0000 0000 0000 0000 0000 0000
0000210 0000 0000 0080 c060 692e 6164 6174 0000
0000220 0590 0000 6000 0000 1000 0000 5200 0000
0000230 0000 0000 0000 0000 0000 0000 0040 c030
0000240 432e 5452 0000 0000 0034 0000 7000 0000
0000250 1000 0000 6000 0000 0000 0000 0000 0000
0000260 0000 0000 0040 c030 742e 736c 0000 0000
0000270 0020 0000 8000 0000 1000 0000 7000 0000
0000280 0000 0000 0000 0000 0000 0000 0040 c030
0000290 0000 0000 0000 0000 0000 0000 0000 0000

各个段和符号表的信息

[root@localhost yara-4.2.3]# objdump -x tests/data/tiny-idata-5200 

tests/data/tiny-idata-5200: file format pei-i386
tests/data/tiny-idata-5200
architecture: i386, flags 0x00000102:
EXEC_P, D_PAGED
start address 0x004014e0

Characteristics 0x30f
relocations stripped
executable
line numbers stripped
symbols stripped
32 bit words
debugging information removed

Time/Date Thu Mar 31 06:44:08 2016
Magic 010b (PE32)
MajorLinkerVersion 2
MinorLinkerVersion 26
SizeOfCode 00002000
SizeOfInitializedData 00007000
SizeOfUninitializedData 00001000
AddressOfEntryPoint 000014e0
BaseOfCode 00001000
BaseOfData 00003000
ImageBase 00400000
SectionAlignment 00001000
FileAlignment 00001000
MajorOSystemVersion 4
MinorOSystemVersion 0
MajorImageVersion 1
MinorImageVersion 0
MajorSubsystemVersion 4
MinorSubsystemVersion 0
Win32Version 00000000
SizeOfImage 00009000
SizeOfHeaders 00001000
CheckSum 0000a8dc
Subsystem 00000003 (Windows CUI)
DllCharacteristics 00000000
SizeOfStackReserve 00200000
SizeOfStackCommit 00001000
SizeOfHeapReserve 00100000
SizeOfHeapCommit 00001000
LoaderFlags 00000000
NumberOfRvaAndSizes 00000010

The Data Directory
Entry 0 00000000 00000000 Export Directory [.edata (or where ever we found it)]
Entry 1 00006000 00000590 Import Directory [parts of .idata]
Entry 2 00000000 00000000 Resource Directory [.rsrc]
Entry 3 00000000 00000000 Exception Directory [.pdata]
Entry 4 00000000 00000000 Security Directory
Entry 5 00000000 00000000 Base Relocation Directory [.reloc]
Entry 6 00000000 00000000 Debug Directory
Entry 7 00000000 00000000 Description Directory
Entry 8 00000000 00000000 Special Directory
Entry 9 00008004 00000018 Thread Storage Directory [.tls]
Entry a 00000000 00000000 Load Configuration Directory
Entry b 00000000 00000000 Bound Import Directory
Entry c 00006104 000000c8 Import Address Table Directory
Entry d 00000000 00000000 Delay Import Directory
Entry e 00000000 00000000 CLR Runtime Header
Entry f 00000000 00000000 Reserved

There is an import table in .idata at 0x406000

The Import Tables (interpreted .idata section contents)
vma: Hint Time Forward DLL First
Table Stamp Chain Name Thunk
00006000 72754374 746e6572 636f7250 00737365 654701c5

Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00001774 00401000 00401000 00001000 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE, DATA
1 .data 00000030 00403000 00403000 00003000 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .rdata 00000550 00404000 00404000 00004000 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
3 .bss 00000400 00405000 00405000 00000000 2**5
ALLOC
4 .idata 00000590 00406000 00406000 00005200 2**2
CONTENTS, ALLOC, LOAD, DATA
5 .CRT 00000034 00407000 00407000 00006000 2**2
CONTENTS, ALLOC, LOAD, DATA
6 .tls 00000020 00408000 00408000 00007000 2**2
CONTENTS, ALLOC, LOAD, DATA
SYMBOL TABLE:
no symbols

readelf命令查看各个段的详细信息

[root@localhost yara-4.2.3]# readelf -a tests/data/tiny-idata-5200
readelf: tests/data/tiny-idata-5200: Error: Not an ELF file - it has the wrong magic bytes at the start

使用readelf读tiny-idata-5200文件出错了,这是因为readelf是读取linux下的elf格式的文件,然而tiny-idata-5200是windows下的pe格式,需要使用readpe来读取

PE(Portable Executable)意为可移植的可执行的文件,常见的EXE、DLL、OCX、SYS、COM都是PE格式文件,Windows操作系统上的程序文件(可能是间接被执行,如DLL)。
ELF(Executable and Linkable Format)意为可执行与可链接格式,一种用于二进制文件、可执行文件、目标代码、共享库和核心转储格式文件,是UNIX系统实验室(USL)作为应用程序二进制接口(Application Binary Interface,ABI)而开发和发布的,也是Linux的主要可执行文件格式。

centos中安装readpe

pip3.7 install readpe
[root@localhost yara-4.2.3]# readpe tests/data/tiny-idata-5200 -Hs
##############
# DOS_HEADER #
##############
╒══════════╤══════════════════════════════════════════╤═════════════════════╕
│ Offset │ Name │ Value │
╞══════════╪══════════════════════════════════════════╪═════════════════════╡
│ 0x0 │ Magic number │ 5a4d "MZ" │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x2 │ Bytes on last page of file │ 0x90 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x4 │ Pages in file │ 0x3 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x6 │ Relocations │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x8 │ Size of header in paragraphs │ 0x4 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0xa │ Minimum extra paragraphs needed │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0xc │ Maximum extra paragraphs needed │ 0xffff │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0xe │ Initial (relative) SS value │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x10 │ Initial SP value │ 0xb8 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x12 │ Checksum │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x14 │ Initial IP value │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x16 │ Initial (relative) CS value │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x18 │ File address of relocation table │ 0x40 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x1a │ Overlay number │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x1c │ Reserved words[4] │ 0,0,0,0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x24 │ OEM identifier (for OEM information) │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x26 │ OEM information; OEM identifier specific │ 0x0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x28 │ Reserved words[10] │ 0,0,0,0,0,0,0,0,0,0 │
├──────────┼──────────────────────────────────────────┼─────────────────────┤
│ 0x3c │ File address of new exe header │ 0x80 │
╘══════════╧══════════════════════════════════════════╧═════════════════════╛

###############
# FILE_HEADER #
###############
╒══════════╤══════════════════════╤════════════╤════════════════════════════════╕
│ Offset │ Name │ Value │ Description │
╞══════════╪══════════════════════╪════════════╪════════════════════════════════╡
│ 0x84 │ Machine │ 0x14c │ IMAGE_FILE_MACHINE_I386 │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ 0x86 │ NumberOfSections │ 0x7 │ 7 │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ 0x88 │ TimeDateStamp │ 0x56FC56B8 │ Wed Mar 30 22:44:08 2016 UTC │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ 0x8c │ PointerToSymbolTable │ 0x0 │ 0 │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ 0x90 │ NumberOfSymbols │ 0x0 │ 0 │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ 0x94 │ SizeOfOptionalHeader │ 0xe0 │ 224 │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ 0x96 │ Characteristics │ 0x30f │ 783 │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ │ │ 0x1 │ IMAGE_FILE_RELOCS_STRIPPED │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ │ │ 0x2 │ IMAGE_FILE_EXECUTABLE_IMAGE │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ │ │ 0x4 │ IMAGE_FILE_LINE_NUMS_STRIPPED │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ │ │ 0x8 │ IMAGE_FILE_LOCAL_SYMS_STRIPPED │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ │ │ 0x100 │ IMAGE_FILE_32BIT_MACHINE │
├──────────┼──────────────────────┼────────────┼────────────────────────────────┤
│ │ │ 0x200 │ IMAGE_FILE_DEBUG_STRIPPED │
╘══════════╧══════════════════════╧════════════╧════════════════════════════════╛

###################
# OPTIONAL_HEADER #
###################
╒══════════╤═════════════════════════════╤══════════╤═══════════════╕
│ Offset │ Name │ Value │ Description │
╞══════════╪═════════════════════════════╪══════════╪═══════════════╡
│ 0x98 │ Magic │ 0x10b │ NT32 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0x9a │ MajorLinkerVersion │ 0x2 │ 2 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0x9b │ MinorLinkerVersion │ 0x1a │ 26 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0x9c │ SizeOfCode │ 0x2000 │ 8192 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xa0 │ SizeOfInitializedData │ 0x7000 │ 28672 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xa4 │ SizeOfUninitializedData │ 0x1000 │ 4096 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xa8 │ AddressOfEntryPoint │ 0x14e0 │ 5344 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xac │ BaseOfCode │ 0x1000 │ 4096 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xb0 │ BaseOfData │ 0x3000 │ 12288 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xb4 │ ImageBase │ 0x400000 │ 4194304 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xb8 │ SectionAlignment │ 0x1000 │ 4096 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xbc │ FileAlignment │ 0x1000 │ 4096 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xc0 │ MajorOperatingSystemVersion │ 0x4 │ 4 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xc2 │ MinorOperatingSystemVersion │ 0x0 │ 0 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xc4 │ MajorImageVersion │ 0x1 │ 1 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xc6 │ MinorImageVersion │ 0x0 │ 0 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xc8 │ MajorSubsystemVersion │ 0x4 │ 4 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xca │ MinorSubsystemVersion │ 0x0 │ 0 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xcc │ Reserved1 │ 0x0 │ 0 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xd0 │ SizeOfImage │ 0x9000 │ 36864 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xd4 │ SizeOfHeaders │ 0x1000 │ 4096 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xd8 │ CheckSum │ 0xa8dc │ 43228 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xdc │ Subsystem │ 0x3 │ WINDOWS_CUI │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xde │ DllCharacteristics │ 0x0 │ 0 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xe0 │ SizeOfStackReserve │ 0x200000 │ 2097152 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xe4 │ SizeOfStackCommit │ 0x1000 │ 4096 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xe8 │ SizeOfHeapReserve │ 0x100000 │ 1048576 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xec │ SizeOfHeapCommit │ 0x1000 │ 4096 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xf0 │ LoaderFlags │ 0x0 │ 0 │
├──────────┼─────────────────────────────┼──────────┼───────────────┤
│ 0xf4 │ NumberOfRvaAndSizes │ 0x10 │ 16 │
╘══════════╧═════════════════════════════╧══════════╧═══════════════╛

####################
# DATA_DIRECTORIES #
####################
╒══════════╤══════════════════════════════════════╤═══════════════════╤════════╕
│ Offset │ Data Directory │ Virtual Address │ Size │
╞══════════╪══════════════════════════════════════╪═══════════════════╪════════╡
│ 0xf8 │ IMAGE_DIRECTORY_ENTRY_EXPORT │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x100 │ IMAGE_DIRECTORY_ENTRY_IMPORT │ 0x6000 │ 0x590 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x108 │ IMAGE_DIRECTORY_ENTRY_RESOURCE │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x110 │ IMAGE_DIRECTORY_ENTRY_EXCEPTION │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x118 │ IMAGE_DIRECTORY_ENTRY_SECURITY │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x120 │ IMAGE_DIRECTORY_ENTRY_BASERELOC │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x128 │ IMAGE_DIRECTORY_ENTRY_DEBUG │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x130 │ IMAGE_DIRECTORY_ENTRY_COPYRIGHT │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x138 │ IMAGE_DIRECTORY_ENTRY_GLOBALPTR │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x140 │ IMAGE_DIRECTORY_ENTRY_TLS │ 0x8004 │ 0x18 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x148 │ IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x150 │ IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x158 │ IMAGE_DIRECTORY_ENTRY_IAT │ 0x6104 │ 0xc8 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x160 │ IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x168 │ IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR │ 0x0 │ 0x0 │
├──────────┼──────────────────────────────────────┼───────────────────┼────────┤
│ 0x170 │ IMAGE_DIRECTORY_ENTRY_RESERVED │ 0x0 │ 0x0 │
╘══════════╧══════════════════════════════════════╧═══════════════════╧════════╛
############
# SECTIONS #
############
╒════════╤═════════════╤════════════╤═════════════════╤════════════════╤═══════════════════╤═════════╤═══════════╕
│ Name │ Raw Addr. │ Raw Size │ Virtual Addr. │ Virtual Size │ Characteristics │ Flags │ Entropy │
╞════════╪═════════════╪════════════╪═════════════════╪════════════════╪═══════════════════╪═════════╪═══════════╡
│ .text │ 0x1000 │ 0x2000 │ 0x1000 │ 0x1774 │ 0x60500060 │ R-E │ 4.89182 │
├────────┼─────────────┼────────────┼─────────────────┼────────────────┼───────────────────┼─────────┼───────────┤
│ .data │ 0x3000 │ 0x1000 │ 0x3000 │ 0x30 │ 0xc0300040 │ RW- │ 0.0892371 │
├────────┼─────────────┼────────────┼─────────────────┼────────────────┼───────────────────┼─────────┼───────────┤
│ .rdata │ 0x4000 │ 0x1000 │ 0x4000 │ 0x550 │ 0x40300040 │ R-- │ 2.42312 │
├────────┼─────────────┼────────────┼─────────────────┼────────────────┼───────────────────┼─────────┼───────────┤
│ .bss │ 0x0 │ 0x0 │ 0x5000 │ 0x400 │ 0xc0600080 │ RW- │ 0 │
├────────┼─────────────┼────────────┼─────────────────┼────────────────┼───────────────────┼─────────┼───────────┤
│ .idata │ 0x5200 │ 0x1000 │ 0x6000 │ 0x590 │ 0xc0300040 │ RW- │ 1.59346 │
├────────┼─────────────┼────────────┼─────────────────┼────────────────┼───────────────────┼─────────┼───────────┤
│ .CRT │ 0x6000 │ 0x1000 │ 0x7000 │ 0x34 │ 0xc0300040 │ RW- │ 0.0447354 │
├────────┼─────────────┼────────────┼─────────────────┼────────────────┼───────────────────┼─────────┼───────────┤
│ .tls │ 0x7000 │ 0x1000 │ 0x8000 │ 0x20 │ 0xc0300040 │ RW- │ 0.0336544 │
╘════════╧═════════════╧════════════╧═════════════════╧════════════════╧═══════════════════╧═════════╧═══════════╛

6.yara规则匹配源码分析

测试调用入口

assert_true_rule_file(
"import \"pe\" \
rule test { \
condition: \
pe.imports(\"KERNEL32.dll\", \"DeleteCriticalSection\") \
}",
"tests/data/tiny-idata-51ff");

参数1是否配置 

"import \"pe\" \
      rule test { \
        condition: \
          pe.imports(\"KERNEL32.dll\", \"DeleteCriticalSection\") \
      }"

参数2待配置的文件

"tests/data/tiny-idata-51ff"

 判断文件是否匹配上规则,read_file先将待分析文件整体读到buf中,然后matches_blob进行匹配

#define assert_true_rule_file(rule, filename)                           \
do { \
char* buf; \
size_t sz; \
if ((sz = read_file(prefix_top_srcdir(filename), &buf)) == -1) { \
fprintf(stderr, "%s:%d: cannot read file '%s'\n", \
__FILE__, __LINE__, filename); \
exit(EXIT_FAILURE); \
} \
if (!matches_blob(rule, (uint8_t*) (buf), sz, NULL, 0)) { \
fprintf(stderr, "%s:%d: rule does not match contents of" \
"'%s' (but should)\n", \
__FILE__, __LINE__, filename); \
exit(EXIT_FAILURE); \
} \
free(buf); \
} while (0);

 匹配函数matches_blob

int matches_blob(
char* rule,
uint8_t* blob,
size_t blob_size,
uint8_t* module_data,
size_t module_data_size)
{
YR_DEBUG_FPRINTF(
2, stderr, "+ %s(blob_size=%zu) {\n", __FUNCTION__, blob_size);

YR_RULES* rules;

if (blob == NULL)
{
blob = (uint8_t*) "dummy";
blob_size = 5;
}

if (compile_rule(rule, &rules) != ERROR_SUCCESS)
{
fprintf(
stderr, "failed to compile rule << %s >>: %s\n", rule, compile_error);
exit(EXIT_FAILURE);
}

SCAN_CALLBACK_CTX ctx = {
.matches = 0,
.module_data = module_data,
.module_data_size = module_data_size,
};

int flags = SCAN_FLAGS_NO_TRYCATCH;
YR_CALLBACK_FUNC callback = _scan_callback;

void* user_data = &ctx;
int timeout = 0;
int scan_result;

if (matches_blob_uses_default_iterator)
{
scan_result = yr_rules_scan_mem(
rules, blob, blob_size, flags, callback, user_data, timeout);
}
else
{
YR_SCANNER* scanner;

assert_true_expr(ERROR_SUCCESS == yr_scanner_create(rules, &scanner));

yr_scanner_set_callback(scanner, callback, user_data);
yr_scanner_set_timeout(scanner, timeout);
yr_scanner_set_flags(scanner, flags);

YR_MEMORY_BLOCK_ITERATOR iterator;
YR_TEST_ITERATOR_CTX iterator_ctx;

init_test_iterator(&iterator, &iterator_ctx, blob, blob_size);

scan_result = yr_scanner_scan_mem_blocks(scanner, &iterator);

yr_scanner_destroy(scanner);
}

if (scan_result != ERROR_SUCCESS)
{
fprintf(
stderr,
"failed to scan using rule << %s >>: error: %d\n",
rule,
scan_result);
exit(EXIT_FAILURE);
}

yr_rules_destroy(rules);

YR_DEBUG_FPRINTF(
2, stderr, "} = %u AKA ctx.matches // %s()\n", ctx.matches, __FUNCTION__);

return ctx.matches;
}

 matches_blob调用栈流程图

yara 分析器_网络

参考

​Getting started — yara 4.2.0 documentation​

​linux实践之ELF文件分析 - 走看看​