Introduction to Antivirus
Overview — A Fundamentals room on how antivirus (AV) products work from the red-team point of view. It explains the building blocks of an AV engine (scanner, unpacker, emulator), the three detection families (static, dynamic, heuristic/behavioural), and then gets hands-on with ClamAV: you hash a missed sample, write your own signature database, build a YARA rule, and finally fingerprint which AV a host is running.
| Platform | TryHackMe |
| Category | Fundamentals |
| Difficulty | Easy |
| Room | Introduction to Antivirus |
Task 1 — Introduction
Antivirus is one of the oldest host-based defences, and any attacker who lands on a Windows workstation will run into it within minutes. The room's premise is that you cannot sensibly talk about evasion before you understand what is being evaded. The learning goals are: what AV is, which detection approaches it uses, how to enumerate the AV on a target, and how to experiment safely in a lab. Helpful background is hashing basics, a first look at YARA rules, and the general idea of host-based detection (AV versus EDR).
Task 2 — Antivirus Software
AV is an additional protective layer that tries to stop malicious files from being stored, executed or spread on an operating system. It runs as a background service, looks at files already on disk as well as new downloads, and decides for each one whether it is hostile. Malware, in this context, means software built to harm the host or its owner: take control of the machine, steal credentials, encrypt files, inject ads or further payloads, or enrol the system in a botnet.
AV versus EDR. Classic AV is file-centric: scan, detect, quarantine or delete. Endpoint Detection and Response watches behaviour across many telemetry sources (process creation, memory, network connections, registry, file activity) and gives analysts a timeline to investigate. Modern commercial products blur the line and bundle both, together with a firewall, anti-exploit protection and encryption tools.
A short history. The first commercial scanner, McAfee's VirusScan (1987), was written to clean the "Brain" virus. Early products were command-line scanners that grepped files for known byte patterns. Today they are GUI suites available for Windows, Linux, macOS, Android and iOS.
Q: What does AV mean?
A: Antivirus
Q: Which PC antivirus vendor implemented the first AV software on the market?
A: McAfee
Q: Antivirus software is a ____-based security solution.
A: host
Task 3 — Antivirus Features
The AV engine is the core that analyses files and removes threats. Products differ in quality, but almost all of them are made from the same set of components:
| Component | What it does | Why it matters |
|---|---|---|
| Scanner | Checks files on demand or in real time, from GUI or command line; some also scan mail, memory and the registry | Entry point for every detection |
| Detection techniques | Signature, heuristic and dynamic methods (see tasks 5 and 6) | Decides the verdict; trade-off between catching more and raising false positives |
| Compressors and archives | Opens ZIP, 7z, RAR, TGZ, XAR and similar containers | Malware is routinely delivered inside archives, so contents must be inspected before the user opens them |
| PE parsing and unpackers | Reads Windows executable headers and reverses common packers such as UPX, ASPack or Armadillo | Packing hides the real code from static signatures; the unpacker restores the original |
| Emulator | Runs a suspicious file (exe, DLL, PDF...) in a virtualised, controlled environment and records API calls, registry and file-system changes, spawned processes, memory dumps and web requests | Reveals behaviour that static inspection cannot; stops once enough evidence is gathered |
Other typical pieces are a self-protection driver (so malware cannot simply kill the AV), firewall and network inspection, a service or daemon, CLI and GUI front ends, and a central management console in enterprise deployments.
The PE header is worth remembering: it describes sections, imports and entry points of a Windows executable. A packed binary usually shows odd section names, high entropy and a tiny import table, which is itself a signal AV engines use.
Q: Which AV feature analyses malware in a safe and isolated environment?
A: Emulator
Q: An ____ feature restores or decrypts compressed executables to their original form.
A: Unpacker
Task 4 — Deploy the VM
The lab is a Windows 10 Pro machine, reachable through the in-browser split view or over RDP from the AttackBox or VPN. Credentials are given on the task page (user thm). From a Linux attacker box:
xfreerdp /v:MACHINE_IP /u:thm /p:'PASSWORD_FROM_TASK_PAGE'
On the desktop you will find a Samples folder and a Files folder, which the next tasks use.
Task 5 — AV Static Detection
AV detection falls into three families: static, dynamic, and heuristic/behavioural. Static detection is the simplest. The engine compares properties of a file (a unique string, a checksum, a byte sequence, or a cryptographic hash such as MD5 or SHA-1) with a database of signatures built from previously analysed malware. A match means "malicious". It is fast and has very few false positives, but it only recognises what is already in the database, which therefore needs constant updates.
The lab uses ClamAV, an open-source engine pre-installed at C:\Program Files\ClamAV. The Samples folder holds five files: the harmless EICAR test file (an industry-standard string every AV must flag), backdoor1 (a C# loader that spawns a process and runs a Metasploit shellcode), backdoor2 (a C# loader that XOR-encrypts the same kind of shellcode and injects it into a running process), AV-Check (a benign C# AV enumerator used in task 7), and notes.txt (benign text).
Scanning with clamscan
"C:\Program Files\ClamAV\clamscan.exe" C:\Users\thm\Desktop\Samples
...\Samples\AV-Check.exe: OK
...\Samples\backdoor1.exe: Win.Malware.Swrort-9872015-0 FOUND
...\Samples\backdoor2.exe: OK
...\Samples\eicar.com: Win.Test.EICAR_HDB-1 FOUND
...\Samples\notes.txt: OK
Only backdoor1 and EICAR are flagged. backdoor1 contains a Metasploit shellcode byte sequence that is already in the official database. backdoor2 carries the same payload but XOR-encrypted, so the byte pattern ClamAV knows no longer appears in the file: a textbook example of why static signatures are easy to sidestep.
Adding --debug shows every module ClamAV loads while scanning. For EICAR it reveals that the match is a plain MD5 hash signature:
LibClamAV debug: FP SIGNATURE: 44d88612fea8a8f36de82e1278abb02f:68:Win.Test.EICAR_HDB-1 # Name: eicar.com, Type: CL_TYPE_TEXT_ASCII
You can reproduce the value yourself with sigtool, which ships with ClamAV. Its --md5 option prints the line in ClamAV's hash-database format, hash:size-in-bytes:filename:
"C:\Program Files\ClamAV\sigtool.exe" --md5 C:\Users\thm\Desktop\Samples\eicar.com
44d88612fea8a8f36de82e1278abb02f:68:eicar.com
Building your own signature database
ClamAV treats any file ending in .hdb as a database of MD5 hash signatures. That lets you teach it about a sample the vendor has never seen. Three steps: hash the file, store the line in an .hdb file, and tell clamscan to use it with -d.
cd C:\Users\thm\Desktop\Samples
"C:\Program Files\ClamAV\sigtool.exe" --md5 backdoor2.exe > thm.hdb
"C:\Program Files\ClamAV\clamscan.exe" -d thm.hdb backdoor2.exe
...\Samples\backdoor2.exe: backdoor2.exe.UNOFFICIAL FOUND
The "UNOFFICIAL" suffix marks a hit from a user-supplied database. The weakness is obvious: the hash covers every byte, so changing a single byte (recompiling, appending data, re-encrypting) produces a completely different MD5 and the signature stops matching. As the task suggests, practise by adding the hash of AV-Check.exe to the same database and rescanning.
YARA rules for static detection
YARA describes patterns instead of whole-file hashes, so a rule can survive small modifications. ClamAV can load YARA rules from files with a .yara extension. A rule has a meta section (free-form information), a strings section (what to look for) and a condition (when to fire). To find a good string for AV-Check, list its printable strings and look at the debug-symbol path that Visual Studio embeds:
strings AV-Check.exe | findstr pdb
C:\Users\thm\source\repos\AV-Check\AV-Check\obj\Debug\AV-Check.pdb
That path is a good fingerprint because compilers leave it behind and developers rarely remove it. A first rule (note the doubled backslashes, which YARA needs to escape the character in a string literal):
rule thm_demo_rule {
meta:
author = "THM: Intro-to-AV-Room"
description = "Look at how the Yara rule works with ClamAV"
strings:
$a = "C:\\Users\\thm\\source\\repos\\AV-Check\\AV-Check\\obj\\Debug\\AV-Check.pdb"
condition:
$a
}
"C:\Program Files\ClamAV\clamscan.exe" -d Desktop\Files\thm-demo-1.yara Desktop\Samples
...\Samples\AV-Check.exe: YARA.thm_demo_rule.UNOFFICIAL FOUND
...\Samples\notes.txt: YARA.thm_demo_rule.UNOFFICIAL FOUND
AV-Check is caught, but so is notes.txt, because that text file happens to contain the same path: a false positive. The fix is to also require that the file is a Windows executable. Every PE file begins with the two bytes MZ (hex 4D 5A), which you can confirm by opening the binary in a hex editor such as HxD. The improved condition demands both facts at once:
strings:
$a = "C:\\Users\\thm\\source\\repos\\AV-Check\\AV-Check\\obj\\Debug\\AV-Check.pdb"
$b = "MZ"
condition:
$b at 0 and $a
Scanning again with thm-demo-2.yara flags only AV-Check.exe and leaves notes.txt alone.
Takeaway for both sides: defenders get precision by combining a file-type check with a distinctive string; attackers learn that anything static (hash, embedded path, constant strings) is trivially changed, which is why later evasion rooms start with modifying exactly those artefacts.
Q: What is the MD5 signature line that sigtool --md5 produces for AV-Check.exe?
A: f4a974b0cf25dca7fbce8701b7ab3a88:6144:AV-Check.exe
Q: Use the strings tool to list all human-readable strings of the AV-Check binary. What is the flag?
A: FLAG (found by piping strings -n 8 AV-Check.exe into findstr THM)
Task 6 — Other Detection Techniques
Dynamic detection
Dynamic detection judges a file by what it does at runtime. Two common methods: hooking and monitoring Windows API calls made by a process, and running the sample in a sandbox, a virtualised environment isolated from the host. If the sandbox verdict is "malicious", the vendor derives a new signature or rule and pushes it to its cloud database so the next copy is caught instantly.
The cost is time and resources: a sample can only be run for a short, limited period. Malware authors exploit this with sandbox evasion: they check for signs of a virtual machine or a real user, or simply sleep for a while before doing anything harmful.
Heuristic and behavioural detection
Heuristics are what keep modern AV useful against brand-new malware. There are two flavours:
- Static heuristics decompile or disassemble the sample and compare the recovered code against fragments of known malware. If the similarity passes a threshold, the file is flagged even though no exact signature exists.
- Dynamic heuristics apply behavioural rules learned from analysing malware in sandboxes. Typical rules: a process trying to read the memory of
lsass.exe(which holds NTLM hashes and Kerberos tickets), or an unknown process opening a listening port and waiting for commands as a C2 implant would.
Putting it together
Imagine a suspicious Foobar.zip reaching the engine. The archive module recognises and extracts it; the file-type identifier picks the right module for the extracted Foobar.exe; the PE parser pulls out header details; if the binary is packed, the unpacker restores the code; finally the collected metadata and code go to the detection logic (signatures, heuristics, emulation) for a verdict. Each stage can be targeted by an attacker, which is exactly why red teamers study them.
Q: Which detection method is used to analyse malicious software inside virtual environments?
A: Dynamic Detection
Task 7 — AV Testing and Fingerprinting
Testing a payload
Which AV is "best" is mostly a matter of environment and preference, and independent labs such as AV-Comparatives publish product comparisons. For an operator, what matters is whether a given payload gets caught. Online multi-engine scanners answer this quickly. VirusTotal runs a sample past 70+ engines and also provides static, sandbox (behaviour) and community information.
Operational warning: VirusTotal shares submitted files with AV vendors, so uploading a custom dropper during an engagement effectively burns it. Red teams therefore prefer services that do not distribute samples (for example Jotti's malware scan, or AntiScan.me with a daily scan quota), or a private lab with the same AV product installed.
Why fingerprint the AV
After initial access you do not know what protects the host. Identifying the product early lets you pick compatible tooling, avoid noisy actions that would trigger alerts and cost you your foothold, and rebuild the same product in a lab to test your techniques. The usual static clues are service names, process names, installed directories, registry keys and loaded DLLs:
| Product | Service name | Process name |
|---|---|---|
| Avast | Avast Antivirus | afwServ.exe, AvastSvc.exe |
| AVG | AVG Antivirus | AVGSvc.exe |
| Avira | AntivirService, Avira.ServiceHost | avguard.exe, Avira.ServiceHost.exe |
| Bitdefender | VSSERV | bdagent.exe, vsserv.exe |
| Kaspersky | AVP<version> | avp.exe, ksde.exe |
| McAfee | McAPExe, Mfemms | MCAPExe.exe, mfemms.exe |
| Microsoft Defender | WinDefend | MsMpEng.exe |
| Norton | Norton Security | NortonSecurity.exe |
| Panda | PavPrSvr | PavPrSvr.exe |
| Trend Micro | TMBMSRV | TMBMSRV.exe |
SharpEDRChecker
SharpEDRChecker is a public C# tool that inspects running processes, file metadata, loaded DLLs, registry keys, services and directories for keywords associated with AV/EDR products. In the lab you open the project in Visual Studio, build it, and run the executable. Trimmed output on this host:
[!] Directory Summary:
[-] C:\Program Files\Windows Defender : defender
[-] C:\Program Files\Windows Defender Advanced Threat Protection : defender, threat
[!] Service Summary:
[-] Sense : defender, threat
[-] WdNisSvc : defender, nissrv
[-] WinDefend : antimalware, defender, malware, msmpeng
[-] wscsvc : antivirus
The conclusion is Microsoft Defender, identified from both directories and services. Since the tool performs many suspicious-looking queries, expect AV to flag it.
Writing your own checker in C#
A custom checker is smaller and has no known signature. The room's AV-Check enumerates processes via WMI (SELECT * FROM Win32_Process) and compares each name against a list of known AV process names, such as MsMpEng.exe, avp.exe, bdagent.exe and AVGSvc.exe. If any matches, it prints "AV Found". The logic is the same as the table above turned into code.
for each process in WMI "select * from win32_process":
if process.Name is in known_av_process_list:
print "--AV Found: " + process.Name
if nothing matched:
print "--AV software is not found!"
Uploading the compiled x86 build to VirusTotal produced a surprise: two engines (MaxSecure and SecureAge APEX) marked this harmless program as malicious, and a 64-bit rebuild added a third (Cyren). These are false positives, probably from machine-learning or rule-based classifiers reacting to the combination of process enumeration and WMI use; the sandbox report even matched a Sigma rule for process ghosting. Two lessons: detection is probabilistic even for benign code, and simply recompiling for a different architecture changes what engines say.
Task 8 — Conclusion
The room closes by recapping the engine components, the detection families, the ClamAV exercises (custom hash database and YARA rule) and AV fingerprinting. The point to carry forward: enumerate the defences first, before escalating privileges or moving laterally, so that you do not trip an alert and lose access.
Key takeaways
- AV is a host-based, real-time scanner whose engine is made of scanner, archive handler, PE parser/unpacker, emulator and detection logic; modern products also add EDR-like behaviour monitoring.
- Static detection (hashes, strings, byte patterns, YARA) is quick and precise but brittle: changing one byte breaks an MD5 signature, and XOR-encrypting a payload hid it from ClamAV.
- YARA rules need context to avoid false positives; combining a unique string with a file-type check (
MZat offset 0) fixed thenotes.txthit. - Dynamic analysis catches what static misses but costs time, so malware tries to detect sandboxes; heuristics generalise to unseen samples at the price of more false positives.
- Never upload engagement payloads to services that share samples with vendors.
- Fingerprint the AV through process names, services, directories and registry keys before acting on a compromised host, and test techniques in a matching lab.