forked from radiactivo/picoctf_2017_writeup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhshell.py
72 lines (58 loc) · 1.72 KB
/
dhshell.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
#!/usr/bin/python2 -u
from hashlib import sha256
from Crypto import Random
from Crypto.Random import random
from Crypto.Cipher import AES
from subprocess import check_output, STDOUT, CalledProcessError
BLOCK_SIZE = 16
R = Random.new()
with open("parameters.txt") as f:
p = int(f.readline().strip())
g = int(f.readline().strip())
password = open("password.txt").read()
def pad(m):
o = BLOCK_SIZE - len(m) % BLOCK_SIZE
return m + o * chr(o)
def unpad(p):
return p[0:-ord(p[-1])]
def send_encrypted(KEY, m):
IV = R.read(BLOCK_SIZE)
aes = AES.new(KEY, AES.MODE_CBC, IV)
c = aes.encrypt(pad(m))
print (IV + c).encode('hex')
def read_encrypted(KEY):
data = raw_input("").decode('hex')
IV, data = data[:BLOCK_SIZE], data[BLOCK_SIZE:]
aes = AES.new(KEY, AES.MODE_CBC, IV)
m = unpad(aes.decrypt(data))
return m
def serve_commands(KEY):
while True:
cmd = read_encrypted(KEY)
try:
output = check_output(cmd, shell=True, stderr=STDOUT)
except CalledProcessError as e:
output = str(e) + "\n"
send_encrypted(KEY, output)
print """Welcome to the
______ _ _ _____ _ _ _
| _ \ | | | / ___| | | | |
| | | | |_| | \ `--.| |__ ___| | |
| | | | _ | `--. \ '_ \ / _ \ | |
| |/ /| | | | /\__/ / | | | __/ | |
|___/ \_| |_/ \____/|_| |_|\___|_|_|
"""
print "Parameters:"
print "p = {}".format(p)
print "g = {}".format(g)
a = random.randint(1, 2**46)
A = pow(g, a, p) # A = g**a mod p
print "A = {}".format(A)
B = int(raw_input("Please supply B: "))
K = pow(B, a, p) # K = B**a mod p
KEY = sha256(str(K)).digest()
pw = read_encrypted(KEY)
if pw == password:
serve_commands(KEY)
else:
send_encrypted("Invalid password!\n")