Firewall
Firewall
A security device that filters network traffic. Applies inbound/outbound rules.
Firewall
A security device that filters network traffic. Applies inbound/outbound rules.
A firewall is a network security system that monitors network traffic and allows or blocks data packets according to predefined security rules. Like a security checkpoint at a building, it inspects all incoming and outgoing traffic to prevent malicious access.
Firewalls operate in two main ways: Stateless and Stateful. A Stateless firewall inspects each packet individually and checks only the Source/Destination IP, Port, and Protocol. A Stateful firewall, on the other hand, tracks the full context of a connection and performs more sophisticated filtering based on session state. For example, response packets to requests initiated from the inside are automatically allowed.
Packet filtering is the core function of a firewall. It controls traffic based on IP addresses and port numbers at layers 3–4 of the seven OSI layers (the network/transport layers). On Linux, iptables/nftables serve as packet-filtering firewalls, and in cloud environments, AWS Security Group, Azure NSG, and similar services do so.
A WAF (Web Application Firewall) is a specialized firewall that operates at layer 7 (the application layer). It blocks web attacks such as SQL Injection, XSS, and CSRF by analyzing HTTP/HTTPS traffic. AWS WAF, Cloudflare WAF, and ModSecurity are representative examples, and they provide defense rules against OWASP Top 10 vulnerabilities by default.
# iptables 기본 규칙 설정
# 모든 입력 차단, 출력 허용 (기본 정책)
sudo iptables -P INPUT DROP
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P FORWARD DROP
# Loopback 허용 (필수)
sudo iptables -A INPUT -i lo -j ACCEPT
# 이미 연결된 세션 허용 (Stateful)
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# SSH 허용 (포트 22)
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# HTTP/HTTPS 허용
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 특정 IP에서만 SSH 허용 (더 안전)
sudo iptables -A INPUT -p tcp -s 203.0.113.50 --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j DROP
# 특정 포트 범위 허용
sudo iptables -A INPUT -p tcp --dport 8000:9000 -j ACCEPT
# ICMP (ping) 허용
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 로그 후 차단 (디버깅용)
sudo iptables -A INPUT -j LOG --log-prefix "DROPPED: "
sudo iptables -A INPUT -j DROP
# 규칙 확인
sudo iptables -L -n -v
# 규칙 저장 (재부팅 후에도 유지)
sudo iptables-save > /etc/iptables/rules.v4
# AWS Security Group (boto3)
import boto3
ec2 = boto3.client('ec2')
# Security Group 생성
response = ec2.create_security_group(
GroupName='web-server-sg',
Description='Security group for web servers',
VpcId='vpc-12345678'
)
security_group_id = response['GroupId']
print(f'Security Group Created: {security_group_id}')
# 인바운드 규칙 추가
ec2.authorize_security_group_ingress(
GroupId=security_group_id,
IpPermissions=[
# SSH 허용 (특정 IP만)
{
'IpProtocol': 'tcp',
'FromPort': 22,
'ToPort': 22,
'IpRanges': [{'CidrIp': '203.0.113.0/24', 'Description': 'Office IP'}]
},
# HTTP 허용 (전체)
{
'IpProtocol': 'tcp',
'FromPort': 80,
'ToPort': 80,
'IpRanges': [{'CidrIp': '0.0.0.0/0', 'Description': 'Public HTTP'}]
},
# HTTPS 허용 (전체)
{
'IpProtocol': 'tcp',
'FromPort': 443,
'ToPort': 443,
'IpRanges': [{'CidrIp': '0.0.0.0/0', 'Description': 'Public HTTPS'}]
},
# 다른 Security Group에서 오는 트래픽 허용
{
'IpProtocol': 'tcp',
'FromPort': 3306,
'ToPort': 3306,
'UserIdGroupPairs': [{'GroupId': 'sg-app-server', 'Description': 'From App Server'}]
}
]
)
# 아웃바운드 규칙 (기본: 전체 허용)
# 필요시 제한 가능
ec2.authorize_security_group_egress(
GroupId=security_group_id,
IpPermissions=[
{
'IpProtocol': 'tcp',
'FromPort': 443,
'ToPort': 443,
'IpRanges': [{'CidrIp': '0.0.0.0/0', 'Description': 'HTTPS outbound only'}]
}
]
)
# UFW (Uncomplicated Firewall) - Ubuntu 기본 방화벽
# iptables를 쉽게 관리할 수 있는 프론트엔드
# UFW 상태 확인
sudo ufw status verbose
# 기본 정책 설정
sudo ufw default deny incoming # 인바운드 기본 차단
sudo ufw default allow outgoing # 아웃바운드 기본 허용
# SSH 허용 (중요: 원격 접속 시 먼저 설정!)
sudo ufw allow ssh # 또는 ufw allow 22
sudo ufw allow 22/tcp
# HTTP/HTTPS 허용
sudo ufw allow http # 또는 ufw allow 80
sudo ufw allow https # 또는 ufw allow 443
# 특정 IP에서만 허용
sudo ufw allow from 203.0.113.50 to any port 22
# 포트 범위 허용
sudo ufw allow 8000:9000/tcp
# 특정 서브넷 허용
sudo ufw allow from 192.168.1.0/24 to any port 3306
# 애플리케이션 프로필 사용
sudo ufw app list # 사용 가능한 앱 프로필
sudo ufw allow 'Nginx Full' # Nginx HTTP + HTTPS
sudo ufw allow 'OpenSSH'
# 규칙 삭제
sudo ufw delete allow 80
sudo ufw delete allow from 203.0.113.50
# UFW 활성화/비활성화
sudo ufw enable
sudo ufw disable
# 모든 규칙 초기화
sudo ufw reset
# 로깅 설정
sudo ufw logging on
sudo ufw logging medium
"We'll place a WAF in front of the web server to block OWASP Top 10 attacks, and configure the Security Group to allow only traffic coming from the ALB. We just need to put the DB server in a Private Subnet and allow access to port 3306 only from the app server's Security Group."
"Abnormal requests are coming in from a specific IP. First, we'll temporarily block that IP range with iptables, and as a fundamental fix, we'll add a Rate Limiting rule in the WAF to automatically block IPs that make more than 100 requests per minute."
"The difference between a Stateful firewall and a Stateless firewall is whether connection state is tracked. AWS Security Group is Stateful, so when inbound traffic is allowed, response traffic is automatically allowed, but NACL is Stateless, so both inbound and outbound must be explicitly configured. In practice, we configure a dual layer of security: instance-level security with Security Group and subnet-level security with NACL."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Opening SSH port 22 to all IPs (0.0.0.0/0) exposes you to brute-force attacks. Allow only the office IP, and connect through a VPN/Bastion Host.
If you only care about inbound and allow all outbound traffic, there is a risk of data exfiltration when infected with malware. Allow only the necessary destinations (package repositories, API servers).
If you accidentally block port 22 while connected remotely via SSH, you will lose access to the server. For UFW, run ufw allow ssh first, and for the cloud, secure a console access method.
The default policy is to block all traffic (Deny All) and explicitly allow only what is needed (Allow List). Add a description (Description) to each rule, and regularly clean up unused rules.