// hermes_pymapper.yar

import "pe"

rule PyMapper_Obfuscated_Python
{
    meta:
        description = "Retained PyMapper XOR wrapper and keystream source structure"
        target_class = "stage1-obfuscated"
        status = "experimental"
        date = "2026-10-24"
    strings:
        $xor = "lambda a,k:bytes(v^k for v in a)" ascii
        $check = "sum((w[i]+p)^i for i in range(len(w)))&255" ascii
        $block = "min(32,len(d)-p)" ascii
        $stream = "o[p+i]=d[p+i]^b[i]" ascii
    condition:
        filesize < 16MB and all of them
}

rule PyMapper_Embedded_PE_Python
{
    meta:
        description = "Retained embedded PE Python wrapper with mapper structure"
        target_class = "stage1-embedded"
        status = "experimental"
        date = "2026-10-24"
    strings:
        $begin = "# >>> PE_B64_BEGIN" ascii
        $end = "# >>> PE_B64_END" ascii
        $allocation = "MEM_COMMIT, MEM_RESERVE = 0x1000, 0x2000" ascii
        $reloc = "mem[target_rva : target_rva + 8]" ascii
    condition:
        filesize < 16MB and all of them
}

rule PyMapper_ServerClientMapper_Source
{
    meta:
        description = "Retained Stage 2 Python mapper source"
        target_class = "stage2-mapper"
        status = "experimental"
        date = "2026-10-24"
    strings:
        $class = "class ServerClientMapper:" ascii
        $magic = "PKG_HDR_MAGIC = 0x50414D53" ascii
        $unpack = "def unpack_package(data: bytes) -> PreparedPackage:" ascii
        $ua = "SyncClient/1.0" ascii
    condition:
        filesize < 1MB and all of them
}

rule LOLD_PE_Configuration
{
    meta:
        description = "LOLD raw PE with configuration section and marker at its start"
        target_class = "stage4-lold"
        status = "experimental"
        date = "2026-10-24"
    strings:
        $marker = "LOLD_CFG_V1" ascii
    condition:
        pe.is_pe and pe.machine == pe.MACHINE_AMD64 and
        for any i in (0 .. pe.number_of_sections - 1) : (
            pe.sections[i].name == ".loldcfg" and
            $marker at pe.sections[i].raw_data_offset
        )
}

rule Hermes_Client_PE_Configuration
{
    meta:
        description = "Hermes raw client PE with RDP configuration section"
        target_class = "stage5-client"
        status = "experimental"
        date = "2026-10-24"
    strings:
        $marker = "RDP_CFG_V1" ascii
    condition:
        pe.is_pe and pe.machine == pe.MACHINE_AMD64 and
        for any i in (0 .. pe.number_of_sections - 1) : (
            pe.sections[i].name == ".rdpcfg" and
            $marker at pe.sections[i].raw_data_offset
        )
}

rule Hermes_Module_PE_ABI
{
    meta:
        description = "Hermes module DLL with olympc section and exported module ABI"
        target_class = "module"
        status = "experimental"
        date = "2026-10-24"
    condition:
        pe.is_pe and pe.machine == pe.MACHINE_AMD64 and pe.is_dll() and
        pe.exports("ClientModuleGetApi") and
        for any i in (0 .. pe.number_of_sections - 1) : (
            pe.sections[i].name == ".olympc"
        )
}