Know Some Cryptography

Lecturer: Иo1lz

 -- Use Python Decode CTF --

     One day, you catch a file which is sent by a spy to his organization.

      While you open it, you see the contents:

from hashlib import md5,sha256
from secret import FLAG
cand = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPWRSTUVWXYZ1234567890@,- _{}'

md5s = []
sha256s = []
for f in FLAG :
    assert f in cand 
    md5s.append( int(md5(f.encode()).hexdigest(),16)%64 )
    sha256s.append( int(sha256(f.encode()).hexdigest(),16)%64 )

# md5s = [41, 63, 46, 51, 6, 26, 42, 50, 44, 33, 29, 50, 27, 28, 30, 17, 31, 19, 46, 50, 33, 45, 26, 26, 29, 31, 52, 33, 1, 45, 31, 22, 50, 50, 50, 50, 50, 31, 22, 50, 44, 26, 44, 49, 50, 49, 26, 45, 31, 30, 22, 44, 30, 31, 17, 50, 50, 50, 31, 43, 52, 50, 53, 31, 30, 17, 26, 31, 46, 41, 44, 26, 31, 52, 50, 30, 31, 26, 39, 31, 46, 33, 27, 1, 42, 50, 31, 30, 12, 26, 27, 52, 31, 30, 12, 31, 46, 26, 27, 14, 50, 31, 22, 52, 33, 31, 41, 50, 46, 31, 22, 23, 41, 31, 53, 26, 21, 31, 33, 30, 31, 19, 39, 51, 33, 30, 39, 51, 12, 58, 60, 31, 41, 33, 53, 31, 3, 17, 50, 31, 51, 26, 29, 52, 31, 33, 22, 26, 31, 41, 51, 54, 41, 29, 52, 31, 19, 23, 33, 30, 44, 26, 27, 38, 8, 50, 29, 15]
# sha256s = [61, 44, 3, 14, 22, 41, 43, 30, 49, 59, 58, 30, 11, 3, 24, 35, 40, 46, 3, 42, 59, 36, 41, 41, 41, 40, 9, 59, 23, 36, 40, 33, 42, 42, 42, 42, 42, 40, 44, 42, 49, 24, 49, 28, 42, 33, 24, 36, 40, 24, 33, 10, 24, 40, 35, 42, 42, 42, 40, 39, 9, 42, 3, 40, 24, 35, 24, 40, 3, 61, 49, 24, 40, 9, 42, 24, 40, 41, 17, 40, 12, 57, 11, 23, 43, 42, 40, 24, 18, 41, 11, 9, 40, 24, 18, 40, 3, 41, 11, 12, 42, 40, 44, 9, 59, 40, 61, 42, 3, 40, 44, 13, 61, 40, 3, 24, 29, 40, 59, 24, 40, 19, 18, 6, 59, 24, 18, 6, 22, 0, 39, 40, 61, 57, 3, 40, 17, 35, 42, 40, 58, 24, 58, 9, 40, 59, 44, 24, 40, 61, 48, 52, 61, 58, 9, 40, 19, 13, 59, 24, 53, 41, 11, 55, 55, 42, 58, 18]

Your mission is to decode the message, but how?

OUTLINE

  • What is Cryptography?
  • MD5 & SHA
  • Let's Capture The Flag

What is Cryptography?

1-3-3-2-2-1-1-1-8-5

O,Draconian devil!

Oh,lame saint!

1-1-2-3-5-8-13-21

Leonardo da Vinci!

The Mona Lisa!

So what is cryptography?

Cryptography or cryptology is the practice and study of techniques for secure communication in the presence of third parties called adversaries.

Modern cryptography

  • mathematics

  • computer science
  • electrical engineering
  • communication science
  • physics

MD5 & SHA

MD5

  • MD5 Message-Digest Algorithm
  • a widely used hash function producing a 128-bit hash value.

SHA

  • Secure Hash Algorithm
  • published by the National Institute of Standards and Technology (NIST) as a U.S. Federal Information Processing Standard (FIPS)

In brief,

they are all hash function, which means they all translate a message into a 32 bits hexadecimal.

Let's Capture The Flag

Step 1.

     Install the package

$ pip install hashlib

Step 2.

     Create the file which is named "[whatever you want].py"

Step 3.

     Import the package and set the initial value.

from hashlib import md5, sha256

