Outbound Traffic Monitor
A small Python tool that watches which processes on my machine talk to the internet, works out which country each connection goes to, and flags anything that looks unusual. Everything stays local in a SQLite file.
Stack: Python, psutil, scikit-learn (IsolationForest), SQLite, ipinfo.io, matplotlib
How it works
flowchart LR
subgraph Capture["Capture (every 5 seconds)"]
direction TB
Conns[Open connections<br/>psutil] --> New[New public<br/>connections only]
New --> Proc[Owning process]
Proc --> Geo[Country lookup<br/>ipinfo.io, cached]
end
DB[(SQLite<br/>traffic_log)]
subgraph Detect["Detect"]
direction TB
Model[IsolationForest<br/>live anomaly score]
Baseline[Per-country baseline<br/>mean + std per day]
Check[check --window 24h]
end
Geo --> DB
DB -- train --> Model
DB -- learn --> Baseline
Model --> Live[Live ALERT<br/>in the monitor]
Baseline --> Check
DB --> Check
Check --> Alerts[(alert_log)]
DB --> Charts[Pie + bar charts]
- Capture: every 5 seconds, list the established connections to public IPs, find the owning process, and look up the remote IP's country. Each connection is logged once when it opens, and loopback and LAN traffic is skipped.
- Risk flag: connections to a small set of high-risk countries (CN, RU, IR, KP) get a flag.
- Live anomaly scoring: after
train, an IsolationForest scores every new connection and the monitor prints an alert for anything unusual. - Baseline checks:
learnworks out each country's normal daily share of traffic, andcheckcompares a time window against it and writes any breaches to an alert table. - Charts: pie and bar charts of connections per country.
How a check decides
Every country seen in the time window goes through three rules. Any rule that fires is recorded as an alert.
flowchart TD
Start([Country's share of traffic<br/>in the window]) --> G{More than 30%<br/>of all traffic?}
G -- yes --> A1[Alert: GLOBAL_THRESHOLD]
G -- no --> R
A1 --> R{High-risk country<br/>and more than 5%?}
R -- yes --> A2[Alert: RISK_COUNTRY]
R -- no --> B
A2 --> B{Baseline on, 3+ days of history,<br/>and above average + 3σ?}
B -- yes --> A3[Alert: BASELINE_SIGMA]
B -- no --> Done([Next country])
A3 --> Done
All three limits are command-line options (--global-thresh, --risk-thresh, --sigma).
Things I learned fixing it
- Log a connection once, not on every poll. Re-logging open connections every 5 seconds made long-lived connections dominate the country percentages.
- Don't send LAN traffic to a geo API. Skipping private IPs and caching lookups keeps the free ipinfo.io limit from running out within minutes.
- Processes disappear mid-scan. A process can exit between listing connections and asking for its name, so that case has to be handled or the monitor crashes.
- One place for file paths. Three scripts had worked out three different database locations, so the CLI was quietly reading an empty database.
- Don't hide failures. Lookup errors used to be swallowed silently. Now they're logged as warnings, so
??in the data has a visible reason.