#SafeSource Create tested # Run in prototype

dev-linux
Ivan Maslov 5 years ago
parent caeab37fbd
commit 4e5dc7485e

@ -34,7 +34,6 @@ def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
outfile.write(iv) outfile.write(iv)
while True: while True:
chunk = infile.read(chunksize) chunk = infile.read(chunksize)
print(chunk)
if len(chunk) == 0: if len(chunk) == 0:
break break
elif len(chunk) % 16 != 0: elif len(chunk) % 16 != 0:
@ -61,4 +60,23 @@ def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
if len(chunk) == 0: if len(chunk) == 0:
break break
outfile.write(decryptor.decrypt(chunk)) outfile.write(decryptor.decrypt(chunk))
outfile.truncate(origsize) outfile.truncate(origsize)
def decrypt_file_bytes(key, in_filename, chunksize=24*1024):
""" Расшифровывает файл, используя AES (режим CBC) с
заданным ключом.
"""
lResult = b''
if not out_filename:
out_filename = os.path.splitext(in_filename)[0]
with open(in_filename, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
iv = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, iv)
with open(out_filename, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
lResult = lResult+decryptor.decrypt(chunk)
lResult=lResult[0:origsize-1]
return lResult

@ -3,26 +3,52 @@ import hashlib # Create hash of the key
import tkinter #tkinter import tkinter #tkinter
from tkinter import filedialog from tkinter import filedialog
from . import Crypter from . import Crypter
import shutil # Copy folder
import os # rm dir, listdir
import glob # list files
import datetime #Get current datetime
from Crypto.Cipher import AES
# Settings
gInUncryptedExtension = "py" # cry for filename.cry
gOutCryptedExtension = "cry" # cry for filename.cry
gFileMaskToDelete = "pyc" #Remove all .pyc files
# Create process run # Create process run
def Run(): def Run():
# Step 1 - select folder to crypt print(f"{str(datetime.datetime.now())}: Start to create crypted distr")
############# Step 1 - select folder to copy
lStep_1_TkinterRoot = tkinter.Tk() lStep_1_TkinterRoot = tkinter.Tk()
lStep_1_TkinterRoot.withdraw() lStep_1_TkinterRoot.withdraw()
#lStep_1_FolderPath = filedialog.askdirectory(parent=lStep_1_TkinterRoot,title='Please select a source code directory to crypt') lStep_1_FolderPath = filedialog.askdirectory(parent=lStep_1_TkinterRoot,title='Please select a source code directory to crypt')
############# Step 2 - Select folder to save
# Step 2 - set password to crypt lStep_2_FolderPath = filedialog.askdirectory(parent=lStep_1_TkinterRoot,title='Please select a folder to save the crypted result')
# Before delete the folder check if directory is empty - else ask user to confirm the deletion
if len(os.listdir(lStep_2_FolderPath)) != 0:
lStep_2_ConfirmDelete = pyautogui.confirm('The destination folder contains some files/folders. The are will be removed. Continue?', buttons=['Yes', 'No'])
if lStep_2_ConfirmDelete=="No":
raise Exception("Stop program - user suggestion. Don't want to clear destination folder")
shutil.rmtree(lStep_2_FolderPath, ignore_errors=False, onerror=None)
############# Step 3 - Copy folder
shutil.copytree(lStep_1_FolderPath, lStep_2_FolderPath)
############# Step 3.1 - Remove files to delete
lPyFilesToDeleteList = [f for f in glob.glob(os.path.join(lStep_2_FolderPath,f"**/*.{gFileMaskToDelete}"), recursive=True)]
for lFileItem in lPyFilesToDeleteList:
#Create right \\ splashes
lFileItem = os.path.abspath(lFileItem)
#Remove old file
os.remove(lFileItem)
############# Step 4 - Get file list with extension .py
lPyFileList = [f for f in glob.glob(os.path.join(lStep_2_FolderPath,f"**/*.{gInUncryptedExtension}"), recursive=True)]
############# Step 5 - Ask and confirm the secret word
lKeyHashStr_1 = hashlib.sha256(pyautogui.password('Please enter the key to protect source code').encode("utf-8")).digest() lKeyHashStr_1 = hashlib.sha256(pyautogui.password('Please enter the key to protect source code').encode("utf-8")).digest()
print(lKeyHashStr_1)
lKeyHashStr_2 = hashlib.sha256(pyautogui.password('Please repeat the key to protect source code').encode("utf-8")).digest() lKeyHashStr_2 = hashlib.sha256(pyautogui.password('Please repeat the key to protect source code').encode("utf-8")).digest()
print(lKeyHashStr_2)
if lKeyHashStr_1 == lKeyHashStr_2: if lKeyHashStr_1 == lKeyHashStr_2:
print("Passed - do AES encryption") for lFileItem in lPyFileList:
from Crypto.Cipher import AES #Create right \\ splashes
#Crypter.encrypt_file(lKeyHashStr_1, "c:\\test.png", "c:\\test.enc") lFileItem = os.path.abspath(lFileItem)
Crypter.decrypt_file(lKeyHashStr_1, "c:\\test.enc", "c:\\testEnc2.png") Crypter.encrypt_file(lKeyHashStr_1, lFileItem, f"{lFileItem[0:-2]}{gOutCryptedExtension}")
#IV = 16 * '\x00' # Вектор инициализации: обсуждается позже #Remove old file
#mode = AES.MODE_CBC os.remove(lFileItem)
#encryptor = AES.new(lKeyHashStr_1, mode, IV=IV) else:
#text = "Text to be crypted!" raise Exception("User set different secret key 1 and key 2")
#ciphertext = encryptor.encrypt(text) ############ Step 6 - Final stage
print(f"{str(datetime.datetime.now())}: Crypted distr is created!")

@ -4,17 +4,15 @@ import os
import sys import sys
import imp import imp
import base64 import base64
from . import Crypter # Crypto functions
EXT = '.b64' EXT = '.cry'
# How to run # How to run
# sys.meta_path.append(Base64Importer(root_pkg_path)) # sys.meta_path.append(Base64Importer(root_pkg_path))
# Init cryptographer # Init cryptographer
def CryptographerInit(inFolderPath): def CryptographerInit(inFolderPath):
print(1)
sys.meta_path.append(Base64Importer(inFolderPath)) sys.meta_path.append(Base64Importer(inFolderPath))
#=============================================================================== #===============================================================================
class Base64Importer(object): class Base64Importer(object):
"""Служит для поиска и импорта python-модулей, кодированных в base64 """Служит для поиска и импорта python-модулей, кодированных в base64
@ -22,10 +20,9 @@ class Base64Importer(object):
Класс реализует Import Protocol (PEP 302) для возможности импортирования Класс реализует Import Protocol (PEP 302) для возможности импортирования
модулей, зашифрованных в base64 из указанного пакета. модулей, зашифрованных в base64 из указанного пакета.
""" """
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
def __init__(self, root_package_path): def __init__(self, root_package_path, inKey):
self.mKeyStr = inKey
self.__modules_info = self.__collect_modules_info(root_package_path) self.__modules_info = self.__collect_modules_info(root_package_path)
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
@ -43,41 +40,31 @@ class Base64Importer(object):
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
def load_module(self, fullname): def load_module(self, fullname):
"""Метод загружает base64 модуль """Метод загружает base64 модуль
Если модуль с именем fullname является base64, то метод попытается его Если модуль с именем fullname является base64, то метод попытается его
загрузить. Возбуждает исключение ImportError в случае любой ошибки. загрузить. Возбуждает исключение ImportError в случае любой ошибки.
""" """
if not fullname in self.__modules_info: if not fullname in self.__modules_info:
raise ImportError(fullname) raise ImportError(fullname)
# Для потокобезопасности # Для потокобезопасности
imp.acquire_lock() imp.acquire_lock()
try: try:
mod = sys.modules.setdefault(fullname, imp.new_module(fullname)) mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
mod.__file__ = "<{}>".format(self.__class__.__name__) mod.__file__ = "<{}>".format(self.__class__.__name__)
mod.__loader__ = self mod.__loader__ = self
if self.is_package(fullname): if self.is_package(fullname):
mod.__path__ = [] mod.__path__ = []
mod.__package__ = fullname mod.__package__ = fullname
else: else:
mod.__package__ = fullname.rpartition('.')[0] mod.__package__ = fullname.rpartition('.')[0]
src = self.get_source(fullname) src = self.get_source(fullname)
try: try:
exec(src) in mod.__dict__ exec(src) in mod.__dict__
except: except:
del sys.modules[fullname] del sys.modules[fullname]
raise ImportError(fullname) raise ImportError(fullname)
finally: finally:
imp.release_lock() imp.release_lock()
return mod return mod
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
def is_package(self, fullname): def is_package(self, fullname):
"""Возвращает True если fullname является пакетом """Возвращает True если fullname является пакетом
@ -93,8 +80,9 @@ class Base64Importer(object):
filename = self.__modules_info[fullname]['filename'] filename = self.__modules_info[fullname]['filename']
try: try:
with open(filename, 'r') as ifile: src = Crypter.decrypt_file_bytes(key = self.mKeyStr, in_filename = filename).decode("utf-8")
src = base64.decodestring(ifile.read()) #with open(filename, 'r') as ifile:
# src = base64.decodestring(ifile.read())
except IOError: except IOError:
src = '' src = ''
@ -132,4 +120,20 @@ class Base64Importer(object):
'ispackage': False 'ispackage': False
} }
return modules return modules
# Settings
gInUncryptedExtension = "py" # cry for filename.cry
gOutCryptedExtension = "cry" # cry for filename.cry
gFileMaskToDelete = "pyc" #Remove all .pyc files
# Create process run
def Run():
print(f"{str(datetime.datetime.now())}: Run decryptography")
############# Step 5 - Ask and confirm the secret word
lKeyHashStr_1 = hashlib.sha256(pyautogui.password('Please enter the key to protect source code').encode("utf-8")).digest()
lKeyHashStr_2 = hashlib.sha256(pyautogui.password('Please repeat the key to protect source code').encode("utf-8")).digest()
if lKeyHashStr_1 == lKeyHashStr_2:
CryptographerInit("", inKey = lKeyHashStr_1)
else:
raise Exception("User set different secret key 1 and key 2")
############ Step 6 - Final stage
print(f"{str(datetime.datetime.now())}: Cryprography module has been successfully initialized")

@ -14,7 +14,7 @@ from pyOpenRPA.Tools.SafeSource import DistrRun
from pyOpenRPA.Tools.SafeSource import DistrCreate from pyOpenRPA.Tools.SafeSource import DistrCreate
#Mode RUN #Mode RUN
if sys.argv[1].upper() == "RUN": if sys.argv[1].upper() == "RUN":
DistrRun.CryptographerInit("") DistrRun.Run()
#Mode CREATE #Mode CREATE
if sys.argv[1].upper() == "CREATE": if sys.argv[1].upper() == "CREATE":
DistrCreate.Run() DistrCreate.Run()

Loading…
Cancel
Save