Created
August 29, 2016 15:00
-
-
Save BookGin/74fbd790c0f23218fe9a6ec47e47fbb9 to your computer and use it in GitHub Desktop.
IceCTF 2016 Stage3 l33tcrypt: server.py
This file contains hidden or 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
#!/usr/bin/python | |
# IceCTF 2016 - Stage3 - l33tcrypt | |
# finalC · 182 solves / 1660· Cryptography · 90 pt | |
# | |
# l33tcrypt is a new and fresh encryption service. | |
# For added security it pads all information with the flag! | |
# Can you get it? nc l33tcrypt.vuln.icec.tf 6001 server.py | |
# [server.py]: | |
from Crypto.Cipher.AES import AESCipher | |
import SocketServer as ss | |
import signal | |
import base64 | |
from secret import KEY, FLAG | |
PORT = 6001 | |
def pad(text, bs): | |
text = text + FLAG | |
pad_num = (bs - len(text) % bs) | |
return text + chr(pad_num) * pad_num | |
def recvline(req): | |
buf = b"" | |
while not buf.endswith(b"\n"): | |
buf += req.recv(1) | |
return buf | |
class RequestHandler(ss.BaseRequestHandler): | |
def handle(self): | |
req = self.request | |
signal.alarm(5) | |
req.sendall("Welcome to l33tserver where all your encryption needs are served.\n") | |
req.sendall("Send me something to encrypt:\n") | |
data = recvline(req).strip() | |
try: | |
data = base64.b64decode(data) | |
except: | |
req.sendall("bad data\n") | |
req.close() | |
return | |
if not data.startswith("l33tserver please"): | |
req.sendall("You didnt say the magic word :(\n") | |
req.close() | |
return | |
c = AESCipher(KEY).encrypt(pad(data, 16)) | |
req.sendall("Your l33tcrypted data:\n") | |
req.sendall(base64.b64encode(c) + "\n") | |
req.close() | |
class TCPServer(ss.ForkingMixIn, ss.TCPServer): | |
pass | |
ss.TCPServer.allow_reuse_address = True | |
server = TCPServer(("0.0.0.0", PORT), RequestHandler) | |
print("Server listening on port %d" % PORT) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment