a =int(input())t = ab =1c = 211403263193325564935177732039311880968900585239198162576891024013795244278301418972476576250867590357558453818398439943850378531849394935408437463406454857158521417008852225672729172355559636113821448436305733721716196982836937454125646725644730318942728101471045037112450682284520951847177283496341601551774254483030077823813188507483073118602703254303097147462068515767717772884466792302813549230305888885204253788392922886375194682180491793001343169832953298914716055094436911057598304954503449174775345984866214515917257380264516918145147739699677733412795896932349404893017733701969358911638491238857741665750286105099248045902976635536517481599121390996091651261500380029994399838070532595869706503571976725974716799639715490513609494565268488194
verified =Falsewhile1:if b ==4: verified =Truebreak d =2+ (1125899906842622if a&2else0)if a&1: b //= delse: b *= d b += (b&c)*(1125899906842622) a //=4if verified: t %= (2**300) t ^=1565959740202953194497459354306763253658344353764229118098024096752495734667310309613713367print(t)
Solution
From the source code we can see that there is 4 possibility of our input since our input processed each 2 bit.
Possible first 2 bits should be 10 and 00 cause if we use 01 and 11 it should produce b == 0 . After struggling to find the base case or constraint for recursive function i try to find the relation of b and c variable. We can see that the first possible value are 10 and 00 , it looks like an entry corner to a maze since we can go right and down if we start from top left corner. Since c is square number (bit_length == 2500), so we try to use this value as the map. For the c value, since it uses and operator , the bit processed should be from the last bit so we need to reverse the binary to start the maze from initial position (0, 0). Last step, just implement Depth First Search (DFS) algorithm to find the correct path. Here is my implementation on python.
from Crypto.Util.number import*defcheck(coor):if(mapp[coor[0]][coor[1]] ==1):returnFalseif(coor[0]<0or coor[0]>49or coor[1]<0or coor[1]>49):returnFalsereturnTruedefparse(path): res =""for i inrange(1,len(path)): tmp = (path[i][0] - path[i-1][0], path[i][1] - path[i-1][1] )if(tmp == (1,0)): res ="10"+ reselif(tmp == (0,1)): res ="00"+ reselif(tmp == (0,-1)): res ="01"+ reselif(tmp == (-1,0)): res ="11"+ reselse:print("???")returnint(res,2)defdfs(coor):if(coor == finish): t =parse(visited) t %= (2**300) t ^=1565959740202953194497459354306763253658344353764229118098024096752495734667310309613713367print(long_to_bytes(t))exit() up = (coor[0], coor[1]-1) down = (coor[0], coor[1]+1) right = (coor[0]+1, coor[1]) left = (coor[0]-1, coor[1])if(check(up)and up notin visited): visited.append(up)if(dfs(up)):returnTrue visited.pop()if(check(down)and down notin visited): visited.append(down)if(dfs(down)):returnTrue visited.pop()if(check(right)and right notin visited): visited.append(right)if(dfs(right)):returnTrue visited.pop()if(check(left)and left notin visited): visited.append(left)if(dfs(left)):returnTrue visited.pop()returnFalsec = 211403263193325564935177732039311880968900585239198162576891024013795244278301418972476576250867590357558453818398439943850378531849394935408437463406454857158521417008852225672729172355559636113821448436305733721716196982836937454125646725644730318942728101471045037112450682284520951847177283496341601551774254483030077823813188507483073118602703254303097147462068515767717772884466792302813549230305888885204253788392922886375194682180491793001343169832953298914716055094436911057598304954503449174775345984866214515917257380264516918145147739699677733412795896932349404893017733701969358911638491238857741665750286105099248045902976635536517481599121390996091651261500380029994399838070532595869706503571976725974716799639715490513609494565268488194
mapp = []a =bin(c)[2:][::-1]assert(len(a)==2500)for i inrange(0,len(a),50): tmp = []for j inrange(50): tmp.append(int(a[i+j])) mapp.append(tmp)visited = [(0,0)]coor = (0,0)finish = (49,49)mapp[finish[0]][finish[1]] =0dfs(coor)
Flag : flag{l4byr1nth_9abe79d712e6dd52c4597}
re-cursed (4 solves)
Description
Scotland forever!
Solution
First step, we should extract the original elf executed in recursed binary. This should be done by set breakpoint on write function and dump the memory
After that we should facing the real cursed binary, compiled using ghc and of course it made using haskell. In this case we can't use https://github.com/gereeter/hsdecomp or another modified hsdecomp. So my initial approach by enumerating what instruction that processed our input. It can be done by doing hardware breakpoint on our input.
From image above we can see that our input stored at 0x7fffffffe701. Next step is doing hardware breakpoint on that address by using command below
rwatch *0x7fffffffe701
After some enumeration i found that there is something like argument processing and we can skip that by setting up breakpoint on 0x4afbae
After hitting breakpoint, search value for our argument. In this case i used RYUK12345 as argument
There are some value found on memory, we can enumerate 1 by 1. In this challenge, i found that 0x507b70 used for next process. The next step is changing the hardware breakpoint each the value moved or processed by the instruction, here is for example
Do the same process until you find something interesting. In this case we found interesting function that process our input at first time, it is ghczmbignum_GHCziNumziInteger_integerXor_info .
Take a look on decompiler it contains so much code just for "xor" operation. But again we can validate which instruction process our value. It is relative simple since the code that doing xor for different value only on line 159 or address 0x49FB98
The function name has pattern, so we can try set breakpoint on function that maybe process our input.
For example we can just do command like below
b *ghczmbignum_GHCziNumziInteger_integerAdd_info
c
For finding the exact instruction it is same, just debug and validate that there is your input processed by the instruction (if it is hard to find through decompiler). At the end i found the following address that processed our input
Since there are many values are same, so i use different value as our input for 5 times to get 5 different output. Each output i saved it to file and named it as cmp{i} . Here is example for cmp5 file, output from input qponmlkjihgfedcba9876543210
Then i wrote a script to parse which value are constant and which value are variable. Also i write optimization to format it to equation format at the end.
defparse(a1): tmp = a1.split(" ")return tmpdefcheck_ret(a1,a2,res2,res3): result = []for i inrange(len(res2)):if((a1 == res2[i]) and (a2 == res3[i])): result.append(i)return resultdefoptimize(res,inp): tmp = res[27:] operator = ['+','-','^'] arr = [] opt = [] data ='' init =Truefor i in tmp:# print(i) # debug check =Truefor j in inp:if(j in i):if(init andlen(data)!=0): opt.append(data) init =False data =' '.join(i[:3]) check =Falsebreakif(check):if("res"in i[0]and"res"in i[2]and"^"== i[1]): opt.append(data)continueelse: tmp2 = []for j inrange(3):if("res["notin i[j]): tmp2.append(i[j]) str_tmp2 =' '.join(tmp2)for j in operator:if(tmp2[0]== j): data =f"({data}) {str_tmp2}"breakif(tmp2[1]== j): data =f"{str_tmp2} ({data})"breakreturn'\n'.join(opt)defcheck(f,g): res = [] res2 = [] res3 = [] cnt_var =0 inp = [f"res[{i}]"for i inrange(27)]for ind inrange(len(g)): a1 =parse(f[ind]) a2 =parse(g[ind])if(a1 == a2): res.append(a1)continue tmp_res = []for i inrange(3):if(a1[i]!= a2[i]): tmp_check =check_ret(a1[i], a2[i], res2, res3) length =len(tmp_check)if(length >0):if(length >1): val ='_'.join(map(str,tmp_check))if(ind <27): inp.append(f"res[{val}]") tmp_res.append(f"res[{val}]")else: tmp_res.append(f"res[{tmp_check[0]}]")else: tmp_res.append(f"var[{cnt_var}]") cnt_var +=1else: tmp_res.append(a1[i]) tmp_res.append(a1[3]) tmp_res.append(f"res[{len(res2)}]") res.append(tmp_res) res2.append(a1[4]) res3.append(a2[4])returnoptimize(res, inp)defdiff(f,g):for i inrange(min(len(f),len(g))): a1 =parse(f[i]) a2 =parse(g[i])if(a1[1]!= a2[1]):return ireturn-1files = [f"cmp{i}"for i inrange(1,6)]for i inrange(len(files)):for j inrange(i+1,len(files)): f =open(files[i],"r").read().split("\n") g =open(files[j],"r").read().split("\n") diff_index =diff(f, g)if(diff_index !=-1):if(len(f)>len(g)): f.pop(diff_index)else: g.pop(diff_index)if(len(f)!=len(g)):print("ERR : ",files[i], files[j], diff_index)else: result =check(f,g) out =open(f"{files[i]}_{files[j]}.out","w") out.write(result) out.close()
Because there is still the same value, i fixed it manually by checking on each output with knowledge of the pattern used. Last, because i'm too lazy to reverse the process i just put the equation on z3 and got the flag also first blood :> .