Skip to content

Instantly share code, notes, and snippets.

@mimoo
Created September 22, 2016 21:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mimoo/8d8c02b9fe299f0ed10bff63092052e8 to your computer and use it in GitHub Desktop.
Save mimoo/8d8c02b9fe299f0ed10bff63092052e8 to your computer and use it in GitHub Desktop.
CS challenge 2 - Submarine
import base64
from Crypto.Cipher import AES
class AES_CBC_Cipher:
def __init__( self, key ):
self.key = key
def pad(self, s):
return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)
def unpad(self, s):
return s[:-ord(s[len(s)-1:])]
def encrypt( self, iv, raw ):
raw = self.pad(raw)
cipher = AES.new( self.key, AES.MODE_CBC, iv )
return base64.b64encode( iv + cipher.encrypt( raw ) )
def decrypt( self, enc ):
enc = base64.b64decode(enc)
iv = enc[:16]
cipher = AES.new(self.key, AES.MODE_CBC, iv )
return self.unpad(cipher.decrypt( enc[16:] ))
from aes_cbc_cipher import AES_CBC_Cipher
from Crypto import Random
secret_key = "abcdefghijklmnop" # not the real one =)
def validate_credentials(user, password):
if user == "qwerty" and password == "asdf":
return True
else:
return False
def login(user, password):
if not validate_credentials(user, password):
return False
cipher = AES_CBC_Cipher(secret_key)
IV = Random.new().read(16)
return cipher.encrypt(IV, user)
def main():
token = raw_input("Do you have a token? (y/n)\n")
if token == "y":
ciphertext = raw_input("token?\n")
cipher = AES_CBC_Cipher(secret_key)
user = cipher.decrypt(ciphertext)
print "You are logged in as", user
else:
user = raw_input("user?\n")
password = raw_input("password?\n")
token = login(user, password)
if not token:
print "Wrong credentials\n"
else:
print "Success. here's your token:"
print token
if __name__ == "__main__":
main()
@shaduk
Copy link

shaduk commented Nov 2, 2016

Any hint?. Is it along the lines of padding oracle attack?

@qkta
Copy link

qkta commented Dec 4, 2016

@shaduk without an oracle huh? lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment