Skip to content

Intermediate · Malware and web security · topic 25 of 43 · 35 min

How defenders detect malware

Explanation

Defenders find malware in layers: matching known file hashes and patterns (signatures, YARA rules), looking at suspicious behaviour (an Office document starting PowerShell), running unknown files in a sandbox, and watching for persistence like new services and startup entries. No single method catches everything.

Why layers

Attackers change their malware all the time. One method alone will miss things. Defence in depth means using several detection methods, so something that slips past one is caught by another.

1. Signatures and hashes

Antivirus started with signatures: a known pattern of bytes or a known file hash. If a file's SHA-256 matches a hash from a threat report, it is the same known-bad file. This is fast and accurate for known malware, but a tiny change to the file gives a new hash.

powershell
PS> Get-FileHash C:\Users\ravi\Downloads\invoice_update.exe -Algorithm SHA256 | Select-Object Hash

Hash
----
<64 hex characters>
Get the hash of a suspicious file, then compare it with threat intelligence.

2. YARA rules

YARA is a free tool that matches files against rules made of text or byte patterns and conditions. Analysts and threat researchers share YARA rules to find families of malware, not just one exact file.

output
rule Suspicious_Invoice_Dropper
{
    meta:
        description = "Example: flags files containing both strings"
    strings:
        $a = "powershell -enc" nocase
        $b = "invoice_update.exe"
    condition:
        $a and $b
}
A simple YARA rule. Real rules are tested to avoid false positives.

3. Behaviour (heuristics and EDR)

Endpoint detection and response (EDR) tools watch what programs do: which process started which, what files and registry keys changed, what network connections were made. Some patterns are rarely innocent:

  • A Word or Excel process (winword.exe, excel.exe) starting PowerShell or cmd.exe.
  • PowerShell with a long encoded command (-enc or -EncodedCommand).
  • A process deleting shadow copies (Windows backups), which ransomware does before encrypting.
  • Hundreds of files renamed in a minute.
  • A program in a temp or downloads folder connecting to the internet every 60 seconds (beaconing).

4. Persistence: how malware survives a reboot

Malware wants to start again after a restart. Places to check:

WhereHow to see itLog evidence
Registry Run keys (HKCU/HKLM\...\CurrentVersion\Run)Autoruns tool from Microsoft Sysinternals, or reg querySysmon Event ID 13 (registry value set)
Scheduled tasksGet-ScheduledTask, Task SchedulerSecurity Event 4698 (scheduled task created)
New servicesGet-Service, services.mscSystem log Event 7045 (service installed)
Startup foldershell:startupFile creation events
Linux cron jobs and systemd servicescrontab -l, systemctl list-unit-filesauth and syslog entries
powershell
PS> Get-ScheduledTask | Where-Object { $_.TaskPath -eq "\" } | Select-Object TaskName, State

TaskName              State
--------              -----
OneDrive Reporting    Ready
WinUpdateHelper       Ready
Tasks in the root folder. "WinUpdateHelper" is not a real Windows task: check what it runs.

5. Sandboxing and reputation

A sandbox is an isolated virtual machine where an unknown file is run and watched. If it tries to encrypt files or call a known-bad server, it is malware. Reputation services check how common and how old a file or domain is.

Careful: Public scanning sites keep the files you upload and may share them. Never upload company documents or files that may contain personal data. Search by hash first; upload only if your company policy allows it.

Common mistakes

  • Trusting "no antivirus alert" as proof of clean. Check behaviour and logs too.
  • Opening a suspicious file on your own work laptop to see what it does.
  • Removing the malware file but not the scheduled task that will download it again.
  • Hunting only on Windows when the attacker is on a Linux server or in the cloud.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

How defenders detect malware · 1/7

Defence in depth

Attackers change malware constantly, so defenders detect in layers.

Prefer a video by a teacher? Find free videos on “how EDR detects malware YARA rules persistence scheduled tasks run keys” on YouTube ↗

In real life: Excel started PowerShell

An EDR alert at a Chennai logistics firm says EXCEL.EXE started powershell.exe with an encoded command. The analyst checks: the user opened an "updated rate card" attachment two minutes earlier, and PowerShell tried to download a file from a newly registered domain. The EDR had already blocked the download. The analyst isolates the laptop, purges the email from all mailboxes, blocks the domain, adds the attachment's hash to the block list, and checks for any other machine that contacted the domain.

output
ParentImage: C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE
Image: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
CommandLine: powershell.exe -nop -w hidden -enc SQBFAFgA...
A Sysmon Event ID 1 (process creation) record showing the suspicious parent and child.

Practice (0/3 done)

Task 1 · Question

Which event is most suspicious?

Task 2 · Code · Python

Write known_bad(files, bad_hashes) where files is a dict of path → SHA-256 hex and bad_hashes is a set of SHA-256 hex strings from threat intelligence (any case). Return a sorted list of paths whose hash is known bad. Compare case-insensitively.

Task 3 · Code · Python

Write suspicious_spawns(events) where events is a list of (parent, child) process names. Return the list of events (as tuples, in order) where the parent is an Office app (winword.exe, excel.exe, powerpnt.exe, outlook.exe) and the child is a shell or script host (powershell.exe, cmd.exe, wscript.exe, mshta.exe). Compare in lower case.

Quick quiz

Question 1

1.What is a weakness of hash-based detection?

Question 2

2.What does an EDR tool mainly watch?

Question 3

3.Which Windows event shows a new service was installed?

Question 4

4.What is a sandbox in malware analysis?

Question 5

5.Why should you avoid uploading company documents to public scanning sites?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is a weakness of hash-based detection?

Answer: Any small change to the file gives a new hash

Hashes match exact files only.

What does an EDR tool mainly watch?

Answer: Process, file, registry and network behaviour on endpoints

EDR records and analyses endpoint behaviour.

Which Windows event shows a new service was installed?

Answer: System log 7045

Event 7045 in the System log records service installation.

What is a sandbox in malware analysis?

Answer: An isolated machine where a suspicious file is run and observed

Sandboxes let analysts watch behaviour safely.

Why should you avoid uploading company documents to public scanning sites?

Answer: Uploaded files can be kept and shared, leaking confidential data

Search by hash first; follow company policy before any upload.

Why can attackers avoid hash-based malware detection?Related

Answer: Changing even one byte of the file gives a new hash

Hash matching only catches exact known files, so it is one layer among several.

From Hashing: fingerprints for data

Fileless malware is hard to catch because…Related

Answer: It runs in memory using built-in tools, leaving few files to scan

Behaviour-based tools (EDR) and logging catch it better than file scans.

From Malware types and how they get in

Signature-based detection is weakest against…Related

Answer: Brand-new attacks with no signature yet

It only knows patterns someone has already written.

From Intrusion detection and prevention (IDS/IPS)

Which Amazon service gives managed threat detection on CloudTrail, VPC flow and DNS data?Related

Answer: GuardDuty

GuardDuty analyses these sources and raises findings.

From Logging and monitoring in the cloud

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Malware and web security” before its exam: The OWASP Top 10 with fixes, Secure coding basics.
  2. Module 6 exam: Malware and web security · 13 questions · 25 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Cloud security basics. 43 topics left in the course before the final exam.

← Previous: Malware types and how they get in