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
|
import os, pty, re, select, signal, struct, subprocess, sys, time
BIN = sys.argv[1] if len(sys.argv) > 1 else "./vvvmmm" BLOB_LEN = 0x296 BLOB_SIG = b"\x1d\x71\xa2\xec\xa6\xe8\xca\xe4" CONST_PAT = b"e4Y8YRXVzg2HRrCUy35CM0Txq91HzMGZ" INP, CST, STK = 0x10000000, 0x10001000, 0x20000000 PAGE = 0x1000
u64 = lambda x: x & 0xFFFFFFFFFFFFFFFF sext = lambda x,b: ((x & ((1<<b)-1)) ^ (1<<(b-1))) - (1<<(b-1)) mulhu = lambda a,b: ((a&0xFFFFFFFFFFFFFFFF)*(b&0xFFFFFFFFFFFFFFFF) >> 64) & 0xFFFFFFFFFFFFFFFF def remu(a,b): a&=0xFFFFFFFFFFFFFFFF; b&=0xFFFFFFFFFFFFFFFF; return a if b==0 else a%b def remuw(a,b): a&=0xFFFFFFFF; b&=0xFFFFFFFF; return a if b==0 else a%b
class Mem: def __init__(s): s.p={} def map(s,a,n): a&=~(PAGE-1); e=(a+n+PAGE-1)&~(PAGE-1) for x in range(a,e,PAGE): s.p.setdefault(x, bytearray(PAGE)) def _po(s,a): b=a&~(PAGE-1); if b not in s.p: raise RuntimeError(f"unmapped {a:#x}") return s.p[b], a-b def rb(s,a,n): o=bytearray() for i in range(n): p,off=s._po(a+i); o.append(p[off]) return bytes(o) def wb(s,a,d): for i,v in enumerate(d): p,off=s._po(a+i); p[off]=v def u8(s,a): return s.rb(a,1)[0] def u16(s,a): return int.from_bytes(s.rb(a,2),'little') def u32(s,a): return int.from_bytes(s.rb(a,4),'little') def u64(s,a): return int.from_bytes(s.rb(a,8),'little') def w32(s,a,v): s.wb(a, struct.pack("<I", v & 0xFFFFFFFF)) def w64(s,a,v): s.wb(a, struct.pack("<Q", v & 0xFFFFFFFFFFFFFFFF))
def rvc(cpu, ins): x, SP, RA = cpu['x'], 2, 1 op, f3 = ins & 3, (ins>>13)&7
if op==0: if f3==2: rd=8+((ins>>2)&7); rs1=8+((ins>>7)&7) u=((ins>>6)&1)<<2 | ((ins>>10)&7)<<3 | ((ins>>5)&1)<<6 a=u64(x[rs1]+u); x[rd]=sext(cpu['m'].u32(a),32) & 0xFFFFFFFFFFFFFFFF; return if f3==6: rs2=8+((ins>>2)&7); rs1=8+((ins>>7)&7) u=((ins>>6)&1)<<2 | ((ins>>10)&7)<<3 | ((ins>>5)&1)<<6 cpu['m'].w32(u64(x[rs1]+u), x[rs2]); return
if op==1: if f3==0: rd=(ins>>7)&0x1F; imm=sext(((ins>>2)&0x1F)|((ins>>12)&1)<<5,6) if rd: x[rd]=u64(x[rd]+imm); return return if f3==2: rd=(ins>>7)&0x1F; imm=sext(((ins>>2)&0x1F)|((ins>>12)&1)<<5,6) if rd: x[rd]=u64(imm); return return if f3==3: rd=(ins>>7)&0x1F if rd==SP: imm=((ins>>6)&1)<<4 | ((ins>>2)&1)<<5 | ((ins>>5)&1)<<6 | ((ins>>3)&3)<<7 | ((ins>>12)&1)<<9 imm=sext(imm,10); x[SP]=u64(x[SP]+imm); return if f3==5: imm=((ins>>3)&7)<<1 | ((ins>>11)&1)<<4 | ((ins>>2)&1)<<5 | ((ins>>7)&1)<<6 | ((ins>>6)&1)<<7 \ | ((ins>>9)&3)<<8 | ((ins>>8)&1)<<10 | ((ins>>12)&1)<<11 cpu['pc']=u64(cpu['pc']-2+sext(imm,12)); return if f3 in (6,7): rs1=8+((ins>>7)&7) imm=((ins>>3)&3)<<1 | ((ins>>10)&3)<<3 | ((ins>>2)&1)<<5 | ((ins>>5)&3)<<6 | ((ins>>12)&1)<<8 imm=sext(imm,9) z = (x[rs1]==0) if (f3==6 and z) or (f3==7 and not z): cpu['pc']=u64(cpu['pc']-2+imm) return if f3==4: subop=(ins>>10)&3 rd=8+((ins>>7)&7) if subop==0: sh=((ins>>2)&0x1F)|((ins>>12)&1)<<5; x[rd]=u64(x[rd]>>sh); return if subop==1: sh=((ins>>2)&0x1F)|((ins>>12)&1)<<5; x[rd]=u64(sext(x[rd],64)>>sh); return if subop==2: imm=sext(((ins>>2)&0x1F)|((ins>>12)&1)<<5,6); x[rd]=u64(x[rd] & u64(imm)); return if subop==3: f2=(ins>>5)&3; b12=(ins>>12)&1; rs2=8+((ins>>2)&7) a,b=x[rd],x[rs2] if b12==1 and f2 in (0,1): aa=a&0xFFFFFFFF; bb=b&0xFFFFFFFF res=((aa-bb) if f2==0 else (aa+bb)) & 0xFFFFFFFF x[rd]=u64(sext(res,32)); return if f2==0: x[rd]=u64(a-b) elif f2==1: x[rd]=u64(a^b) elif f2==2: x[rd]=u64(a|b) elif f2==3: x[rd]=u64(a&b) else: raise RuntimeError("bad c.alu") return
if op==2: if f3==0: rd=(ins>>7)&0x1F; sh=((ins>>2)&0x1F)|((ins>>12)&1)<<5 if rd: x[rd]=u64(x[rd]<<sh); return return if f3==2: rd=(ins>>7)&0x1F u=((ins>>4)&7)<<2 | ((ins>>12)&1)<<5 | ((ins>>2)&3)<<6 x[rd]=u64(sext(cpu['m'].u32(u64(x[SP]+u)),32)) if rd else 0; return if f3==6: rs2=(ins>>2)&0x1F u=((ins>>9)&0xF)<<2 | ((ins>>7)&3)<<6 cpu['m'].w32(u64(x[SP]+u), x[rs2]); return if f3==4: rs1=(ins>>7)&0x1F; rs2=(ins>>2)&0x1F; b12=(ins>>12)&1 if b12==0: if rs2==0: cpu['pc']=x[rs1]; return x[rs1]=x[rs2]; return else: if rs1==0 and rs2==0: raise StopIteration if rs2==0: x[RA]=cpu['pc']; cpu['pc']=x[rs1]; return x[rs1]=u64(x[rs1]+x[rs2]); return
raise RuntimeError(f"unhandled rvc {ins:#x}")
def step(cpu, limit): m,x=cpu['m'],cpu['x'] if cpu['pc']>=limit: return False b0=m.u8(cpu['pc']) if (b0&3)!=3: ins=m.u16(cpu['pc']); cpu['pc']=u64(cpu['pc']+2); rvc(cpu,ins); return True ins=m.u32(cpu['pc']); pc0=cpu['pc']; cpu['pc']=u64(cpu['pc']+4) op=ins&0x7F; rd=(ins>>7)&0x1F; f3=(ins>>12)&7; rs1=(ins>>15)&0x1F; rs2=(ins>>20)&0x1F; f7=(ins>>25)&0x7F I=sext(ins>>20,12) if op==0x37: x[rd]=u64(ins&0xFFFFF000); return True if op==0x13: a=x[rs1] if f3==0: x[rd]=u64(a+I) elif f3==4: x[rd]=u64(a ^ u64(I)) elif f3==5: sh=(ins>>20)&0x3F x[rd]=u64((a>>sh) if f7==0 else (sext(a,64)>>sh)) elif f3==3: x[rd]=1 if (a&0xFFFFFFFFFFFFFFFF) < (u64(I)&0xFFFFFFFFFFFFFFFF) else 0 else: raise RuntimeError("op-imm") return True if op==0x03: a=u64(x[rs1]+I) if f3==4: x[rd]=m.u8(a) elif f3==2: x[rd]=u64(sext(m.u32(a),32)) elif f3==6: x[rd]=m.u32(a) elif f3==3: x[rd]=m.u64(a) else: raise RuntimeError("load") return True if op==0x23: S=sext(((ins>>7)&0x1F)|(((ins>>25)&0x7F)<<5),12); a=u64(x[rs1]+S) if f3==2: m.w32(a, x[rs2]) elif f3==3: m.w64(a, x[rs2]) else: raise RuntimeError("store") return True if op==0x63: B=sext((((ins>>8)&0xF)<<1)|(((ins>>25)&0x3F)<<5)|(((ins>>7)&1)<<11)|(((ins>>31)&1)<<12),13) take = (x[rs1]==x[rs2]) if f3==0 else (x[rs1]!=x[rs2]) if f3==1 else None if take is None: raise RuntimeError("branch") if take: cpu['pc']=u64(pc0+B) return True if op==0x33: a,b=x[rs1],x[rs2] if f7==0 and f3==0: x[rd]=u64(a+b) elif f7==0 and f3==4: x[rd]=u64(a^b) elif f7==0x20 and f3==0: x[rd]=u64(a-b) elif f7==1: if f3==0: x[rd]=u64((a*b)&0xFFFFFFFFFFFFFFFF) elif f3==3: x[rd]=mulhu(a,b) elif f3==7: x[rd]=u64(remu(a,b)) else: raise RuntimeError("M") else: raise RuntimeError("op") return True if op==0x3B: a,b=x[rs1]&0xFFFFFFFF, x[rs2]&0xFFFFFFFF if f7==1 and f3==7: x[rd]=u64(sext(remuw(a,b),32)) else: raise RuntimeError("op-32") return True raise RuntimeError(f"opcode {op:#x} at {pc0:#x}")
def run_blob(blob, const, inp48): m=Mem() m.map(0,0x1000); m.wb(0,blob) m.map(INP,0x1000); m.wb(INP, (inp48+b"\0"*48)[:48]) m.map(CST,0x1000); m.wb(CST, const+b"\0") m.map(STK-0x4000,0x4000) cpu={'m':m,'x':[0]*32,'pc':0} cpu['x'][2]=STK; cpu['x'][10]=INP; cpu['x'][11]=CST for _ in range(2_000_000): if not step(cpu,len(blob)): break base=STK-0x60 return [m.u32(base+o) for o in range(4,0x34,4)]
def extract_expected(blob): pend={}; out=[] pc=0 while pc<len(blob): if (blob[pc]&3)!=3: pc+=2; continue ins=int.from_bytes(blob[pc:pc+4],"little") op=ins&0x7F; rd=(ins>>7)&0x1F; f3=(ins>>12)&7; rs1=(ins>>15)&0x1F if op==0x37: pend[rd]=ins&0xFFFFF000 if op==0x13 and f3==0 and pc>=0x19E: imm=sext(ins>>20,12) if rs1 in pend: out.append((pend[rs1]+imm)&0xFFFFFFFF) pc+=4 if len(out)<12: raise RuntimeError("extract expected failed") return out[-12:]
def extract_runtime(binpath): master, slave = pty.openpty() p = subprocess.Popen([binpath], stdin=slave, stdout=slave, stderr=slave, close_fds=True) os.close(slave) buf=b""; t0=time.time() while time.time()-t0<5 and b"%48c" not in buf: r,_,_=select.select([master],[],[],0.2) if r: try: buf+=os.read(master,4096) except OSError: break os.kill(p.pid, signal.SIGSTOP) maps=open(f"/proc/{p.pid}/maps","r",errors="ignore").read().splitlines() rws=[] for ln in maps: m=re.match(r"^([0-9a-f]+)-([0-9a-f]+)\s+rw..",ln) if m: rws.append((int(m.group(1),16), int(m.group(2),16))) mem=open(f"/proc/{p.pid}/mem","rb",0) blob=const=None for a0,a1 in rws: mem.seek(a0); b=mem.read(a1-a0) if const is None: i=b.find(CONST_PAT) if i!=-1: const=b[i:i+32] if blob is None: j=b.find(BLOB_SIG) if j!=-1: blob=b[j:j+BLOB_LEN] if blob and const: break os.kill(p.pid, signal.SIGKILL) if not (blob and const): raise RuntimeError("cannot find blob/const (maybe /proc/pid/mem blocked?)") return blob,const
def main(): blob,const = extract_runtime(BIN) exp = extract_expected(blob) mask = run_blob(blob,const,b"\0"*48) inp = b"".join(struct.pack("<I",(mask[i]^exp[i])&0xFFFFFFFF) for i in range(12)) try: inner = inp.decode("ascii") except: inner = inp.decode("latin1") print(f"flag{{{inner}}}")
if __name__=="__main__": main()
|