Skip to content

Instantly share code, notes, and snippets.

@00xc

00xc/solve.py Secret

Created May 16, 2021
Embed
What would you like to do?
OMH 2021 CTF - GRUB solution
from constraint import *
if __name__ == '__main__':
all_chars = tuple(list("RSTUVWXYZ"))
inp_len = 81
p = Problem()
p.addVariables(range(0, inp_len), all_chars)
# t "27,18,9,0,1,2,11,10,""
p.addConstraint(lambda e: ord("R") <= ord(e), [27])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [27, 18])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [18, 9])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [9, 0])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [0, 1])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [1, 2])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [2, 11])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [11, 10])
# t "59,50,41,32,40,48,"
p.addConstraint(lambda e: ord("R") <= ord(e), [59])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [59, 50])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [50, 41])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [41, 32])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [32, 40])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [40, 48])
# t "51,50,49,48,"
p.addConstraint(lambda e: ord("R") <= ord(e), [51])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [51, 50])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [50, 49])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [49, 48])
# t "6,7,8,17,26,"
p.addConstraint(lambda e: ord("R") <= ord(e), [6])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [6, 7])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [7, 8])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [8, 17])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [17, 26])
# t "54,64,74,66,76,"
p.addConstraint(lambda e: ord("R") <= ord(e), [54])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [54, 64])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [64, 74])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [74, 66])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [66, 76])
# t "79,80,70,"
p.addConstraint(lambda e: ord("R") <= ord(e), [79])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [79, 80])
p.addConstraint(lambda *e: ord(e[0]) < ord(e[1]), [80, 70])
p.addConstraint(AllDifferentConstraint(), range(0, 9))
p.addConstraint(AllDifferentConstraint(), range(9, 18))
p.addConstraint(AllDifferentConstraint(), range(18, 27))
p.addConstraint(AllDifferentConstraint(), range(27, 36))
p.addConstraint(AllDifferentConstraint(), range(36, 45))
p.addConstraint(AllDifferentConstraint(), range(45, 54))
p.addConstraint(AllDifferentConstraint(), range(54, 63))
p.addConstraint(AllDifferentConstraint(), range(63, 72))
p.addConstraint(AllDifferentConstraint(), range(72, 81))
p.addConstraint(AllDifferentConstraint(), [0, 9, 18, 27, 36, 45, 54, 63, 72])
p.addConstraint(AllDifferentConstraint(), [1, 10, 19, 28, 37, 46, 55, 64, 73])
p.addConstraint(AllDifferentConstraint(), [2, 11, 20, 29, 38, 47, 56, 65, 74])
p.addConstraint(AllDifferentConstraint(), [3, 12, 21, 30, 39, 48, 57, 66, 75])
p.addConstraint(AllDifferentConstraint(), [4, 13, 22, 31, 40, 49, 58, 67, 76])
p.addConstraint(AllDifferentConstraint(), [5, 14, 23, 32, 41, 50, 59, 68, 77])
p.addConstraint(AllDifferentConstraint(), [6, 15, 24, 33, 42, 51, 60, 69, 78])
p.addConstraint(AllDifferentConstraint(), [7, 16, 25, 34, 43, 52, 61, 70, 79])
p.addConstraint(AllDifferentConstraint(), [8, 17, 26, 35, 44, 53, 62, 71, 80])
p.addConstraint(AllDifferentConstraint(), [ 0, 1, 2, 9, 10, 11, 18, 19, 20])
p.addConstraint(AllDifferentConstraint(), [ 3, 4, 5, 12, 13, 14, 21, 22, 23])
p.addConstraint(AllDifferentConstraint(), [ 6, 7, 8, 15, 16, 17, 24, 25, 26])
p.addConstraint(AllDifferentConstraint(), [27, 28, 29, 36, 37, 38, 45, 46, 47])
p.addConstraint(AllDifferentConstraint(), [30, 31, 32, 39, 40, 41, 48, 49, 50])
p.addConstraint(AllDifferentConstraint(), [33, 34, 35, 42, 43, 44, 51, 52, 53])
p.addConstraint(AllDifferentConstraint(), [54, 55, 56, 63, 64, 65, 72, 73, 74])
p.addConstraint(AllDifferentConstraint(), [57, 58, 59, 66, 67, 68, 75, 76, 77])
p.addConstraint(AllDifferentConstraint(), [60, 61, 62, 69, 70, 71, 78, 79, 80])
p.addConstraint(AllDifferentConstraint(), [0, 10, 20, 30, 40, 50, 60, 70, 80])
p.addConstraint(AllDifferentConstraint(), [8, 16, 24, 32, 40, 48, 56, 64, 72])
solution = p.getSolution()
s = "".join(v for k, v in sorted(solution.items(), key=lambda e: e[0]))
print(s)
# Result: VWXZRYSTUUZYTSWXRVTRSVUXWZYSTWRZVYUXYVRSXUZWTXUZYWTRVSRYTWVSUXZWSUXTZVYRZXVUYRTSW
# Translates to: DOO73LBP6EI461D6HP3N2MM6M953PKJ8MK1A2BGAB8FCIQE9Q4B96E
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment