# III. Phát hiện mối đe dọa bằng YARA

## Tổng quát về YARA

```
rule Detect_Mimikatz 
{
    strings:
        $mimi_str1 = "sekurlsa::logonpasswords"
        $mimi_str2 = "Mimikatz"
		$mimi_str3 = "mimikatz"
    condition:
        any of ($mimi_str1, $mimi_str2, $mimi_str3)
}
rule Detect_DOCX
{
	strings:
		$docx_str1 = { 50 4B 03 04 14 00 06 00 }
		$docx_str2 = "word/document.xml"
	condition:
		$docx_str1 at 0 and $docx_str2
}
rule Detect_XLSX
{
	strings:
		$xlsx_str1 = { 50 4B 03 04 14 00 06 00 }
        $xlsx_str2 = "xl/workbook.xml"
	condition:
		$xlsx_str1 at 0 and $xlsx_str2
}
rule Detect_PDF
{
	strings:
		$pdf_str1 = "%PDF-"
		$pdf_str2 = "%%EOF"
	condition:
		$pdf_str1 at 0 and $pdf_str2
}
rule Detect_PE
{
	strings:
		$pe_str = "MZ"
	condition:
		$pe_str at 0
}
rule Detect_ELF
{
	strings:
		$elf_str = { 7F 45 4C 46 }
	condition:
		$elf_str at 0
}
```

Ví dụ như trên có thể dễ dàng hiểu, và muốn đi sâu hơn thì ta sẽ tìm hiểu ở link chi tiết bên dưới nhé \[1]

Sau khi ta tạo được file chứa các rule đấy thì ta sẽ compile file này thành file yara để có thể tìm kiếm.

Câu lệnh như sau: `yarac [OPTION]... [NAMESPACE:]SOURCE_FILE... OUTPUT_FILE`

Ví dụ như trên: `yarac rule.txt rule.yar` -> ta sẽ nhận được file biên dịch `rule.yar`

Tiếp theo là ta sẽ thực hiện quét đối với những rule mà mình vừa đặt nhé...

Với cú pháp: `yara [OPTION]... [NAMESPACE:]RULES_FILE... FILE | DIR | PID`

Với ví dụ trên đó sẽ là: `yara -C rule.yar -r C:\Users\IEUser\Downloads`

Với câu lệnh trên chúng ta sẽ quét được thư mục `Downloads` và các thư mục con chứa trong nó (với việc sử dụng `-r`).

Như vậy về cơ bản là vậy.

Điều quan trọng của bài thực hành lần này đó là khả năng phân tích của chúng ta để từ đó ta đưa được ra rule nhận dạng những loại phần mềm nào là nguy hiểm đối với hệ thống của chúng ta. Hay có thể đơn giản là ta muốn quét để lọc những file nào có những đặc điểm tương đồng như cùng một loại tập tin, cũng có thể là chứa cùng dữ liệu gì đó...

Bài 3 đến đây kết thúc nhé ....

***

Nguồn:

1. Học về các quy tắc để viết file rule: <https://yara.readthedocs.io/en/stable/writingrules.html>
2. Học về cách thực thi bằng câu lệnh: <https://yara.readthedocs.io/en/stable/commandline.html>
3. Tải YARA: <https://github.com/VirusTotal/yara/releases/download/v4.5.0/yara-master-2251-win64.zip>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viettaliii.gitbook.io/home/education/window-pe-.net/phong-thi-nghiem-phong-thu/iii.-phat-hien-moi-de-doa-bang-yara.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
