Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to write a computer virus

Hello! I am going to explain how to write a computer virus.

NOTE: It is not against the law or a crime to make a computer virus. However, if that virus spreads to other computers intentionally or by mistake, you've violated the law, and you could be held liable for any damages it causes.

Also please note that this is meant to be used for educational purposes only.

So let's get started!

First off, a virus is a small piece of software that attaches itself to real programs.

To begin, to show how it can move up and down directories, create a .replit file and in it write

Then in the console run import os. Then run os.remove("main.py")

Create a folder named whatver you put before /virus.py, in my case, directory. In the folder create a file called virus.py

Now that it is set up, let us start actually writing the virus!

In virus.py write

Here you can see the imports and the virus signature. The signature allows us to tell if a file is infected yet or not.

Here is the next peice of code, searching for files to infect:

Now that we have searched, let's start infecting!

Now let's actually run the virus!

The final output should be:

And there is a functional virus! But what if we want to be able to infect files that are not python scripts? Don't worry! It is really very simple.

In search, below filestoinfect.append(path+"/"+filename) write

Then in infect, above infectPython(virus, filestoinfect), write

Then, above the infectPython method, write

The final result should be

Now we can infect .txt files! But what if we want even more? This is how to infect java files, and make them infect other files too.

First, in the same directory as virus.py, create a file called virus.java.
In the new file write

And again, replace the signature with your's.

Now back in virus.py, in search, below the .txt file check, add

In infect, under virus = open(os.path.abspath(targetFile)), write

Below infectPython(virus, filestoinfect), write

And below virus.close(), write

Below infectPython, write

The final result in virus.py should be

To test the virus, create files outside the folder, inside the folder, inside folders inside the folder, etc.

And that is how to write a simple virus! I hope you enjoyed and will have a wonderful rest of your day!


This is pretty awesome. I'm learning to become an ethical hacker, so this was a good read. A+ mate.


I got a task to write the virus but I didn't know how to write it. After reading this article I have written my own virus and I also take help from the research proposal writer to complete my assignment about writing the virus. This is really helpful That's why I like this website. There is a lot of people helping in coding on this website.


Wow, this is actually cool!

I just skimmed it since I don't know Python much, but I get the overall idea. I bet an actual virus would be able to infect files of many more types.

Aight, time to self-infect myself with this heck yeh!