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

جواب «Step 2: Guard» چالش 5 از جلسه‌ی 34

سؤال

(سخت‌تر) قدم ۲: بعد از خروجی قدم ۱، برای هر ارتباط در traffic (هر کدام (فرستنده، گیرنده، پورت)) بخش دو طرف را پیدا کن و اگر جفتشان در allowed بود allow وگرنه deny را چاپ کن، مثل deny guest -> family port 445. ممنوع‌ها را در denied بشمار. اگر پورت در decoy_ports بود (چه اجازه چه ممنوع) یک خط دیگر هم چاپ کن: alert: 203.0.113.66 touched decoy port 2323 و فرستنده را به فهرست alerts اضافه کن. آخر از همه denied: 3, alerts: 2. خطهای تازه‌ی خروجی:
allow family -> internet port 443
deny guest -> family port 445
allow family -> smart port 8008
deny smart -> family port 22
deny internet -> family port 2323
alert: 203.0.113.66 touched decoy port 2323
allow smart -> internet port 443
allow family -> family port 2323
alert: 192.168.1.30 touched decoy port 2323
denied: 3, alerts: 2

راهنما

a = zone_of(src)، b = zone_of(dst) و if (a, b) in allowed:. بعد از چاپ تصمیم، یک if port in decoy_ports: جدا (نه elif)، چون تله حتی وقتی ارتباط اجازه دارد هم باید زنگ بزند.

یک جواب درست

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)}")

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