Tags: python rev
Rating:
Hopefully you've read all the details of the challenge now. Here's what they gave us:
blue hens hens hens hens hens
blue blue blue blue hens hens hens
blue blue hens hens
blue blue blue blue hens hens hens hens hens
blue blue blue blue blue blue blue blue blue blue
blue blue blue hens
blue blue blue blue blue blue blue blue blue blue blue
blue blue blue blue blue blue blue blue blue blue
blue blue blue blue blue blue blue hens hens hens hens hens hens hens hens hens hens hens hens hens hens hens
blue blue blue blue blue blue blue blue hens hens hens hens
blue blue blue hens hens hens hens
blue blue blue blue blue blue blue blue blue hens
(shortened to not take up space)
Given the file as well as the instructions used, I guessed that it was probably a long, loop structure code. Instead of doing it by hand (because it's way too long), let's automate it by translating it and executing it in Python!
```
r = open("rev/bluehens/flag.bluehens", 'r')
w = open("rev/bluehens/translated.txt", 'w')
translator = {
1: "SET",
2: "ADD",
3: "SUB",
4: "MUL",
5: "GTL",
6: "GBL",
7: "GUL",
8: "CTA",
9: "CTS",
10: "SKP",
11: "PRT"
}
for line in r:
line = line.split(' ')
line[-1] = line[-1][:-1]
bluecount = 0
hencount = 0
for i in line:
if i == "blue": bluecount += 1
elif i == "hens": hencount += 1
w.write(translator[bluecount])
w.write(" ")
if hencount > 0:
w.write(str(hencount))
w.write('\n')
#print(line, bluecount, hencount)
w.close()
```
The above code takes the original file we were given and basically translates it into the machine code described in the problem. All that's left is to write a script to actually execute this new machine code!
```
f = open("rev/bluehens/translated.txt", 'r').read()
f = f.split('\n')[:-1]
f = [a.split(' ') for a in f]
for i in range(len(f)):
if f[i][1] != '':
f[i][1] = int(f[i][1])
register = 0
counter = 0
index = 0
while(index != len(f) - 1):
i, arg= f[index]
goto = False
if i == 'SET':
register = arg
elif i == 'ADD':
register += arg
elif i == 'SUB':
register -= arg
elif i == 'MUL':
register *= arg
elif i == 'GTL':
goto = True
index = arg - 1
elif i == 'GBL':
goto = True
index -= arg
elif i == 'GUL':
goto = True
index += arg
elif i == 'CTA':
counter += arg
elif i == 'CTS':
counter -= arg
elif i == 'SKP':
if counter == 0:
index += 1
elif i == 'PRT':
print(chr(register), end='')
if not goto:
index += 1
```
Run the script to get the flag:
UDCTF{fight_fight_fight_f0r_D3l4war3}