본문 바로가기
Security/[게임] CTF 풀이

[crypto] 2016 SecconCTF - Vigenere writeups

by blackcon 2016. 12. 12.

[문제]

Vigenere

k: ????????????

p: SECCON{???????????????????????????????????}

c: LMIG}RPEDOEEWKJIQIWKJWMNDTSR}TFVUFWYOCBAJBQ

k=key, p=plain, c=cipher, md5(p)=f528a6ab914c1ecf856a1d93103948fe

[풀이]

import md5

p = "SECCON{???????????????????????????????????}"
c = 'LMIG}RPEDOEEWKJIQIWKJWMNDTSR}TFVUFWYOCBAJBQ'
m = "ABCDEFGHIJKLMNOPQRSTUVWXYZ{}"
a = []
for i in range(len(p[:7])):
    for j in range(len(m)):
        if m[j] == p[i]:
            a.append( j )

b = []
for i in range(len(p[:7])):
    for j in range(len(m)):
        if m[j] == c[i]:
            b.append( j )
_k = ''
for i in range(len(b)):
    _k += m[b[i]-a[i]]

print "[+] key:", _k+("?"*(12-len(_k)))

##############
a = []
for i in c:
    for j in range(len(m)):
        if m[j] == i:
            a.append( j )

b = []
for i in _k:
    for j in range(len(m)):
        if m[j] == i:
            b.append( j )

## b/f    
print "[!] b/f ..." 
q = b
for z in m:
    for y in m:
        for x in m:
            for u in m:
                for o in m:
                    k = z+y+x+u+o
                    d = []
                    for i in k:
                        for j in range(len(m)):
                            if m[j] == i:
                                d.append( j )
                    o = b+d
                    flag = ''
                    for i in range( len(a) ):
                        flag += m[(a[i]-o[i%len(o)])%len(m)]
                    _md = md5.new( flag ).hexdigest()
                    if _md == "f528a6ab914c1ecf856a1d93103948fe":
                        print "[+] key:", _k+k
                        print "[+] flag:", flag
                        exit()

'''
blackcon@bk{~/seccon/crypto}:python vigenere.py
[+] key: VIGENER?????
[!] b/f ...
[+] key: VIGENERECODE
[+] flag: SECCON{ABABABCDEDEFGHIJJKLMNOPQRSTTUVWXYYZ}
'''