Production-grade guide to attack surface management covering architecture patterns, implementation strategies, testing approaches, and operational best practices for enterprise engineering teams.
Attack Surface Management (ASM) is the continuous process of discovering, mapping, and reducing the attack surface of an organisation’s digital assets. It matters when systems are complex, change frequently, and security is not just a gate but an ongoing practice—especially in environments where new services, APIs, and endpoints appear daily, and assets are forgotten or misconfigured.
The first step in ASM is identifying all assets that are exposed to external or internal threats. This includes public IPs, DNS names, open ports, active services, and deployed applications. A discovery process must run continuously, not just at release time.
To initiate discovery, use a combination of network and service-level probes. Start with a basic scan using nmap with aggressive timing and service detection:
nmap -sS -sT -A -p- -oN discovery-results.txt 192.168.1.0/24
This command performs TCP SYN and connect scans, enables OS and service detection (-A), scans all ports (-p-), and saves output in a standard format. The resulting discovery-results.txt file contains host status, open ports, service versions, and OS guesses.
For larger environments, automate discovery with masscan for rapid, high-throughput scanning:
masscan -p 1-65535 --rate 10000 -oL scan-results.json 10.0.0.0/16
This command uses a rate of 10,000 packets per second, scans all ports, and outputs results in JSON. The output can be parsed to extract IPs, ports, and timestamps for further processing.
Once discovered, assets must be normalized. Each asset should be represented as a record with fields: hostname, ip, port, protocol, service, version, status, and last_seen. A common format is a JSON array:
[
{
"hostname": "api.example.com",
"ip": "192.0.2.1",
"port": 443,
"protocol": "tcp",
"service": "http",
"version": "nginx/1.18.0",
"status": "up",
"last_seen": "2024-05-10T14:32:15Z"
},
{
"hostname": "auth.example.com",
"ip": "192.0.2.2",
"port": 8080,
"protocol": "tcp",
"service": "gunicorn",
"version": "20.1.0",
"status": "up",
"last_seen": "2024-05-10T14:32:15Z"
}
]
This normalized data is the foundation for all subsequent ASM activities. It is consumed by other services, stored in a database, and used as input to monitoring and alerting systems.
With a list of discovered assets, the next step is to build a graph of interconnections and dependencies. Each asset may expose multiple endpoints, APIs, or services. Mapping involves understanding not only what is exposed but also how assets relate.
Use curl to probe each discovered endpoint and extract metadata. For example, to discover REST API endpoints from a web server:
curl -s -H "Accept: application/json" http://api.example.com/ | jq -r '.endpoints[] | .path, .method'
This command fetches the root of an API, parses the JSON response, and extracts each endpoint’s path and method. The output can be enriched with additional metadata like authentication type, rate limits, and versioning.
For API discovery, use OpenAPI (Swagger) specifications. Retrieve and parse an OpenAPI spec to extract all paths, operations, and parameters:
curl -s http://api.example.com/swagger.json | jq '
.paths | to_entries[] | {
path: .key,
methods: .value | to_entries[] | .key,
parameters: .value.parameters // []
}'
This yields a structured view of every API endpoint and its characteristics. Combine this with service discovery data to build a complete map.
Map relationships between services using HTTP headers and metadata. For instance, a service may set X-Forwarded-For, X-Request-ID, or Link headers that reveal upstream dependencies. Extract these with a script:
#!/bin/bash
set -e
while read -r url; do
echo "Analyzing $url"
response=$(curl -s -H "User-Agent: ASM Discovery Agent/1.0" -I "$url")
# Extract upstream services from headers
echo "$response" | grep -E '^Link:' | sed 's/^Link: //'
echo "$response" | grep -E '^X-Forwarded-For:' | sed 's/^X-Forwarded-For: //'
echo "$response" | grep -E '^X-Request-ID:' | sed 's/^X-Request-ID: //'
done < assets.txt
This script consumes a list of URLs and outputs header-based relationships. The output can be ingested into a graph database like Neo4j or stored in a relational database as edge records.
An attack surface is dynamic. New assets appear, old ones become stale, services evolve, and configurations drift. Tracking changes requires periodic discovery runs and delta analysis.
Use diff to compare two discovery snapshots:
diff -u discovery-v1.json discovery-v2.json
This generates a unified diff showing new, removed, and modified assets. For automation, process the diff and generate a changelog:
#!/bin/bash
set -e
TEMP_DIR=$(mktemp -d)
trap 'rm -rf $TEMP_DIR' EXIT
# Extract key fields from each JSON
jq -r '.[] | .hostname, .ip, .port, .status' discovery-v1.json > $TEMP_DIR/v1-keys
jq -r '.[] | .hostname, .ip, .port, .status' discovery-v2.json > $TEMP_DIR/v2-keys
# Find new assets
comm -23 <(sort $TEMP_DIR/v1-keys) <(sort $TEMP_DIR/v2-keys) | paste - - - - | while IFS=$'\t' read -r host ip port status; do
echo "NEW: $host ($ip):$port - $status"
done
# Find removed assets
comm -13 <(sort $TEMP_DIR/v1-keys) <(sort $TEMP_DIR/v2-keys) | paste - - - - | while IFS=$'\t' read -r host ip port status; do
echo "REMOVED: $host ($ip):$port - $status"
done
# Find modified assets
comm -12 <(sort $TEMP_DIR/v1-keys) <(sort $TEMP_DIR/v2-keys) | paste - - - - | while IFS=$'\t' read -r host ip port status; do
echo "MODIFIED: $host ($ip):$port - $status"
done
This script identifies new, removed, and modified assets between two snapshots. It is idempotent and can be scheduled via cron or systemd.
Track changes in a database table asset_history with columns: asset_id, hostname, ip, port, status, timestamp, change_type (new, removed, modified). Each discovery run appends to this table.
Once the surface is mapped and tracked, the focus shifts to reducing it—removing unneeded assets, hardening configurations, and enforcing policies.
A common failure is misconfigured TLS. Use openssl to test TLS configurations across services:
openssl s_client -connect api.example.com:443 -servername api.example.com -servername auth.example.com < /dev/null
This command connects to a TLS endpoint and outputs the entire handshake, certificate chain, and cipher suite details. Parse the output to verify:
TLS_AES_256_GCM_SHA512)A silent failure occurs when openssl s_client does not use the correct servername or verify options, leading to undetected TLS misconfigurations. Always include -servername and -verify 1 to validate the certificate chain.
To enforce security policies, define a JSON schema for required configurations. For example, a minimal TLS policy for public APIs:
{
"tls": {
"minimum_version": "1.2",
"ciphers": [
"TLS_AES_256_GCM_SHA512",
"TLS_CHACHA20_POLY1305_SHA256",
"TLS_AES_128_GCM_SHA256"
],
"hsts": true,
"hsts_max_age": 31536000,
"include_subdomains": true,
"ocsp_stapling": true,
"certificate_authority": "Let's Encrypt"
}
}
Validate this schema against real service configurations using jq and jsonschema:
jq -c '{
"hostname": .hostname,
"ip": .ip,
"port": .port,
"tls": {
"minimum_version": .tls_version,
"ciphers": .ciphers,
"hsts": .hsts,
"hsts_max_age": .hsts_max_age,
"include_subdomains": .hsts_include_subdomains,
"ocsp_stapling": .ocsp_stapling
}
}' assets.json | \
jsonschema -i - - < tls-policy.json
This command converts asset data into the expected format and validates it against the policy schema. Any deviation produces an error message:
{
"message": "Validation failed",
"errors": [
{
"instance": {
"hostname": "api.example.com",
"ip": "192.0.2.1",
"port": 443,
"tls": {
"minimum_version": "1.1",
"ciphers": [
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"
],
"hsts": true,
"hsts_max_age": 15768000,
"include_subdomains": false,
"ocsp_stapling": false
}
},
"schema": {
"type": "object",
"properties": {
"tls": {
"type": "object",
"properties": {
"minimum_version": {
"enum": [
"1.2",
"1.3"
]
},
"ciphers": {
"items": {
"enum": [
"TLS_AES_256_GCM_SHA512",
"TLS_CHACHA20_POLY1305_SHA256",
"TLS_AES_128_GCM_SHA256"
]
}
}
}
}
}
},
"error": "Property minimum_version must be one of [1.2, 1.3]"
}
]
}
This error highlights that 1.1 is not acceptable for minimum_version, and TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 is missing from the required ciphers list.
ASM is not a one-off task. It must be maintained through automation and tooling.
Create a scan service that runs every 4 hours:
# /etc/systemd/system/asm-scan.service
[Unit]
Description=Attack Surface Management Discovery
After=network.target
[Service]
Type=oneshot
User=asm
Environment=PATH=/usr/local/bin:/usr/bin:/bin
ExecStart=/opt/asm/bin/discovery.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
This systemd service runs the discovery script and logs to journalctl. The script combines nmap, masscan, curl, and jq into a single pipeline that outputs normalized JSON and updates the central database.
Use git to version the ASM configuration. Store discovery scripts, policy schemas, and change detection logic in a repository:
# .asm/config/tls-policy.json
# .asm/scripts/discovery.sh
# .asm/policies/production.json
# .asm/README.md
Track changes, review PRs, and roll back configurations when issues arise.
nmap scans only the load balancer’s IP, not the backend servers. Solution: run discovery on both front-end and backend networks.asset_history table to identify assets with no discovery in the last 30 days and flag them for review.hostname, another uses fqdn, and a third uses domain. Solution: normalize fields during discovery and use a canonical schema.changes table for audit and alerting.ASM is not just a set of tools but a practice: discover, map, track, reduce, and repeat. It is the backbone of continuous security visibility in a complex, evolving environment.
This page was rewritten on 12 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy