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 268 269 270 271 272 273
| from pwn import *
ip = 300 reg=0 originalCode=b'\x00'*600+open('code.bin','rb').read() CodeList=[] assert(len(originalCode)%2==0) for i in range(len(originalCode)//2): CodeList.append(u16(originalCode[i*2:i*2+2]))
def rol(num): assert(0<=num<=0xffff) return ((num<<1)&0xffff)+(num>>15)
def rol_back(num): assert(0<=num<=0xffff) return (num>>1)+(num%2)*(1<<15)
def rev(num): return (~num)&0xffff
def jmp(addr): global ip info(f'ip={hex(ip)}:set ip to {hex(addr)}') if addr-ip>30 or addr-ip<0: input(f"error at ip={hex(ip)}") ip=addr
def pattern_nor_reg(code): src0,src1,dst=code[0],code[1],code[2] if dst!=7: return -1 return [src0,src1]
def nor_reg_handler(result): reg=0xffffffff return f'reg=~(*({hex(result[0])})|*({hex(result[1])}))'
def pattern_not(code): src0,src1,dst=code[0],code[1],code[2] if src0!=src1: return -1 return [src0,dst]
def not_handler(result): return f'*({hex(result[1])})=~(*({hex(result[0])}))'
def pattern_load_reg(code): src0,src1,dst=code[0],code[1],code[2] if src0!=src1 or dst!=7: return -1 return src0
def load_reg_handler(result): reg=rev(CodeList[result]) return f'reg=~(*({hex(result)}))'
def pattern_save_reg(code): src0,src1,dst=code[0],code[1],code[2] if src0!=src1 or src0!=7: return -1 return dst
def save_reg_handler(result): global reg global ip if result==0: jmp(reg) info('regjump') ip-=3 return return f'*({hex(result)})=~reg'
def pattern_jmp(code): addr=pattern_load_reg(code) if addr==-1: return -1 code=code[3:] src0,src1,dst=code[0],code[1],code[2] if src0==src1==7 and dst==0: return addr return -1
def jmp_handler(result): jmp(CodeList[result])
def pattern_load_imm(code): addr=pattern_jmp(code) if addr==-1: return -1 if not (CodeList[addr]-addr==2 and pattern_load_reg(code[8:])==addr+1): return -1 return code[7]
def load_imm_handler(result): global reg reg=result return f'reg={hex(result)}'
def pattern_put(code): chraddr=pattern_load_reg(code) if chraddr==-1: return -1 code=code[3:] pos=pattern_save_reg(code) if pos!=3: return -1 code=code[3:] imm=pattern_load_imm(code) if imm!=1: return -1 code=code[11:] pos=pattern_save_reg(code) if pos!=4: return -1 return chraddr
def put_handler(result): return f"putchar('{chr(CodeList[result])}')"
def pattern_get(code): imm=pattern_load_imm(code) if imm!=1: return -1 code=code[11:] pos=pattern_save_reg(code) if pos!=6: return -1 code=code[3:] pos=pattern_load_reg(code) if pos!=5: return -1 code=code[3:] addr=pattern_save_reg(code) if addr==-1: return -1 return addr
def get_handler(result): return f"*({hex(result)})=getchar()"
def pattern_load_mem(code): addrsrc=pattern_load_reg(code) code=code[3:] addrdst=pattern_save_reg(code) if addrsrc==-1 or addrdst==-1: return -1 return [addrsrc,addrdst]
def load_mem_handler(result): return f"*({hex(result[1])})={f'*({hex(result[0])})' if result[0]!=7 else 'reg'}"
def pattern_rol(code): result=pattern_load_mem(code) if result==-1: return -1 if result[0]!=result[1]: return -1 addr=result[0] code=code[6:] result=pattern_load_mem(code) if result==-1: return -1 if result[1]!=addr or result[0]!=1: return -1 return addr
def pattern_rols(code): global ip i=1 addr0=pattern_rol(code) if addr0==-1: return -1 ip+=12 while True: addr1=pattern_rol(CodeList[ip:]) if addr1==-1 or addr1!=addr0: break i+=1 ip+=12 addr0=addr1 return [addr0, i%16]
def rols_handler(result): return f'rol(*({hex(result[0])}), {str(result[1])})'
patterns=[[pattern_rols,rols_handler,0],[pattern_get,get_handler,20],[pattern_put,put_handler,20],[pattern_load_imm,load_imm_handler,11],[pattern_jmp,jmp_handler,0],[pattern_load_mem,load_mem_handler,6],[pattern_load_reg,load_reg_handler,3],[pattern_save_reg,save_reg_handler,3],[pattern_not,not_handler,3],[pattern_nor_reg,nor_reg_handler,3]]
def disassemble(): global ip ip=300 while ip<len(CodeList): flag=0 for pattern in patterns: result=pattern[0](CodeList[ip:]) if result!=-1: dis=pattern[1](result) if dis!=None: print(f'{hex(ip)}:{dis}') ip+=pattern[2] flag=1 break if flag==0: input(f'unknown instruction at ip={hex(ip)}') def solve1(pc,l): global ip ip=pc diff=0x78e-0x689 for i in range(l): for pattern in patterns: result=pattern[0](CodeList[ip:]) if result!=-1: dis=pattern[1](result) if dis!=None: dis=dis break print(chr(rol(CodeList[pattern_not(CodeList[ip:])[0]])),end='') ip+=diff def solve2(pc,l): global ip ip=pc diff1=0xf41-0xefc diff2=0xf8c-0xf41 for j in range(l): result=pattern_not(CodeList[ip:]) num1=CodeList[result[0]] ip+=diff1 result=pattern_not(CodeList[ip:]) num2=CodeList[result[0]] ip+=diff2 for i in range(32,127): if (rev(rev(num1)|i)|rev(rev(i)|num1)) == num2: print(chr(i),end='') def solve3(pc,l): global ip ip=pc diff=0x138e-0x1337 for i in range(l): for pattern in patterns: result=pattern[0](CodeList[ip:]) if result!=-1: dis=pattern[1](result) if dis!=None: dis=dis break print(chr(rol_back(CodeList[pattern_not(CodeList[ip:])[0]])),end='') ip+=diff
solve1(0x689,9) solve2(0xefc,2) solve1(0x10d6,3) solve3(0x1337,4) solve2(0x1487,1) solve3(0x1523,1) solve1(0x1628,1) solve2(0x1673,1) solve1(0x17bd,1) solve2(0x1808,1) solve3(0x18a4,1) solve1(0x19a9,1) solve3(0x1a00,1) solve2(0x1a4b,3) solve1(0x1cb5,1) solve2(0x1d00,1) solve3(0x1d9c,1) solve2(0x1de7,1) solve3(0x1e83,1)
|