Create update_blocklist.py
Browse files
belel-shield/sentinel/scanner/update_blocklist.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
update_blocklist.py
|
| 4 |
+
Belel Shield – Manual Blocklist Updater (with SHA-256 verification)
|
| 5 |
+
Downloads belel-blocklist.json + belel-blocklist.checksums.json,
|
| 6 |
+
verifies integrity, then updates ~/.belel/belel-blocklist.json.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import urllib.request, json, hashlib
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
BASE_URL = "https://raw.githubusercontent.com/TTOPM/be-core-bridge/main/belel-shield/sentinel/blocklists"
|
| 13 |
+
BLOCKLIST_URL = f"{BASE_URL}/belel-blocklist.json"
|
| 14 |
+
CHECKSUM_URL = f"{BASE_URL}/belel-blocklist.checksums.json"
|
| 15 |
+
|
| 16 |
+
DATA_DIR = Path.home() / ".belel"
|
| 17 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 18 |
+
CACHE_FILE = DATA_DIR / "belel-blocklist.json"
|
| 19 |
+
TMP_FILE = DATA_DIR / "belel-blocklist.tmp"
|
| 20 |
+
CHECK_FILE = DATA_DIR / "belel-blocklist.checksums.json"
|
| 21 |
+
LOG_FILE = DATA_DIR / "update.log"
|
| 22 |
+
|
| 23 |
+
def _log(msg: str):
|
| 24 |
+
print(msg)
|
| 25 |
+
with open(LOG_FILE, "a") as f:
|
| 26 |
+
f.write(msg + "\n")
|
| 27 |
+
|
| 28 |
+
def _sha256(path: Path) -> str:
|
| 29 |
+
h = hashlib.sha256()
|
| 30 |
+
with open(path, "rb") as f:
|
| 31 |
+
for chunk in iter(lambda: f.read(8192), b""):
|
| 32 |
+
h.update(chunk)
|
| 33 |
+
return h.hexdigest()
|
| 34 |
+
|
| 35 |
+
def update_blocklist():
|
| 36 |
+
try:
|
| 37 |
+
_log(f"[*] Fetching checksum: {CHECKSUM_URL}")
|
| 38 |
+
urllib.request.urlretrieve(CHECKSUM_URL, CHECK_FILE)
|
| 39 |
+
meta = json.loads(CHECK_FILE.read_text())
|
| 40 |
+
|
| 41 |
+
expected_algo = meta.get("algo")
|
| 42 |
+
expected_hash = meta.get("hash")
|
| 43 |
+
artifact = meta.get("artifact", "belel-blocklist.json")
|
| 44 |
+
|
| 45 |
+
if expected_algo != "sha256" or not expected_hash:
|
| 46 |
+
_log("[!] Invalid checksum file (missing algo/hash). Aborting.")
|
| 47 |
+
return
|
| 48 |
+
|
| 49 |
+
_log(f"[*] Fetching blocklist: {BLOCKLIST_URL}")
|
| 50 |
+
urllib.request.urlretrieve(BLOCKLIST_URL, TMP_FILE)
|
| 51 |
+
|
| 52 |
+
actual_hash = _sha256(TMP_FILE)
|
| 53 |
+
if actual_hash.lower() != expected_hash.lower():
|
| 54 |
+
_log(f"[!] HASH MISMATCH for {artifact}: expected {expected_hash}, got {actual_hash}. Aborting update.")
|
| 55 |
+
TMP_FILE.unlink(missing_ok=True)
|
| 56 |
+
return
|
| 57 |
+
|
| 58 |
+
TMP_FILE.replace(CACHE_FILE)
|
| 59 |
+
data = json.loads(CACHE_FILE.read_text())
|
| 60 |
+
_log(f"[+] Blocklist verified & updated: {len(data.get('ip_ranges', []))} IP ranges, {len(data.get('domains', []))} domains")
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
_log(f"[!] Blocklist update failed: {e}")
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
update_blocklist()
|