Skip to content

CHƯƠNG 11: FIREWALL¤

1. FIREWALL CƠ BẢN¤

Định nghĩa¤

  • Stop unauthorized traffic giữa các networks
  • Separate trusteduntrusted components
  • Filtering data, redirecting traffic, protecting attacks

3 Requirements¤

Text Only
1. ALL traffic phải đi qua firewall
2. CHỈ authorized traffic được pass
3. Firewall phải immune to penetration (hardened OS)

Firewall Policy (3 loại control)¤

1. User Control: Dựa vào user role 2. Service Control: Dựa vào network address, protocol, port 3. Direction Control: Inbound/Outbound traffic

Firewall Actions¤

  • ACCEPT: Cho phép đi qua
  • DROP: Từ chối im lặng (không reply)
  • REJECT: Từ chối + gửi ICMP reply

Filtering Types¤

  • Ingress: Inspect incoming (bảo vệ internal từ outside)
  • Egress: Inspect outgoing (ngăn internal user ra ngoài)

2. 3 LOẠI FIREWALL¤

2.1 Packet Filter (Stateless)¤

Text Only
✓ Kiểm tra packet headers (IP, port, protocol)
✗ KHÔNG nhìn payload
✗ KHÔNG track connection state
✗ Mỗi packet độc lập

Ưu: Nhanh, đơn giản
Nhược: Không hiểu context, dễ bypass

2.2 Stateful Firewall¤

Text Only
✓ Track connection state (NEW, ESTABLISHED, RELATED)
✓ Maintain connection table
✓ Hiểu context của packets

Ưu: An toàn hơn stateless
Nhược: Phức tạp hơn, tốn resource

Connection States: - NEW: Packet đầu tiên của flow mới - ESTABLISHED: Connection 2 chiều đã thiết lập - RELATED: Liên quan đến connection khác (vd: FTP data) - INVALID: Không follow expected behavior

2.3 Application/Proxy Firewall¤

Text Only
✓ Inspect đến APPLICATION layer
✓ Acts as intermediary (proxy)
✓ Client → Proxy → Server (2 separate connections)
✓ Authenticate users trực tiếp

Ưu: Deep inspection, chống IP spoofing
Nhược: Chậm, phải implement proxy cho mỗi protocol

3. NETFILTER & KERNEL MODULES¤

Netfilter¤

  • Framework trong Linux kernel cho packet processing
  • Cung cấp hooks trên packet traversal path

5 Netfilter Hooks (IPv4)¤

Text Only
┌─────────────────────────────────────────┐
│  NF_IP_PRE_ROUTING                      │
│  (Packet vừa vào network stack)         │
└──────────────┬──────────────────────────┘
        ┌──────┴──────┐
        │  Routing?   │
        └──────┬──────┘
         ┌─────┴─────┐
         ↓           ↓
  NF_IP_LOCAL_IN   NF_IP_FORWARD
  (Đến local)      (Forward qua)
         ↓           ↓
    Local Process   NF_IP_POST_ROUTING
         ↓           (Ra khỏi system)
  NF_IP_LOCAL_OUT
  (Từ local ra)
  NF_IP_POST_ROUTING

Hooks mapping: - PRE_ROUTING: Ngay khi packet đến - LOCAL_IN: Packet đến local process - FORWARD: Packet đi qua (router) - LOCAL_OUT: Packet từ local process - POST_ROUTING: Trước khi ra khỏi interface

Loadable Kernel Modules (LKM)¤

C
// Template
#include <linux/module.h>
#include <linux/kernel.h>

static int __init my_init(void) {
    printk(KERN_INFO "Module loaded\n");
    return 0;
}

static void __exit my_cleanup(void) {
    printk(KERN_INFO "Module removed\n");
}

module_init(my_init);
module_exit(my_cleanup);

Commands:

Bash
make                    # Compile
sudo insmod mymod.ko    # Insert module
sudo rmmod mymod        # Remove module
lsmod                   # List modules
dmesg                   # View kernel messages

Netfilter Verdicts¤

  • NF_ACCEPT: Cho packet đi tiếp
  • NF_DROP: Discard packet
  • NF_QUEUE: Pass to userspace
  • NF_STOLEN: Module xử lý, netfilter quên packet
  • NF_REPEAT: Gọi lại callback

Simple Firewall Example¤

C
#include <linux/netfilter_ipv4.h>

unsigned int block_telnet(void *priv, struct sk_buff *skb,
                          const struct nf_hook_state *state) {
    struct iphdr *iph = ip_hdr(skb);
    struct tcphdr *tcph = tcp_hdr(skb);

    if (iph->protocol == IPPROTO_TCP && 
        ntohs(tcph->dest) == 23) {
        printk("Blocking telnet\n");
        return NF_DROP;
    }
    return NF_ACCEPT;
}

static struct nf_hook_ops telnet_ops = {
    .hook     = block_telnet,
    .pf       = PF_INET,
    .hooknum  = NF_INET_LOCAL_OUT,
    .priority = NF_IP_PRI_FIRST,
};

nf_register_net_hook(&init_net, &telnet_ops);  // Register

4. IPTABLES¤

Cấu trúc Hierarchy¤

Text Only
TABLES
  ├─ filter (default) - Packet filtering
  ├─ nat            - Address translation
  ├─ mangle         - Packet modification
  └─ raw            - Connection tracking exceptions

CHAINS (tương ứng Netfilter hooks)
  ├─ INPUT          - NF_IP_LOCAL_IN
  ├─ OUTPUT         - NF_IP_LOCAL_OUT
  ├─ FORWARD        - NF_IP_FORWARD
  ├─ PREROUTING     - NF_IP_PRE_ROUTING
  └─ POSTROUTING    - NF_IP_POST_ROUTING

RULES
  └─ Match conditions + Target action

Packet Traversal Flow¤

Text Only
         ┌─────────────┐
         │ PREROUTING  │
         └──────┬──────┘
         ┌──────┴──────┐
         │  Routing?   │
         └──────┬──────┘
          ┌─────┴─────┐
          ↓           ↓
      ┌───────┐   ┌─────────┐
      │ INPUT │   │ FORWARD │
      └───┬───┘   └────┬────┘
          ↓            ↓
     Local Process  ┌──────────────┐
          ↓          │ POSTROUTING  │
      ┌────────┐     └──────────────┘
      │ OUTPUT │
      └────┬───┘
    ┌──────────────┐
    │ POSTROUTING  │
    └──────────────┘

Syntax Cơ Bản¤

Bash
iptables [-t table] -A chain <rule> -j <target>

-t table        : filter (default), nat, mangle, raw
-A chain        : Append to chain
-I chain [num]  : Insert at position
-D chain num    : Delete rule
-L              : List rules
-F              : Flush (delete all)
--line-numbers  : Show rule numbers

Rule Matching¤

Bash
# Layer 2 (Interface)
-i eth0         # Input interface
-o eth1         # Output interface

# Layer 3 (IP)
-s 192.168.1.5      # Source IP
-s 192.168.1.0/24   # Source network
-d 10.0.0.1         # Destination IP

# Layer 4 (Protocol)
-p tcp                  # Protocol
-p tcp --dport 80       # Destination port
-p tcp --sport 1024:    # Source port range
-p icmp --icmp-type echo-request

Targets (Actions)¤

  • ACCEPT: Cho phép
  • DROP: Từ chối im lặng
  • REJECT: Từ chối + ICMP reply
  • LOG: Log packet (rồi tiếp tục)
  • SNAT/DNAT: NAT
  • MASQUERADE: Dynamic SNAT

5. IPTABLES EXAMPLES¤

Basic Firewall¤

Bash
# 1. Flush all rules
sudo iptables -F
sudo iptables -t nat -F

# 2. Set default policies
sudo iptables -P INPUT ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P FORWARD ACCEPT

# 3. Allow loopback
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A OUTPUT -o lo -j ACCEPT

# 4. Allow DNS (port 53)
sudo iptables -A INPUT -p udp --sport 53 -j ACCEPT
sudo iptables -A OUTPUT -p udp --dport 53 -j ACCEPT

# 5. Allow SSH (22) and HTTP (80) incoming
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 6. Allow all outgoing TCP
sudo iptables -A OUTPUT -p tcp -j ACCEPT

# 7. Change default to DROP
sudo iptables -P INPUT DROP
sudo iptables -P OUTPUT DROP
sudo iptables -P FORWARD DROP

Block Specific IP/User¤

Bash
# Block source IP
sudo iptables -A INPUT -s 192.168.30.6 -j DROP

# Block user (OUTPUT only)
sudo iptables -A OUTPUT -m owner --uid-owner seed -j DROP

Stateful Firewall¤

Bash
# Allow established/related connections
sudo iptables -A INPUT -m conntrack \
  --ctstate ESTABLISHED,RELATED -j ACCEPT

sudo iptables -A OUTPUT -m conntrack \
  --ctstate ESTABLISHED,RELATED -j ACCEPT

# Allow new SSH and HTTP
sudo iptables -A INPUT -p tcp --dport 22 \
  -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 \
  -m conntrack --ctstate NEW -j ACCEPT

# Default DROP
sudo iptables -P INPUT DROP
sudo iptables -P OUTPUT DROP

Management Commands¤

Bash
# List rules
sudo iptables -L -v -n --line-numbers
sudo iptables -t nat -L -v -n

# Delete specific rule
sudo iptables -D INPUT 3

# Flush all
sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t mangle -F

# Check connection tracking
sudo conntrack -L

# Save/Restore rules
sudo iptables-save > rules.txt
sudo iptables-restore < rules.txt

6. NAT (NETWORK ADDRESS TRANSLATION)¤

SNAT (Source NAT)¤

Thay đổi Source IP - dùng cho OUTGOING traffic

Bash
# Static SNAT
sudo iptables -t nat -A POSTROUTING -o eth0 \
  -j SNAT --to-source 203.0.113.5

# Dynamic SNAT (MASQUERADE)
sudo iptables -t nat -A POSTROUTING -o eth0 \
  -j MASQUERADE

# Enable IP forwarding
sudo sysctl net.ipv4.ip_forward=1

Use case: Private network ra Internet (home router)

DNAT (Destination NAT)¤

Thay đổi Destination IP - dùng cho INCOMING traffic

Bash
# Port forwarding
sudo iptables -t nat -A PREROUTING -p tcp --dport 8000 \
  -j DNAT --to-destination 192.168.1.100:80

# Load balancing (random)
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 \
  -m statistic --mode random --probability 0.5 \
  -j DNAT --to-destination 192.168.1.10:80

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 \
  -m statistic --mode random --probability 0.5 \
  -j DNAT --to-destination 192.168.1.20:80

# Load balancing (round-robin)
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 \
  -m statistic --mode nth --every 2 --packet 0 \
  -j DNAT --to-destination 192.168.1.10:80

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 \
  -m statistic --mode nth --every 2 --packet 1 \
  -j DNAT --to-destination 192.168.1.20:80

Use case: Port forwarding, load balancing, DMZ


7. IPTABLES EXTENSIONS¤

Conntrack Module¤

Bash
# Match connection state
-m conntrack --ctstate NEW,ESTABLISHED,RELATED,INVALID

# Example: Only allow outgoing if established
sudo iptables -A OUTPUT -p tcp \
  -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

Owner Module (OUTPUT only)¤

Bash
# Match by user ID
sudo iptables -A OUTPUT -m owner --uid-owner alice -j DROP

# Match by group
sudo iptables -A OUTPUT -m owner --gid-owner admin -j ACCEPT

Other Modules¤

Bash
# Match multiple ports
-m multiport --dports 80,443,8080

# Rate limiting
-m limit --limit 5/minute --limit-burst 10

# String matching
-m string --string "password" --algo bm

# Time-based rules
-m time --timestart 09:00 --timestop 17:00

8. PACKET MODIFICATION (MANGLE TABLE)¤

TTL Modification¤

Bash
# Increase TTL by 5
sudo iptables -t mangle -A PREROUTING \
  -j TTL --ttl-inc 5

# Set TTL to specific value
sudo iptables -t mangle -A PREROUTING \
  -j TTL --ttl-set 64

TOS (Type of Service)¤

Bash
sudo iptables -t mangle -A PREROUTING \
  -j TOS --set-tos 0x10

9. BYPASSING FIREWALLS¤

9.1 SSH Tunneling (Port Forwarding)¤

Local Port Forwarding:

Bash
# Forward local port 8000 to work:23 via apollo
ssh -L 8000:work:23 user@apollo

# Usage: telnet localhost 8000
# → Traffic: home:8000 → SSH tunnel → apollo → work:23

Remote Port Forwarding:

Bash
# On work machine
ssh -R 8000:localhost:80 user@home

# Access on home: curl localhost:8000
# → Traffic: home:8000 → SSH tunnel → work:80

Scenario 1: Company firewall blocks telnet - Firewall allows SSH to internal machine - Use SSH tunnel to reach blocked services

Scenario 2: Company blocks Facebook - Use outside machine to proxy - SSH tunnel forwards HTTP via home machine

9.2 Dynamic Port Forwarding (SOCKS Proxy)¤

Bash
# Create SOCKS proxy on port 9000
ssh -D 9000 -C home

# Configure browser: SOCKS5 proxy = localhost:9000
# → All traffic through SSH tunnel

Đặc điểm: - Không specify destination trước - Client software cần SOCKS support - Flexible hơn static forwarding

9.3 VPN (Virtual Private Network)¤

  • Tạo encrypted tunnel giữa 2 networks
  • Firewall không thấy traffic bên trong
  • Cannot conduct filtering

10. WEB PROXY¤

Chức năng¤

  • Control browser access
  • Cache web content
  • Filter URLs/content
  • Anonymize requests

Setup Methods¤

1. Browser config: Set proxy trong settings 2. iptables redirect:

Bash
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 \
  -j REDIRECT --to-port 3128  # Proxy port
3. Network bridge: Proxy trên bridge transparent

Evading Egress Filtering¤

Text Only
User → Firewall (block facebook.com) → Internet
         (blocked)

User → Proxy (allowed IP) → Internet (Facebook)
         (allowed!)

Anonymizing Proxy¤

  • Hide client IP from destination servers
  • Server chỉ thấy proxy IP

GHI NHỚ NHANH¤

3 Firewall Types¤

Stateless: Headers only, no context Stateful: Track connections (NEW, ESTABLISHED, RELATED) Proxy: Application layer, deep inspection

5 Netfilter Hooks¤

PRE_ROUTING → (Routing) → LOCAL_IN/FORWARD LOCAL_OUT → POST_ROUTING

iptables Tables¤

filter: Packet filtering (default) nat: SNAT/DNAT mangle: Packet modification raw: Connection tracking control

iptables Chains¤

INPUT: Incoming to local OUTPUT: Outgoing from local FORWARD: Passing through (router) PREROUTING: Before routing decision POSTROUTING: After routing decision

NAT Types¤

SNAT: Change source IP (POSTROUTING, outgoing) DNAT: Change dest IP (PREROUTING, incoming) MASQUERADE: Dynamic SNAT (for DHCP)

Connection States¤

NEW: First packet ESTABLISHED: 2-way communication RELATED: Related to existing (FTP) INVALID: Unexpected behavior

Bypass Methods¤

SSH Tunnel: Local/Remote port forwarding SOCKS Proxy: Dynamic forwarding VPN: Encrypted tunnel

Basic Commands¤

Bash
iptables -L              # List
iptables -F              # Flush
iptables -A INPUT ...    # Append
iptables -D INPUT 2      # Delete
iptables -P INPUT DROP   # Policy
conntrack -L             # Connections
dmesg                    # Kernel log

Rule Template¤

Bash
iptables -t [table] -A [chain] \
  -i [in-if] -o [out-if] \
  -s [src-ip] -d [dst-ip] \
  -p [protocol] --dport [port] \
  -m [module] [module-opts] \
  -j [target]

FIREWALL - CÂU HỎI TRẮC NGHIỆM¤

PHẦN 1: FIREWALL CƠ BẢN¤

Câu 1: Firewall là gì?

A. Phần mềm diệt virus

B. Hệ thống ngăn chặn unauthorized traffic giữa các networks

C. Web server

D. Database

Đáp án: B


Câu 2: 3 requirements của firewall là gì?

A. Nhanh, rẻ, dễ dùng

B. All traffic qua firewall, chỉ authorized traffic pass, firewall immune to penetration

C. Chỉ cần block attack

D. Chỉ cần mã hóa

Đáp án: B


Câu 3: Service Control trong firewall policy dựa vào gì?

A. User role

B. Network address, protocol, port

C. Time

D. Location

Đáp án: B


Câu 4: Ingress filtering là gì?

A. Inspect outgoing traffic

B. Inspect incoming traffic để bảo vệ internal network

C. Inspect internal traffic

D. Block all traffic

Đáp án: B


Câu 5: Egress filtering là gì?

A. Inspect incoming traffic

B. Inspect outgoing traffic để ngăn internal users ra ngoài

C. Allow all traffic

D. Encrypt traffic

Đáp án: B


Câu 6: DROP khác REJECT như thế nào?

A. Giống nhau

B. DROP từ chối im lặng, REJECT từ chối + gửi ICMP reply

C. DROP nhanh hơn

D. REJECT an toàn hơn

Đáp án: B


PHẦN 2: 3 LOẠI FIREWALL¤

Câu 7: Packet Filter Firewall còn gọi là gì?

A. Stateful Firewall

B. Stateless Firewall

C. Proxy Firewall

D. Dynamic Firewall

