-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
289 lines (214 loc) · 8.46 KB
/
install.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
from getpass import getpass
import json
import os
import sys
import subprocess
from enum import Enum
PYREQS_URL = "https://raw.githubusercontent.com/badtechnologies/bdsh/main/requirements.txt"
GIT_URL = "https://github.com/badtechnologies/bdsh"
BDSH_SRC_URL = "https://raw.githubusercontent.com/badtechnologies/bdsh/main/bdsh.py"
BPL_URL = "https://raw.githubusercontent.com/badtechnologies/bpl/main/lib"
PYREQS = "requirements.txt"
BDSH_SRC = "bdsh.py"
BDSH_DIRS = ['cfg', 'prf', 'exec']
BDSH_ROOT = "bdsh"
class InstallType(Enum):
STANDARD = "std" # standard bdsh installation
SYSTEM = "sys" # system-wide bdsh installation; made for BadOS Shell System
def display():
for install_type in InstallType:
print(f"\t> {install_type.value} ({install_type.name})")
# default args
install_type = InstallType.STANDARD
def install_package(package_name: str):
package_name = package_name.strip()
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("OK")
except subprocess.CalledProcessError:
print("FAILED")
exit(0x81)
def prompt(prompt: str, on_cancel: callable, **default: str):
while (s := input((prompt + " [y/n] ") or default).lower()) not in {'y', 'n'}:
pass
if s == 'n':
on_cancel()
def print_header(header: str):
print('\n'+(f" {header} ").center(50, '='))
def print_task(task: str):
print(task+'...', end=" ", flush=True)
if __name__ == "__main__":
# parse args
for arg in sys.argv:
arg = arg.split("=")
if len(arg) < 1:
continue
if arg[0] == "type":
try:
install_type = InstallType(arg[1])
except ValueError as e:
print(f"{e}, choose from:")
InstallType.display()
exit()
print(f"BDSH INSTALLATION TOOL, {install_type.name} INSTALL\n(c) Bad Technologies\n")
if install_type is not InstallType.SYSTEM and os.path.exists(BDSH_ROOT):
prompt("This will replace your current bdsh configs, proceed?", lambda: exit(0))
print_header("SETUP ENV")
if install_type is not InstallType.SYSTEM:
print_task("Upgrading environment package installer")
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("OK")
except subprocess.CalledProcessError:
print("FAILED")
print_task("Installing system HTTP client")
install_package("requests")
import requests
if os.path.exists(PYREQS):
print("Package list already download, skipping")
else:
print_task(f"Downloading package list")
res = requests.get(PYREQS_URL)
if not res.ok:
print("FAILED")
if install_type is not InstallType.SYSTEM:
print(f"Please download 'requirements.txt' manually from the bdsh repo: {GIT_URL}")
exit(0x82)
with open(PYREQS, 'wb') as f:
f.write(res.content)
print(f"OK")
with open(PYREQS, 'r') as f:
lines = f.readlines()
pkgs = len(lines)
for i, pkg in enumerate(lines, 1):
print_task(f"Installing system packages ({i}/{pkgs})")
install_package(pkg)
print("\nCleaning up")
os.remove(PYREQS)
# IMPORT STATEMENTS (AFTER INSTALLING PIP PACKAGES)
from paramiko import RSAKey
print_header("DOWNLOAD BDSH")
if os.path.exists(BDSH_SRC):
print("bdsh source already downloaded, skipping")
else:
print_task("Downloading bdsh source")
res = requests.get(BDSH_SRC_URL)
if not res.ok:
print("FAILED")
if install_type is not InstallType.SYSTEM:
print(f"Please download 'bdsh.py' manually from the bdsh repo: {GIT_URL}")
exit(0x83)
with open(BDSH_SRC, 'wb') as f:
f.write(res.content)
print("OK")
import bdsh
print_header("INIT BDSH")
print_task("Initializing bdsh directory structure")
try:
if not os.path.exists(BDSH_ROOT):
os.mkdir(BDSH_ROOT)
for dir in BDSH_DIRS:
path = os.path.join(BDSH_ROOT, dir)
if not os.path.exists(path):
os.mkdir(path)
print("OK")
except Exception as e:
print(f"FAILED\n{e}")
exit(0x84)
if install_type is not InstallType.SYSTEM:
print(f"Populated bdsh root ({BDSH_ROOT}/) successfully")
print("Starting virtual bdsh session")
virtsh = bdsh.Shell(sys.stdout, sys.stdin)
print(bdsh.Shell(None, None).header)
print_header("CREATE USERS")
users = {}
while True:
username = input("Enter username\t\t")
password = getpass("Enter password\t\t")
if username.strip() == '' or password.strip() == '':
print("Empty username or password, try again")
continue
if username in users:
print("Username in use, try again")
continue
users[username] = password
def exitloop():
global username
username = None
prompt("Create another?", exitloop)
if username is None:
break
with open(f'{BDSH_ROOT}/cfg/users.json', 'w') as f:
json.dump(users, f)
for username in users.keys():
path = os.path.join(f'{BDSH_ROOT}/prf', username)
if not os.path.exists(path):
os.mkdir(path)
print(f"Created {len(users)} user(s)")
print_header("INSTALL BPM")
install_packages = True
while install_packages:
print_task("Fetching bpm from bpl")
res = requests.get(f'{BPL_URL}/bpm/bpl.json')
if not res.ok:
print(f"""FAILED\nSomething went wrong while fetching bpm from bpl, more information below:
\tError:\t\tHTTP {res.status_code} {res.reason}
\tLibrary:\t{BPL_URL}
\tResponse:\t{res.content.decode()}""")
prompt("Try again?", lambda: globals().update(install_packages=False))
else:
print("OK")
break
if install_packages:
meta = res.json()
print_task(f"Installing bpm-{meta['version']} ({meta['name']})")
res = requests.get(f'{BPL_URL}/bpm/{meta["bin"]}')
while install_packages:
if not res.ok:
print(f"""FAILED\nSomething went wrong while downloading bpm binaries, more information below:
\tError:\t\tHTTP {res.status_code} {res.reason}
\tLibrary:\t{BPL_URL}
\tResponse:\t{res.content.decode()}
\tRequested Bin:\t{meta['bin']}
\tMetadata:\t{meta}""")
prompt("Try again?", lambda: globals().update(install_packages=False))
else:
break
if install_packages:
with open(virtsh.get_path('exec', 'bpm'), 'wb') as f:
f.write(res.content)
print("OK")
print_header("INSTALL PACKAGES")
virtsh.run_line("bpm install -y core")
else:
print("[!] bpm could not be installed, no packages installed.")
print_header("SETUP BADBANDSSH")
key = RSAKey.generate(bits=2048)
key.write_private_key_file(virtsh.get_path('cfg', 'badbandssh_rsa_key'))
print("Stored BadBandSSH private key")
print_header("CREATE LAUNCHER")
if not os.path.exists("bin"):
os.mkdir("bin")
binpath = os.path.abspath(BDSH_SRC)
if sys.platform.startswith("win"):
with open(os.path.join("bin", "bdsh.bat"), "w") as f:
f.write(f'@echo off\n{sys.executable} {binpath} %*')
print("Created WINDOWS launcher")
else:
with open(os.path.join("bin", "bdsh"), "w") as f:
f.write(f'#!/bin/bash\n{sys.executable} {binpath} "$@"')
os.chmod(os.path.join("bin", "bdsh"), 0o755)
if install_type is not InstallType.SYSTEM:
print("Created UNIX launcher")
else:
print("Created launcher")
if install_type is not InstallType.SYSTEM:
print(f"[!] Please add the following path to your PATH after installation completes:\n\t{os.path.abspath('bin')}")
getpass("Press ENTER to continue...")
print_header("CLEANING UP")
print("Done!\n")
if install_type is not InstallType.SYSTEM:
print(f"""After adding the bdsh binaries to PATH, you can run bdsh with:
bdsh
Or, run the binary directly by running this file:
{os.path.join(os.path.abspath('bin'), "bdsh.bat" if sys.platform.startswith("win") else "bdsh")}""")