Advertisement
saleks28

tov4

Jan 16th, 2021
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.46 KB | None | 0 0
  1. from os import system
  2. import string
  3. from random import randint, choice
  4.  
  5. CONST_LAB2_NAME = "BinaryCookieReader.py"
  6. CONST_PYINSTALLER_PATH = 'C:\\Users\\alexs\\AppData\\Roaming\\Python\\Python37\\Scripts\\pyinstaller.exe'
  7.  
  8. # Массив с командами - присвоения, импорт, арифметика
  9. PLAIN_COMMANDS = [
  10.     "i = 0\n",
  11.     "qwe_var = len(\"hello there\")\n",
  12.     "z = 100\n",
  13.     "pppp = 0xFF + 123\n",
  14.     "temp_aqwe = 15 + 120 + 0xFF\n",
  15.     "shift = 128 >> 4\n",
  16.     "a = len(\"good\")\n",
  17.     "b = list()\n\tb.append(55)\n",
  18. ]
  19.  
  20. FUNC_CALL_COMMANDS = [
  21.     "str_arr = [''] * 1000\n",
  22.     "CONST_VAR = \"var\"\n",
  23.     "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",
  24.     "print([i for i in range(len([1, 3, 4, 5]))])\n",
  25.     "message = input('Enter message: ').strip()\n",
  26.     "mode = input('Encrypt/Decrypt [E/D]: ').strip().lower()\n",
  27.     "tmp_d = bin(0xaa00ff) if 1 > 0 else 0\n",
  28.     "char_a=5\n\tchar_b=6\n\treturn '0b' + ''.join(str(int(char_a != char_b)))\n",
  29.     "def __init__(self, size_table, charge_factor=None, lim_charge=None):\n\t\tself.size_table = size_table\n",
  30. ]
  31.  
  32.  
  33. def get_function_name(function_name_length):
  34.     letters = string.ascii_lowercase + '_'
  35.     result_str = ''.join(choice(letters) for i in range(function_name_length))
  36.     return result_str
  37.  
  38.  
  39. def get_function_body(function_body_length):
  40.     func_body = []
  41.     for i in range(0, function_body_length):
  42.         func_body.append('\t' + choice(PLAIN_COMMANDS)) if i % 2 == 0 else func_body.append('\t' + choice(FUNC_CALL_COMMANDS))
  43.     return func_body
  44.  
  45.  
  46. def get_function_params(function_params_num):
  47.     letters = string.ascii_lowercase + '_'
  48.     params = []
  49.     for i in range(function_params_num):
  50.         param_length = randint(1, 6)
  51.         params.append(''.join(choice(letters) for j in range(param_length)))
  52.     return params
  53.  
  54.  
  55. def create_function(function_name, function_body, function_params, function_return_params):
  56.     func = f"def {function_name}("
  57.     for i in function_params:
  58.         func += i
  59.         func += ','
  60.     func += '):\n'
  61.     for i in function_body:
  62.         func += i
  63.     if len(function_return_params) != 0:
  64.         func += "\treturn "
  65.         for i in range(len(function_return_params)):
  66.             func += function_return_params[i]
  67.             func += ','
  68.     with open("temp.txt", "w") as f:
  69.         f.write(func)
  70.     return func
  71.  
  72.  
  73. def add_trash_code(source_data):
  74.     res = ''
  75.     functions_number = randint(1, 3)
  76.     for i in range(functions_number):
  77.         # Кол-во строк в теле функции
  78.         function_body_length = randint(100, 500)
  79.         # Размер имени функции
  80.         function_name_length = randint(2, 15)
  81.         # Количество параметров функции
  82.         function_param_num = randint(0, 5)
  83.         # Количество возвращаемых параметров из функции
  84.         function_return_num = randint(0, 3)
  85.  
  86.         # Собираем функцию
  87.         function_name = get_function_name(function_name_length)
  88.         function_body = get_function_body(function_body_length)
  89.         function_params = get_function_params(function_param_num)
  90.         function_return_params = get_function_params(function_return_num)
  91.  
  92.         function = create_function(function_name, function_body, function_params, function_return_params)
  93.         res += function
  94.         res += "\n"
  95.     for i in source_data:
  96.         res += i
  97.     return res
  98.  
  99.  
  100. def read_source_data():
  101.     source_data = ""
  102.     with open(CONST_LAB2_NAME, "r") as file:
  103.         source_data = file.readlines()
  104.     return source_data
  105.  
  106.  
  107. def write_result(res):
  108.     with open('result.py', "w") as file:
  109.         file.write(res)
  110.  
  111.  
  112. def main():
  113.     # Считываем исходное содержимое файла из CONST_LAB2_NAME
  114.     source_data = read_source_data()
  115.     # Добавляем в начало буфера мусорный код, не влияющий на функционал исходной программы
  116.     res = add_trash_code(source_data)
  117.     # Записываем в буфер исходный код и пишем обратно в файл
  118.     write_result(res)
  119.  
  120.  
  121. def build():
  122.     build_command = CONST_PYINSTALLER_PATH + " " + "result.py" + " --onefile"
  123.     system(build_command)
  124.     pass
  125.  
  126.  
  127. if __name__ == '__main__':
  128.     main()
  129.     build()
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement