Skip to content

Commit b0b9c81

Browse files
committed
Classify special IP ranges before private IPs
1 parent 1772773 commit b0b9c81

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/iocraft/extractors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ def dedupe(indicators: list[Indicator]) -> list[Indicator]:
8585

8686
def classify_ip(address: ipaddress._BaseAddress) -> tuple[str, ...]:
8787
tags = ["network"]
88-
if address.is_private:
89-
tags.append("private-ip")
90-
elif address.is_loopback:
88+
if address.is_loopback:
9189
tags.append("loopback-ip")
9290
elif address.is_multicast:
9391
tags.append("multicast-ip")
9492
elif address.is_reserved:
9593
tags.append("reserved-ip")
9694
elif address.is_unspecified:
9795
tags.append("unspecified-ip")
96+
elif address.is_private:
97+
tags.append("private-ip")
9898
else:
9999
tags.append("public-ip")
100100
return tuple(tags)

tests/test_extractors.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import unittest
44
from pathlib import Path
55

6-
from iocraft.extractors import extract_from_paths, extract_from_text
6+
from iocraft.extractors import classify_ip, extract_from_paths, extract_from_text
7+
import ipaddress
78

89

910
ROOT = Path(__file__).resolve().parents[1]
@@ -37,6 +38,19 @@ def test_dedupe(self) -> None:
3738
domains = [indicator for indicator in indicators if indicator.type == "domain"]
3839
self.assertEqual(len(domains), 1)
3940

41+
def test_ip_classification_prefers_specific_tags(self) -> None:
42+
cases = {
43+
"127.0.0.1": "loopback-ip",
44+
"0.0.0.0": "unspecified-ip",
45+
"224.0.0.1": "multicast-ip",
46+
"240.0.0.1": "reserved-ip",
47+
"10.0.0.1": "private-ip",
48+
"8.8.8.8": "public-ip",
49+
}
50+
for value, expected_tag in cases.items():
51+
with self.subTest(value=value):
52+
self.assertIn(expected_tag, classify_ip(ipaddress.ip_address(value)))
53+
4054

4155
if __name__ == "__main__":
4256
unittest.main()

0 commit comments

Comments
 (0)