Phishing Api Keys on Docker
How Phishing Api Keys Manifests in Docker
Phishing API keys in Docker environments typically occurs through exposed service endpoints that mimic legitimate services. Attackers exploit the ephemeral nature of containers and the common practice of running services on default ports to create convincing phishing scenarios.
The most prevalent attack pattern involves an attacker deploying a container that listens on a commonly used API port (like 8080, 5000, or 3000) and responds to requests with a service that appears legitimate but captures credentials. For example, an attacker might deploy a container that mimics a Redis or database service, returning success responses to any authentication attempt while logging the credentials.
# Malicious Dockerfile that captures API keys
FROM python:3.9-slim
WORKDIR /app
COPY capture.py .
EXPOSE 6379
CMD ["python", "-u", "capture.py"]# capture.py - logs any connection attempt
import socket
import logging
logging.basicConfig(filename='/tmp/credentials.log', level=logging.INFO)
def handle_client(client_socket):
data = client_socket.recv(1024)
logging.info(f"Received: {data}")
client_socket.send(b"+OK")
client_socket.close()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('0.0.0.0', 6379))
server.listen(5)
while True:
client_sock, addr = server.accept()
handle_client(client_sock)Another common scenario involves Docker Compose configurations where services are exposed to the wrong network or have overly permissive network policies. An attacker might modify a docker-compose.yml file to expose internal services to the public internet:
version: '3.8'
services:
api:
build: .
ports:
- "8080:8080" # Exposed to internet
networks:
- public
redis:
image: redis:alpine
ports:
- "6379:6379" # Exposed to internet
networks:
- public
deploy:
restart_policy:
condition: on-failure
networks:
public:
driver: bridgeContainer orchestration platforms like Kubernetes can amplify these risks when combined with Docker. An attacker might deploy a service that intercepts traffic meant for legitimate services by using the same service name or namespace, leveraging Docker's networking model to capture credentials before they reach the intended destination.
Docker-Specific Detection
Detecting phishing API keys in Docker environments requires a multi-layered approach that examines both container configurations and runtime behavior. The first step is to audit Docker Compose and Dockerfile configurations for exposed ports and overly permissive network settings.
# Check for exposed ports in running containers
docker ps --format "table {{.Names}}\t{{.Ports}}"Look for containers exposing common API ports (8080, 5000, 3000, 6379, 27017, etc.) without authentication. Containers running as root or with privileged flags are also indicators of potential phishing setups.
Network inspection can reveal suspicious inter-container communication patterns. Use Docker's network commands to identify containers that shouldn't be communicating:
# List all networks and their containers
docker network ls
docker network inspect <network_id>For automated detection, middleBrick's black-box scanning approach is particularly effective for Docker environments. Since middleBrick requires no credentials or agents, you can scan your Docker services directly from the host:
# Scan a Docker service running on localhost
middlebrick scan http://localhost:8080middleBrick tests for 12 security categories including authentication bypass and data exposure, which are critical for identifying phishing setups. The scanner's parallel architecture means it can test multiple endpoints across your Docker network in seconds.
Log analysis is crucial for detection. Monitor Docker logs for unusual connection patterns or authentication attempts:
# Monitor suspicious authentication attempts
docker logs <container_id> | grep -E "(auth|login|password|key)"Implement Docker Content Trust to verify image integrity, as phishing attacks often involve compromised or malicious images. Use:
export DOCKER_CONTENT_TRUST=1Finally, network-level detection using tools like Wireshark or tcpdump can identify data exfiltration attempts from containers that might be capturing credentials:
# Monitor network traffic for credential patterns
tcpdump -i docker0 port 8080 or port 5000Docker-Specific Remediation
Remediating phishing API key vulnerabilities in Docker requires both configuration hardening and runtime protections. Start with Dockerfile best practices to prevent credential exposure:
# Secure Dockerfile practices
FROM python:3.9-slim
# Use non-root user
RUN addgroup --system app && adduser --system --group app
USER app
# Don't install unnecessary packages
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
&& rm -rf /var/lib/apt/lists/*
# Use multi-stage builds to reduce attack surface
FROM python:3.9-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
FROM python:3.9-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
# Set secure defaults
ENV PYTHONUNBUFFERED=1
EXPOSE 8080
CMD ["python", "app.py"]Implement network segmentation in Docker Compose to isolate services:
version: '3.8'
services:
api:
build: .
ports:
- "8080:8080"
networks:
- api-network
deploy:
restart_policy:
condition: on-failure
security_opt:
- no-new-privileges:true
read_only: true
tmpfs:
- /tmp
redis:
image: redis:alpine
networks:
- internal-network
deploy:
restart_policy:
condition: on-failure
read_only: true
tmpfs:
- /tmp:rw,noexec,nosuid,size=64m
networks:
api-network:
driver: bridge
internal-network:
driver: bridgeUse Docker secrets for API keys instead of environment variables or hardcoded values:
# Create a Docker secret
docker secret create api-key /path/to/api-key.txtImplement mutual TLS (mTLS) between services to prevent man-in-the-middle attacks:
version: '3.8'
services:
api:
build: .
networks:
- secure-network
deploy:
labels:
- "traefik.enable=true"
- "traefik.http.middlewares.api-auth.forwardauth=api/auth"
networks:
secure-network:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16Enable Docker's built-in security scanning by integrating with Docker Scout or similar tools to identify vulnerable images before deployment:
# Scan an image for vulnerabilities
docker scout cves <image:tag>Implement runtime monitoring with Docker's audit features:
# Enable Docker audit logging
docker run --security-opt apparmor=docker-default \
--cap-drop ALL \
--read-only \
my-api-serviceFor API authentication, use JWT tokens with short expiration times and implement rate limiting at the container level:
# Rate limiting middleware for Flask
from flask import Flask, request, jsonify
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
app = Flask(__name__)
limiter = Limiter(
get_remote_address,
app=app,
default_limits=["200 per minute", "500 per hour"]
)
@app.route('/api/endpoint')
@limiter.limit("10/minute")
def api_endpoint():
return jsonify(success=True)