Fraction Fun

Category
Reverse Engineering
Points
50
Tags

by Niral I got zip file, i extracted it, and i got server.py, code.txt, and output.txt

server.py:

def execute(code, inp):
    values = code.split(" ")
    inp = int(inp)
    a = [int(i.split("/")[0]) for i in values]
    b = [int(i.split("/")[1]) for i in values]
    ...
    for _ in range(1000):
        changed = False
        for i in range(len(a)):
            if inp % b[i] == 0:
                inp = inp * a[i] // b[i]
                changed = True
        if not changed:
            break
    ...
    return inp

code.txt:

132037/2 5053/3 6179/5 11923333/7 26314511297/11 4309842885778741/13 1507081871/17 8140842992713/19 2303746780288325777/23 31/37 29/31 43/47 41/43 59/61 53/59 257/29 277/41 307/53 59851837/157 380692393061/163 179840600492380063/167 8841668387/173 74869345501501/179 81633042063844053697/181 2248751/191 24341/193 25853/197 71/73 67/71 83/89 79/83 101/103 97/101 263/67 281/79 283/97 131079601/199 1677100110841/211 3004041937984268273/223 16129/227 131/229 137/233 2685619/239 73439775749/241 270281038127131201/251 109/113 107/109 131/137 127/131 149/151 139/149 269/107 271/127 293/139

output.txt:

26583695521295564229597702539606779011424487630673118238848488250483324638316493786570869155140262109049480728720762458861072382684496180304334780580927278480859828726271190354563801424026577071092295383902034809987118743623980912225475900244424293925091408859063688998972149202047364837931904804899042122574503994569600746938637960913073394707892294153090918898328022107247581272863802253341432320938165400233000775086785908939290420113613066550342438527239433033068361060160630630082143980259608478649688184001887031227134526555373378453043380082886248214045569103863602855235129007027468851693345088850218355279083544799043710805153297299609903475798568030451596050516808739152859459909989354534948107258456557490085460286436562247329508838657444598086742992481999935339366611914188475448009484000721883947866826487274989647499669472627139328680685664054694766806899629567782668520200835436391615456137547263096541357115999084091125914898574531262690211384194317518215317516788665114719421885068620084565914276013819753391228437274756802904194382475006545796394208126066131704391781275616585871926844307150203052130711672595603719081095076166720941879003314070745909826291196703785409557136061565397541026718608726589513196689900333744709590748515676202353872332605737968135033
Code Analysis

server.py:

  1. It loops up to 1000 times
  2. If inp is divisible by b[i], then it replaces inp with (inp * a[i]) // b[i]
  3. Once no more rules apply, the loop breaks.

To reverse this logic, we do the opposite:

  1. Instead of multiplying and dividing, we will multiply by b[i] and divide by a[i].
  2. Then we perform inp = inp * b[i] // a[i]
Solver
code = open("code.txt").read().strip().split(" ")
output = int(open("output.txt").read())

a = [int(i.split("/")[0]) for i in code]
b = [int(i.split("/")[1]) for i in code]

inp = output

for _ in range(1000):
    changed = False
    for i in range(len(a)):
        if inp % a[i] == 0:
            inp = inp * b[i] // a[i]
            changed = True
    if not changed:
        break

print(inp)
Fraction Fun Flag: Breach{0n3_0f_c0nw4y5_7ur1n6_c0mpl373_w0nd3r5}