ENع
→Detection
الدرس 17 · العمليات الأمنية

Logging and detection

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة11 خطوة5 أسئلةAdvanced
عن هذا الدرس

You cannot detect what you never wrote down. Follow one sign-in service from logging to response: it writes one structured JSON line per attempt, keeps passwords and tokens out of the log, and copies every line to a central store. Then a burst of failed sign-ins arrives, and thirteen lines of real Python, a detection rule with a 60-second window per user, spot it and raise an alert. Finish with triage, tuning the rule, and the response, using guidance from NIST, OWASP and CISA.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 17 · تفاعلي
01 / 11

What a log is

NIST defines a log as a record of the events in an organisation's systems. Each event should say when, where, who and what. You cannot detect what you never wrote down, so logging comes first.

log     a record of events (NIST SP 800-92)
when    the time it happened
where   which app or server
who     which user, from which address
what    the event and its outcome
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. What a log is

    NIST defines a log as a record of the events in an organisation's systems. Each event should say when, where, who and what. You cannot detect what you never wrote down, so logging comes first.

    log     a record of events (NIST SP 800-92)
    when    the time it happened
    where   which app or server
    who     which user, from which address
    what    the event and its outcome
  2. One line per sign-in

    Ana mistypes her password once, then gets in. The sign-in service writes one line per attempt, a small JSON (plain text data) record: time, user, address and event. OWASP names a failure authn_login_fail. Failures matter most.

    auth.log (JSON Lines: one event per line)
    {"t": 20, "user": "ana", "ip": "198.51.100.20", "event": "authn_login_fail"}
    {"t": 26, "user": "ana", "ip": "198.51.100.20", "event": "authn_login_success"}
    t       seconds since the log began
    event   names from the OWASP Logging Vocabulary
  3. Never log the secret

    A common mistake is logging the whole sign-in request, password included. Then anyone who reads the logs can sign in as Ana. OWASP says never log passwords, session IDs, access tokens or keys. Log the outcome instead.

    mistake  {"user": "ana", "password": "..."}
    fix      {"user": "ana", "event": "authn_login_fail"}
    never    passwords, session IDs, access tokens,
             keys, connection strings
    also     encode input so it cannot fake a line
  4. Collect logs in one place

    Each line is copied at once to a central log store. An intruder on the server then cannot quietly erase it, and lines from many systems can be compared. Clocks are synced, so their times line up.

    ship      every line, as it is written
    store     central, read-only, tamper detection
    clocks    synced on every server
    why       logs kept only on the server can be
              deleted by whoever breaks in
  5. A burst of failures

    Now someone at 203.0.113.7 tries password after password for ana. Each guess fails, and each failure becomes a line: five in 40 seconds, from an address Ana never uses. The evidence is in the log.

    t=100  ana  203.0.113.7  fail
    t=112  ana  203.0.113.7  fail
    t=121  ana  203.0.113.7  fail
    t=133  ana  203.0.113.7  fail
    t=140  ana  203.0.113.7  fail
    five failures in 40 s, one address
  6. A detection rule reads lines

    A detection rule is code that watches the log. This one reads each line, turns the JSON into a dictionary, and keeps only failures. Each user gets their own list, q, of recent failure times. Ana's typo is there.

    for line in sys.stdin     one event at a time
    json.loads(line)          text → dictionary
    if event == authn_login_fail
    q = fails["ana"]          a deque per user
    q                         [20]   (the typo)
  7. A 60-second window

    When the failure at t=100 arrives, it joins q. The while loop then drops times 60 seconds old or more, so the typo at t=20 falls out. The rule only counts recent failures, not old mistakes.

    q.append(100)       q = [20, 100]
    100 - 20 = 80   ≥ 60: popleft()
    q = [100]
    deque     a list that is fast at both ends
  8. Five in a minute: alert

    More failures arrive at 112, 121, 133, 140. None is a minute old yet, so q grows to five. len(q) equals LIMIT, and the rule prints an alert naming the user and the address.

    q = [100, 112, 121, 133, 140]
    140 - 100 = 40  < 60: nothing dropped
    len(q) == LIMIT == 5
    print → ALERT ana 203.0.113.7
    a sixth failure in the window would not repeat it
  9. An analyst checks the alert

    The alert reaches an analyst, who triages it: is it real? They compare it with other logs. Ana signs in from 198.51.100.20, not 203.0.113.7, and nobody there knows the password. It is real: declare an incident.

    alert     ALERT ana 203.0.113.7
    triage    real attack, or a false alarm?
    compare   Ana's usual address 198.51.100.20
              this burst from 203.0.113.7
    verdict   real: declare an incident
  10. Tuning the rule

    Rules need tuning. A limit too low raises false alarms for every typo, and tired analysts start ignoring alerts. Too high, and real attacks slip by. Slow guessing, or one password tried on many accounts, needs other rules.

    LIMIT, WINDOW = 5, 60
    too low    false positives: alert fatigue
    too high   false negatives: missed attacks
    also need  per-address rules, slow-and-low rules
    review     after every incident
  11. Respond, then learn

    The analyst responds: the service blocks the address, checks that no guess worked, and turns on MFA (a second proof, like a phone code) for Ana. NIST's incident guide links detect, respond and recover. The response is logged too, and the rule is reviewed.

    contain   block 203.0.113.7 at the sign-in service
    check     no authn_login_success from that address
    protect   MFA on for ana
    record    the response is logged as well
    learn     review the rule (NIST SP 800-61r3)

اختبار يتاح بعد الخطوة الأخيرة.