Đáp án: B


Câu 8: Packet Filter Firewall kiểm tra gì?

A. Payload

B. Application data

C. Packet headers (IP, port, protocol) only

D. User credentials

Đáp án: C


Câu 9: Packet Filter Firewall có track connection state không?

A. Có

B. Không - mỗi packet độc lập

C. Tùy cấu hình

D. Chỉ track TCP

Đáp án: B


Câu 10: Stateful Firewall khác Stateless ở điểm nào?

A. Nhanh hơn

B. Track connection state, maintain connection table

C. Đơn giản hơn

D. Rẻ hơn

Đáp án: B


Câu 11: Connection states trong Stateful Firewall gồm gì?

A. OPEN, CLOSED

B. NEW, ESTABLISHED, RELATED, INVALID

C. UP, DOWN

D. ACTIVE, INACTIVE

Đáp án: B


Câu 12: ESTABLISHED state có nghĩa gì?

A. Packet đầu tiên

B. Connection 2 chiều đã thiết lập

C. Connection đóng

D. Connection lỗi

Đáp án: B


Câu 13: NEW state có nghĩa gì?

A. Connection đã thiết lập

B. Packet đầu tiên của flow mới

C. Packet cuối cùng

D. Lỗi

Đáp án: B


Câu 14: RELATED state dùng khi nào?

A. Packet lỗi

B. Connection liên quan đến existing connection (vd: FTP data)

C. Packet đầu tiên

D. Packet cuối

Đáp án: B


Câu 15: Application/Proxy Firewall inspect đến layer nào?

A. Physical layer

B. Network layer

C. Transport layer

D. Application layer

Đáp án: D


Câu 16: Proxy Firewall hoạt động như thế nào?

A. Forward packets trực tiếp

B. Acts as intermediary, client → proxy → server (2 separate connections)

C. Chỉ kiểm tra headers

D. Không làm gì

Đáp án: B


Câu 17: Ưu điểm của Proxy Firewall?

A. Rất nhanh

B. Deep inspection, authenticate users, chống IP spoofing

C. Rất đơn giản

D. Không cần config

Đáp án: B


Câu 18: Nhược điểm của Proxy Firewall?

A. Không an toàn

B. Chậm, phải implement proxy cho mỗi protocol mới

C. Không có nhược điểm

D. Quá đơn giản

Đáp án: B


PHẦN 3: NETFILTER¤

Câu 19: Netfilter là gì?

A. Firewall software

B. Framework trong Linux kernel cho packet processing, cung cấp hooks

C. Web server

D. Database

Đáp án: B


Câu 20: Netfilter có bao nhiêu hooks cho IPv4?

A. 3 hooks

B. 4 hooks

C. 5 hooks

D. 10 hooks

Đáp án: C


Câu 21: 5 Netfilter hooks là gì?

A. INPUT, OUTPUT, FORWARD only

B. PRE_ROUTING, LOCAL_IN, FORWARD, LOCAL_OUT, POST_ROUTING

C. ACCEPT, DROP, REJECT

D. TCP, UDP, ICMP

Đáp án: B


Câu 22: NF_IP_PRE_ROUTING xảy ra khi nào?

A. Packet ra khỏi system

B. Ngay khi packet vào network stack, trước routing decision

C. Sau routing

D. Khi packet đến application

Đáp án: B


Câu 23: NF_IP_LOCAL_IN dùng cho traffic nào?

A. Traffic đi qua (forward)

B. Traffic đến local process

C. Traffic ra ngoài

D. All traffic

Đáp án: B


Câu 24: NF_IP_FORWARD dùng cho gì?

A. Traffic đến local

B. Traffic từ local ra

C. Traffic đi qua system (routing/forwarding)

D. Không dùng

Đáp án: C


Câu 25: NF_IP_POST_ROUTING xảy ra khi nào?

A. Đầu tiên khi packet vào

B. Trước khi packet ra khỏi system, sau routing

C. Giữa routing

D. Không bao giờ

Đáp án: B


Câu 26: Để block outgoing telnet (port 23), dùng hook nào?

A. NF_IP_PRE_ROUTING

B. NF_IP_LOCAL_IN

C. NF_IP_LOCAL_OUT

D. NF_IP_FORWARD

Đáp án: C


Câu 27: Netfilter verdict NF_ACCEPT có nghĩa gì?

A. Drop packet

B. Cho packet đi tiếp trong stack

C. Queue packet

D. Gọi lại callback

Đáp án: B


Câu 28: NF_DROP làm gì?

A. Accept packet

B. Discard packet (bỏ)

C. Forward packet

D. Queue packet

Đáp án: B


Câu 29: NF_STOLEN có nghĩa gì?

A. Drop packet

B. Netfilter quên packet, module xử lý tiếp

C. Accept packet

D. Queue packet

Đáp án: B


PHẦN 4: LOADABLE KERNEL MODULES¤

Câu 30: LKM là viết tắt gì?

A. Linux Kernel Manager

B. Loadable Kernel Module

C. Linux Kernel Memory

D. Link Kernel Module

Đáp án: B


Câu 31: Ưu điểm của LKM?

A. Không có ưu điểm

B. Dynamically add/remove modules, không cần recompile kernel

C. Chỉ an toàn hơn

D. Nhanh hơn

Đáp án: B


Câu 32: Command insert kernel module?

A. sudo modload mymod.ko

B. sudo insmod mymod.ko

C. sudo addmod mymod.ko

D. sudo load mymod.ko

Đáp án: B


Câu 33: Command remove kernel module?

A. sudo delmod mymod

B. sudo rmmod mymod

C. sudo remove mymod

D. sudo unload mymod

Đáp án: B


Câu 34: Command list loaded modules?

A. ps aux

B. ls -la

C. lsmod

D. modlist

Đáp án: C


Câu 35: Command view kernel messages?

A. cat /var/log/kernel

B. tail /var/log/messages

C. dmesg

D. journalctl only

Đáp án: C


Câu 36: module_init() làm gì?

A. Remove module

B. Specify initialization function khi module inserted

C. List modules

D. Update module

Đáp án: B


Câu 37: module_exit() làm gì?

A. Insert module

B. Specify cleanup function khi module removed

C. List modules

D. Restart module

Đáp án: B


PHẦN 5: IPTABLES CƠ BẢN¤

Câu 38: iptables gồm mấy phần?

A. Chỉ user-space

B. Kernel part (Xtables) + User-space program (iptables)

C. Chỉ kernel

D. Không có phần nào

Đáp án: B


Câu 39: iptables có bao nhiêu tables chính?

A. 2 tables

B. 3 tables

C. 4 tables: filter, nat, mangle, raw

D. 5 tables

Đáp án: C


Câu 40: Default table của iptables là gì?

A. nat

B. mangle

C. filter

D. raw

Đáp án: C


Câu 41: filter table dùng để làm gì?

A. NAT

B. Packet filtering (block/allow)

C. Packet modification

D. Connection tracking

Đáp án: B


Câu 42: nat table dùng để làm gì?

A. Filtering

B. Address translation (SNAT/DNAT)

C. Logging

D. Rate limiting

Đáp án: B


Câu 43: mangle table dùng để làm gì?

A. NAT

B. Filtering

C. Packet modification (TTL, TOS,...)

D. Connection tracking

Đáp án: C


Câu 44: INPUT chain tương ứng Netfilter hook nào?

A. NF_IP_PRE_ROUTING

B. NF_IP_LOCAL_IN

C. NF_IP_FORWARD

D. NF_IP_POST_ROUTING

Đáp án: B


Câu 45: FORWARD chain dùng cho traffic gì?

A. Đến local process

B. Từ local process

C. Traffic đi qua (routing/forwarding)

D. Tất cả traffic

Đáp án: C


Câu 46: PREROUTING chain xảy ra khi nào?

A. Sau routing

B. Trước routing decision

C. Chỉ cho local traffic

D. Cuối cùng

Đáp án: B


Câu 47: POSTROUTING chain xảy ra khi nào?

A. Đầu tiên

B. Giữa routing

C. Sau routing, trước khi packet ra interface

D. Không bao giờ

Đáp án: C


PHẦN 6: IPTABLES SYNTAX¤

Câu 48: Command append rule vào chain?

A. iptables -I INPUT

B. iptables -A INPUT

C. iptables -D INPUT

D. iptables -L INPUT

Đáp án: B


Câu 49: Command list rules?

A. iptables -A

B. iptables -L

C. iptables -D

D. iptables -F

Đáp án: B


Câu 50: Command flush (xóa all) rules?

A. iptables -D

B. iptables -L

C. iptables -F

D. iptables -A

Đáp án: C


Câu 51: Command delete rule number 3 từ INPUT chain?

A. iptables -D INPUT 3

B. iptables -A INPUT 3

C. iptables -F INPUT 3

D. iptables -L INPUT 3

