Skip to content

Commit d502d3f

Browse files
furkansahinclaude
andcommitted
Block non-root access to EC2 instance metadata endpoint
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e69bbb8 commit d502d3f

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/sbin/nft -f
2+
3+
flush ruleset
4+
5+
table inet imds_protection {
6+
chain output {
7+
type filter hook output priority 0; policy accept;
8+
9+
# Allow root to access IMDS
10+
meta skuid 0 ip daddr 169.254.169.254 accept
11+
meta skuid 0 ip6 daddr fd00:ec2::254 accept
12+
13+
# Block all other users from accessing IMDS
14+
ip daddr 169.254.169.254 drop
15+
ip6 daddr fd00:ec2::254 drop
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[Unit]
2+
Description=Block non-root access to EC2 instance metadata
3+
DefaultDependencies=no
4+
Before=network-pre.target
5+
Wants=network-pre.target
6+
7+
[Service]
8+
Type=oneshot
9+
ExecStart=/bin/sh -c 'grep -qi amazon /sys/devices/virtual/dmi/id/sys_vendor 2>/dev/null && nft -f /etc/nftables.conf || true'
10+
11+
[Install]
12+
WantedBy=multi-user.target

common/setup_base.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,11 @@ usermod --append --groups cert_readers prometheus
112112
echo "[setup_base.sh] Restricting su to sudo group..."
113113
echo -e '\nauth required pam_wheel.so group=sudo\n' | tee -a /etc/pam.d/su
114114

115+
echo "=== [setup_base.sh] Setting up IMDS protection ==="
116+
117+
apt-get install -y nftables
118+
cp /tmp/common/assets/imds-protection.nftables.conf /etc/nftables.conf
119+
cp /tmp/common/assets/imds-protection.service /etc/systemd/system/imds-protection.service
120+
systemctl enable imds-protection.service
121+
115122
echo "=== [setup_base.sh] Complete ==="

0 commit comments

Comments
 (0)