from secrets import users, salt import hashlib import base64 import json import http.server withopen("flag.txt","r") as f: FLAG = f.read().strip() defgethash(*items): c = 0 for item in items: if item isNone: continue c ^= int.from_bytes(hashlib.md5(f"{salt}[{item}]{salt}".encode()).digest(), "big") # it looks so complex! but is it safe enough? returnhex(c)[2:] assert"admin"in users assert users["admin"] == "admin" hashed_users = dict((k,gethash(k,v)) for k,v in users.items()) eval(int.to_bytes(0x636d616f686e69656e61697563206e6965756e63696165756e6320696175636e206975616e6363616361766573206164^8651845801355794822748761274382990563137388564728777614331389574821794036657729487047095090696384065814967726980153,160,"big",signed=True).decode().translate({ord(c):Nonefor c in"\x00"})) # what is it? defdecrypt(data:str): for x inrange(5): data = base64.b64encode(data).decode() # ummm...? It looks like it's just base64 encoding it 5 times? truely? return data __page__ = base64.b64encode("PCFET0NU...KPC9odG1sPg==") classMyHandler(http.server.BaseHTTPRequestHandler): defdo_GET(self): try: if self.path == "/": self.send_response(200) self.end_headers() self.wfile.write(__page__) else: self.send_response(404) self.end_headers() self.wfile.write(b"404 Not Found") except Exception as e: print(e) self.send_response(500) self.end_headers() self.wfile.write(b"500 Internal Server Error") defdo_POST(self): try: if self.path == "/login": body = self.rfile.read(int(self.headers.get("Content-Length"))) payload = json.loads(body) params = json.loads(decrypt(payload["params"])) print(params) if params.get("username") == "admin": self.send_response(403) self.end_headers() self.wfile.write(b"YOU CANNOT LOGIN AS ADMIN!") print("admin") return if params.get("username") == params.get("password"): self.send_response(403) self.end_headers() self.wfile.write(b"YOU CANNOT LOGIN WITH SAME USERNAME AND PASSWORD!") print("same") return hashed = gethash(params.get("username"),params.get("password")) for k,v in hashed_users.items(): if hashed == v: data = { "user":k, "hash":hashed, "flag": FLAG if k == "admin"else"flag{YOU_HAVE_TO_LOGIN_IN_AS_ADMIN_TO_GET_THE_FLAG}" } self.send_response(200) self.end_headers() self.wfile.write(json.dumps(data).encode()) print("success") return self.send_response(403) self.end_headers() self.wfile.write(b"Invalid username or password") else: self.send_response(404) self.end_headers() self.wfile.write(b"404 Not Found") except Exception as e: print(e) self.send_response(500) self.end_headers() self.wfile.write(b"500 Internal Server Error") if __name__ == "__main__": server = http.server.HTTPServer(("", 9999), MyHandler) server.serve_forever()
1 2 3 4 5 6 7 8 9 10 11 12
hashed = gethash(params.get("username"),params.get("password")) for k,v in hashed_users.items(): if hashed == v: data = { "user":k, "hash":hashed, "flag": FLAG if k == "admin"else"flag{YOU_HAVE_TO_LOGIN_IN_AS_ADMIN_TO_GET_THE_FLAG}" } self.send_response(200) self.end_headers() self.wfile.write(json.dumps(data).encode()) print("success")
defgethash(*items): c = 0 for item in items: if item isNone: continue c ^= int.from_bytes(hashlib.md5(f"{salt}[{item}]{salt}".encode()).digest(), "big") # it looks so complex! but is it safe enough? returnhex(c)[2:]
异或操作
1 2 3 4 5 6
for k,v in hashed_users.items()
hashed_users = dict((k,gethash(k,v)) for k,v in users.items())