Đáp án: A


Câu 52: -i trong iptables là gì?

A. Insert rule

B. Input interface

C. IP address

D. ICMP type

Đáp án: B


Câu 53: -o trong iptables là gì?

A. Open port

B. Output interface

C. Owner

D. Option

Đáp án: B


Câu 54: -s trong iptables chỉ gì?

A. Server

B. Source IP address

C. Service

D. Socket

Đáp án: B


Câu 55: -p tcp --dport 80 có nghĩa gì?

A. Source port 80

B. Protocol TCP, destination port 80

C. Protocol UDP, port 80

D. All ports

Đáp án: B


Câu 56: -j ACCEPT là gì?

A. Jump to chain ACCEPT

B. Target action: ACCEPT packet

C. Just accept

D. Join accept

Đáp án: B


Câu 57: Command set default policy INPUT chain to DROP?

A. iptables -A INPUT DROP

B. iptables -L INPUT DROP

C. iptables -P INPUT DROP

D. iptables -D INPUT DROP

Đáp án: C


Câu 58: Command để show rule numbers?

A. iptables -L

B. iptables -L --line-numbers

C. iptables -L -n

D. iptables -L -v

Đáp án: B


PHẦN 7: IPTABLES EXAMPLES¤

Câu 59: Block source IP 192.168.1.5?

A. iptables -A INPUT -s 192.168.1.5 -j ACCEPT

B. iptables -A INPUT -s 192.168.1.5 -j DROP

C. iptables -A OUTPUT -s 192.168.1.5 -j DROP

D. iptables -D INPUT -s 192.168.1.5

Đáp án: B


Câu 60: Allow incoming SSH (port 22)?

A. iptables -A INPUT -p tcp --dport 22 -j DROP

B. iptables -A INPUT -p tcp --sport 22 -j ACCEPT

C. iptables -A INPUT -p tcp --dport 22 -j ACCEPT

D. iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT

Đáp án: C


Câu 61: Allow all outgoing TCP traffic?

A. iptables -A INPUT -p tcp -j ACCEPT

B. iptables -A OUTPUT -p tcp -j ACCEPT

C. iptables -A FORWARD -p tcp -j ACCEPT

D. iptables -P OUTPUT ACCEPT

Đáp án: B


Câu 62: Allow loopback interface?

A. iptables -A INPUT -i lo -j DROP

B. iptables -A INPUT -i lo -j ACCEPT

C. iptables -A OUTPUT -o lo -j DROP

D. B và iptables -A OUTPUT -o lo -j ACCEPT

Đáp án: D


Câu 63: Block user "seed" từ sending packets?

A. iptables -A INPUT -m owner --uid-owner seed -j DROP

B. iptables -A OUTPUT -m owner --uid-owner seed -j DROP

C. iptables -A FORWARD -m owner --uid-owner seed -j DROP

D. Không thể làm

Đáp án: B - Owner module chỉ dùng cho OUTPUT chain


Câu 64: Allow DNS queries (port 53)?

A. iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT

B. iptables -A OUTPUT -p udp --dport 53 -j ACCEPT

C. iptables -A INPUT -p tcp --sport 53 -j ACCEPT

D. B và C (DNS dùng UDP)

Đáp án: D


PHẦN 8: STATEFUL FIREWALL¤

Câu 65: Command check connection tracking?

A. iptables -L

B. netstat -an

C. conntrack -L

D. ss -an

Đáp án: C


Câu 66: Stateful firewall rule allow established connections?

A. iptables -A INPUT -j ACCEPT

B. iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

C. iptables -A INPUT -p tcp -j ACCEPT

D. iptables -P INPUT ACCEPT

Đáp án: B


Câu 67: Extension nào dùng để match connection state?

A. state

B. conntrack

C. owner

D. limit

Đáp án: B


Câu 68: --ctstate có thể có giá trị nào?

A. UP, DOWN

B. NEW, ESTABLISHED, RELATED, INVALID

C. OPEN, CLOSED

D. ACTIVE, PASSIVE

Đáp án: B


PHẦN 9: NAT¤

Câu 69: SNAT là gì?

A. Source NAT - thay đổi source IP

B. Simple NAT

C. Secure NAT

D. Server NAT

Đáp án: A


Câu 70: DNAT là gì?

A. Dynamic NAT

B. Destination NAT - thay đổi destination IP

C. Direct NAT

D. Default NAT

Đáp án: B


Câu 71: SNAT dùng chain nào?

A. PREROUTING

B. INPUT

C. OUTPUT

D. POSTROUTING

Đáp án: D


Câu 72: DNAT dùng chain nào?

A. POSTROUTING

B. OUTPUT

C. PREROUTING

D. FORWARD

Đáp án: C


Câu 73: MASQUERADE khác SNAT như thế nào?

A. Giống nhau

B. MASQUERADE là dynamic SNAT (cho DHCP/dynamic IP)

C. MASQUERADE nhanh hơn

D. SNAT tốt hơn mọi trường hợp

Đáp án: B


Câu 74: Command enable IP forwarding?

A. echo 1 > /proc/sys/net/ipv4/ip_forward

B. sudo sysctl net.ipv4.ip_forward=1

C. iptables -t nat -F

D. A hoặc B

Đáp án: D


Câu 75: SNAT use case chính?

A. Web server

B. Private network ra Internet (home router)

C. DNS server

D. Email server

Đáp án: B


Câu 76: DNAT use case chính?

A. Chỉ cho filtering

B. Port forwarding, load balancing

C. Chỉ cho logging

D. Không có use case

Đáp án: B


Câu 77: Load balancing với DNAT dùng module nào?

A. owner

B. conntrack

C. statistic (--mode random hoặc nth)

D. limit

Đáp án: C


PHẦN 10: BYPASSING FIREWALLS¤

Câu 78: SSH Local Port Forwarding syntax?

A. ssh -L local_port:remote_host:remote_port user@ssh_server

B. ssh -R local_port:remote_host:remote_port user@ssh_server

C. ssh -D port user@ssh_server

D. ssh -N user@ssh_server

Đáp án: A


Câu 79: SSH Remote Port Forwarding syntax?

A. ssh -L port:host:port user@server

B. ssh -R remote_port:local_host:local_port user@server

C. ssh -D port user@server

D. ssh -N user@server

Đáp án: B


Câu 80: SSH Dynamic Port Forwarding (SOCKS) syntax?

A. ssh -L 9000 user@server

B. ssh -R 9000 user@server

C. ssh -D 9000 -C user@server

D. ssh -N user@server

Đáp án: C


Câu 81: SOCKS proxy là gì?

A. Static proxy

B. Dynamic proxy không specify destination trước

C. Web proxy only

D. DNS proxy

Đáp án: B


Câu 82: Client software cần gì để dùng SOCKS proxy?

A. Không cần gì

B. Native SOCKS support

C. Chỉ browser

D. VPN client

Đáp án: B


Câu 83: VPN bypass firewall như thế nào?

A. Không bypass được

B. Encrypted tunnel, firewall không thấy traffic bên trong

C. Chỉ thay đổi IP

D. Chỉ tăng tốc độ

Đáp án: B


Câu 84: Web proxy có thể dùng để làm gì?

A. Chỉ cache web

B. Evade egress filtering, anonymize requests

C. Chỉ filter URLs

D. Chỉ log traffic

Đáp án: B


Câu 85: Anonymizing proxy làm gì?

A. Tăng tốc độ

B. Hide client IP from destination servers

C. Filter virus

D. Encrypt all traffic

Đáp án: B


GHI NHỚ QUAN TRỌNG¤

Firewall Types¤

Type Check State Layer
Stateless Headers L3/L4
Stateful Headers+State L3/L4
Proxy Deep L7

5 Netfilter Hooks¤

Text Only
PRE_ROUTING → (routing) → LOCAL_IN
                    FORWARD
LOCAL_OUT → POST_ROUTING

iptables Structure¤

Text Only
TABLES: filter, nat, mangle, raw
CHAINS: INPUT, OUTPUT, FORWARD, 
        PREROUTING, POSTROUTING
RULES: match + target

Connection States¤

  • NEW: Packet đầu tiên
  • ESTABLISHED: 2-way communication
  • RELATED: Liên quan existing (FTP)
  • INVALID: Unexpected

NAT¤

  • SNAT: POSTROUTING, change source
  • DNAT: PREROUTING, change dest
  • MASQUERADE: Dynamic SNAT

Bypass Methods¤

  • SSH -L: Local forwarding
  • SSH -R: Remote forwarding
  • SSH -D: SOCKS proxy
  • VPN: Encrypted tunnel

Key Commands¤

Bash
iptables -L              # List
iptables -F              # Flush
iptables -A INPUT ...    # Append
iptables -P INPUT DROP   # Policy
conntrack -L             # Connections
insmod/rmmod             # LKM
dmesg                    # Kernel log