A computer virus is a type of malicious program that inserts copies of itself into other programs, documents or boot records and spreads when the infected host is executed or opened. In cybersecurity, the defining property of a virus is that it needs a host and a user or system action to replicate, which distinguishes it from a self-propagating worm.
The word virus is often used loosely for any malware, but specialists reserve it for self-replicating code that infects other objects. Viruses dominated the threat landscape from the late 1980s to the early 2000s, and although ransomware, infostealers and trojans are now more common, file infectors and macro viruses still appear in incident response work.
How a computer virus infects and replicates
A computer virus operates through a life cycle of dormancy, propagation, triggering and execution. It stays hidden inside a host, copies itself into new hosts whenever its code runs, and eventually performs its payload when a trigger condition such as a date, a counter or a specific event is met.
Infection techniques differ by host type. A file infector modifies an executable so that the viral code runs first and then passes control to the original program, which keeps the user unaware. A boot sector virus replaces the code that runs before the operating system loads. A macro virus hides in the scripting language of office documents and runs when the document is opened with macros enabled.
| Phase | What the virus does | Typical indicator |
|---|---|---|
| Dormancy | Waits inside the host without visible activity | Altered file size or hash |
| Propagation | Copies itself into other files, disks or documents | Many files modified at once |
| Triggering | Checks a date, count or event condition | Activity on a specific date |
| Execution | Runs the payload, from messages to data destruction | Corrupted files, system instability |
Types of computer viruses
Computer viruses are classified mainly by the object they infect and by the evasion technique they use. These categories overlap, so one sample can be both a file infector and polymorphic.
| Type | Host or technique | Characteristic behavior |
|---|---|---|
| File infector | Executable files such as PE or ELF | Appends or prepends viral code |
| Boot sector virus | Master boot record or volume boot record | Loads before the operating system |
| Macro virus | Office documents with embedded macros | Spreads through shared documents |
| Polymorphic virus | Encrypted body with a mutating decryptor | Changes its appearance on each infection |
| Metamorphic virus | Rewrites its own code | No constant decryptor to detect |
| Resident virus | Stays in memory after the host exits | Infects files as they are opened |
Polymorphic and metamorphic viruses were developed specifically to defeat signature scanners. They forced antivirus vendors to adopt emulation, generic decryption and behavioral analysis, techniques that remain central to modern endpoint protection.
Computer virus compared with worms, trojans and other malware
A virus differs from other malware chiefly in how it spreads. Malware is the umbrella term; a virus, a computer worm and a trojan are distinct propagation models, and a single modern threat may combine them.
| Malware type | Needs a host file | Self-replicates | Main spread mechanism |
|---|---|---|---|
| Computer virus | Yes | Yes | Infected files, documents or media |
| Computer worm | No | Yes | Network services and vulnerabilities |
| Trojan | No | No | Deception of the user |
| Ransomware | No | Varies | Often delivered by other malware |
| Fileless malware | No | Rarely | Scripts and memory-only execution |
The practical consequence is that a virus depends on sharing behavior: copying files, exchanging removable media or sending documents. A worm can spread across a network without any user action, which is why worms historically produced faster and wider outbreaks than classic viruses.
Detection and removal of computer viruses
Viruses are detected by a combination of signature matching, heuristics, emulation and behavior monitoring. Signatures catch known samples quickly; heuristics and sandboxed emulation identify new or mutated variants by what the code attempts to do.
Removal is more delicate than for standalone malware because the virus is woven into legitimate files. A disinfection routine must locate the original entry point, strip the viral code and restore the host, which is not always possible. When disinfection fails, the safe choice is to restore the file from a clean backup or reinstall the affected software.
| Symptom | Likely cause | Recommended response |
|---|---|---|
| Executables grow in size | File infector appending code | Isolate host, scan offline, restore from backup |
| System fails before boot | Boot sector infection | Boot from trusted media and repair boot records |
| Documents prompt to enable macros | Possible macro virus | Keep macros disabled, inspect in a sandbox |
| Scanner flags different hashes of one family | Polymorphic variant | Rely on behavioral and generic detection |
Prevention practices and common mistakes
Effective virus prevention relies on reducing execution of untrusted code and limiting how far an infection can spread. Most measures are inexpensive and overlap with general malware defense.
- Enable macro blocking for files from the internet and allow only signed macros where business processes require them.
- Use application allowlisting so that unknown executables cannot run on critical systems.
- Enable Secure Boot and UEFI firmware protections to block boot-level infections.
- Maintain offline or immutable backups, because infected files may be unrecoverable.
- Scan removable media and shared drives automatically before files are opened.
A frequent mistake is to assume that one antivirus scan proves a system clean. Resident and polymorphic viruses can evade or reinfect during a scan, so investigators prefer offline scanning from trusted media and verification of file hashes against known-good versions.
In everyday language
A computer virus is a small harmful program that hides inside something you already trust, such as a program or a document. When you open that file, the virus runs too and quietly copies itself into other files.
It behaves much like a cold passed between people: it cannot travel on its own, so it spreads when infected files are shared. The best protection is to avoid opening files you do not trust, keep security software up to date and keep backups.
Seen in the real world
An engineering firm with about 120 workstations notices that several CAD workstations run slowly and that many executable files on a shared network drive have grown by the same number of bytes. The help desk first suspects a failing disk, but the endpoint protection console begins flagging a known file infector family.
The security team disconnects the shared drive, boots the affected machines from trusted media and runs an offline scan. About 2,300 executables are infected; roughly a third cannot be disinfected safely and are restored from the previous night’s immutable backup. The source turns out to be an old installer on a contractor’s USB drive. The firm then enables automatic scanning of removable media and application allowlisting, and no reinfection is observed in the following months.