All write-ups
20 min read

The Ultimate Guide to Network & Cryptography

NetworkingCryptoRSAProtocols

The Hacker's Field Manual: Network Security & Applied Cryptography

Welcome to the definitive field guide. This manual synthesizes the critical foundations of Applied Cryptography and Network Security. Whether you are auditing architectures, dissecting packets, or breaking legacy ciphers, this knowledge is your weapon.


Part 1: Applied Cryptography

Cryptography is the art and science of secure communication in the presence of adversarial third parties.

1.1 The Fundamentals

Every cryptographic schema consists of two primary algorithms:

Cryptosystems generally fall into two categories based on key distribution:

  1. Symmetric Cryptography: A single key is shared between parties and used for both encryption and decryption.
  2. Asymmetric Cryptography: Uses a key pair—a public key for encryption and a private key for decryption.

[!NOTE] Power Analysis (Side-Channel Attack) When a chip encrypts data, its power consumption fluctuates based on the operations performed. Attackers can analyze these power spikes to deduce the underlying algorithm and potentially extract keys. Modern algorithms incorporate mitigations for this.

1.2 Classic Ciphers and Their Weaknesses

Early encryption relied heavily on substitution and transposition.

1.3 XOR and The One-Time Pad

The XOR (Exclusive OR) operation is the backbone of digital cryptography.

0 ⊕ 0 = 0    |    1 ⊕ 1 = 0
0 ⊕ 1 = 1    |    1 ⊕ 0 = 1

In an XOR Cipher, the plaintext and a key (pad) of equal length are XORed bit-by-bit. If the pad is completely random and used exactly once, this forms a One-Time Pad, which is mathematically unbreakable. Danger: Reusing the pad (Many-Time Pad) destroys security, allowing attackers to deduce the key through ciphertext correlations.

1.4 Modern Symmetric Ciphers

Modern ciphers operate either on blocks of data or continuous streams.

Block Ciphers

Block ciphers encrypt data in fixed-size chunks. They typically use an iterative architecture:

  1. Key Scheduler: Derives multiple subkeys from the master key.
  2. Round Function: Applies substitutions and permutations iteratively using the subkeys.

Stream Ciphers & Modes of Operation

Stream ciphers encrypt bits individually. However, block ciphers can be turned into stream ciphers using specific modes of operation.

1.5 The Key Exchange Problem

How do parties securely agree on a symmetric key over an insecure channel?

Diffie-Hellman Key Exchange

A revolutionary protocol allowing two parties to establish a shared secret over an insecure channel.

  1. Alice and Bob agree on a large prime $p$ and a base generator $g$.
  2. Alice picks a secret $a$ and sends $A = g^a \pmod p$.
  3. Bob picks a secret $b$ and sends $B = g^b \pmod p$.
  4. Both compute the shared secret: $S = B^a \pmod p = A^b \pmod p = g^{ab} \pmod p$.

[!WARNING] Diffie-Hellman does not authenticate the parties. It is heavily vulnerable to Man-in-the-Middle (MitM) attacks. An attacker can intercept the exchanges, negotiate separate keys with Alice and Bob, and relay decrypted/re-encrypted traffic seamlessly.

1.6 Asymmetric Cryptography: RSA

RSA solves the key distribution problem using a mathematical trapdoor (prime factorization).

Key Generation:

  1. Choose two large primes, $p$ and $q$.
  2. Compute $n = p \times q$.
  3. Compute Euler's totient: $\phi(n) = (p-1) \times (q-1)$.
  4. Choose public exponent $e$ such that $1 < e < \phi(n)$ and $e$ is coprime to $\phi(n)$.
  5. Compute private exponent $d$ such that $(e \times d) \pmod{\phi(n)} = 1$.

Encryption & Decryption:

Optimization Note: Decryption (exponentiating with $d$) is computationally heavy. The Chinese Remainder Theorem (CRT) is often used to dramatically speed up RSA decryption.

1.7 Practical Attack: Meet in the Middle

Why don't we use 2DES (encrypting twice with two different 56-bit keys)? Because of the Meet-in-the-Middle attack. An attacker encrypts the known plaintext with all possible $K_1$ and decrypts the ciphertext with all possible $K_2$. Where the intermediate values match, the keys are found.

# A stylized snippet demonstrating a Meet-in-the-Middle on a custom Double-Cipher
dizionario = {}

# Step 1: Encrypt plaintext with all possible k1 and store in dictionary
for a in string.ascii_lowercase:
  for b in string.ascii_lowercase:
    for c in string.ascii_lowercase:
      for d in string.ascii_lowercase:
        k1 = a+b+c+d
        c1 = encrypt(messaggio, k1)
        dizionario[c1] = k1

# Step 2: Decrypt ciphertext with all possible k2 and check for collision
for a in string.ascii_lowercase:
  for b in string.ascii_lowercase:
    for c in string.ascii_lowercase:
      for d in string.ascii_lowercase:
        k2 = a+b+c+d
        c2 = decrypt(messaggioCriptato, k2)

        # Collision found! We have our keys.
        if c2 in dizionario:
          flag = dizionario[c2] + k2
          print("CCIT{" + "{}".format(flag) + "}")
          sys.exit(0)

Part 2: Network Security

To attack or defend a network, one must intimately understand its layers, protocols, and routing behaviors.

2.1 The Models: OSI vs TCP/IP

Data Encapsulation: As data moves down the stack, each layer adds its own header (or footer), creating a Protocol Data Unit (PDU).

2.2 Layer 2: Data Link & Ethernet

2.3 Layer 3: Network & IP Routing

The Internet Protocol (IP) handles global packet routing.

Address Resolution Protocol (ARP): Maps Layer 3 IPs to Layer 2 MACs. If a device needs to talk to an IP on the local subnet, it broadcasts an ARP Request: "Who has this IP? Tell my MAC."

Routing: Routers use a Routing Table to determine the next hop. Routes can be directly connected, statically assigned, or defined by a default gateway.

2.4 NAT & Port Forwarding

To bridge private networks with the public internet:

2.5 Layer 4: Transport (TCP vs UDP)

2.6 Application Layer: DNS & HTTP

2.7 Virtualization & Containerization

Modern networks are heavily virtualized.

Feature Virtual Machines (VMs) Containers (Docker)
Architecture Hardware virtualization (Hypervisor) OS virtualization (Shared Kernel)
OS Footprint Full guest OS per VM Shares host OS
Isolation High (Complete abstraction) Relaxed (Process/namespace level)
Resource Usage Heavy (CPU, RAM, Disk) Lightweight and scalable

2.8 Attack Surface & Perimeters

A security perimeter divides internal assets (high trust) from the external world (zero trust).

Network Sniffing & Interception

To analyze (or steal) traffic, attackers use sniffers (e.g., tcpdump, Wireshark).


Stay sharp. Trust nothing. Verify everything.