[Hands-on] “AI, create a rule for me!” πŸ€– Automatically Generate YARA Rules in 1 Second with Selected Features

Hello, smart security analysts! πŸ•΅οΈβ€β™‚οΈπŸ’»

Last time, we identified the “flower” of malware analysis: ‘Unique Signatures’. From typos left by hackers, project paths, to core machine code (Hex) that never changes… excellent ingredients have been gathered, haven’t they? πŸ₯©πŸ₯¦πŸ₯•

Now it’s time to use these ingredients to complete the “dish” called YARA Rule. However, manually typing YARA syntax (rule, meta, strings, $a = …) can lead to typos and mismatched parentheses, causing errors.

So today, we will conduct a prompt engineering hands-on session where we simply provide the features we found to AI (ChatGPT, Claude, etc.) and ask it to generate YARA rule code with perfect syntax.

Let’s create an expert-level rule file in just 1 minute! β±οΈπŸš€


1. πŸ“‹ Preparations: Results from Last Time

Please prepare your ‘Personal Fingerprint List’ compiled from the last hands-on session. (Example data is as follows.)

  • Malware Name: Ransomware_DarkV1
  • Text String 1: “C:ProjectDarkbot.pdb” (PDB Path)
  • Text String 2: “Don’t kill my process!” (Unusual Message)
  • Hex String: { E8 ?? ?? ?? ?? 85 C0 74 0F } (Core Function Call and Branch)

2. πŸ—£οΈ [Hands-on Step 1] Hypnotize AI: “You are a YARA expert”

To prevent AI from generating sloppy rules, you must assign it the persona of a ‘Senior Analyst’ and demand ‘strict adherence to syntax’.

πŸ—οΈ YARA Generation Master Prompt

Copy the content below and input it into the AI. (Fill in the parentheses with your data!)

[Role Assignment] You are a malware analyst with 15 years of experience and an expert in YARA rule creation.

>

[Task Request] Based on the ‘Selected Features’ I provide, please write valid YARA rule code that can detect the corresponding malware.

>

[Input Data]

>

1. Rule Name: Win32_Ransomware_DarkV1

2. Text Strings:

>

“C:ProjectDarkbot.pdb” (Type: ascii, nocase)

>

1. – “Don’t kill my process!” (Type: wide, ascii)

2. Hex String:

– { E8 ?? ?? ?? ?? 85 C0 74 0F } (Description: Key Verification Logic)

>

[Writing Guidelines – Important!]

>

1. Meta Section: Include Author (Me), Date (Today), Description, and Threat Level (High).

2. Strings Section: Name variables intuitively, such as $s_pdb, $s_msg, $h_logic.

3. Condition Section:

– First, check if the file header is MZ (uint16(0) == 0x5A4D). (Optimization)

– And (AND) ensure the hex string is always included, and configure the logic to detect if at least one text string is found.

4. Comments: Add brief comments (//) for each section and string.


3. πŸ“€ [Hands-on Step 2] Review and Verify AI-Generated Code

When you input the prompt above, the AI will output neatly formatted code like the example below.

πŸ€– AI Generation Result (Example)

rule Win32_Ransomware_DarkV1 {
    meta:
        author = "Me"
        date = "2025-12-16"
        description = "Detects DarkV1 Ransomware based on PDB path and unique strings"
        severity = "High"
        version = "1.0"

    strings:
        // PDB Path found in the binary (Unique identifier)
        $s_pdb = "C:\Project\Dark\bot.pdb" ascii nocase

        // Suspicious message displayed to user
        $s_msg = "Don't kill my process!" wide ascii

        // Key Verification Logic (Function Call + Test + JZ)
        // Wildcards (??) used for variable address
        $h_logic = { E8 ?? ?? ?? ?? 85 C0 74 0F }

    condition:
        // 1. Check for valid Windows PE Header (Magic Number MZ)
        uint16(0) == 0x5A4D and 

        // 2. File size check to reduce false positives (Optional but recommended)
        filesize < 5MB and

        // 3. Main Detection Logic:
        // Hex signature MUST exist AND at least one text string
        $h_logic and ($s_pdb or $s_msg)
}

πŸ” Verification Points (Human Verification) You need to check with an analyst’s eye whether the AI has done a good job.

  1. Escape Characters: Are backslashes () correctly doubled (\) in the path? (YARA recognizes as a special character, so it must be written as \. AI handles this brilliantly! πŸ‘)
  2. Variable Names: Are variable names like $s_pdb, $h_logic beautifully named as we requested?
  3. Condition Logic:
  • Is uint16(0) == 0x5A4D (header check) at the very beginning? (Essential for speed optimization)
  • Is the parenthetical handling for $h_logic and ($s_pdb or $s_msg) logically correct?

4. πŸ’‘ Pro Tip: Make Conditions Smarter!

If you are concerned about false positives, you can ask the AI to “modify the conditions to be stricter.”

[Additional Request Prompt] “Modify the Condition section. The hex string must always be present, and both (ALL) text strings must be found for detection. Also, add a check to only scan if the file size is less than 2MB.”

This way, the AI will change it to and ($s_pdb and $s_msg) and add the filesize < 2MB condition.


5. ⚠️ Caution: Check for AI ‘Hallucinations’

Sometimes AI creates syntax that doesn’t exist in YARA. (Especially in the regular expression part)

  • Tip: Paste the generated code into an editor like VS Code (with YARA extension installed). If there are syntax errors, a red underline will appear. πŸ–οΈ

πŸŽ‰ Conclusion: Now, just ‘Copy & Paste’ and you’re done!

Congratulations! πŸ‘ Now, without having to sift through complex YARA manuals, you can instantly create detection rules in 3 steps: “Feature Discovery -> Request to AI -> Code Generation”.

This method particularly shines in fast-paced environments like Security Operations Centers (SOC) or Computer Emergency Response Teams (CERT). It’s an efficient division of labor where analysts focus on ‘feature discovery’ and leave ‘coding’ to AI.

Next time, we will conduct a hands-on session to “scan actual malware samples with the generated YARA rule file and test if they are properly detected.” Look forward to the thrilling moment when your self-made antivirus works! πŸ›‘οΈβœ¨


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *