Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from os import system
- import string
- from random import randint, choice
- CONST_LAB2_NAME = "BinaryCookieReader.py"
- CONST_PYINSTALLER_PATH = 'C:\\Users\\alexs\\AppData\\Roaming\\Python\\Python37\\Scripts\\pyinstaller.exe'
- # Массив с командами - присвоения, импорт, арифметика
- PLAIN_COMMANDS = [
- "i = 0\n",
- "qwe_var = len(\"hello there\")\n",
- "z = 100\n",
- "pppp = 0xFF + 123\n",
- "temp_aqwe = 15 + 120 + 0xFF\n",
- "shift = 128 >> 4\n",
- "a = len(\"good\")\n",
- "b = list()\n\tb.append(55)\n",
- ]
- FUNC_CALL_COMMANDS = [
- "str_arr = [''] * 1000\n",
- "CONST_VAR = \"var\"\n",
- "a=4\n\tb=8\n\tif a < 0 or b < 0:\n\t\traise ValueError('the value of both input must be positive')\n",
- "print([i for i in range(len([1, 3, 4, 5]))])\n",
- "message = input('Enter message: ').strip()\n",
- "mode = input('Encrypt/Decrypt [E/D]: ').strip().lower()\n",
- "tmp_d = bin(0xaa00ff) if 1 > 0 else 0\n",
- "char_a=5\n\tchar_b=6\n\treturn '0b' + ''.join(str(int(char_a != char_b)))\n",
- "def __init__(self, size_table, charge_factor=None, lim_charge=None):\n\t\tself.size_table = size_table\n",
- ]
- def get_function_name(function_name_length):
- letters = string.ascii_lowercase + '_'
- result_str = ''.join(choice(letters) for i in range(function_name_length))
- return result_str
- def get_function_body(function_body_length):
- func_body = []
- for i in range(0, function_body_length):
- func_body.append('\t' + choice(PLAIN_COMMANDS)) if i % 2 == 0 else func_body.append('\t' + choice(FUNC_CALL_COMMANDS))
- return func_body
- def get_function_params(function_params_num):
- letters = string.ascii_lowercase + '_'
- params = []
- for i in range(function_params_num):
- param_length = randint(1, 6)
- params.append(''.join(choice(letters) for j in range(param_length)))
- return params
- def create_function(function_name, function_body, function_params, function_return_params):
- func = f"def {function_name}("
- for i in function_params:
- func += i
- func += ','
- func += '):\n'
- for i in function_body:
- func += i
- if len(function_return_params) != 0:
- func += "\treturn "
- for i in range(len(function_return_params)):
- func += function_return_params[i]
- func += ','
- with open("temp.txt", "w") as f:
- f.write(func)
- return func
- def add_trash_code(source_data):
- res = ''
- functions_number = randint(1, 3)
- for i in range(functions_number):
- # Кол-во строк в теле функции
- function_body_length = randint(100, 500)
- # Размер имени функции
- function_name_length = randint(2, 15)
- # Количество параметров функции
- function_param_num = randint(0, 5)
- # Количество возвращаемых параметров из функции
- function_return_num = randint(0, 3)
- # Собираем функцию
- function_name = get_function_name(function_name_length)
- function_body = get_function_body(function_body_length)
- function_params = get_function_params(function_param_num)
- function_return_params = get_function_params(function_return_num)
- function = create_function(function_name, function_body, function_params, function_return_params)
- res += function
- res += "\n"
- for i in source_data:
- res += i
- return res
- def read_source_data():
- source_data = ""
- with open(CONST_LAB2_NAME, "r") as file:
- source_data = file.readlines()
- return source_data
- def write_result(res):
- with open('result.py', "w") as file:
- file.write(res)
- def main():
- # Считываем исходное содержимое файла из CONST_LAB2_NAME
- source_data = read_source_data()
- # Добавляем в начало буфера мусорный код, не влияющий на функционал исходной программы
- res = add_trash_code(source_data)
- # Записываем в буфер исходный код и пишем обратно в файл
- write_result(res)
- def build():
- build_command = CONST_PYINSTALLER_PATH + " " + "result.py" + " --onefile"
- system(build_command)
- pass
- if __name__ == '__main__':
- main()
- build()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement