بتل کد Battle Codeدارد باز می‌شود…

جواب «Step 3: The Castle Report» چالش 6 از جلسه‌ی 34

سؤال

(سخت‌تر) قدم ۳: بعد از خروجی قدم ۲ گزارش قلعه را چاپ کن. خط اول از متغیرها: castle report: 6 devices, 3 design problems, 3 denied, 2 alerts. امتیاز: 10 - problems - 2 * len(alerts) و اگر کمتر از صفر شد 0؛ چاپ کن score: 3/10. بعد برای هر IP در alerts (به ترتیب) که بخشش internet نیست چاپ کن inside alert: 192.168.1.30 (family). آخر از همه phase 2 complete: well done, network guardian!. خطهای تازه‌ی خروجی:
castle report: 6 devices, 3 design problems, 3 denied, 2 alerts
score: 3/10
inside alert: 192.168.1.30 (family)
phase 2 complete: well done, network guardian!

راهنما

score = 10 - problems - 2 * len(alerts) و if score < 0: score = 0. برای هشدار داخلی: for ip in alerts: و if zone_of(ip) != "internet":.

یک جواب درست

import ipaddress

zones = {
    "family": "192.168.1.0/24",
    "guest": "192.168.50.0/24",
    "smart": "192.168.60.0/24",
}
DEFAULTS = ["admin", "1234", "password", "0000", "12345"]
devices = [
    {"name": "my-laptop", "ip": "192.168.1.10", "kind": "computer", "password": "Kite-Moon-River-7"},
    {"name": "mum-phone", "ip": "192.168.1.11", "kind": "phone", "password": "Tea-Garden-Lamp-3"},
    {"name": "smart-tv", "ip": "192.168.60.4", "kind": "smart", "password": "Owl-Rain-Cup-58"},
    {"name": "door-cam", "ip": "192.168.1.30", "kind": "smart", "password": "admin"},
    {"name": "speaker", "ip": "192.168.60.7", "kind": "smart", "password": "1234"},
    {"name": "guest-phone", "ip": "192.168.50.20", "kind": "phone", "password": "Sky-Bike-Pear-90"},
]
allowed = [
    ("family", "family"),
    ("family", "smart"),
    ("family", "internet"),
    ("guest", "internet"),
    ("smart", "internet"),
]
decoy_ports = [2323]
traffic = [
    ("192.168.1.10", "203.0.113.20", 443),
    ("192.168.50.20", "192.168.1.10", 445),
    ("192.168.1.11", "192.168.60.4", 8008),
    ("192.168.60.7", "192.168.1.10", 22),
    ("203.0.113.66", "192.168.1.30", 2323),
    ("192.168.60.4", "203.0.113.50", 443),
    ("192.168.1.30", "192.168.1.10", 2323),
]

def zone_of(ip):
    for name in zones:
        if ipaddress.ip_address(ip) in ipaddress.ip_network(zones[name]):
            return name
    return "internet"

problems = 0
for d in devices:
    z = zone_of(d["ip"])
    found = []
    if d["kind"] == "smart" and z != "smart":
        found.append("move to the smart zone")
    if d["password"] in DEFAULTS:
        found.append("change the default password")
    if len(found) == 0:
        print(d["name"], "(" + z + "): ok")
    for f in found:
        print(d["name"], "(" + z + "):", f)
    problems = problems + len(found)
print("design problems:", problems)

denied = 0
alerts = []
for src, dst, port in traffic:
    a = zone_of(src)
    b = zone_of(dst)
    if (a, b) in allowed:
        action = "allow"
    else:
        action = "deny"
        denied = denied + 1
    print(action, a, "->", b, "port", port)
    if port in decoy_ports:
        print("alert:", src, "touched decoy port", port)
        alerts.append(src)
print(f"denied: {denied}, alerts: {len(alerts)}")

score = 10 - problems - 2 * len(alerts)
if score < 0:
    score = 0
print(f"castle report: {len(devices)} devices, {problems} design problems, {denied} denied, {len(alerts)} alerts")
print(f"score: {score}/10")
for ip in alerts:
    if zone_of(ip) != "internet":
        print(f"inside alert: {ip} ({zone_of(ip)})")
print("phase 2 complete: well done, network guardian!")

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن