Intermediate · Malware and web security · topic 25 of 43 · 35 min
How defenders detect malware
Explanation
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.
PS> Get-FileHash C:\Users\ravi\Downloads\invoice_update.exe -Algorithm SHA256 | Select-Object Hash
Hash
----
<64 hex characters>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.
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
}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:
| Where | How to see it | Log evidence |
|---|---|---|
| Registry Run keys (HKCU/HKLM\...\CurrentVersion\Run) | Autoruns tool from Microsoft Sysinternals, or reg query | Sysmon Event ID 13 (registry value set) |
| Scheduled tasks | Get-ScheduledTask, Task Scheduler | Security Event 4698 (scheduled task created) |
| New services | Get-Service, services.msc | System log Event 7045 (service installed) |
| Startup folder | shell:startup | File creation events |
| Linux cron jobs and systemd services | crontab -l, systemctl list-unit-files | auth and syslog entries |
PS> Get-ScheduledTask | Where-Object { $_.TaskPath -eq "\" } | Select-Object TaskName, State
TaskName State
-------- -----
OneDrive Reporting Ready
WinUpdateHelper Ready5. 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.
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
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.
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...Practice (0/3 done)
Which event is most suspicious?
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- The OWASP Top 10 with fixesMalware and web security · same module
- Secure coding basicsMalware and web security · same module
- Logging and monitoring in the cloudCloud security basics
- TCP/IP and the network layersNetworking for security
- Threat intelligence and indicators of compromiseThreat intelligence and frameworks
- Password hashing and cracking (your own hashes only)Ethical Hacking and Penetration Testing (legal labs) · Deeper lab skills: passwords, privilege, Metasploit, wireless
Questions people ask about this
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.
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.
Signature-based detection is weakest against…Related
Answer: Brand-new attacks with no signature yet
It only knows patterns someone has already written.
Which Amazon service gives managed threat detection on CloudTrail, VPC flow and DNS data?Related
Answer: GuardDuty
GuardDuty analyses these sources and raises findings.
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
- 2 more topics in “Malware and web security” before its exam: The OWASP Top 10 with fixes, Secure coding basics.
- Module 6 exam: Malware and web security · 13 questions · 25 min, without help. 4 topics of this module still to finish first.
- Then the next module, Cloud security basics. 43 topics left in the course before the final exam.