-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathfix-docker-external-access.sh
More file actions
executable file
·425 lines (351 loc) · 14.1 KB
/
fix-docker-external-access.sh
File metadata and controls
executable file
·425 lines (351 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/bin/bash
# Universal Docker External Access Fix Script
# This script fixes Docker container external access issues on any Ubuntu system
# Works on AWS, Azure, GCP, bare metal, or any other deployment
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}✅${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠️${NC} $1"
}
print_error() {
echo -e "${RED}❌${NC} $1"
}
print_info() {
echo -e "${BLUE}ℹ️${NC} $1"
}
print_header() {
echo -e "${BLUE}🔧 $1${NC}"
echo "$(printf '=%.0s' {1..50})"
}
# Function to detect primary network interface dynamically
detect_primary_interface() {
print_info "Detecting primary network interface..."
# Method 1: Try to get the default route interface (most reliable)
PRIMARY_INTERFACE=$(ip route | grep default | awk '{print $5}' | head -1)
# Method 2: Fallback to first active non-loopback interface with IP
if [ -z "$PRIMARY_INTERFACE" ]; then
print_warning "No default route found, trying alternative detection..."
PRIMARY_INTERFACE=$(ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '^127\.' | head -1 | xargs -I {} ip -4 addr show | grep -B2 {} | grep -oP '(?<=^\d+:\s)[^:@]+' | head -1)
fi
# Method 3: Final fallback to any UP interface except loopback
if [ -z "$PRIMARY_INTERFACE" ]; then
print_warning "Trying final fallback method..."
PRIMARY_INTERFACE=$(ip link show | grep -E '^[0-9]+:' | grep 'state UP' | grep -v lo | awk -F': ' '{print $2}' | head -1 | cut -d'@' -f1)
fi
# Method 4: Common interface names fallback
if [ -z "$PRIMARY_INTERFACE" ]; then
print_warning "Using common interface name detection..."
for iface in eth0 ens5 ens3 enp0s3 enp0s8 ens160 ens192; do
if ip link show "$iface" >/dev/null 2>&1 && ip link show "$iface" | grep -q "state UP"; then
PRIMARY_INTERFACE="$iface"
break
fi
done
fi
if [ -z "$PRIMARY_INTERFACE" ]; then
print_error "Could not detect any suitable network interface."
print_info "Available interfaces:"
ip link show | grep -E '^[0-9]+:' | awk -F': ' '{print " - " $2}' | sed 's/@.*$//'
echo ""
read -p "Please enter your primary network interface name: " PRIMARY_INTERFACE
if [ -z "$PRIMARY_INTERFACE" ]; then
print_error "No interface specified. Exiting."
exit 1
fi
fi
# Validate the interface exists and is UP
if ! ip link show "$PRIMARY_INTERFACE" >/dev/null 2>&1; then
print_error "Interface '$PRIMARY_INTERFACE' does not exist!"
exit 1
fi
if ! ip link show "$PRIMARY_INTERFACE" | grep -q "state UP"; then
print_warning "Interface '$PRIMARY_INTERFACE' is not UP, but continuing..."
fi
print_status "Detected/Using primary interface: $PRIMARY_INTERFACE"
# Get interface IP for information
INTERFACE_IP=$(ip -4 addr show "$PRIMARY_INTERFACE" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)
if [ -n "$INTERFACE_IP" ]; then
print_info "Interface IP: $INTERFACE_IP"
fi
}
# Function to detect Docker bridge interfaces
detect_docker_bridges() {
print_info "Detecting Docker bridge interfaces..."
DOCKER_BRIDGES=$(ip link show | grep -E 'br-[a-f0-9]+|docker0' | awk -F': ' '{print $2}' | cut -d'@' -f1)
if [ -z "$DOCKER_BRIDGES" ]; then
print_warning "No Docker bridge interfaces found. Docker might not be running."
print_info "Starting Docker service..."
sudo systemctl start docker
sleep 5
DOCKER_BRIDGES=$(ip link show | grep -E 'br-[a-f0-9]+|docker0' | awk -F': ' '{print $2}' | cut -d'@' -f1)
fi
if [ -n "$DOCKER_BRIDGES" ]; then
print_status "Found Docker bridges: $(echo $DOCKER_BRIDGES | tr '\n' ' ')"
else
print_error "No Docker bridges found even after starting Docker!"
exit 1
fi
}
# Function to backup current iptables rules
backup_iptables() {
print_info "Creating backup of current iptables rules..."
BACKUP_FILE="/tmp/iptables-backup-$(date +%Y%m%d-%H%M%S).rules"
sudo iptables-save > "$BACKUP_FILE"
print_status "Backup saved to: $BACKUP_FILE"
}
# Function to check if rule already exists
rule_exists() {
local rule="$1"
sudo iptables -C $rule >/dev/null 2>&1
}
# Function to apply Docker networking fixes
apply_docker_fixes() {
print_header "Applying Docker External Access Fixes"
# Check current FORWARD policy
CURRENT_POLICY=$(sudo iptables -L FORWARD | head -1 | grep -oP '(?<=policy )[A-Z]+')
print_info "Current FORWARD policy: $CURRENT_POLICY"
# Fix 1: Allow forwarding from external interface to Docker bridges
print_info "Adding FORWARD rule: external interface → Docker bridges"
for bridge in $DOCKER_BRIDGES; do
if ! rule_exists "FORWARD -i $PRIMARY_INTERFACE -o $bridge -j ACCEPT"; then
sudo iptables -I FORWARD -i "$PRIMARY_INTERFACE" -o "$bridge" -j ACCEPT
print_status "Added rule: $PRIMARY_INTERFACE → $bridge"
else
print_info "Rule already exists: $PRIMARY_INTERFACE → $bridge"
fi
done
# Fix 2: Allow return traffic from Docker bridges to external interface
print_info "Adding FORWARD rule: Docker bridges → external interface"
for bridge in $DOCKER_BRIDGES; do
if ! rule_exists "FORWARD -i $bridge -o $PRIMARY_INTERFACE -j ACCEPT"; then
sudo iptables -I FORWARD -i "$bridge" -o "$PRIMARY_INTERFACE" -j ACCEPT
print_status "Added rule: $bridge → $PRIMARY_INTERFACE"
else
print_info "Rule already exists: $bridge → $PRIMARY_INTERFACE"
fi
done
# Fix 3: Set FORWARD policy to ACCEPT if it's DROP
if [ "$CURRENT_POLICY" = "DROP" ]; then
print_info "Changing FORWARD policy from DROP to ACCEPT"
sudo iptables -P FORWARD ACCEPT
print_status "FORWARD policy changed to ACCEPT"
else
print_status "FORWARD policy already set to ACCEPT"
fi
# Fix 4: Ensure Docker-specific rules for common container ports
print_info "Adding specific rules for common container ports..."
# Common web ports used by SOC tools
for port in 80 443 5443 7001 7002 7003 7004 7005 7006 7007 7008 7009; do
# Allow external to container
if ! rule_exists "FORWARD -i $PRIMARY_INTERFACE -p tcp --dport $port -j ACCEPT"; then
sudo iptables -I FORWARD -i "$PRIMARY_INTERFACE" -p tcp --dport $port -j ACCEPT
fi
# Allow container to external (return traffic)
if ! rule_exists "FORWARD -o $PRIMARY_INTERFACE -p tcp --sport $port -j ACCEPT"; then
sudo iptables -I FORWARD -o "$PRIMARY_INTERFACE" -p tcp --sport $port -j ACCEPT
fi
done
print_status "Added rules for common SOC tool ports"
}
# Function to make rules persistent
make_persistent() {
print_header "Making Rules Persistent"
# Check if iptables-persistent is installed
if ! dpkg -l | grep -q iptables-persistent; then
print_info "Installing iptables-persistent..."
sudo apt update >/dev/null 2>&1
DEBIAN_FRONTEND=noninteractive sudo apt install -y iptables-persistent >/dev/null 2>&1
print_status "iptables-persistent installed"
else
print_status "iptables-persistent already installed"
fi
# Save current rules
print_info "Saving iptables rules..."
sudo mkdir -p /etc/iptables
sudo iptables-save | sudo tee /etc/iptables/rules.v4 >/dev/null
print_status "Rules saved to /etc/iptables/rules.v4"
# Enable netfilter-persistent service
sudo systemctl enable netfilter-persistent >/dev/null 2>&1
print_status "netfilter-persistent service enabled"
}
# Function to test connectivity
test_connectivity() {
print_header "Testing Container Connectivity"
# Get server IP
SERVER_IP=$(ip -4 addr show "$PRIMARY_INTERFACE" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)
if [ -n "$SERVER_IP" ]; then
print_info "Testing connectivity to $SERVER_IP..."
# Test some common ports
for port in 7003 7008 7004; do
print_info "Testing port $port..."
if timeout 5 bash -c "</dev/tcp/$SERVER_IP/$port" >/dev/null 2>&1; then
print_status "Port $port is accessible"
else
print_warning "Port $port is not responding (service might not be running)"
fi
done
else
print_warning "Could not determine server IP for testing"
fi
}
# Function to display current iptables status
show_status() {
print_header "Current iptables Configuration"
echo "FORWARD Chain Policy:"
sudo iptables -L FORWARD | head -1
echo ""
echo "FORWARD Chain Rules (first 10):"
sudo iptables -L FORWARD -n --line-numbers | head -11
echo ""
echo "NAT Rules for Docker:"
sudo iptables -t nat -L DOCKER -n | grep -E "DNAT|REDIRECT" | head -5
echo ""
echo "Docker Bridge Interfaces:"
ip link show | grep -E 'br-[a-f0-9]+|docker0' | awk -F': ' '{print " - " $2}' | cut -d'@' -f1
}
# Function to create systemd service for applying rules on boot
create_startup_service() {
print_header "Creating Startup Service"
SERVICE_FILE="/etc/systemd/system/docker-networking-fix.service"
sudo tee "$SERVICE_FILE" >/dev/null << EOF
[Unit]
Description=Docker External Access Networking Fix
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/home/ubuntu/CyberBlueSOCx/fix-docker-external-access.sh --apply-only
User=root
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable docker-networking-fix.service >/dev/null 2>&1
print_status "Created and enabled docker-networking-fix.service"
}
# Main execution
main() {
print_header "Universal Docker External Access Fix"
echo "This script fixes Docker container external access on any Ubuntu system"
echo "Supports: AWS EC2, Azure VMs, GCP, bare metal, VirtualBox, VMware, etc."
echo ""
# Check if running as root or with sudo
if [ "$EUID" -eq 0 ]; then
print_warning "Running as root. This is acceptable for system configuration."
elif ! sudo -n true 2>/dev/null; then
print_error "This script requires sudo privileges. Please run with sudo or as root."
exit 1
fi
# Parse arguments
APPLY_ONLY=false
SKIP_PERSISTENT=false
SKIP_SERVICE=false
for arg in "$@"; do
case $arg in
--apply-only)
APPLY_ONLY=true
shift
;;
--skip-persistent)
SKIP_PERSISTENT=true
shift
;;
--skip-service)
SKIP_SERVICE=true
shift
;;
-h|--help)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --apply-only Only apply iptables rules (for systemd service)"
echo " --skip-persistent Skip making rules persistent"
echo " --skip-service Skip creating systemd service"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Full setup with all features"
echo " $0 --skip-service # Setup without systemd service"
exit 0
;;
esac
done
# Step 1: Detect network interface
detect_primary_interface
# Step 2: Detect Docker bridges
detect_docker_bridges
# Step 3: Backup current rules (unless apply-only mode)
if [ "$APPLY_ONLY" = false ]; then
backup_iptables
fi
# Step 4: Apply fixes
apply_docker_fixes
# Step 5: Make persistent (unless skipped or apply-only mode)
if [ "$APPLY_ONLY" = false ] && [ "$SKIP_PERSISTENT" = false ]; then
make_persistent
fi
# Step 6: Create systemd service (unless skipped or apply-only mode)
if [ "$APPLY_ONLY" = false ] && [ "$SKIP_SERVICE" = false ]; then
create_startup_service
fi
# Step 7: Show status
if [ "$APPLY_ONLY" = false ]; then
show_status
fi
# Step 8: Test connectivity (unless apply-only mode)
if [ "$APPLY_ONLY" = false ]; then
test_connectivity
fi
# Final summary
echo ""
print_header "Docker External Access Fix Complete!"
if [ -n "$INTERFACE_IP" ]; then
echo "🌐 Your server IP: $INTERFACE_IP"
echo "🔗 Test MISP access: https://$INTERFACE_IP:7003"
echo "👤 MISP credentials: admin@admin.test / admin"
fi
echo ""
print_status "Docker containers should now be accessible externally"
print_status "Rules will persist across reboots"
if [ "$SKIP_SERVICE" = false ] && [ "$APPLY_ONLY" = false ]; then
print_status "Systemd service created for automatic rule application"
fi
echo ""
print_info "Troubleshooting commands:"
echo " - Check rules: sudo iptables -L FORWARD -n"
echo " - Test ports: sudo ss -tlnp | grep :PORT"
echo " - View logs: sudo journalctl -u docker-networking-fix"
echo " - Reapply: $0 --apply-only"
}
# Detect platform for informational purposes
detect_platform() {
if [ -f /sys/hypervisor/uuid ] && grep -q "^ec2" /sys/hypervisor/uuid 2>/dev/null; then
echo "AWS EC2"
elif [ -f /sys/class/dmi/id/sys_vendor ]; then
case $(cat /sys/class/dmi/id/sys_vendor 2>/dev/null) in
"Microsoft Corporation") echo "Azure" ;;
"Google") echo "Google Cloud" ;;
"QEMU") echo "QEMU/KVM" ;;
"VMware, Inc.") echo "VMware" ;;
"innotek GmbH") echo "VirtualBox" ;;
*) echo "Physical/Other" ;;
esac
else
echo "Unknown"
fi
}
# Show platform info
PLATFORM=$(detect_platform)
print_info "Detected platform: $PLATFORM"
# Run main function
main "$@"