Skip to content
TCP Connection Spike Diagnosis: Distinguish Attacks from Normal Traffic Surges
Operations 37 min read

TCP Connection Spike Diagnosis: Distinguish Attacks from Normal Traffic Surges

This comprehensive guide teaches system administrators how to diagnose sudden TCP connection spikes by analyzing connection states, source IP distributions, queue overflows, and traffic patterns to distinguish between normal traffic growth and attacks like SYN floods, CC attacks, or connection leaks, with concrete Linux commands and mitigation strategies.

Raymond Ops
Raymond Ops
Raymond Ops

Problem Background

Production monitoring alerts show a server's TCP connections surging from thousands to tens or hundreds of thousands. Operators must quickly determine: is this normal business growth or an attack? If an attack, what type? What are the source IPs to block? Can current resources handle the load, or is scaling/rate-limiting needed?

Misjudgment risks include blocking legitimate users, failing to block attackers causing crashes, or wasteful over-scaling.

Core Knowledge

TCP Connection State Machine

Connections traverse states during three-way handshake (SYN_SENT → SYN_RECV → ESTABLISHED) and four-way teardown (FIN_WAIT1/2 → CLOSE_WAIT → TIME_WAIT). Key states: LISTEN (server waiting), SYN_RECV (half-open), ESTABLISHED (active), CLOSE_WAIT (passive close pending app), TIME_WAIT (active close waiting 2MSL).

Abnormal Traffic State Signatures

SYN Flood: Massive SYN_RECV; attacker sends SYN but no ACK, exhausting half-connection queue.

CC Attack: Massive ESTABLISHED; attacker completes handshakes and sends HTTP requests, exhausting CPU/memory/bandwidth.

Slow HTTP Attack: Many ESTABLISHED with tiny traffic; attacker sends headers slowly, holding connections open.

Connection Leak: Many CLOSE_WAIT or TIME_WAIT; app fails to close connections properly.

Linux TCP Queues

Two queues manage connections: SYN Queue (half-connection) stores SYN_RECV entries, size controlled by net.ipv4.tcp_max_syn_backlog; overflow drops new SYNs. Accept Queue (full-connection) stores completed handshakes awaiting accept(), size = min( listen() backlog, net.core.somaxconn); overflow behavior set by net.ipv4.tcp_abort_on_overflow (drop or RST). Detect overflows via netstat -s | grep "SYNs to LISTEN" and netstat -s | grep "overflowed".

Conntrack Connection Tracking

iptables/nftables track every connection in a conntrack table (one entry per connection). Max entries set by net.netfilter.nf_conntrack_max; table full logs nf_conntrack: table full and drops new connections. Check usage: conntrack -C (current), sysctl net.netfilter.nf_conntrack_max (max).

Common Attack Tool Signatures

hping3: Crafts TCP/UDP/ICMP; used for SYN Flood; spoofed source IPs, random source ports.

slowhttptest: Simulates slow HTTP; identifiable User-Agent.

ab/wrk/vegeta: HTTP load tools; User-Agent contains tool name.

Botnets: Distributed; many IPs with similar request patterns.

Quick Diagnosis Flow (6 Steps)

1. Connection Count & State Distribution

ss -tan | awk '{print $1}' | sort | uniq -c | sort -rn

Normal: mostly ESTABLISHED (~3500), some TIME_WAIT (~500). SYN Flood: 50,000 SYN_RECV. CC Attack: 80,000 ESTABLISHED. Thresholds: SYN_RECV >10k → SYN Flood; ESTABLISHED >2× baseline → CC or growth; CLOSE_WAIT >5k → app leak; TIME_WAIT >30k → optimize.

2. Connection Growth Rate

watch -n 1 'ss -tan | wc -l'

Normal: ~20/sec. Attack: thousands/sec. Threshold: >1000/sec → likely attack; <100/sec → normal fluctuation.

3. Source IP Distribution

ss -tan | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -20

Normal: dispersed (tens per IP). Single-IP attack: one IP with 30k+. Distributed: hundreds of IPs each ~500. Thresholds: single IP >1000 → single-source; top 10 IPs >80% total → distributed; top 10 <20% → normal or massive botnet.

4. Target Port Analysis

ss -tan | awk '{print $4}' | cut -d: -f2 | sort | uniq -c | sort -rn | head -10

Normal: concentrated on business ports (80, 443). Port scan: many ports with similar low counts.

5. Queue Overflow Check

