ids.py share a common set of primitives: a sliding-window counter, a set-based unique-port tracker, an anti-spam throttle, and a CIDR whitelist. This page documents each primitive with the real implementation from the source.
Sliding window counters
Rate-based detectors (SYN flood, DDoS, UDP flood) measure how many packets arrive within a fixed time window. The implementation uses a plain Pythonlist of Unix timestamps appended on every packet, then filtered to keep only entries within the window:
ids.py
ids.py
| Detector | Window | Keyed by |
|---|---|---|
| SYN flood | 500 ms | Source IP |
| DDoS distribuido | 1 s | Destination IP |
| UDP flood | 1 s | Destination IP |
Port scan tracking
Port scan detection does not use a time window. Instead,detectar_escaneo_puertos maintains a set of destination ports per source IP. Because sets automatically deduplicate entries, len(set) always reflects the number of unique ports probed, regardless of how many times each port was hit:
ids.py
iniciar_monitoreo() resets the global puertos_por_ip dictionary.
Throttle anti-spam
A sustained attack generates thousands of matching packets per second. Without throttling,guardar_ataque() would fire thousands of times for the same IP, flooding SQLite, the CSV dataset, and the Telegram channel.
The throttle uses a dictionary that records the last alert timestamp for each IP:
ids.py
advertencias_cont[ip_src] counter still increments on every call that passes the throttle check, providing a cumulative warning count per IP.
Whitelist checking
Thedetectar_exploit and detectar_sql_injection detectors skip packets originating from trusted IPs before applying any pattern matching. Trust is evaluated in two steps:
- Exact-match set —
IPS_CONFIABLEScontains individual IP addresses:
ids.py
- CIDR range match —
ip_en_rangos(ip)tests against a list ofipaddress.ip_networkobjects:
ids.py
any() short-circuits on the first match, so the check is efficient even with a long list of ranges.
Trusted CIDR ranges:
| Range | Provider |
|---|---|
10.0.0.0/8 | RFC 1918 private class A |
172.16.0.0/12 | RFC 1918 private class B |
20.110.205.0/24 | Microsoft Azure |
40.0.0.0/8 | Microsoft Azure |
52.0.0.0/8 | Amazon Web Services |
54.0.0.0/8 | Amazon Web Services |
104.16.0.0/12 | Cloudflare |
140.82.0.0/16 | GitHub |
143.204.0.0/16 | Amazon CloudFront |
34.192.0.0/12 | Google Cloud |
35.192.0.0/12 | Google Cloud |
172.217.0.0/16 | |
2.22.20.0/24 | Akamai |
52.178.17.0/24 | Microsoft Azure |
The whitelist only protects
detectar_exploit and detectar_sql_injection. Rate-based detectors (SYN flood, DDoS, UDP flood) do not skip whitelisted IPs, because a compromised CDN edge node could still participate in a flood.SQL injection regex
The SQL injection detector compiles a single case-insensitive multi-branch regular expression. Each branch targets a distinct injection technique:ids.py
- Length filter: payloads longer than 1,000 bytes are skipped (binary protocols, file uploads).
- Exclusion list: payloads containing common HTTP parameter names (
order=desc,limit=,search=,token=,session=,csrf,user-agent, etc.) are skipped to avoid false positives from normal web traffic.
Exploit port list
ThePUERTOS_EXPLOIT set is defined inline inside detectar_exploit:
ids.py
ACK, PSH, FIN, or other flags represent established session traffic and are ignored — this prevents false positives from legitimate ongoing connections to these services.
| Port | Service | Notable CVEs / campaigns |
|---|---|---|
| 21 | FTP | CVE-2010-4221, anonymous auth abuse |
| 22 | SSH | Brute force, credential stuffing, Mirai |
| 23 | Telnet | Mirai botnet, cleartext credential capture |
| 69 | TFTP | Unauthenticated read/write, firmware injection |
| 135 | MS-RPC | CVE-2003-0352 (DCOM RPC), Blaster worm |
| 139 | NetBIOS-SSN | SMB relay, legacy Windows enumeration |
| 445 | SMB | CVE-2017-0144 (EternalBlue), WannaCry |
| 3389 | RDP | CVE-2019-0708 (BlueKeep), DejaBlue |
| 5900 | VNC | CVE-2006-2369, authentication bypass |