generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
81 lines (51 loc) · 2.03 KB
/
api.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
#!/usr/bin/env python3
from typing import Iterator
import contextlib
# https://github.com/oalieno/mini-pwntools
import minipwn as pwn
class API:
def __init__(self, io: pwn.remote):
self.io = io
def register(self, username: bytes, password: bytes, secret: bytes) -> bytes:
self.io.sendlineafter(b'> ', b'REGISTER')
self.io.sendlineafter(b': ', username)
self.io.sendlineafter(b': ', password)
self.io.sendlineafter(b': ', secret)
return self.io.recvline().strip()
def login(self, username: bytes, password: bytes) -> bytes:
self.io.sendlineafter(b'> ', b'LOGIN')
self.io.sendlineafter(b': ', username)
self.io.sendlineafter(b': ', password)
return self.io.recvline().strip()
def logout(self) -> bytes:
self.io.sendlineafter(b'> ', b'LOGOUT')
return self.io.recvline().strip()
def encrypt(self, plaintext: bytes, username: bytes = None) -> bytes:
self.io.sendlineafter(b'> ', b'ENCRYPT')
if username is not None:
self.io.sendlineafter(b': ', username)
self.io.sendlineafter(b': ', plaintext.hex().encode())
line = self.io.recvline().strip()
if line.startswith(b'error'):
return line
ciphertext = self.io.recvline().strip().decode()
return bytes.fromhex(ciphertext)
def decrypt(self, ciphertext: bytes) -> bytes:
self.io.sendlineafter(b'> ', b'DECRYPT')
self.io.sendlineafter(b': ', ciphertext.hex().encode())
line = self.io.recvline().strip()
if line.startswith(b'error'):
return line
plaintext = self.io.recvline().strip().decode()
return bytes.fromhex(plaintext)
def exit(self) -> bytes:
self.io.sendlineafter(b'> ', b'EXIT')
return self.io.recvline().strip()
@contextlib.contextmanager
def connect(hostname: str, port: int = 17171) -> Iterator[API]:
io = pwn.remote(hostname, port)
api = API(io)
try:
yield api
finally:
io.s.close()