netstat -s | grep -E "SYNs to LISTEN|overflowed|dropped"

SYNs dropped >10k/min → SYN Flood. Listen queue overflow >5k/min → accept queue full (slow app or CC).

6. Conntrack Usage

current=$(conntrack -C); max=$(sysctl -n net.netfilter.nf_conntrack_max); echo "scale=2; $current * 100 / $max" | bc

Usage >90% → overload/attack. "table full" in dmesg → new connections rejected.

Deep Analysis: Differentiating Normal vs Attack

Connection Lifecycle Analysis

ss -tan | awk '{print $5,$1}' | awk -F: '{print $1,$NF}' | sort | uniq -c | sort -rn | head -20

Normal: mostly ESTABLISHED, few TIME_WAIT, SYN_RECV <1%. SYN Flood: massive SYN_RECV, few ESTABLISHED. CC: massive ESTABLISHED, short-lived connections.

HTTP Request Pattern Analysis (Nginx logs)

tail -1000 /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20

Normal: diverse User-Agents, URLs match business, random intervals. CC: single/forged User-Agent, URLs concentrated on heavy endpoints (e.g., /api/search), regular intervals. Malicious crawlers: crawler keywords in UA, many 404s, ignore robots.txt.

Traffic Volume Analysis

iftop -i eth0

Normal: traffic proportional to connections, bidirectional. SYN Flood: many connections, tiny traffic (only SYN), mostly inbound. CC: high connections, high traffic (full HTTP). Slow attack: high connections, low traffic, long duration.

Source IP Geo/ASN Analysis

geoiplookup 203.0.113.50

Normal: IPs from business regions, legitimate ISPs/cloud. Abnormal: concentrated in obscure countries, data-center/proxy ASNs, listed on AbuseIPDB.

TCP Flag Distribution

tcpdump -i eth0 -nn -c 1000 'tcp[tcpflags] & tcp-syn != 0' | wc -l

Normal: SYN ≈ ACK. SYN Flood: SYN ≫ ACK. Reset attack: massive RST.

Mitigation Measures

Emergency IP Blocking

iptables single: iptables -I INPUT -s 203.0.113.50 -j DROP. Batch via ipset (better performance): create ipset create blacklist hash:ip, add IPs, then iptables -I INPUT -m set --match-set blacklist src -j DROP.

Rate Limiting & Connection Limits

iptables new-connection rate:

iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --set

+

iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --update --seconds 1 --hitcount 20 -j DROP

. iptables concurrent limit:

iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 100 --connlimit-mask 32 -j REJECT

. Nginx limit_req/limit_conn: define zones in http block, apply in location with burst/nodelay.

Kernel TCP Parameter Hardening

sysctl -w net.ipv4.tcp_syncookies=1          # SYN Flood defense
sysctl -w net.ipv4.tcp_max_syn_backlog=8192  # larger SYN queue
sysctl -w net.ipv4.tcp_synack_retries=2      # fewer SYN-ACK retries
sysctl -w net.ipv4.tcp_syn_retries=2         # fewer SYN retries
sysctl -w net.ipv4.tcp_tw_reuse=1            # reuse TIME_WAIT
sysctl -w net.netfilter.nf_conntrack_max=1000000  # larger conntrack table
# persist in /etc/sysctl.conf

Application-Layer Protection

CAPTCHA/JS Challenge for suspicious IPs.

CDN/DDoS services: Cloudflare, Akamai, AWS Shield, Alibaba Cloud.

WAF: ModSecurity, Nginx WAF, cloud WAF.

Monitoring & Alerting (Prometheus)

groups:
- name: tcp_alerts
  rules:
  - alert: TCPConnectionSpike
    expr: node_netstat_Tcp_CurrEstab > 10000
    for: 1m
    labels: { severity: warning }
    annotations: { summary: "TCP连接数异常(当前 {{ $value }})" }
  - alert: SYNFloodSuspected
    expr: rate(node_netstat_Tcp_PassiveOpens[1m]) > 1000
    for: 2m
    labels: { severity: critical }
    annotations: { summary: "疑似SYN Flood攻击(被动打开速率 {{ $value }}/s)" }
  - alert: ConntrackTableFull
    expr: node_nf_conntrack_entries / node_nf_conntrack_entries_limit > 0.9
    for: 5m
    labels: { severity: warning }
    annotations: { summary: "Conntrack表使用率超过90%" }

Real-World Case Studies

Case 1: Single-Source SYN Flood

Alert: connections 3k → 50k. ss showed 48k SYN_RECV. Source IP analysis: 45k from 203.0.113.50. netstat -s showed 500k SYNs dropped. Action: iptables -I INPUT -s 203.0.113.50 -j DROP, enabled SYN cookies, raised tcp_max_syn_backlog to 16384. Result: connections normalized to 3k within 1 minute.

Case 2: Distributed CC Attack

Symptoms: slow responses, CPU 90%, 80k connections. ss: 75k ESTABLISHED. Top 200 IPs each 300-500 connections, accounting for 90% total. Nginx logs: all targeting /api/search, identical User-Agent Mozilla/5.0 (compatible). Action: ipset block IPs >200 connections, Nginx limit_req on /api/search (zone=search_zone burst=5 nodelay), enabled Cloudflare. Result: connections dropped to 10k, CPU to 30%.

Case 3: Connection Leak (CLOSE_WAIT Pileup)

Pattern: connections grow until app restart, then recur. ss showed 30k CLOSE_WAIT. ss -tanp state close-wait revealed 28k owned by Java PID 12345. Root cause: HttpClient not calling response.close() or client.close(). Fix: use try-with-resources to ensure CloseableHttpResponse auto-closes.

Prevention Measures

Baseline Monitoring: Record 7-day average node_netstat_Tcp_CurrEstab; alert if current > 2× baseline for 5m.

Regular Stress Testing: wrk -t 10 -c 1000 -d 60s http://example.com/; test SYN Flood defense with hping3 -S -p 80 --flood in staging.

Automated Response: Alertmanager webhook → Python Flask script parsing alert labels → ipset add blacklist $ip.

Cloud DDoS Protection: Use Cloudflare (free tier), AWS Shield, Alibaba/Tencent Cloud for large attacks.

Network Architecture: Place web behind CDN/LB, use Anycast, isolate critical services, whitelist management ports.

Advanced Analysis Tools

tcpdump Packet Capture

# Capture 1000 pure SYN packets
 tcpdump -i eth0 -nn -c 1000 'tcp[tcpflags] & tcp-syn != 0 and tcp[tcpflags] & tcp-ack == 0' -w syn.pcap
 # Analyze source IP distribution
 tcpdump -r syn.pcap -nn | awk '{print $3}' | cut -d. -f1-4 | sort | uniq -c | sort -rn
 # Check source port randomness (SYN Flood trait)
 tcpdump -r syn.pcap -nn | awk '{print $3}' | cut -d. -f5 | cut -d: -f1 | sort | uniq | wc -l

ss/netstat Advanced Filters

ss -tan state syn-recv      # SYN_RECV only
 ss -tan state established   # ESTABLISHED only
 ss -tan state close-wait    # CLOSE_WAIT only
 ss -tan '( dport = :80 or sport = :80 )'  # port 80
 ss -tano                    # show timers
 ss -tanm                    # show memory usage

bpftrace/eBPF Kernel-Level Analysis

# New connection rate per process
 bpftrace -e 'kprobe:tcp_v4_connect { @[comm] = count(); }'
 # SYN receive rate
 bpftrace -e 'kprobe:tcp_v4_rcv /arg1->th.syn/ { @syn_per_sec = count(); }'
 # Conntrack insertion failures
 bpftrace -e 'kprobe:nf_conntrack_in { @failures[retval] = count(); }'

ngrep Application-Layer Inspection

ngrep -W byline -q 'GET|POST' tcp port 80
 ngrep -W byline -q 'User-Agent: ' tcp port 80

Advanced Protection Techniques

ML-Based Anomaly Detection

Features: connections/min, source IP entropy, URL distribution, UA diversity, connection duration distribution. Tools: Elastic Security, Wazuh, Suricata with ML.

eBPF/XDP Kernel-Level Defense

XDP drops malicious packets at NIC driver, far faster than iptables. Example XDP program parses Ethernet/IP/TCP headers, checks SYN flag, can implement SYN cookies or rate limiting per IP in kernel.

BGP Flowspec Upstream Mitigation

For massive DDoS, advertise FlowSpec rules to upstream ISP to discard attack traffic at their edge. Requires ISP support.

Fail2Ban Automated Banning

Monitor Nginx logs; jail config with maxretry=100 in findtime=60s, bantime=3600, action iptables-multiport. Filter matches HTTP request lines.

Chaos Engineering Drills

Simulate SYN Flood

