Cryptography in CTF for beginners: from cipher recognition to automatic hacking

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
362
Reaction score
596
Deposit
0$
The line Gur synt vf cvpbPGS{abg_gbb_onq_bs_n_ceboyrz} – ROT13. One click in CyberChef – and the answer: The flag is picoCTF{not_too_bad_of_a_problem}. Thirty seconds for someone who recognized the pattern. The clock is for someone who has climbed into RSA calculators.

The Crypto category in the CTF is arranged paradoxically: most entry-level tasks do not require higher mathematics – only the ability to recognize what you are dealing with. The hardest part is not hacking the cipher, but determining its type. Sounds corny until you wait half an hour over the hex line that turned out to be a stupid base64 without padding.

Next is a practical guide for three basic elements of cryptanalysis: base-coding, Caesar cipher and XOR. From visual markers to automatic solvers in Python.
How are crypto CTF tasks arranged

In the format of Jeopardy – the most common format of CTF-competitions – tasks are scattered into categories: Web, PWN, Reverse, Forensics, Crypto and others. Crypto (cryptography) is one of the main, and the tasks inside are divided into several levels:

Encoding – base64, base32, hex, ASCII transformation. This is not encryption, but the translation of data from one form to another. There is no key – you only need to define the format.
Classic ciphers — Caesar, Wieger, wildcards, Rail Fence. Algorithms have long been broken, the task is to recognize the type and apply the right approach to hacking.
XOR encryption is a broken operation with a key. The simplest version of “real” encryption, but with predictable weaknesses with short keys.
Modern cryptography – RSA, AES, elliptical curves. Here already need algebra and number theory.

The article covers the first three levels – everything that occurs in the vast majority of crypto CTF tasks for beginners. And these skills work far beyond the competition: in real attacks base64 and XOR are used to offscate payloads. In MITRE ATT&CK, this is described as Data Encoding (T1132) for C2 communications and Obfuscated Files or Information (T1027) for masking malicious files. The same techniques as the CTFs are only higher rates.

Surrounding requirements for practical sections:

Python 3.6+ (for scripts-solvers)
Browser with access to gchq.github.io/CyberChef (no installation required)
Optional: pip install xortool to Automate Multi-Byte XOR Analysis
OS: any (Linux, Windows, macOS). In Linux utilities base64 and base32 available from the box

Base-coding recognition by visual markers

The first thing a beginner encounters in a crypto CTF is a string of obscure characters. Before you decrypt anything, you need to understand: is it a cipher or just encoding? The difference is fundamental – reversible encoding without a key, the cipher is not. According to RFC 4648, each base-coding uses a fixed alphabet, and the type is determined in seconds.
Works if: the string contains only characters from the alphabet of the corresponding encoding. It does not work if: the data is additionally encrypted after coding - then base64 decoding will give binary debris, not readable text.

Three rules of rapid identification:

There is == at the end and lowercase letters - most likely base64. The most frequent encoding in CTF, and in life: JWT-tokens, cookies, API parameters.
There is = at the end, only capital letters and numbers 2-7 base — 32. It is less common, but if you worked with Google Authenticator - saw TOTP secrets in base32.
Only hex symbols 0-9, A-F, the length of the multiples is two - base16 (hex). Classic genre: the data is allegedly “encrypted”, but in fact simply coded.

For base58, the characteristic feature is the absence of visually ambiguous symbols 0, O, I, l. If the line is similar to the Bitcoin address, it is worth trying base58 decoding.
Invested Encodings and CyberChef Magic

Frequent trap in CTF — nested encodings: base64 inside base32 inside hex. The doll. Unwinding by hand is a waste of time. CyberChef for CTF has a Magic (magic wand icon) function: it automatically determines the type of encoding and offers a chain of operations to remove all layers.

Manual algorithm for cases where Magic fails to:

Determine the outer layer by visual markers from the table above.
Decode one layer.
Look at the result - if again porridge, return to step 1.
Repeat until the readable text or flag format appears.

In the terminology of MITRE ATT & CK, multiple layers of coding are standard practice of obfuscation. The Deobfuscate/Decode Files or Information (T1140) technique describes the process of sequentially removing layers when analyzing malware. The same skills as for CTF, with real response.
Caesar's cheat: hacking from manual overkill to automation

Caesar’s cipher is a wildcard, where each letter shifts to a fixed number of positions in the alphabet. Shift 3: A→D, B→E, C→F. ROT13 is a special case with a shift of 13, popular because double application returns the source text (13 + 13 = 26, the full turnover of the alphabet). Essentially, a cipher that deciphers itself.

How to recognize Caesar's cipher in the task:

The text looks “almost like English” – the structure of words in place (the gaps, the length of words is realistic), but the letters “not those”.
Punctuation signs, numbers and gaps are not usually affected.
If a fragment of a known flag format is visible in the task (for example, cvpbPGS{ – each letter is shifted from picoCTF{), it's a hundred percent Caesar marker. You can't guess any further.

Works if: the alphabet is standard (Latin A-Z), the shift is one for all symbols. Does not work if: used Cyrillic with a non-standard alphabet, polyalphabetic substitution (Vigener's cipher), or the author of the task mixed the alphabet arbitrarily (general monoalphavite cipher).
Brute force Caesar cipher in Python

Only 25 possible shifts, not counting zero. Going through everything is a trivial task:

def caesar_brute(ciphertext):
for shift in range(1, 26):
result = ""
for ch in ciphertext:
if ch.isalpha():
base = ord('A') if ch.isupper() else ord('a')
result += chr((ord(ch) - base - shift) % 26 + base)
else:
result += ch
print(f" {shift:2d}: {result}")

caesar_brute("Gur synt vf cvpbPGS{abg_gbb_onq_bs_n_ceboyrz}")

The script will display 25 lines. The option is where the text is read. When shifting 13 we get: The flag is picoCTF{not_too_bad_of_a_problem}. For CTF, there is enough eyes – 25 lines are viewed in seconds. In CyberChef, the same result is an operation ROT13 or Caesar Cipher Decode with the key value.

The nuance on which the stumbles: if after ROT13 the text is still unread, but the punctuation signs look strange - try ROT47. It shifts the entire range of printed ASCII from ! to ~, capturing numbers and special symbols.
Frequency Cipher Analysis for Long Texts

For short strings, the brute force is more effective – 25 options are viewed instantly. But if the cipher text is a whole paragraph or page, frequency analysis allows you to determine the shift without overtaking.

Principle: in the English text the most frequent letter — E (approximately 12.7% of appearances), then T (about 9.1%), A (about 8.2%). If the most common letter in the cipher text is R, the likely shift = position R minus position E = 13. Check the hypothesis with decryption – and in the vast majority of cases it is confirmed.

The principle works for any monoalphavite wildcard, not just for Caesar. On dCode (dcode.fr), frequency analysis of ciphers is performed automatically - the service shows the distribution of letters in the form of a histogram. For Russian-language texts, the most frequent letter - О, behind her Е and А.
XOR cipher: decryption from one byte to multi-byte key

XOR (eXclusive OR) is a smash operation, the foundation of the simplest encryption. Each XOR bit of data is with the corresponding key bit. The main property: A XOR K XOR K = A Double application of one key returns the original data. The same operation encrypts and decrypts. Beautiful and simple – that’s why XOR is so loved by the authors of malwary.

In real attacks, XOR is used everywhere. MITRE ATT&CK technique Obfuscated Files or Information (T1027) includes XOR obfuction as one of the most common methods of masking payloads. The XOR decryption skill is a direct preparation for dealing with real incidents.

How to recognize XOR encryption in the task:

The result is binary data, often presented in hex format.
If the key is short (1-4 bytes), in the hex-representation you can see repeating patterns with a period equal to the length of the key.
With single-byte XOR with ASCII-text zero bytes (\x00) appear in positions where the symbol of the public text coincided with the key.

Works if: key - one byte (256 options) or a short multi-byte key up to 32 bytes. It does not work if: the key is equal to the length of the data (one-time pad is mathematically unhackable) or the data was compressed before XOR encryption.
Selection of single-byte XOR key

Single-byte XOR is the most frequent option in crypto CTF tasks for beginners. The key is one number from 0 to 255. We flip all the options and filter by readability:

def xor_single_byte_brute(data_hex):
data = bytes.fromhex(data_hex)
for key in range(256):
result = bytes([b ^ key for b in data])
if all(32 <= b < 127 for b in result):
print(f"Key 0x{key:02x}: {result.decode('ascii')}")


xor_single_byte_brute("4f626b6b68275078756b63")

The script filters the results by the criterion of “all bytes – printed ASCII”. For short lines, a few keys can be suitable, but the desired one is where the text is meaningful. To improve accuracy, you add a calculation of the frequency of spaces and letters e, t, a The more “English” patterns, the more likely the right key.

In CyberChef, a similar result is an operation XOR Brute Force – outputs all 256 options with illumination of the most probable.
Multi-byte XOR and attack by known open text

If the key is longer than one byte, the direct overkill is exponential: for two-byte - 65 536 variants, for the four-byte - more than 4 billion. But in the CTF there is a trump card – a well-known open text.

The flag format is usually announced in advance: picoCTF{, flag{, HTB{. If the ciphertext contains an encrypted flag, it is enough XOR to start a cipher text with a known start - the result will give a key or a fragment of it. Principle: if ciphertext = plaintext XOR key, that key = ciphertext XOR plaintext.


cipher_start = bytes.fromhex("0b150a0202")
known_plaintext = b"flag{"
key = bytes([c ^ p for c, p in zip(cipher_start, known_plaintext)])
print(f"Ключ: {key}")

Having received a fragment of the key, determine its length. If the key is applied cyclically (and this is the standard scheme of the repeating-key XOR), the pattern will be repeated every N byte. Restoring the full key, decrypt the entire text.

There is a utility for automation xortool. Installation: pip install xortool. Launch: xortool -l <длина_ключа> -c 20 файл – parameter -c 20 indicates that the most frequent symbol of plain text is space (0x20, which is true for the English text). The utility itself calculates the likely length of the key according to the match index and selects the value.
Crypto CTF tools: CyberChef and not only

CyberChef (gchq.github.io/CyberChef) is the first tool to open on any crypto task. Developed by GCHQ (British special service), works in a browser without installation. For the tasks of the initial and middle level it is enough for the eyes.

Operations that are needed most often:

From Base64 / From Base32 / From Hex – decoding encodings in one click.
Magic – automatic recognition of nested encodings and removal of all layers.
ROT13 / Caesar Cipher Decode – hacking of Caesar’s cipher by overtaking shifts.
XOR / XOR Brute Force – the use of XOR with a given key or a selection of single-byte keys.

CyberChef strength in the chains of operations (recipes). Example: From Hex → XOR (key 0x42) → From Base64 – three layers of obfuscation are removed for one load. Recipes can be saved and shared with the team.
Automatic hacking of ciphers: step-by-step methodology

To solve crypto CTF tasks, you need not intuition, but a methodical algorithm. Here is a scheme that works for primary and mid-level tasks:

Step 1. Determine the type of data. Look at the alphabet of the line. Only hex symbols? There is == At the end? Only capital letters plus numbers 2-7? We check with the table of visual markers of base-encoding. Binary data is probably a XOR or block cipher.

Step 2. Check the encoding. Trying From Base64, From Hex, From Base32 in CyberChef. If the result is readable text or other encoding, it is not a cipher. Unwind the layers to the end. Magic does this automatically.

Step 3. Check the classic ciphers. If the text is “almost readable”, it is likely a wildcard. Brute force cipher Caesar (25 shifts) is the first thing worth trying. Did not help - Vigener (key clue is often in the condition of the task). For unidentified ciphers, the dCode Cipher Identifier.

Step 4. Check the XOR. Binary data without encoding patterns is the XOR Brute Force in CyberChef for a single-byte key. For multi-byte - an attack by a well-known open text through the flag format.

Step 5. Check the flag format. After each step, search for the flag format as a result: flag{, picoCTF{, HTB{, ctf{. Searching for a substring is faster than subtracting with your eyes.

Step 6. nested layers. If after one round of decoding a new porridge is obtained, we return to step 1. Invested encodings are the standard reception of CTF task authors.
Typical traps for beginners

Patterns, which consistently lose time beginners:

Hex without prefix 0x. The line 48656c6c6f – not a cipher, but a hex-coded Hello. See only symbols 0-9 and a-f with an even line length – the first thing From Hex.

Base64 without padding. Some implementations are being cleaned = from the end of the base64 line. If the string contains the upper and lowercase plus numbers, but without = It could still be base64. CyberChef decodes without padding.

The double base64. The result of the first decoding base64 again looks like base64 – capital, lowercase, numbers, =. Apply From Base64 repeatedly until you receive the readable text or binary data. Don’t be lazy – the authors of the tasks were also not lazy when they wrapped up.

ROT47 instead of ROT13. ROT13 only works with letters. ROT47 shifts the entire range of printed ASCII from ! to ~ Numbers and special symbols are also changing. If after ROT13 the text is unreadable and the punctuation marks look strange – ROT47 in CyberChef.

ASCII codes instead of text. The line 72 101 108 108 111 – decimal ASCII letters codes Hello. There are options in the eight (110 145 154 154 157) or binary system (01001000 01100101 01101100 01101100 01101111). CyberChef operation From Decimal / From Octal / From Binary solves the problem instantly.

None of these traps require deep knowledge of cryptography. We need only the methodicality and the eye on the visual patterns. That is why solving entry-level crypto-tasks is the ideal entry point in cryptoanalysis: the entry barrier is minimal, and the skill of recognizing patterns is universal.

I’ve seen this more than once: participants who have learned RSA mathematics and can explain the Diffy-Hellman protocol on the board fail on base64-in-base32-in-hex tasks. The problem is not intelligence – pattern recognition and knowledge of theory live in different skill planes. Pattern-matching trains only by solving dozens of tasks in a row, no book does.

I will risk giving a forecast: in the next year or two crypto-tasks of the entry level on the CTF will become more complicated not in mathematics, but in the number of nested layers and non-standard encodings. CyberChef Magic is already coping with simple cases, and the job makers are forced to use custom alphabets and XOR combinations with multi-layer coding that automation does not take. The only way not to get stuck is to write your solvers at least at the level of a ten-line script.
 
Top Bottom