cand = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPWRSTUVWXYZ1234567890@,- _{}'
md5Encode = [41, 63, 46, 51, 6, 26, 42, 50, 44, 33, 29, 50, 27, 28, 30, 17, 31, 19, 46, 50, 33, 45, 26, 26, 29, 31, 52, 33, 1, 45, 31, 22, 50, 50, 50, 50, 50, 31, 22, 50, 44, 26, 44, 49, 50, 49, 26, 45, 31, 30, 22, 44, 30, 31, 17, 50, 50, 50, 31, 43, 52, 50, 53, 31, 30, 17, 26, 31, 46, 41, 44, 26, 31, 52, 50, 30, 31, 26, 39, 31, 46, 33, 27, 1, 42, 50, 31, 30, 12, 26, 27, 52, 31, 30, 12, 31, 46, 26, 27, 14, 50, 31, 22, 52, 33, 31, 41, 50, 46, 31, 22, 23, 41, 31, 53, 26, 21, 31, 33, 30, 31, 19, 39, 51, 33, 30, 39, 51, 12, 58, 60, 31, 41, 33, 53, 31, 3, 17, 50, 31, 51, 26, 29, 52, 31, 33, 22, 26, 31, 41, 51, 54, 41, 29, 52, 31, 19, 23, 33, 30, 44, 26, 27, 38, 8, 50, 29, 15]
sha256Encode = [61, 44, 3, 14, 22, 41, 43, 30, 49, 59, 58, 30, 11, 3, 24, 35, 40, 46, 3, 42, 59, 36, 41, 41, 41, 40, 9, 59, 23, 36, 40, 33, 42, 42, 42, 42, 42, 40, 44, 42, 49, 24, 49, 28, 42, 33, 24, 36, 40, 24, 33, 10, 24, 40, 35, 42, 42, 42, 40, 39, 9, 42, 3, 40, 24, 35, 24, 40, 3, 61, 49, 24, 40, 9, 42, 24, 40, 41, 17, 40, 12, 57, 11, 23, 43, 42, 40, 24, 18, 41, 11, 9, 40, 24, 18, 40, 3, 41, 11, 12, 42, 40, 44, 9, 59, 40, 61, 42, 3, 40, 44, 13, 61, 40, 3, 24, 29, 40, 59, 24, 40, 19, 18, 6, 59, 24, 18, 6, 22, 0, 39, 40, 61, 57, 3, 40, 17, 35, 42, 40, 58, 24, 58, 9, 40, 59, 44, 24, 40, 61, 48, 52, 61, 58, 9, 40, 19, 13, 59, 24, 53, 41, 11, 55, 55, 42, 58, 18]

You can copy these from "[V]355463.py"

Step 4.

     Create the decode dictionary and list

Use "dict.fromkeys(seq[, value])"

# ...

candList = list(cand)
FLAG = ''
md5Decode = []
sha256Decode = []
initList = ['-1']
md5Dictionary = dict.fromkeys(initList)
sha256Dictionary = dict.fromkeys(initList)

Step 5.

     Complete the decode dictionary

# ...

for i in cand:
    # We'll complete the md5 dictionary here
# ...
for i in cand:
    m = int(md5(i.encode()).hexdigest(), 16) % 64
    
  1. md5(i.encode())
  2. translate to hexadecimal         md5(i.encode()).hexdigest()
  3. change type into integer                             int(md5(i.encode()).hexdigest(), 16)
  4. %64
# ...
for i in cand:
    m = int(md5(i.encode()).hexdigest(), 16) % 64
    if md5Dictionary.__contains__(m):
        tmp = ''
        tmp += (i + str(md5Dictionary[m]))
        md5Dictionary[m] = tmp
    else:
        md5Dictionary.setdefault(m, i)
# ...
for i in cand:
    s = int(sha256(i.encode()).hexdigest(), 16) % 64
    if sha256Dictionary.__contains__(s):
        tmp = ''
        tmp += (i + str(sha256Dictionary[s]))
        sha256Dictionary[s] = tmp
    else:
        sha256Dictionary.setdefault(s, i)

Be the same with SHA256

# ...

for i in md5Encode:
    md5Decode.append(md5Dictionary[i])

for i in sha256Encode:
    sha256Decode.append(sha256Dictionary[i])

Step 7.

      Find the probably value of ciphertext by dictionary

# ...

for i in range(len(md5Encode)):
    break_flag = False
    for j in md5Decode[i]:
        for z in sha256Decode[i]:
            if j == z:
                break_flag = True
                FLAG += z
                break
        if break_flag:
            break
