forked from Defelo/PyCrypCli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpycrypcli.py
525 lines (493 loc) · 21.1 KB
/
pycrypcli.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import getpass
import os
import re
try:
import readline
except ImportError:
import pyreadline as readline
import sys
from typing import List, Optional, Tuple
from client import Client
from exceptions import *
SERVER: str = "wss://ws.cryptic-game.net/"
def is_uuid(x: str) -> bool:
return bool(re.match(r"^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$", x))
def extract_wallet(content: str) -> Optional[List[str]]:
if re.match(r"^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} [0-9a-f]{10}$", content):
return content.split()
return None
class Game:
COMMANDS: List[str] = [
"status",
"whoami",
"hostname",
"help",
"ls",
"l",
"dir",
"touch",
"cat",
"rm",
"cp",
"mv",
"exit",
"quit",
"logout",
"clear",
"history",
"morphcoin",
"pay",
"service",
"spot",
"connect",
]
def __init__(self, server: str, session_file: List[str]):
self.client: Client = Client(server)
self.session_file: List[str] = session_file
self.session_token: str = None
self.device_uuid: str = None
self.hostname: str = None
self.username: str = None
self.login_stack: List[str] = []
readline.parse_and_bind("tab: complete")
readline.set_completer(self.completer)
def complete_arguments(self, cmd: str, args: List[str]) -> List[str]:
if cmd in ("cat", "rm", "cp", "mv", "pay"):
if len(args) == 1:
return [file["filename"] for file in self.client.get_all_files(self.device_uuid)]
elif cmd == "morphcoin":
if len(args) == 1:
return ["create", "look"]
elif len(args) == 2:
if args[0] == "look":
return [file["filename"] for file in self.client.get_all_files(self.device_uuid)]
elif cmd == "service":
if len(args) == 1:
return ["create", "bruteforce", "portscan"]
elif len(args) == 2:
if args[0] == "create":
return ["bruteforce", "portscan", "ssh", "telnet"]
return []
def completer(self, text: str, state: int) -> Optional[str]:
cmd, *args = readline.get_line_buffer().split(" ") or [""]
if not args:
options: List[str] = self.COMMANDS
else:
options: List[str] = self.complete_arguments(cmd, args)
options: List[str] = [o + " " for o in options if o.startswith(text)]
if state < len(options):
return options[state]
return None
def load_session(self) -> bool:
try:
content: dict = json.load(open(os.path.join(*self.session_file)))
if "token" in content:
self.session_token: str = content["token"]
self.client.session(self.session_token)
return True
except FileNotFoundError:
pass
except json.JSONDecodeError:
pass
return False
def save_session(self):
for i in range(1, len(self.session_file)):
path: str = os.path.join(*self.session_file[:i])
if not os.path.exists(path):
os.mkdir(path)
path: str = os.path.join(*self.session_file)
with open(path, "w") as file:
json.dump({"token": self.session_token}, file)
file.flush()
def delete_session(self):
os.remove(os.path.join(*self.session_file))
def register(self) -> bool:
username: str = input("Username: ")
mail: str = input("Email Address: ")
password: str = getpass.getpass("Password: ")
confirm_password: str = getpass.getpass("Confirm Password: ")
if password != confirm_password:
print("Passwords don't match.")
return False
try:
self.session_token: str = self.client.register(username, mail, password)
self.save_session()
return True
except WeakPasswordException:
print("Password is too weak.")
except UsernameAlreadyExistsException:
print("Username already exists.")
except InvalidEmailException:
print("Invalid email")
return False
def login(self) -> bool:
username: str = input("Username: ")
password: str = getpass.getpass("Password: ")
try:
self.session_token: str = self.client.login(username, password)
self.save_session()
return True
except InvalidLoginException:
print("Invalid Login Credentials.")
return False
def get_file(self, filename: str) -> Optional[dict]:
files: List[dict] = self.client.get_all_files(self.device_uuid)
for file in files:
if file["filename"] == filename:
return file
return None
def get_service(self, name: str) -> Optional[dict]:
services: List[dict] = self.client.get_services(self.device_uuid)
for service in services:
if service["name"] == name:
return service
return None
def update_host(self, device_uuid: str = None):
if device_uuid is None:
devices: List[dict] = self.client.get_all_devices()
if not devices:
devices: List[dict] = [self.client.create_device()]
self.hostname: str = devices[0]["name"]
self.device_uuid: str = devices[0]["uuid"]
else:
self.device_uuid: str = device_uuid
self.hostname: str = self.client.device_info(device_uuid)["name"]
def update_username(self):
self.username: str = self.client.info()["name"]
def remote(self) -> bool:
return len(self.login_stack) > 1
def mainloop(self):
history: List[str] = []
self.update_host()
self.update_username()
self.login_stack.append(self.device_uuid)
print(f"Logged in as {self.username}.")
while True:
if self.remote():
prompt: str = "\033[38;2;255;64;23m"
else:
prompt: str = "\033[38;2;100;221;23m"
prompt += f"{self.username}@{self.hostname} $ \033[0m"
cmd: str = None
args: List[str] = None
try:
command: List[str] = input(prompt).lstrip().split()
if not command:
continue
cmd, *args = command
except EOFError:
print("exit")
if self.remote():
self.login_stack.pop()
self.update_host(self.login_stack[-1])
else:
exit()
except KeyboardInterrupt:
print("^C")
continue
command: str = cmd + " " + " ".join(args)
if history[-1:] != [command]:
history.append(command)
if cmd in ("exit", "quit"):
if self.remote():
self.login_stack.pop()
self.update_host(self.login_stack[-1])
else:
exit()
elif cmd == "logout":
if self.remote():
self.login_stack.pop()
self.update_host(self.login_stack[-1])
else:
self.delete_session()
print("Logged out.")
break
elif cmd == "help":
for command in Game.COMMANDS:
print(command)
elif cmd == "status":
online: int = self.client.info()["online"]
print(f"Online players: {online}")
elif cmd == "whoami":
self.update_username()
print(self.username)
elif cmd == "hostname":
if args:
name: str = " ".join(args)
self.client.change_device_name(self.device_uuid, name)
self.update_host(self.device_uuid)
if not args:
print(self.hostname)
elif cmd in ("ls", "l", "dir"):
files: List[dict] = self.client.get_all_files(self.device_uuid)
for file in files:
print(file["filename"])
elif cmd == "touch":
if not args:
print("usage: touch <filename> [content]")
continue
filename, *content = args
content: str = " ".join(content)
self.client.create_file(self.device_uuid, filename, content)
elif cmd == "cat":
if not args:
print("usage: cat <filename>")
continue
filename: str = args[0]
file: dict = self.get_file(filename)
if file is None:
print("File does not exist.")
continue
print(file["content"])
elif cmd == "rm":
if not args:
print("usage: rm <filename>")
continue
filename: str = args[0]
file: dict = self.get_file(filename)
if file is None:
print("File does not exist.")
continue
content: str = file["content"]
wallet: Tuple[str, str] = extract_wallet(content)
if wallet is not None:
wallet_uuid, wallet_key = wallet
try:
amount: int = self.client.get_wallet(wallet_uuid, wallet_key)["amount"]
while True:
choice: str = input(f"\033[38;2;255;51;51mThis file contains {amount} morphcoin. "
f"Do you want to delete the corresponding wallet? [yes|no] \033[0m")
if choice in ("yes", "no"):
break
print(f"'{choice}' is not one of the following: yes, no")
if choice == "yes":
self.client.delete_wallet(wallet_uuid, wallet_key)
print("The wallet has been deleted.")
else:
print("The following key might now be the only way to access your wallet.")
print("Note that you can't create another wallet without this key.")
print(content)
except InvalidKeyException:
pass
self.client.remove_file(self.device_uuid, file["uuid"])
elif cmd == "cp":
if len(args) != 2:
print("usage: cp <source> <destination>")
continue
source: str = args[0]
destination: str = args[1]
file: dict = self.get_file(source)
if file is None:
print("File does not exist.")
continue
self.client.create_file(self.device_uuid, destination, file["content"])
elif cmd == "mv":
if len(args) != 2:
print("usage: mv <source> <destination>")
continue
source: str = args[0]
destination: str = args[1]
file: dict = self.get_file(source)
if file is None:
print("File does not exist.")
continue
self.client.create_file(self.device_uuid, destination, file["content"])
self.client.remove_file(self.device_uuid, file["uuid"])
elif cmd == "clear":
print(end="\033c")
elif cmd == "history":
for line in history:
print(line)
elif cmd == "morphcoin":
if len(args) != 2 or args[0] not in ("look", "create"):
print("usage: morphcoin look|create <filename>")
continue
filename: str = args[1]
if args[0] == "create":
try:
uuid, key = self.client.create_wallet()
self.client.create_file(self.device_uuid, filename, uuid + " " + key)
except AlreadyOwnAWalletException:
print("You already own a wallet")
elif args[0] == "look":
file: dict = self.get_file(filename)
if file is None:
print("File does not exist.")
continue
wallet: Tuple[str, str] = extract_wallet(file["content"])
if wallet is None:
print("File is no wallet file.")
continue
try:
amount: int = self.client.get_wallet(*wallet)["amount"]
except InvalidWalletException:
print("Invalid wallet file. Wallet does not exist.")
continue
except InvalidKeyException:
print("Invalid wallet file. Key is incorrect.")
continue
print(f"{amount} morphcoin")
elif cmd == "pay":
if len(args) < 3:
print("usage: pay <filename> <receiver> <amount> [usage]")
continue
file: dict = self.get_file(args[0])
if file is None:
print("File does not exist.")
continue
wallet: Tuple[str, str] = extract_wallet(file["content"])
if wallet is None:
print("File is no wallet file.")
continue
wallet_uuid, wallet_key = wallet
receiver: str = args[1]
if not is_uuid(receiver):
print("Invalid receiver.")
continue
if not args[2].isnumeric():
print("amount is not a number.")
continue
amount: int = int(args[2])
try:
self.client.get_wallet(wallet_uuid, wallet_key)
except InvalidWalletException:
print("Invalid wallet file. Wallet does not exist.")
continue
except InvalidKeyException:
print("Invalid wallet file. Key is incorrect.")
continue
try:
self.client.send(wallet_uuid, wallet_key, receiver, amount, " ".join(args[3:]))
print(f"Sent {amount} morphcoin to {receiver}.")
except SourceWalletTransactionDebtException:
print("The source wallet would make debt. Transaction canceled.")
except InvalidWalletException:
print("Destination wallet does not exist.")
elif cmd == "service":
if len(args) < 1 or args[0] not in ("create", "bruteforce", "portscan"):
print("usage: service create|bruteforce|portscan")
elif args[0] == "create":
if len(args) != 2 or args[1] not in ("bruteforce", "portscan", "telnet", "ssh"):
print("usage: service create <bruteforce|portscan|telnet|ssh>")
continue
try:
self.client.create_service(self.device_uuid, args[1])
print("Service was created")
except AlreadyOwnServiceException:
print("You already created this service")
elif args[0] == "bruteforce":
if len(args) != 3:
print("usage: service bruteforce <target-device> <target-service>")
continue
target_device: str = args[1]
target_service: str = args[2]
if not is_uuid(target_device):
print("Invalid target device")
continue
if not is_uuid(target_service):
print("Invalid target service")
continue
service: dict = self.get_service("bruteforce")
if service is None:
print("You have to create a bruteforce service before you use it")
continue
try:
result: dict = self.client.use_service(
self.device_uuid, service["uuid"],
target_device=target_device, target_service=target_service
)
assert result["ok"]
if "access" in result:
if result["access"]:
print("Access granted - use `connect <device>`")
else:
print("Access denied. The bruteforce attack was not successful")
else:
print("You started a bruteforce attack")
except UnknownServiceException:
print("Unknown service. Attack couldn't be started.")
elif args[0] == "portscan":
if len(args) != 2:
print("usage: service portscan <device>")
continue
target: str = args[1]
if not is_uuid(target):
print("Invalid target")
continue
service: dict = self.get_service("portscan")
if service is None:
print("You have to create a portscan service before you use it")
continue
result: dict = self.client.use_service(self.device_uuid, service["uuid"], target_device=target)
if not result["services"]:
print("That device doesn't have any running services")
for service in result["services"]:
name: str = service["name"]
uuid: str = service["uuid"]
port: int = service["running_port"]
print(f" - {name} on port {port} (UUID: {uuid})")
elif cmd == "spot":
device: dict = self.client.spot()
name: str = device["name"]
powered: bool = device["powered_on"]
uuid: str = device["uuid"]
powered_text: str = ["\033[38;2;255;51;51mno", "\033[38;2;100;246;23myes"][powered] + "\033[0m"
print(f"Name: '{name}'")
print(f"UUID: {uuid}")
print(f"Powered on: {powered_text}")
service: dict = self.get_service("portscan")
if service is not None:
print("Services:")
result: dict = self.client.use_service(self.device_uuid, service["uuid"], target_device=uuid)
if not result["services"]:
print(" This device doesn't have any running services")
for service in result["services"]:
name: str = service["name"]
uuid: str = service["uuid"]
port: int = service["running_port"]
print(f" - {name} on port {port} (UUID: {uuid})")
elif cmd == "connect":
if len(args) != 1:
print("usage: connect <device>")
continue
uuid: str = args[0]
if not is_uuid(uuid):
print("Invalid device")
continue
if self.client.part_owner(uuid):
self.login_stack.append(uuid)
self.update_host(uuid)
else:
print("Access denied")
else:
print("Command could not be found.")
print("Type `help` for a list of commands.")
def main():
game: Game = Game(SERVER, [os.path.expanduser("~"), ".config", "pycrypcli", "session.json"])
if len(sys.argv) > 1:
arg: str = sys.argv[1]
if arg.lower() in ("signup", "register"):
if not game.register():
print("Registration failed.")
return
else:
print("Python Cryptic Game Client (https://github.com/Defelo/PyCrypCli)")
print()
print(f"Usage: {sys.argv[0]} [help|signup]")
return
else:
login_needed: bool = False
try:
if not game.load_session():
login_needed: bool = True
except InvalidSessionTokenException:
game.delete_session()
login_needed: bool = True
if login_needed:
if not game.login():
print("Login failed.")
return
assert game.session_token is not None
game.mainloop()
if __name__ == '__main__':
main()