Files
BigBodyCobainandClaude Opus 5.5 9295049025 Add C2IntelFeeds Cobalt Strike servers to the malware C2 layer
C2IntelFeeds (github.com/drb-ra/C2IntelFeeds, CC BY-NC-SA 4.0) lists
likely Cobalt Strike team servers from the last 30 days. Each IP is
located with the same cached RIPEstat lookup as ViriBack, and both feeds
share the per-poll lookup budget. Credited in the Osiris notice and the
README source table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 22:25:12 -07:00

187 lines
7.8 KiB
Python

"""Malware C2 layer: Feodo Tracker plus ViriBack and C2IntelFeeds hosts located by RIPEstat."""
from datetime import date
import pytest
from services.fetchers import malware
class _Resp:
def __init__(self, status_code, payload=None, text=""):
self.status_code = status_code
self._payload = payload
self.text = text
def json(self):
return self._payload
FEODO = [
{"ip_address": "1.2.3.4", "dst_port": 443, "malware": "QakBot", "status": "offline",
"first_seen": "2026-01-02 10:00:00", "last_online": "2026-03-05", "country": "US"},
{"ip_address": "5.6.7.8", "dst_port": 8080, "malware": "Emotet", "status": "offline",
"first_seen": "2026-02-01 10:00:00", "last_online": "2026-03-12", "country": "GB"},
{"ip_address": "9.9.9.9", "dst_port": 80, "malware": "Pikabot", "status": "offline",
"first_seen": "2026-02-01 10:00:00", "last_online": "2026-03-20", "country": "ZZ"},
]
VIRIBACK = (
"Family,URL,IP,FirstSeen\n"
"Supershell,http://43.133.248.198:8888/supershell/login/,43.133.248.198,14-11-2026\n"
"Chaos,http://157.245.82.98:8080/,157.245.82.98,11-11-2026\n"
"Lunex,https://cel2esia.top/login,172.64.80.1,09-11-2026\n"
"Romulus,http://towerrush.cam/,91.92.43.239,05-11-2026\n"
"Romulus,http://towerrush.cam/login,91.92.43.239,06-11-2026\n"
"AsyncRAT,http://10.0.0.5/,10.0.0.5,10-11-2026\n"
"Amadey,http://45.1.2.3/panel,45.1.2.3,01-01-2025\n"
"Broken,http://45.9.9.9/,not-an-ip,12-11-2026\n"
"Broken,http://45.9.9.8/,45.9.9.8,someday\n"
)
C2INTEL = (
"#ip,port,ioc\n"
"1.117.74.144,18443,Possible Cobaltstrike C2 IP\n"
"1.117.74.144,443,Possible Cobaltstrike C2 IP\n"
"104.21.1.1,443,Possible Cobaltstrike C2 IP\n"
"192.168.1.1,443,Possible Cobaltstrike C2 IP\n"
"1.12.253.219,99999,Possible Cobaltstrike C2 IP\n"
"garbage\n"
)
def _geo(lat, lng, country, city):
return {"data": {"located_resources": [{"locations": [
{"latitude": lat, "longitude": lng, "country": country, "city": city}]}]}}
GEO = {
"43.133.248.198": _geo(37.56, 126.97, "KR", "Seoul"),
"157.245.82.98": _geo(40.79, -74.02, "US", "North Bergen"),
"91.92.43.239": {"data": {"located_resources": [{"locations": []}]}},
"1.117.74.144": _geo(31.23, 121.47, "CN", "Shanghai"),
}
@pytest.fixture(autouse=True)
def _fresh_caches(monkeypatch):
monkeypatch.setattr(malware, "_feed_cache", {})
monkeypatch.setattr(malware, "_geo_cache", {})
monkeypatch.setitem(malware.latest_data, "malware_threats", {})
monkeypatch.setattr(malware, "is_any_active", lambda *_: True)
def _serve(monkeypatch, feodo=None, viriback=None, geo=None, c2intel=None):
urls = []
def fake_fetch(url, *args, **kwargs):
urls.append(url)
if url == malware.FEODO_URL:
return _Resp(200, feodo) if feodo is not None else _Resp(503)
if url == malware.VIRIBACK_URL:
return _Resp(200, text=viriback) if viriback is not None else _Resp(503)
if url == malware.C2INTEL_URL:
return _Resp(200, text=c2intel) if c2intel is not None else _Resp(503)
ip = url.split("resource=")[1].split("&")[0]
payload = (geo or {}).get(ip)
return _Resp(200, payload) if payload is not None else _Resp(503)
monkeypatch.setattr(malware, "fetch_with_curl", fake_fetch)
return urls
def test_parse_viriback_groups_recent_public_ips():
hosts = malware.parse_viriback(VIRIBACK, date(2026, 11, 18))
# Cloudflare fronts, private addresses, rows past the window and bad rows are dropped.
assert [h["ip"] for h in hosts] == ["43.133.248.198", "157.245.82.98", "91.92.43.239"]
supershell, _, romulus = hosts
assert supershell["port"] == 8888
assert romulus["listings"] == 2
assert romulus["first"] == date(2026, 11, 5) and romulus["last"] == date(2026, 11, 6)
assert romulus["families"] == ["Romulus"]
assert romulus["port"] == 80
def test_parse_c2intel_groups_ports_by_ip():
hosts = malware.parse_c2intel(C2INTEL)
# Cloudflare fronts, private addresses, bad ports and bad lines are dropped.
assert hosts == [{"ip": "1.117.74.144", "ports": [18443, 443], "listings": 2, "family": "Cobalt Strike"}]
def test_c2intel_servers_are_plotted(monkeypatch):
_serve(monkeypatch, feodo=FEODO, viriback=VIRIBACK, geo=GEO, c2intel=C2INTEL)
threats = malware.fetch_malware_threats()
servers = [t for t in threats if t["threat_type"] == "c2_server"]
assert [(t["ip"], t["port"], t["city"], t["malware"]) for t in servers] == [
("1.117.74.144", 18443, "Shanghai", "Cobalt Strike")]
payload = malware.latest_data["malware_threats"]
assert payload["source"] == "abuse.ch Feodo Tracker + ViriBack C2 Tracker + C2IntelFeeds"
# C2IntelFeeds rows carry no date, so they do not move newest_seen.
assert payload["newest_seen"] == "2026-11-14"
def test_both_feeds_are_plotted_and_unlocated_hosts_dropped(monkeypatch):
_serve(monkeypatch, feodo=FEODO, viriback=VIRIBACK, geo=GEO)
threats = malware.fetch_malware_threats()
panels = [t for t in threats if t["threat_type"] == "c2_panel"]
assert {t["threat_type"] for t in threats} == {"botnet_c2", "c2_panel"}
assert [t["ip"] for t in panels] == ["43.133.248.198", "157.245.82.98"]
assert panels[0]["lat"] == 37.56 and panels[0]["city"] == "Seoul" and panels[0]["malware"] == "Supershell"
payload = malware.latest_data["malware_threats"]
assert payload["source"] == "abuse.ch Feodo Tracker + ViriBack C2 Tracker"
assert payload["newest_seen"] == "2026-11-14"
def test_feeds_and_locations_are_cached(monkeypatch):
urls = _serve(monkeypatch, feodo=FEODO, viriback=VIRIBACK, geo=GEO, c2intel=C2INTEL)
malware.fetch_malware_threats()
first = len(urls)
malware.fetch_malware_threats()
# The second poll only asks Feodo again: both feeds and every answered lookup are reused.
assert urls[first:] == [malware.FEODO_URL]
def test_geolocation_is_budgeted_per_poll(monkeypatch):
rows = "".join(f"Fam,http://45.0.0.{i}/,45.0.0.{i},10-11-2026\n" for i in range(1, 11))
geo = {f"45.0.0.{i}": _geo(50.0, 8.0, "DE", "Frankfurt") for i in range(1, 11)}
monkeypatch.setattr(malware, "GEO_LOOKUPS_PER_RUN", 4)
_serve(monkeypatch, feodo=[], viriback="Family,URL,IP,FirstSeen\n" + rows, geo=geo)
assert len(malware.fetch_malware_threats()) == 4
assert len(malware.fetch_malware_threats()) == 8
assert len(malware.fetch_malware_threats()) == 10
def test_geolocation_budget_is_shared_by_both_feeds(monkeypatch):
viriback = "Family,URL,IP,FirstSeen\n" + "".join(
f"Fam,http://45.0.0.{i}/,45.0.0.{i},10-11-2026\n" for i in range(1, 4))
c2intel = "".join(f"46.0.0.{i},443,Possible Cobaltstrike C2 IP\n" for i in range(1, 4))
geo = {f"{p}.0.0.{i}": _geo(50.0, 8.0, "DE", "Frankfurt") for p in (45, 46) for i in range(1, 4)}
monkeypatch.setattr(malware, "GEO_LOOKUPS_PER_RUN", 4)
_serve(monkeypatch, feodo=[], viriback=viriback, geo=geo, c2intel=c2intel)
first = malware.fetch_malware_threats()
assert [t["threat_type"] for t in first] == ["c2_panel"] * 3 + ["c2_server"]
assert len(malware.fetch_malware_threats()) == 6
def test_feodo_still_plots_when_viriback_fails(monkeypatch):
_serve(monkeypatch, feodo=FEODO, viriback=None)
threats = malware.fetch_malware_threats()
payload = malware.latest_data["malware_threats"]
# ZZ has no centroid, so its later date is not plotted and not counted.
assert {t["threat_type"] for t in threats} == {"botnet_c2"}
assert payload["total"] == 2
assert payload["source"] == "abuse.ch Feodo Tracker"
assert payload["newest_seen"] == "2026-03-12"
def test_newest_seen_is_none_when_every_feed_fails(monkeypatch):
_serve(monkeypatch)
malware.fetch_malware_threats()
payload = malware.latest_data["malware_threats"]
assert payload["total"] == 0
assert payload["newest_seen"] is None