Malicious Code
Malware is code written to harm, spy on or take control of a system. It differs from errors because it is deliberate.
Types of Malicious Code
- Virus: attaches itself to a host file or program and spreads when the host is run, usually with user action. Phases: dormant, propagation, triggering and execution. Types include file infectors, boot-sector viruses, macro viruses and polymorphic viruses that change their form to avoid detection.
- Worm: a self-replicating program that spreads by itself across networks without a host file or user action, using flaws in network services. Its spread is exponential. Example: the Morris worm (1988).
- Trojan horse: a program that looks useful but secretly does harm, such as opening a back door.
- Logic bomb: code that runs when a condition occurs (a date or an event).
- Trapdoor (back door): a hidden entry that bypasses normal authentication.
- Ransomware: encrypts the victim's files and demands payment (WannaCry, 2017).
- Spyware and keyloggers: secretly record activity and passwords.
- Rootkit: hides the presence of an attacker at a deep level of the operating system.
- Botnet: many infected machines controlled remotely.
- Adware and scareware: unwanted advertising and fake alarms.
Targeted Malicious Code
Targeted attacks aim at a specific victim, using inside knowledge. Examples: spear-phishing with a tailored e-mail, advanced persistent threats (APT) in which skilled attackers stay hidden for a long time to steal data, and Stuxnet (2010), which targeted industrial control systems. Targeted code often uses zero-day flaws, is carefully built to avoid detection, and may exist as only one copy, so signature-based antivirus rarely sees it.
How Malware Spreads and Hides
Spread: e-mail attachments, malicious links, infected downloads, removable drives, network flaws, and supply-chain compromise. Hiding: encryption, polymorphism, packing, rootkit techniques and long dormancy.
Definition: Worm. A self-replicating malicious program that spreads across networks on its own, without attaching to a host file or needing a user to run it.
Table 2.2: Virus, worm and trojan
| Type | Needs a host? | Spreads by | Example |
|---|---|---|---|
| Virus | Yes (a file) | User running infected file | Macro virus |
| Worm | No | Network flaws, by itself | Morris worm |
| Trojan | Looks like a useful program | User installs it | Fake utility with back door |
Example 2.2: Exponential spread of a worm
Problem: A worm infects one host. Every infected host infects one new host every 2 hours, so the number doubles every 2 hours. The network has 500 vulnerable hosts. Find the infected hosts at 10 hours and the time to reach all hosts.
Solution:
- After t hours the number is 2^(t/2), until the limit of 500.
- At 10 hours: 2⁵ = 32 hosts.
- At 16 hours: 2⁸ = 256; at 18 hours: 2⁹ = 512, which is more than 500, so all 500 hosts are infected.
- So the whole network falls within 18 hours; most infections happen in the last few hours.
Result: 32 hosts at 10 hours and all 500 hosts by about 18 hours; speed of response in the early hours matters most.
hosts, cap = 1, 500
for hour in range(0, 20, 2):
print(f"Hour {hour:2}: {hosts:3} hosts infected")
hosts = min(hosts * 2, cap)
Output
Hour 0: 1 hosts infected
Hour 2: 2 hosts infected
Hour 4: 4 hosts infected
Hour 6: 8 hosts infected
Hour 8: 16 hosts infected
Hour 10: 32 hosts infected
Hour 12: 64 hosts infected
Hour 14: 128 hosts infected
Hour 16: 256 hosts infected
Hour 18: 500 hosts infected
Common mistakes
- Using the words virus and worm for the same thing.
- Opening attachments from unknown senders.
- Relying only on signatures for targeted or new malware.
- Ignoring the early hours of an outbreak.
Key takeaways
- Virus needs a host; worm spreads by itself; trojan pretends to be useful.
- Others: logic bomb, trapdoor, ransomware, spyware, rootkit, botnet.
- Targeted code: spear-phishing, APT, Stuxnet; often uses zero-days.
- Worm spread is exponential.
Practice questions
- Differentiate a virus and a worm.
- What is a logic bomb?
- What is an APT?
Answers to the practice questions
- A virus needs a host file and user action; a worm spreads across networks by itself.
- Code that runs when a condition, such as a date, is met.
- A skilled attacker who stays hidden for a long time to steal data from a specific target.
Sources
- Official BCA syllabus Computer Security, course code 25BCA406DS02, Unit II
