TL;DR
Code becomes a virus when it self-replicates and performs unwanted actions without the user’s consent. It’s illegal once that code causes damage, disruption, or gains unauthorised access to computer systems – usually under laws like the Computer Misuse Act (UK) or similar legislation in other countries.
What Makes Code a Virus?
- Self-Replication: A key characteristic. Viruses copy themselves and spread to other files, computers, or networks. This is different from regular software that needs manual installation.
- Unwanted Actions: These actions can include:
- Deleting files
- Stealing data (passwords, financial information)
- Displaying unwanted messages
- Taking control of your computer
- Using your computer to attack others
- Lack of Consent: The user isn’t aware the code is running these actions. Even if someone downloads a program knowing it does *something*, it’s still illegal if that ‘something’ includes malicious activity without clear, informed consent.
Examples
Here are some examples to illustrate:
- Simple Keylogger: Code that records keystrokes. If secretly installed and used to steal passwords, it’s a virus and illegal.
- Worm: A self-replicating program that spreads across networks without needing a host file (unlike traditional viruses). Illegal if it causes damage or disruption.
- Trojan Horse: Disguises itself as legitimate software but contains malicious code. Illegal once the hidden code performs unwanted actions.
// Example of simple, potentially harmful code (DO NOT RUN)
#include <iostream>
int main() {
std::cout << "This is a demonstration only!" << std::endl;
// In reality, this could delete files or steal data.
return 0;
}
When Does it Become Illegal?
- The Computer Misuse Act (UK): This is the main law in the UK. Key offences include:
- Section 1: Unauthorised access to computer material – simply getting into a system without permission is illegal.
- Section 2: Unauthorised access with intent to commit further offences (e.g., stealing data).
- Section 3: Impairing the operation of a computer – damaging or disrupting systems.
- Damage/Disruption: If your code causes any harm to computers, networks, or data, it’s likely illegal. This includes financial loss, data breaches, and system downtime.
- Unauthorised Access: Gaining access to systems you don’t have permission to be in is a crime, even if you don’t *immediately* cause damage.
- Malware Creation & Distribution: Creating or spreading viruses, worms, Trojans, etc., is illegal.
Penalties
Penalties for violating the Computer Misuse Act can include:
- Fines: Significant financial penalties.
- Imprisonment: Depending on the severity of the offence, you could face a prison sentence (up to 10 years in some cases).
- Criminal Record: A conviction will result in a criminal record.
Preventing Legal Issues
- Get Permission: Always obtain explicit permission before accessing or modifying any computer system you don’t own.
- Ethical Hacking/Penetration Testing: If conducting security testing, get written consent from the owner of the system *before* starting. A clear scope of work is essential.
- Code Reviews: Have your code reviewed by others to identify potential vulnerabilities and malicious behaviour.
- Stay Informed: Keep up-to-date with cybersecurity best practices and relevant legislation.

