|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import time |
| 5 | +import copy |
| 6 | + |
| 7 | +from checklib import * |
| 8 | + |
| 9 | +argv = copy.deepcopy(sys.argv) |
| 10 | + |
| 11 | +from pwn import * |
| 12 | +from aquarius_lib import * |
| 13 | +import aquarius_assembler as asm |
| 14 | + |
| 15 | + |
| 16 | +def machine_with_known_output(output: bytes) -> bytes: |
| 17 | + code = b"" |
| 18 | + for i, c in enumerate(output): |
| 19 | + code += asm.str8(i, c) |
| 20 | + |
| 21 | + code += asm.mov("r0", asm.SYSCALL_WRITE) |
| 22 | + code += asm.mov("r1", 0) |
| 23 | + code += asm.mov("r2", len(output)) |
| 24 | + code += asm.syscall() |
| 25 | + return code |
| 26 | + |
| 27 | + |
| 28 | +def machine_with_password(output: bytes, password: bytes) -> bytes: |
| 29 | + password_promt = machine_with_known_output(b"password: ") |
| 30 | + wrong_password = machine_with_known_output(b"wrong password\n") + asm.hlt() |
| 31 | + output_data = machine_with_known_output(output) + asm.hlt() |
| 32 | + code = b"" |
| 33 | + code += password_promt |
| 34 | + code += asm.mov("r0", asm.SYSCALL_READ) |
| 35 | + code += asm.mov("r1", 0) |
| 36 | + code += asm.mov("r2", len(password)) |
| 37 | + code += asm.syscall() |
| 38 | + code += asm.mov("r1", 1) |
| 39 | + for i, c in enumerate(password): |
| 40 | + code += asm.ldr8("r3", i) |
| 41 | + code += asm.cmp("r2", asm.CMP_EQ, "r3", c) |
| 42 | + code += asm.band("r1", "r2") |
| 43 | + code += asm.rjmp("r1", len(wrong_password)) |
| 44 | + code += wrong_password |
| 45 | + code += output_data |
| 46 | + return code |
| 47 | + |
| 48 | + |
| 49 | +class Checker(BaseChecker): |
| 50 | + vulns: int = 1 |
| 51 | + timeout: int = 5 |
| 52 | + uses_attack_data: bool = True |
| 53 | + |
| 54 | + def __init__(self, *args, **kwargs): |
| 55 | + super(Checker, self).__init__(*args, **kwargs) |
| 56 | + self.mch = CheckMachine(self) |
| 57 | + |
| 58 | + def action(self, action, *args, **kwargs): |
| 59 | + try: |
| 60 | + super(Checker, self).action(action, *args, **kwargs) |
| 61 | + except (pwnlib.exception.PwnlibException, EOFError): |
| 62 | + self.cquit(Status.DOWN, "got connect error", "got pwntools connect error") |
| 63 | + except UnicodeDecodeError: |
| 64 | + self.cquit(Status.MUMBLE, "got unicode error", "got unicode error") |
| 65 | + |
| 66 | + def check(self): |
| 67 | + with self.mch.connect() as io: |
| 68 | + output = rnd_string(32).encode() |
| 69 | + rom = machine_with_known_output(output + b"\n") + asm.hlt() |
| 70 | + machine_id = self.mch.upload_vm(io, rom, Status.MUMBLE) |
| 71 | + self.mch.run_vm(io, machine_id, Status.MUMBLE) |
| 72 | + res = io.recvline() |
| 73 | + # raise ValueError(rom) |
| 74 | + self.assert_eq( |
| 75 | + res.strip(), output, f"invalid flag on {rom}", Status.CORRUPT |
| 76 | + ) |
| 77 | + self.cquit(Status.OK) |
| 78 | + |
| 79 | + self.cquit(Status.OK) |
| 80 | + |
| 81 | + def put(self, flag_id: str, flag: str, vuln: str): |
| 82 | + with self.mch.connect() as io: |
| 83 | + password = rnd_string(32) |
| 84 | + rom = machine_with_password(flag.encode() + b"\n", password.encode()) |
| 85 | + machine_id = self.mch.upload_vm(io, rom, Status.MUMBLE) |
| 86 | + self.mch.exit(io) |
| 87 | + self.cquit(Status.OK, f"{machine_id}", f"{machine_id}:{password}") |
| 88 | + |
| 89 | + def get(self, flag_id: str, flag: str, vuln: str): |
| 90 | + machine_id, password = flag_id.split(":") |
| 91 | + with self.mch.connect() as io: |
| 92 | + self.mch.run_vm(io, machine_id, Status.CORRUPT) |
| 93 | + io.recvuntil(b"password: ") |
| 94 | + io.send(password) |
| 95 | + res = io.recvline() |
| 96 | + self.assert_eq(res.strip(), flag.encode(), "invalid flag", Status.CORRUPT) |
| 97 | + self.cquit(Status.OK) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + # import base64 |
| 102 | + # |
| 103 | + # print(base64.b64encode(machine_with_password(b"kek", b"lol"))) |
| 104 | + # print(asm.cmp("r2", asm.CMP_EQ, "r3", 1337)) |
| 105 | + # exit(0) |
| 106 | + c = Checker(argv[2]) |
| 107 | + |
| 108 | + try: |
| 109 | + c.action(argv[1], *argv[3:]) |
| 110 | + except c.get_check_finished_exception(): |
| 111 | + cquit(Status(c.status), c.public, c.private) |
0 commit comments