Custom YARA Rules
File Inspection Engine can load your own YARA rules in addition to the built-in Spectra Core rules. When one of your rules matches a file, the file can be classified as malicious, or as suspicious in paranoid mode, with a threat name taken from the rule's metadata.
Custom YARA rules slow down file analysis. The more rules are loaded, the bigger the slowdown.
Writing Rules That Classify
The rule's tags and meta fields decide how a match classifies a file and which threat name it gets. For more information, see YARA and Spectra Core.
A rule tagged tc_detection suspicious classifies a file as suspicious only in paranoid mode.
Preparing Rule Files
File Inspection Engine loads rules from /rl/yara-rules in the container, including its subdirectories. It handles the files there as follows:
-
Only files with the
.yaraor.yarextension are loaded, regardless of case, for example.YAR. Other files, such asREADME.mdorLICENSE, are skipped and listed in one log entry:{"level":"info","files":["/rl/yara-rules/LICENSE","/rl/yara-rules/README.md"],"message":"Skipping files in the user YARA rules that are not .yara or .yar files"} -
Hidden files and directories, such as
.git, are skipped without a log entry. -
Each file gets its own namespace, so your rule names can't clash with the built-in rules.
-
Rules are loaded as YARA-X rules. YARA-X is compatible with most YARA rules, but it's stricter in a few places, so some older rules might need changes. For example, rules that use the
pefieldSECTION_MEM_16BIT, theelffieldSHF_WRITE, or thedexfieldDEX_FILE_MAGIC_035don't load. -
Files that use
includeare skipped, and a warning names each one. The wordincludein comments, strings, or regular expressions doesn't count.{"level":"warn","path":"/rl/yara-rules/include_rule.yara","message":"Skipping a user YARA rule file that uses include, which is not supported"}
File Inspection Engine supports the following YARA modules:
- PE
- ELF
- Dex
- Macho
- String
- Math
- Hash
- Time
- Dotnet
A rule file that doesn't compile stops File Inspection Engine from becoming ready. This includes a file with an unsupported module, a syntax error, or a UTF-8 byte order mark (BOM). Save rule files without a BOM. Check your rules before you deploy them, for example with yr check <rules directory>. yr check finds compile errors, but it accepts some rules that File Inspection Engine rejects, such as rules that use modules File Inspection Engine doesn't support. The YARA-X command-line tool, yr, is available from YARA-X releases. For more information, see /readyz returns 429 after adding custom YARA rules.
File Inspection Engine loads rules only at startup. After you add, change, or remove a rule, restart File Inspection Engine.
Adding Rules with Docker
Mount a host directory with your rules read-only at /rl/yara-rules.
docker run ... -v /path/to/yara-rules:/rl/yara-rules:ro <image>
On hosts with SELinux, use :ro,z instead of :ro.
Don't mount your rules over /opt/titanium_core/yara. That directory holds the built-in rules, and mounting over it hides them.
To apply rule changes, restart the container.
docker restart <container>
Adding Rules with Kubernetes
The Helm chart mounts a persistent volume claim (PVC) with your rules at the correct path. Rules on the PVC are kept across pod restarts and helm upgrade.
Configuring the Helm Chart
The following table lists the Helm values for custom rules.
| Value | Default | Description |
|---|---|---|
storage.yaraRules.enabled | false | Mounts a PVC with rules, read-only. |
storage.yaraRules.existingPvcName | "" | Uses this existing PVC. If empty, the chart creates a PVC named <fullname>-yara-rules-claim. |
storage.yaraRules.size | 1Gi | Size of the PVC that the chart creates. Ignored when existingPvcName is set. |
storage.yaraRules.className | "" | Storage class of the PVC that the chart creates. Defaults to storage.className. Ignored when existingPvcName is set. |
helm uninstall deletes a PVC that the chart created, together with the rules on it. A PVC set in existingPvcName is kept.
Copying Rules to the PVC
A PVC that the chart creates is empty, so you need to copy your rules to it. If you use existingPvcName, you can fill the PVC whichever way suits your storage, or use the following steps.
The File Inspection Engine image doesn't include tar, so kubectl cp to the File Inspection Engine pod doesn't work. Instead, use a temporary helper pod that mounts the same PVC.
In the following commands, replace these placeholders:
<namespace>with the File Inspection Engine namespace.<fullname>with the chart's full name. This is the release name if it containsfie, otherwise<release>-fie, orfullnameOverrideif it's set. IfnameOverrideis set, its value replacesfie. For example, releasefiegivesfie, and releasescannergivesscanner-fie.<pvc>with the name of the rules PVC:<fullname>-yara-rules-claim, or the value ofexistingPvcName.
To see the actual names, run kubectl -n <namespace> get deployment,pvc.
A PVC that the chart creates is ReadWriteOnce, so the helper pod must run on the same node as the File Inspection Engine pod. On a single-node cluster, this happens automatically. On a multi-node cluster, either scale File Inspection Engine to 0 replicas while you copy the rules, or add "nodeName":"<node>" to the spec in the helper pod overrides. To see which node the File Inspection Engine pod runs on, use kubectl -n <namespace> get pod -o wide.
-
Start a helper pod with the PVC mounted at
/rules:kubectl -n <namespace> run yara-loader --image=busybox --restart=Never --overrides='{"spec":{"containers":[{"name":"yara-loader","image":"busybox","command":["sleep","3600"],"volumeMounts":[{"name":"rules","mountPath":"/rules"}]}],"volumes":[{"name":"rules","persistentVolumeClaim":{"claimName":"<pvc>"}}]}}'kubectl -n <namespace> wait --for=condition=Ready pod/yara-loader -
Copy the rule files to the PVC, and check that they're there:
kubectl -n <namespace> cp my-rule.yara yara-loader:/rules/kubectl -n <namespace> exec yara-loader -- ls -l /rulesTo remove a rule, run
kubectl -n <namespace> exec yara-loader -- rm /rules/my-rule.yara. -
Delete the helper pod, and restart File Inspection Engine to load the rules:
kubectl -n <namespace> delete pod yara-loaderkubectl -n <namespace> rollout restart deployment/<fullname>kubectl -n <namespace> rollout status deployment/<fullname>
Understanding When Your Rules Don't Run
Before analyzing a file, /scan looks up its SHA1 hash. If the hash has a user override, a goodware analyst override, or a match in the malicious threat data, /scan returns that verdict without analyzing the file, so your rules don't run. In paranoid mode, the same applies to a match in the suspicious threat data.
Overrides apply only to the file you submit, not to files unpacked from it. For more information, see Classification Overrides.