forked from prototype74/HyperUBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_secure_cfg.py
362 lines (319 loc) · 11.7 KB
/
update_secure_cfg.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright 2021-2022 nunopenim @github
# Copyright 2021-2022 prototype74 @github
#
# Licensed under the PEL (Penim Enterprises License), v1.0
#
# You may not use this file or any of the content within it, unless in
# compliance with the PE License
from sys import version_info
if (version_info.major, version_info.minor) < (3, 8):
print("Python v3.8+ is required to start Secure-Config-Updater! Please "
"update Python to v3.8 or newer "
"(current version: {}.{}.{}).".format(
version_info.major, version_info.minor, version_info.micro))
quit(1)
from platform import system # noqa: E402
from sys import executable, platform, stdin # noqa: E402
import os # noqa: E402
IS_WINDOWS = (True if system().lower() == "windows" or
os.name == "nt" or platform.startswith("win") else False)
SECURE_CONFIG = os.path.join(".", "userbot", "secure_config")
if IS_WINDOWS:
try:
import colorama
colorama.init()
WIN_COLOR_ENABLED = True
except (ImportError, ModuleNotFoundError):
WIN_COLOR_ENABLED = False
except Exception as e:
WIN_COLOR_ENABLED = False
print(f"Exception: {e}")
if IS_WINDOWS:
import msvcrt
else:
import termios
import tty
class Colors:
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
RED_BG = "\033[101m"
END = "\033[0m"
def setColorText(text: str, color: Colors) -> str:
if IS_WINDOWS and not WIN_COLOR_ENABLED:
return text # don't use ANSI codes
return f"{color}{text}{Colors.END}"
try:
from cffi import __version__
except (ImportError, ModuleNotFoundError):
print(setColorText("cffi is not installed!", Colors.RED))
if IS_WINDOWS:
print(setColorText("Please install cffi package by "
"executing 'pip install cffi'",
Colors.YELLOW))
else:
print(setColorText("Please install cffi package by "
"executing 'python3 -m pip install cffi'",
Colors.YELLOW))
quit(1)
try:
# just to ensure proper version is installed (required by pyAesCrypt)
cffi_ver = tuple(map(int, __version__.split(".")))
if cffi_ver < (1, 15, 0):
print(setColorText("cffi version 1.15.0 is required. "
f"Current version is {__version__}", Colors.RED))
if IS_WINDOWS:
print(setColorText("Please upgrade cffi package by "
"executing 'pip install --upgrade cffi'",
Colors.YELLOW))
else:
print(setColorText("Please install cffi package by "
"executing 'python3 -m pip install --upgrade "
"cffi'", Colors.YELLOW))
quit()
except Exception as e:
print(setColorText("Unable to check cffi version: {e}", Colors.RED))
quit(1)
try:
import pyAesCrypt
except (ImportError, ModuleNotFoundError):
print(setColorText("pyAesCrypt is not installed!", Colors.RED))
if IS_WINDOWS:
print(setColorText("Please install pyAesCrypt package by "
"executing 'pip install pyAesCrypt'",
Colors.YELLOW))
else:
print(setColorText("Please install pyAesCrypt package by "
"executing 'python3 -m pip install pyAesCrypt'",
Colors.YELLOW))
quit(1)
# from userbot/sysutils/getpass.py
def _GetwchPOSIX():
if IS_WINDOWS:
return ""
# based on: https://docs.python.org/3/library/termios.html#example
fd = stdin.fileno()
prev_attr = termios.tcgetattr(fd)
try:
tty.setraw(fd, termios.TCSAFLUSH)
char = stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, prev_attr)
return char
_getwch = msvcrt.getwch if IS_WINDOWS else _GetwchPOSIX
def _getpass(prompt):
print(prompt, end="", flush=True)
password_chars = []
while True:
char = _getwch()
if ord(char) in (10, 13):
break
elif ord(char) in (8, 127):
if len(password_chars) > 0:
password_chars.pop()
print("\b \b", end="", flush=True)
elif ord(char) in (3,):
raise KeyboardInterrupt
else:
password_chars.append(char)
print("*", end="", flush=True)
print()
password = "".join(password_chars)
return password
def _getAPIsAndSession() -> tuple:
while True:
try:
api_key = input("Please enter your App api_id (API Key): ")
except KeyboardInterrupt:
print()
raise KeyboardInterrupt
try:
api_key = int(api_key)
break
except ValueError:
print(setColorText("Invalid input. Try again...",
Colors.YELLOW))
while True:
try:
api_hash = input("Please enter your App api_hash (API Hash): ")
except KeyboardInterrupt:
print()
raise KeyboardInterrupt
if len(api_hash) == 32:
break
elif len(api_hash) > 0:
print(setColorText("Invalid input. API Hash should have a "
"length of 32 characters!", Colors.YELLOW))
else:
print(setColorText("Invalid input. Try again...",
Colors.YELLOW))
while True:
try:
string_session = input("Please enter your valid "
"String Session: ")
except KeyboardInterrupt:
print()
raise KeyboardInterrupt
if string_session:
break
else:
print(setColorText("Invalid input. Try again...",
Colors.YELLOW))
return (api_key, api_hash, string_session)
def _setup_password() -> str:
print("Your password must have at least a length of 4 characters. "
"Maximum length is 1024 characters. You can anytime skip this by "
"typing \"S\" as single character password.")
print()
while True:
try:
password = _getpass("Your new password: ")
except KeyboardInterrupt:
print()
raise KeyboardInterrupt
if len(password) >= 4 and len(password) <= 1024:
break
elif password.lower() == "s":
print(setColorText("Continuing without password...",
Colors.YELLOW))
return ""
elif len(password) < 4:
print(setColorText("Password too short.", Colors.YELLOW))
elif len(password) > 1024:
print(setColorText("Password too long.", Colors.YELLOW))
else:
print(setColorText("Invalid input. Try again...",
Colors.YELLOW))
while True:
try:
retype_pwd = _getpass("Retype your password: ")
except KeyboardInterrupt:
print()
raise KeyboardInterrupt
if password == retype_pwd:
break
elif retype_pwd.lower() == "s":
print(setColorText("Continuing without password...",
Colors.YELLOW))
return ""
else:
print(setColorText("Passwords did not match. Try again...",
Colors.YELLOW))
return password
def _secure_configs(api_key: int, api_hash: str,
string_session: str, password: str) -> bool:
try:
print("Securing configs...")
if os.path.exists(SECURE_CONFIG):
os.remove(SECURE_CONFIG)
if os.path.exists("_temp.py"):
os.remove("_temp.py")
secure_configs = (f'API_KEY = "{api_key}"\n'
f'API_HASH = "{api_hash}"\n'
f'STRING_SESSION = "{string_session}"')
with open("_temp.py", "w") as cfg_file:
cfg_file.write(secure_configs)
cfg_file.close()
pyAesCrypt.encryptFile(infile="_temp.py",
outfile=SECURE_CONFIG,
passw=password,
bufferSize=(64 * 1024))
os.remove("_temp.py")
if os.path.exists(SECURE_CONFIG):
print(setColorText("Configs secured", Colors.GREEN))
return True
print(setColorText("Failed to secure configs: file not created",
Colors.RED))
except Exception as e:
print(setColorText(f"Failed to secure configs: {e}", Colors.RED))
return False
def _start_userbot():
py_exec = executable if " " not in executable else '"' + executable + '"'
try:
tcmd = [py_exec, "-m", "userbot"]
os.execle(py_exec, *tcmd, os.environ)
except Exception as e:
print(setColorText(f"Failed to start HyperUBot: {e}",
Colors.RED))
return
def main():
print(setColorText("Secure-Config-Updater", Colors.CYAN) +
" creates a new secured config file with your App api_id (API Key), "
"App api_hash (API Hash) and String Session stored to protect these "
"sensitive data from unauthorized access")
print()
while True:
try:
inp = input("Continue? (y/n): ")
except KeyboardInterrupt:
print()
raise KeyboardInterrupt
if inp.lower() in ("y", "yes"):
break
elif inp.lower() in ("n", "no"):
raise KeyboardInterrupt
else:
print(setColorText("Invalid input. Try again...", Colors.YELLOW))
api_key, api_hash, string_session = _getAPIsAndSession()
if not api_key or not api_hash or not string_session:
print(setColorText("Input values are not valid", Colors.RED))
return
print()
print("There is an option to setup a password to your secure config "
"to increase the security of your sensitive data. This is "
"optional and not mandatory but extra protection doesn't hurt.")
print(setColorText("If you forgot your password, you have to create "
"a new secure config!", Colors.YELLOW))
print()
set_pwd = False
while True:
try:
inp = input("Set password? (y/n): ")
except KeyboardInterrupt:
print()
raise KeyboardInterrupt
if inp.lower() in ("y", "yes"):
set_pwd = True
break
elif inp.lower() in ("n", "no"):
break
else:
print(setColorText("Invalid input. Try again...", Colors.YELLOW))
password = ""
if set_pwd:
print()
password = _setup_password()
print()
cfg_secured = _secure_configs(api_key, api_hash, string_session, password)
if cfg_secured:
print()
if IS_WINDOWS:
print("Run the following command to start HyperUBot: " +
setColorText("python -m userbot", Colors.CYAN))
else:
while True:
try:
inp = input("Start HyperUBot? (y/n): ")
except KeyboardInterrupt:
print()
raise KeyboardInterrupt
if inp.lower() in ("y", "yes"):
_start_userbot()
break
elif inp.lower() in ("n", "no"):
break
else:
print(setColorText("Invalid input. Try again...",
Colors.YELLOW))
return
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Exiting...")
except (BaseException, Exception) as e:
print(setColorText(f"Secure-Config-Updater stopped: {e}",
Colors.RED_BG))
quit(1)
quit()