print(FLAG)

Step 8.

      Compare the two result then capture the flag

from hashlib import md5, sha256

cand = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPWRSTUVWXYZ1234567890@,- _{}'
candList = list(cand)
md5Encode = [41, 63, 46, 51, 6, 26, 42, 50, 44, 33, 29, 50, 27, 28, 30, 17, 31, 19, 46, 50, 33, 45, 26, 26, 29, 31, 52, 33, 1, 45, 31, 22, 50, 50, 50, 50, 50, 31, 22, 50, 44, 26, 44, 49, 50, 49, 26, 45, 31, 30, 22, 44, 30, 31, 17, 50, 50, 50, 31, 43, 52, 50, 53, 31, 30, 17, 26, 31, 46, 41, 44, 26, 31, 52, 50, 30, 31, 26, 39, 31, 46, 33, 27, 1, 42, 50, 31, 30, 12, 26, 27, 52, 31, 30, 12, 31, 46, 26, 27, 14, 50, 31, 22, 52, 33, 31, 41, 50, 46, 31, 22, 23, 41, 31, 53, 26, 21, 31, 33, 30, 31, 19, 39, 51, 33, 30, 39, 51, 12, 58, 60, 31, 41, 33, 53, 31, 3, 17, 50, 31, 51, 26, 29, 52, 31, 33, 22, 26, 31, 41, 51, 54, 41, 29, 52, 31, 19, 23, 33, 30, 44, 26, 27, 38, 8, 50, 29, 15]
sha256Encode = [61, 44, 3, 14, 22, 41, 43, 30, 49, 59, 58, 30, 11, 3, 24, 35, 40, 46, 3, 42, 59, 36, 41, 41, 41, 40, 9, 59, 23, 36, 40, 33, 42, 42, 42, 42, 42, 40, 44, 42, 49, 24, 49, 28, 42, 33, 24, 36, 40, 24, 33, 10, 24, 40, 35, 42, 42, 42, 40, 39, 9, 42, 3, 40, 24, 35, 24, 40, 3, 61, 49, 24, 40, 9, 42, 24, 40, 41, 17, 40, 12, 57, 11, 23, 43, 42, 40, 24, 18, 41, 11, 9, 40, 24, 18, 40, 3, 41, 11, 12, 42, 40, 44, 9, 59, 40, 61, 42, 3, 40, 44, 13, 61, 40, 3, 24, 29, 40, 59, 24, 40, 19, 18, 6, 59, 24, 18, 6, 22, 0, 39, 40, 61, 57, 3, 40, 17, 35, 42, 40, 58, 24, 58, 9, 40, 59, 44, 24, 40, 61, 48, 52, 61, 58, 9, 40, 19, 13, 59, 24, 53, 41, 11, 55, 55, 42, 58, 18]
md5Decode = []
sha256Decode = []
FLAG = ''
initList = ['-1']
md5Dictionary = dict.fromkeys(initList)
sha256Dictionary = dict.fromkeys(initList)

for i in cand:
    m = int(md5(i.encode()).hexdigest(), 16) % 64
    s = int(sha256(i.encode()).hexdigest(), 16) % 64
    if md5Dictionary.__contains__(m):
        tmp = ''
        tmp += (i + str(md5Dictionary[m]))
        md5Dictionary[m] = tmp
    else:
        md5Dictionary.setdefault(m, i)

    if sha256Dictionary.__contains__(s):
        tmp = ''
        tmp += (i + str(sha256Dictionary[s]))
        sha256Dictionary[s] = tmp
    else:
        sha256Dictionary.setdefault(s, i)

for i in md5Encode:
    md5Decode.append(md5Dictionary[i])

for i in sha256Encode:
    sha256Decode.append(sha256Dictionary[i])


for i in range(len(md5Encode)):
    break_flag = False
    for j in md5Decode[i]:
        for z in sha256Decode[i]:
            if j == z:
                break_flag = True
                FLAG += z
                break
        if break_flag:
            break
print(FLAG)

Title Text

Subtitle

AIS3{0N_May_16th @Sead00g said Heeeee ReMEMBerEd tH4t heee UseD thE SAME set 0f On1iNe to01s to S01Ve Rsa AeS RCA DE5 at T-cat-cup, AnD 7he kEys aRE AlWAys TCat2019Key}

Thanks for listening

Know Some Cryptography -- Use Python Decode CTF --

By Иo1lz

Know Some Cryptography -- Use Python Decode CTF --

  • 155