hping3 -S -p 80 --flood --rand-source 192.168.1.10
 watch -n 1 'ss -tan | awk "{print $1}" | sort | uniq -c | sort -rn'
 sysctl net.ipv4.tcp_syncookies
 netstat -s | grep "SYN cookies"

Simulate CC Attack

ab -n 100000 -c 1000 http://192.168.1.10/api/search
 wrk -t 10 -c 5000 -d 60s --latency http://192.168.1.10/
 tail -f /var/log/nginx/error.log | grep "limiting requests"

Validate Defenses

# Test iptables rate limit
 iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --set
 iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --update --seconds 1 --hitcount 10 -j DROP
 for i in {1..20}; do curl -I http://localhost/; done
 # Test conntrack exhaustion
 sysctl -w net.netfilter.nf_conntrack_max=1000
 ab -n 10000 -c 100 http://localhost/
 dmesg | grep "nf_conntrack: table full"
 sysctl -w net.netfilter.nf_conntrack_max=1000000

Monitoring Dashboard (Grafana)

Panel 1: TCP State Distribution

{ "targets": [ { "expr": "node_netstat_Tcp_CurrEstab", "legendFormat": "ESTABLISHED" }, { "expr": "sum(node_sockstat_TCP_tw)", "legendFormat": "TIME_WAIT" } ] }

Panel 2: Top 10 Source IPs (custom exporter)

# tcp_exporter.py collects via `ss -tan | awk ...` and exposes `tcp_top_ips_connections{ip}` gauge

Panel 3: Connection Growth Rate

{ "expr": "rate(node_netstat_Tcp_PassiveOpens[1m])", "legendFormat": "被动打开速率" }

DingTalk Alert Template

{ "msgtype": "markdown", "markdown": { "title": "TCP连接异常告警", "text": "## TCP连接异常

**服务器**: {{ $labels.instance }}

**当前连接数**: {{ $value }}

**告警时间**: {{ $activeAt }}

[查看详情](http://grafana/d/tcp-monitor)" } }

Compliance & Security

Log Retention

Per security compliance (e.g., China MLPS), retain full network flow logs ≥6 months. Use Suricata/Zeek for metadata, ELK/Splunk for centralized storage.

Attack Forensics

Record: time window, source IPs/CIDRs, attack type, target port/URL, traffic volume, packet captures (pcap). Tools: Whois, AbuseIPDB, VirusTotal.

Incident Response Process

Detect (0-5m): Receive alert, verify not false positive.

Triage (5-10m): Check state distribution, source IPs, classify attack.

Emergency Block (10-15m): Ban top attacker IPs, enable rate limits.

Deep Analysis (15-30m): Packet capture, app logs, impact assessment.

Continuous Tuning (30m+): Adjust defenses, kernel params, engage ISP/cloud DDoS.

Post-Mortem: Document attack signatures, update rules, improve monitoring, rehearse response.

Summary Checklists

Investigation Checklist

[ ] Connection state distribution → identify anomalous states

[ ] Connection growth rate → gauge attack intensity

[ ] Source IP distribution → locate attack origin

[ ] Target ports → determine attack target

[ ] Queue overflow stats → assess impact scope

[ ] Conntrack usage → evaluate system resources

[ ] HTTP logs (if web) → analyze request patterns

[ ] System resources (CPU, mem, bandwidth) → capacity check

Response Checklist

[ ] Ban top attacker IPs (iptables/ipset)

[ ] Enable SYN Cookies

[ ] Configure connection rate limits

[ ] Configure concurrent connection limits

[ ] Tune kernel TCP parameters

[ ] Apply Nginx rate limiting

[ ] Activate CDN/DDoS protection service

[ ] Preserve attack logs & evidence

Prevention Checklist

[ ] Establish TCP connection baseline

[ ] Configure monitoring alerts

[ ] Regular stress testing & defense validation

[ ] Deploy automated response scripts

[ ] Use cloud DDoS protection services

[ ] Harden network architecture

[ ] Periodic review & update of defense policies

[ ] Regular incident response drills

Persistence Checklist

[ ] Save iptables rules to /etc/sysconfig/iptables [ ] Save sysctl params to /etc/sysctl.conf [ ] Save Nginx config to config files

[ ] Save ipset rules to startup scripts

[ ] Configure boot-time loading of all defenses

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPNetwork MonitoringDDoS MitigationiptablesCC AttackLinux OperationsConnection LeakSYN Flood
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.