From 5929b46c08078f5f9e96274ee09c796f0b363f42 Mon Sep 17 00:00:00 2001 From: Mari Wahl Date: Wed, 8 Oct 2014 01:38:11 -0400 Subject: [PATCH] some small fixes --- .../cryptography/crypto-200/caesarCipher.py | 32 ++++++------ CTFs/WARGAMES/README.md | 3 ++ CTFs/WARGAMES/krypton/cipher.txt | 1 + CTFs/WARGAMES/krypton/freq_count.py | 27 ++++++++++ CTFs/WARGAMES/krypton/freq_count_vig.py | 51 +++++++++++++++++++ Cryptography/README.md | 6 +-- Cryptography/RotationCiphers/README.md | 6 --- Cryptography/RotationCiphers/caesarCipher.py | 32 ++++++------ 8 files changed, 118 insertions(+), 40 deletions(-) create mode 100644 CTFs/WARGAMES/README.md create mode 100644 CTFs/WARGAMES/krypton/cipher.txt create mode 100644 CTFs/WARGAMES/krypton/freq_count.py create mode 100644 CTFs/WARGAMES/krypton/freq_count_vig.py delete mode 100644 Cryptography/RotationCiphers/README.md diff --git a/CTFs/2014-CSAW-CTF/cryptography/crypto-200/caesarCipher.py b/CTFs/2014-CSAW-CTF/cryptography/crypto-200/caesarCipher.py index 6efc7d3..b569914 100644 --- a/CTFs/2014-CSAW-CTF/cryptography/crypto-200/caesarCipher.py +++ b/CTFs/2014-CSAW-CTF/cryptography/crypto-200/caesarCipher.py @@ -7,9 +7,7 @@ __author__ = "bt3gl" import string -FREQ_ENGLISH = [0.0749, 0.0129, 0.0354, 0.0362, 0.1400, 0.0218, 0.0174, 0.0422, 0.0665, 0.0027, 0.0047, 0.0357, - 0.0339, 0.0674, 0.0737, 0.0243, 0.0026, 0.0614, 0.0695, 0.0985, 0.0300, 0.0116, 0.0169, 0.0028, - 0.0164, 0.0004] +FREQ_ENGLISH = [0.0749, 0.0129, 0.0354, 0.0362, 0.1400, 0.0218, 0.0174, 0.0422, 0.0665, 0.0027, 0.0047, 0.0357,0.0339, 0.0674, 0.0737, 0.0243, 0.0026, 0.0614, 0.0695, 0.0985, 0.0300, 0.0116, 0.0169, 0.0028, 0.0164, 0.0004] @@ -38,7 +36,7 @@ def frequency(msg): # Compute the word frequencies dict_freq = dict([(c,0) for c in string.lowercase]) diff = 0.0 - for c in msg: + for c in msg.lower(): if 'a'<= c <= 'z': diff += 1 dict_freq[c] += 1 @@ -50,14 +48,13 @@ def frequency(msg): def decipher(msg): # Decipher by frequency - min_delta = 1000 - best_rotation = 0 + min_delta, best_rotation = 20, 0.0 freq = frequency(msg) - for key in range(26): - d = delta(freq, FREQ_ENGLISH) + for k in range(26): + d = delta(freq[k:] + freq[:k], FREQ_ENGLISH) if d < min_delta: min_delta = d - best_rotation = key + best_rotation = k return cipher(msg, -best_rotation) @@ -74,12 +71,17 @@ def decipher_simple(msg): if __name__ == '__main__': + # creating a cipher key = 13 text = 'hacker school is awesome!' - cip = cipher(text, key) - dec = decipher(cip) + cipe = cipher(text, key) + print "Cipher: " + cipe + + # decyphering + with open('cipher.txt', 'r') as f: + cip = f.readlines() + cip = cip[0].strip() + cipe = decipher(cip) + print "Decipher: " + cipe - print "Cipher: " + cip - print "Decipher: " + dec - - assert(text == dec) \ No newline at end of file + diff --git a/CTFs/WARGAMES/README.md b/CTFs/WARGAMES/README.md new file mode 100644 index 0000000..00495d3 --- /dev/null +++ b/CTFs/WARGAMES/README.md @@ -0,0 +1,3 @@ +## Writeups: + +[Narnia 1-5]: http://bt3gl.github.io/smashing-the-stack-for-fun-or-wargames-narnia-0-4.html diff --git a/CTFs/WARGAMES/krypton/cipher.txt b/CTFs/WARGAMES/krypton/cipher.txt new file mode 100644 index 0000000..39b1bfa --- /dev/null +++ b/CTFs/WARGAMES/krypton/cipher.txt @@ -0,0 +1 @@ +CGZNL YJBEN QYDLQ ZQSUQ NZCYD SNQVU BFGBK GQUQZ QSUQN UZCYD SNJDS UDCXJ ZCYDS NZQSU QNUZB WSBNZ QSUQN UDCXJ CUBGS BXJDS UCTYV SUJQG WTBUJ KCWSV LFGBK GSGZN LYJCB GJSZD GCHMS UCJCU QJLYS BXUMA UJCJM JCBGZ CYDSN CGKDC ZDSQZ DVSJJ SNCGJ DSYVQ CGJSO JCUNS YVQZS WALQV SJJSN UBTSX COSWG MTASN BXYBU CJCBG UWBKG JDSQV YDQAS JXBNS OQTYV SKCJD QUDCX JBXQK BMVWA SNSYV QZSWA LWAKB MVWAS ZBTSS QGWUB BGJDS TSJDB WCUGQ TSWQX JSNRM VCMUZ QSUQN KDBMU SWCJJ BZBTT MGCZQ JSKCJ DDCUE SGSNQ VUJDS SGZNL YJCBG UJSYY SNXBN TSWAL QZQSU QNZCY DSNCU BXJSG CGZBN YBNQJ SWQUY QNJBX TBNSZ BTYVS OUZDS TSUUM ZDQUJ DSICE SGNSZ CYDSN QGWUJ CVVDQ UTBWS NGQYY VCZQJ CBGCG JDSNB JULUJ STQUK CJDQV VUCGE VSQVY DQASJ UMAUJ CJMJC BGZCY DSNUJ DSZQS UQNZC YDSNC USQUC VLANB FSGQG WCGYN QZJCZ SBXXS NUSUU SGJCQ VVLGB ZBTTM GCZQJ CBGUS ZMNCJ LUDQF SUYSQ NSYNB WMZSW TBUJB XDCUF GBKGK BNFAS JKSSG QGWDC USQNV LYVQL UKSNS TQCGV LZBTS WCSUQ GWDCU JBNCS UESGN SUDSN QCUSW JBJDS YSQFB XUBYD CUJCZ QJCBG QGWQN JCUJN LALJD SSGWB XJDSU COJSS GJDZS GJMNL GSOJD SKNBJ STQCG VLJNQ ESWCS UMGJC VQABM JCGZV MWCGE DQTVS JFCGE VSQNQ GWTQZ ASJDZ BGUCW SNSWU BTSBX JDSXC GSUJS OQTYV SUCGJ DSSGE VCUDV QGEMQ ESCGD CUVQU JYDQU SDSKN BJSJN QECZB TSWCS UQVUB FGBKG QUNBT QGZSU QGWZB VVQAB NQJSW KCJDB JDSNY VQLKN CEDJU TQGLB XDCUY VQLUK SNSYM AVCUD SWCGS WCJCB GUBXI QNLCG EHMQV CJLQG WQZZM NQZLW MNCGE DCUVC XSJCT SQGWC GJKBB XDCUX BNTSN JDSQJ NCZQV ZBVVS QEMSU YMAVC UDSWJ DSXCN UJXBV CBQZB VVSZJ SWSWC JCBGB XDCUW NQTQJ CZKBN FUJDQ JCGZV MWSWQ VVAMJ JKBBX JDSYV QLUGB KNSZB EGCUS WQUUD QFSUY SQNSU diff --git a/CTFs/WARGAMES/krypton/freq_count.py b/CTFs/WARGAMES/krypton/freq_count.py new file mode 100644 index 0000000..069bd1e --- /dev/null +++ b/CTFs/WARGAMES/krypton/freq_count.py @@ -0,0 +1,27 @@ +#!/bin/python + +import string +import sys +import operator + +def find_frequency(msg): + dict_freq = dict([(c, 0) for c in string.lowercase]) + total_letters = 0.0 + for c in msg.lower(): + if 'a'<= c <= 'z': + dict_freq[c] += 1 + total_letters += 1 + list_freq = sorted(dict_freq.items(), key=operator.itemgetter(1)) + return list_freq + + + +def main(filename): + with open(filename, 'r') as f: + cipher = f.readlines() + cipher = cipher[0].strip() + print(find_frequency(cipher)) + + +if __name__ == "__main__": + main(str(sys.argv[1])) diff --git a/CTFs/WARGAMES/krypton/freq_count_vig.py b/CTFs/WARGAMES/krypton/freq_count_vig.py new file mode 100644 index 0000000..cf3307f --- /dev/null +++ b/CTFs/WARGAMES/krypton/freq_count_vig.py @@ -0,0 +1,51 @@ +#!/bin/python + +__author__= 'bt3gl' + +import string +import sys +import operator + + +FREQ_ENGLISH = [0.0749, 0.0129, 0.0354, 0.0362, 0.1400, 0.0218, 0.0174, 0.0422, 0.0665, 0.0027, 0.0047, 0.0357,0.0339, 0.0674, 0.0737, 0.0243, 0.0026, 0.0614, 0.0695, 0.0985, 0.0300, 0.0116, 0.0169, 0.0028, 0.0164, 0.0004] + + +def find_frequency(msg): + dict_freq = dict([(c, 0) for c in string.lowercase]) + total_letters = 0.0 + for c in msg.lower(): + if 'a'<= c <= 'z': + dict_freq[c] += 1 + total_letters += 1 + list_freq = sorted(dict_freq.items(), key=operator.itemgetter(1)) + return [(c, freq/total_letters) for (c, freq) in list_freq] + + + +def main(filename): + with open(filename, 'r') as f: + cipher = f.readlines() + cipher = cipher[0].strip() + flist = find_frequency(cipher) + elist = dict((k, value) for (k, value) in zip(string.lowercase, FREQ_ENGLISH)) + elist = sorted(elist.items(), key=operator.itemgetter(1)) + trans, key = '', '' + for i, f in enumerate(flist): + trans += f[0] + key += elist[i][0] + print "CIPHER: %s -> %.5f, ENGLISH: %s -> %.5f" %(f[0], f[1], elist[i][0], elist[i][1]) + print "Key is " + key + " for " + trans + + # print key sorted to translate to a-z + res = zip(trans, key) + res.sort() + trans, key = '', '' + for letter in res: + trans += letter[1].upper() + key += letter[0].upper() + print "tr [" + key + "] [" + trans + "]" + + +if __name__ == "__main__": + main(str(sys.argv[1])) + diff --git a/Cryptography/README.md b/Cryptography/README.md index 3e1f9a2..9ee4461 100644 --- a/Cryptography/README.md +++ b/Cryptography/README.md @@ -3,6 +3,8 @@ - https://www.cryptool.org/en/cryptool1-en - frequency analyses online: +http://www.simonsingh.net/The_Black_Chamber/hintsandtips.html +http://www.xarg.org/tools/caesar-cipher/ ## ROT13 @@ -21,7 +23,3 @@ In Python we can use: ```"YRIRY GJB CNFFJBEQ EBGGRA".decode(encoding="ROT13")``` https://docs.python.org/2/library/codecs.html#codec-base-classes --- - -Online: - -http://www.xarg.org/tools/caesar-cipher/ diff --git a/Cryptography/RotationCiphers/README.md b/Cryptography/RotationCiphers/README.md deleted file mode 100644 index c5460a1..0000000 --- a/Cryptography/RotationCiphers/README.md +++ /dev/null @@ -1,6 +0,0 @@ -CryptoAnalysis -============== - -* Several implementations of Caesar cipher with frequency analysis. - -* Vinegere code. \ No newline at end of file diff --git a/Cryptography/RotationCiphers/caesarCipher.py b/Cryptography/RotationCiphers/caesarCipher.py index 6efc7d3..b569914 100644 --- a/Cryptography/RotationCiphers/caesarCipher.py +++ b/Cryptography/RotationCiphers/caesarCipher.py @@ -7,9 +7,7 @@ __author__ = "bt3gl" import string -FREQ_ENGLISH = [0.0749, 0.0129, 0.0354, 0.0362, 0.1400, 0.0218, 0.0174, 0.0422, 0.0665, 0.0027, 0.0047, 0.0357, - 0.0339, 0.0674, 0.0737, 0.0243, 0.0026, 0.0614, 0.0695, 0.0985, 0.0300, 0.0116, 0.0169, 0.0028, - 0.0164, 0.0004] +FREQ_ENGLISH = [0.0749, 0.0129, 0.0354, 0.0362, 0.1400, 0.0218, 0.0174, 0.0422, 0.0665, 0.0027, 0.0047, 0.0357,0.0339, 0.0674, 0.0737, 0.0243, 0.0026, 0.0614, 0.0695, 0.0985, 0.0300, 0.0116, 0.0169, 0.0028, 0.0164, 0.0004] @@ -38,7 +36,7 @@ def frequency(msg): # Compute the word frequencies dict_freq = dict([(c,0) for c in string.lowercase]) diff = 0.0 - for c in msg: + for c in msg.lower(): if 'a'<= c <= 'z': diff += 1 dict_freq[c] += 1 @@ -50,14 +48,13 @@ def frequency(msg): def decipher(msg): # Decipher by frequency - min_delta = 1000 - best_rotation = 0 + min_delta, best_rotation = 20, 0.0 freq = frequency(msg) - for key in range(26): - d = delta(freq, FREQ_ENGLISH) + for k in range(26): + d = delta(freq[k:] + freq[:k], FREQ_ENGLISH) if d < min_delta: min_delta = d - best_rotation = key + best_rotation = k return cipher(msg, -best_rotation) @@ -74,12 +71,17 @@ def decipher_simple(msg): if __name__ == '__main__': + # creating a cipher key = 13 text = 'hacker school is awesome!' - cip = cipher(text, key) - dec = decipher(cip) + cipe = cipher(text, key) + print "Cipher: " + cipe + + # decyphering + with open('cipher.txt', 'r') as f: + cip = f.readlines() + cip = cip[0].strip() + cipe = decipher(cip) + print "Decipher: " + cipe - print "Cipher: " + cip - print "Decipher: " + dec - - assert(text == dec) \ No newline at end of file +