🎯 Learning Objectives
- Understand IP addresses, subnets, and routing
- Learn about common ports and protocols
- Comprehend TCP vs UDP differences
- Recognize how firewalls work
- Identify network security concepts
An IP address (Internet Protocol address) is a unique numerical identifier assigned to each device connected to a computer network. Think of it as your computer's home address.
# IPv4 Address Classes
Class A: 1.0.0.0 - 126.255.255.255
Class B: 128.0.0.0 - 191.255.255.255
Class C: 192.0.0.0 - 223.255.255.255
Class D: 224.0.0.0 - 239.255.255.255 (Multicast)
Class E: 240.0.0.0 - 255.255.255.255 (Reserved)
# IPv6 Address Types
Global Unicast: 2000::/3
Link-Local: fe80::/10
Unique Local: fc00::/7
Multicast: ff00::/8
10.0.0.0 - 10.255.255.255 (10.0.0.0/8)
172.16.0.0 - 172.31.255.255 (172.16.0.0/12)
192.168.0.0 - 192.168.255.255 (192.168.0.0/16)
DNS is like the phonebook of the Internet. It translates domain names (like google.com) into IP addresses that computers use to identify each other.
Your computer asks the local DNS resolver (usually provided by your ISP) to resolve the domain.
The resolver contacts a root DNS server to find the TLD (.com, .org, etc.) server.
The resolver contacts the TLD server for the specific domain.
The resolver contacts the authoritative DNS server that has the actual IP address.
The IP address is returned to your computer, which can now connect to the website.
| Record Type | Purpose | Example |
|---|---|---|
| A | IPv4 Address | example.com → 93.184.216.34 |
| AAAA | IPv6 Address | example.com → 2606:2800:220:1 |
| CNAME | Canonical Name (alias) | www.example.com → example.com |
| MX | Mail Exchange | example.com → mail.example.com |
| TXT | Text Record | SPF, DKIM verification |
Ports are virtual endpoints for network communication. They allow a single device to run multiple network services simultaneously.
| Port | Service | Description | Security Note |
|---|---|---|---|
| 20/21 | FTP | File Transfer | ⚠️ Unencrypted, use SFTP |
| 22 | SSH | Secure Shell | ✅ Secure remote access |
| 23 | Telnet | Unencrypted remote access | ⚠️ Never use, use SSH |
| 25 | SMTP | Email sending | ⚠️ Use TLS/SSL |
| 53 | DNS | Domain Name System | Critical infrastructure |
| 80 | HTTP | Web traffic | ⚠️ Use HTTPS instead |
| 443 | HTTPS | Secure Web traffic | ✅ Encrypted |
| 3306 | MySQL | Database | ⚠️ Never expose to Internet |
| 3389 | RDP | Remote Desktop | ⚠️ VPN required |
TCP and UDP are the two main transport layer protocols used for transmitting data over networks.
Client Server
| |
|-------- SYN ---------------→ |
| "Want to connect" |
| |
|←------- SYN-ACK ----------- |
| "OK, ready" |
| |
|-------- ACK ---------------→ |
| "Let's go!" |
| |
|===== Connection Established ===|
A firewall is a network security device or software that monitors and controls incoming and outgoing network traffic based on predetermined security rules.
Hardware device that filters traffic between networks. Typically placed at network perimeter.
Software running on individual computers. Controls traffic to/from that specific device.
Cloud-based firewall services that protect cloud infrastructure.
# Allow established connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow SSH (port 22)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Allow HTTP (port 80)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# Allow HTTPS (port 443)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# Drop everything else
iptables -A INPUT -j DROP
# List rules
iptables -L -v -n
Dividing a network into smaller segments to isolate sensitive systems and limitattack spread.
# Example VLAN Configuration
!
interface GigabitEthernet0/1
description Guest Network
switchport mode access
switchport access vlan 20
!
interface GigabitEthernet0/2
description Corporate Network
switchport mode access
switchport access vlan 10
!
interface GigabitEthernet0/3
description Server Farm
switchport mode access
switchport access vlan 30
Creates an encrypted tunnel for secure communication over public networks.
import socket
from concurrent.futures import ThreadPoolExecutor
def scan_port(host: str, port: int, timeout: float = 1.0) -> dict:
"""Scan a single port."""
result = {
'host': host,
'port': port,
'status': 'closed',
'service': None
}
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
connection = sock.connect_ex((host, port))
if connection == 0:
result['status'] = 'open'
# Try to get service name
try:
result['service'] = socket.getservbyport(port)
except:
result['service'] = 'unknown'
sock.close()
except socket.error:
result['status'] = 'error'
return result
def scan_common_ports(host: str) -> list:
"""Scan most common ports."""
common_ports = [21, 22, 23, 25, 53, 80, 110, 143, 443, 445,
3306, 3389, 5432, 8080, 8443]
results = []
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(scan_port, host, port) for port in common_ports]
for future in futures:
results.append(future.result())
return sorted(results, key=lambda x: x['port'])
# Example usage
host = "example.com"
print(f"Scanning {host}...")
results = scan_common_ports(host)
for r in results:
if r['status'] == 'open':
print(f"Port {r['port']}: OPEN ({r.get('service', 'N/A')})")
Build a Python script that performs basic network diagnostics:
import socket
import subprocess
import platform
def network_diagnostics(target: str):
"""Perform basic network diagnostics on a target."""
results = {
'target': target,
'ip_address': None,
'ping_result': None,
'dns_resolution': None,
'open_ports': []
}
# 1. Resolve hostname
try:
results['ip_address'] = socket.gethostbyname(target)
results['dns_resolution'] = 'Success'
except socket.gaierror:
results['dns_resolution'] = 'Failed'
return results
# 2. Ping the target
param = '-n' if platform.system().lower() == 'windows' else '-c'
command = ['ping', param, '4', target]
try:
ping_output = subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True)
results['ping_result'] = 'Reachable'
except subprocess.CalledProcessError:
results['ping_result'] = 'Unreachable'
return results
# Test
if __name__ == "__main__":
result = network_diagnostics("google.com")
print(f"Target: {result['target']}")
print(f"IP: {result['ip_address']}")
print(f"DNS: {result['dns_resolution']}")
print(f"Ping: {result['ping_result']}")
Design a small business network with the following requirements:
Write iptables rules that implement your firewall design.