Created
October 8, 2019 07:54
-
-
Save sea0h7e/f5b7d2ef61969fe6599d9cc2b1b1f475 to your computer and use it in GitHub Desktop.
InCTF2019 - cookiegen (Crypto, 951 pts, 8 solves)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pwn import * | |
from Crypto.Util.number import * | |
import hmac | |
from hashlib import md5 | |
context.log_level = 'critical' | |
p = 337333846325195852023465984016735747017640658020735865443882234978293187151183899366894634062588357161 | |
def register(name, base): | |
con = remote('18.216.251.207', 3197) | |
con.recvuntil('register: ') | |
con.sendline('1') | |
con.recvuntil('Enter your username: ') | |
con.sendline(name) | |
con.recvuntil('token: ') | |
con.sendline(str(base)) | |
con.recvuntil('auth token: ') | |
return con.recvuntil('\n', True) | |
def login(token): | |
con = remote('18.216.251.207', 3197) | |
con.recvuntil('register: ') | |
con.sendline('2') | |
con.recvuntil('token: ') | |
con.sendline(token) | |
con.interactive() | |
prefix = "cookie?username=" | |
target = 'admin' + '=' * 10 | |
name = '1' * 15 | |
enc1 = register(name, 233).split(':')[0].decode('hex') | |
enc1 = [enc1[16 * i : 16 * i + 16] for i in range(len(enc1) // 16)] | |
suffix = xor(md5(enc1[1]).digest(), xor(md5(enc1[2]).digest(), target + '=')) | |
con = remote('18.216.251.207', 3197) | |
enc2 = register(name + '=' + suffix, 233).split(':')[0].decode('hex') | |
enc2 = [enc2[16 * i : 16 * i + 16] for i in range(len(enc2) // 16)] | |
payload = ''.join(enc1[:2])+ enc2[3] + ''.join(enc1[1:]) | |
''' | |
def TonelliShanks(p, n): | |
Q, S = p - 1, 0 | |
while Q % 2 == 0: | |
S += 1 | |
Q //= 2 | |
z = 0 | |
while True: | |
if pow(z, (p - 1) / 2, p) == p - 1: | |
break | |
z += 1 | |
M = S | |
c = pow(z, Q, p) | |
t = pow(n ,Q, p) | |
R = pow(n, (Q + 1) / 2, p) | |
while True: | |
if t == 0: | |
r = 0 | |
break | |
elif t == 1: | |
r = R | |
break | |
for i in range(M): | |
if pow(t, pow(2, i, p), p) == 1: | |
break | |
b = pow(c, pow(2, M - i - 1, p), p) | |
M = i | |
c = pow(b, 2, p) | |
t = t * c % p | |
R = R * b % p | |
return r, p - r | |
p = 337333846325195852023465984016735747017640658020735865443882234978293187151183899366894634062588357161 | |
n = p - 1 | |
a, b = TonelliShanks(p, n) | |
assert pow(a, 2, p) == n | |
assert pow(b, 2, p) == n | |
''' | |
base = 121994163782610545260245062826237002508494556869862920055894528697525559257543698142374390705394532781 | |
pos = [1L, 215339682542585306763220921190498744509146101150872945387987706280767627893640201224520243357193824380L, 121994163782610545260245062826237002508494556869862920055894528697525559257543698142374390705394532781L, 337333846325195852023465984016735747017640658020735865443882234978293187151183899366894634062588357160L] | |
for i in range(4): | |
shared_key = long_to_bytes(pos[i]) | |
token = hmac.new(shared_key, 'admin').hexdigest() | |
fake = payload.encode('hex') + ':' + long_to_bytes(base).encode("hex") + ":" + token | |
print fake | |
login(fake) | |
#inctf{great_j0b_with_CBC_Bit_flipping_n_small_subgroup_confinement!} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from cookiegen import Service | |
from secret import key, iv, secret, x, flag | |
p = 337333846325195852023465984016735747017640658020735865443882234978293187151183899366894634062588357161 | |
class Unbuffered(object): | |
def __init__(self, stream): | |
self.stream = stream | |
def write(self, data): | |
self.stream.write(data) | |
self.stream.flush() | |
def writelines(self, datas): | |
self.stream.writelines(datas) | |
self.stream.flush() | |
def __getattr__(self, attr): | |
return getattr(self.stream, attr) | |
sys.stdout = Unbuffered(sys.stdout) | |
print "Welcome to CookieGen service!" | |
print "[1] Register" | |
print "[2] Login" | |
choice = int(raw_input("Select if you want to login or register: ")) | |
if choice == 1: | |
username = raw_input("Enter your username: ") | |
if "admin" in username: | |
print "[-] Invalid username!" | |
sys.exit(0) | |
base = int(raw_input("Enter your base for authentication token: ")) | |
if base < 2 or base >= p-1: | |
print "[-] Invalid base value!" | |
sys.exit(0) | |
service_obj = Service(key, secret, p, x, iv) | |
print "Here, take your cookie and auth token: ", service_obj.register(username, base) | |
elif choice == 2: | |
session_cookie = raw_input("Enter your cookie along with auth token: ") | |
service_obj = Service(key, secret, p, x, iv) | |
username = service_obj.login(session_cookie) | |
if username == None: | |
print "Invalid session cookie or auth token!" | |
elif username != "admin": | |
print "Welcome ", username | |
else: | |
print "Welcome admin! Here, take you flag", flag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment