Scenario test · 90 minutes
Find where an internal service fails
- Find the layer at which each of three services fails, with evidence
- Use addresses, ports, DNS, HTTP, TLS and a proxy together on one problem
- Write a short incident report that another person can check
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
You work in the platform team of a small company. At 09:10 the billing job stops with the message "cannot collect data from orders, payments and reports". The job reads its three addresses from a file, endpoints.conf. Nobody changed the job, but it sends a stored session cookie to orders. Somebody changed something on the services last night, and nobody knows what. The team also renewed the certificate of the payments service at 09:05 this morning, and since then a user laptop shows "certificate is not yet valid" for it.
Your lead gives you the job of finding out. For each of the three services, you must say at which layer it fails (the name, the port, the certificate or the page), prove it, and say what must change. The company network is 10.20.0.0/22. The lead also wants to know if the orders service saw requests from outside that network.
- Billing job → Orders: GET /orders on 8096, stored cookie
- Billing job → Payments: GET /status on 8097, over TLS
- Billing job → Reports: GET /daily on 8098
All of it runs on your own machine. Save the program below as incident.py in ~/krlab/networking and start it with python3 incident.py. It writes the files endpoints.conf, access.log, clock.txt, neigh.txt and pay.pem (the certificate of the payments service) in the current folder, and starts three services on 127.0.0.1. Leave it running. Use your tools of the course: trace.py, curl, ss, openssl, ipaddress, ntp.py and proxy.py.
incident.py
import http.server
import ssl
import subprocess
import threading
LOG = """10.20.1.7 GET /orders 200
10.20.2.31 GET /orders 200
10.20.0.9 GET /login 200
203.0.113.40 GET /orders 401
198.51.100.23 GET /admin 403
10.20.3.250 GET /orders 200
10.20.4.8 GET /orders 401
192.168.5.5 GET /login 200
10.20.1.88 GET /orders 200
198.51.100.23 GET /login 200
10.20.0.14 GET /orders 200
203.0.113.40 GET /orders 401
2001:db8:10:1::25 GET /orders 200
2001:db8:10:2::9 GET /admin 403
"""
CLOCK = "1000.000 1420.010 1420.011 1000.021\n" # t1 t2 t3 t4 of the laptop
NEIGH = """10.20.0.1 dev eth0 lladdr 02:00:00:00:00:66 REACHABLE
10.20.0.9 dev eth0 lladdr 02:00:00:00:00:09 STALE
10.20.0.14 dev eth0 lladdr 02:00:00:00:00:14 STALE
10.20.0.77 dev eth0 lladdr 02:00:00:00:00:66 REACHABLE
"""
CONF = """orders http://127.0.0.1:8096/orders
payments https://localhost:8097/status
reports http://127.0.0.1:8098/daily
"""
class Handler(http.server.BaseHTTPRequestHandler):
server_version = "internal/1"
sys_version = ""
def log_message(self, *args):
pass
def do_GET(self):
code, body, headers = self.server.route(self.path, self.headers.get("Cookie", ""))
self.send_response(code)
for name, value in headers.items():
self.send_header(name, value)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
class Server(http.server.ThreadingHTTPServer):
def handle_error(self, request, client_address):
pass # a client that refuses the certificate is not an error here
def orders(path, cookie):
if path == "/login":
return 200, b"signed in\n", {"Set-Cookie": "sid=Q9-77c1; HttpOnly"}
if path == "/orders" and "sid=Q9-77c1" in cookie:
return 200, b"orders=17\n", {}
return (401, b"sign in first\n", {}) if path == "/orders" else (404, b"not found\n", {})
def payments(path, cookie):
return (200, b"payments ok\n", {}) if path == "/status" else (404, b"not found\n", {})
def reports(path, cookie):
return (200, b"reports=4\n", {}) if path == "/daily" else (404, b"not found\n", {})
def start(port, route, tls=False):
server = Server(("127.0.0.1", port), Handler)
server.route = route
if tls:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain("pay.pem", "pay.key")
server.socket = context.wrap_socket(server.socket, server_side=True)
threading.Thread(target=server.serve_forever, daemon=True).start()
open("access.log", "w").write(LOG)
open("endpoints.conf", "w").write(CONF)
open("clock.txt", "w").write(CLOCK)
open("neigh.txt", "w").write(NEIGH)
subprocess.run(["openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-keyout", "pay.key",
"-out", "pay.pem", "-days", "30", "-subj", "/CN=pay.lab.test",
"-addext", "subjectAltName=DNS:pay.lab.test"], check=True, stderr=subprocess.DEVNULL)
start(8096, orders)
start(8097, payments, tls=True)
start(8095, reports)
print("incident is running. Files: access.log, clock.txt, neigh.txt, endpoints.conf, pay.pem. Press Ctrl+C to stop.", flush=True)
threading.Event().wait()Your tasks
There are no steps here. Choose the tools yourself.
- Read
endpoints.conf. Trace each of the three addresses and note, for each, the last stage that worked and the first that failed. Use the ladder of lesson 13 and say the OSI layer (lesson 1) of the failure. - List the sockets that listen on the ports 8095 to 8099 and find which service of
incident.pyruns on each. Compare the result with the ports inendpoints.conf. - Study the orders service. Find out what it answers without a cookie. Sign in at
/login, keep the cookie, and ask again. Read theSet-Cookieheader. Judge its flags against what you know about session cookies. - Study the payments service. Trust its certificate (
pay.pem) and ask for the address inendpoints.conf. Read the names that the certificate accepts. Find a call that works, and say what the real fix is. - Read
access.log. Count the different IPv4 source addresses that are outside the network10.20.0.0/22. Then count the lines with an IPv6 client, and say what a report script that reads only dotted IPv4 addresses does with them. - Start your logging proxy so that it listens on port 8090 and forwards to the orders service. Send the sign-in request through it. Say what a person on the network could read, and say what would protect it.
clock.txtis one NTP sample of the laptop (the four time stamps of lesson 6, in seconds). Work out the offset, and say how it explains the certificate message.neigh.txtis the neighbour table of the orders host. Find the sign that could mean ARP spoofing (lesson 3), and say what you would check next.- Write your report (see "Hand in").
Answer these from your own results.
Stop incident.py when you finish, and check that the ports are free.
Hand in
Write a short report in a text file, report.md, of one page. This is the kind of document that you can show to an employer, so write it for a reader who was not there. Use these headings:
- Summary. Two sentences: what failed, and the one thing to do first.
- Findings. A table with one row for each service: the service, the layer where it fails (the rung of the ladder and its OSI layer number), the evidence (the command and the line of output), and the fix.
- What works. What you checked and found to be fine, so that nobody checks it twice.
- Outside requests. What
access.logshows and what you suggest. - Evidence. The commands that you ran, in order, with the key lines of their output.
Review your work
Compare your report with this model, after you have written your own.
Model solution
Orders. The name and the port are fine, and the connection works. The fault is at the page level (rung http, layer 7). python3 trace.py http://127.0.0.1:8096/orders ends with HTTP/1.0 401 Unauthorized. The service wants a cookie. curl -si http://127.0.0.1:8096/login returns Set-Cookie: sid=Q9-77c1; HttpOnly. With the cookie, /orders returns orders=17. The billing job sends an old cookie, and the service wants a new sign-in. The cookie has HttpOnly but no Secure flag and no SameSite flag, so over plain HTTP a person on the path can read it.
Payments. The name localhost resolves, and the TCP connection works. The fault is at the certificate (rung tls, layer 5 or 6). python3 trace.py https://localhost:8097/status ends with FAILED at TLS and self-signed certificate. With the certificate as the trusted root, curl --cacert pay.pem https://localhost:8097/status ends with exit code 60 and the text no alternative certificate subject name matches target hostname 'localhost'. openssl x509 -in pay.pem -noout -ext subjectAltName shows DNS:pay.lab.test. The certificate was made for another name than the one in the file. A call that works: curl --cacert pay.pem --resolve pay.lab.test:8097:127.0.0.1 https://pay.lab.test:8097/status returns payments ok. The real fix is a DNS record for pay.lab.test, and the job must use that name. Do not use -k.
Reports. The name resolves, but the port is wrong (rung tcp, layer 4). python3 trace.py http://127.0.0.1:8098/daily ends with FAILED at TCP: ConnectionRefusedError, so the host is there and nothing listens on 8098. ss -tlnp | grep -E ':809[5-9]\b' shows three listeners: 8095, 8096 and 8097. A request to /daily on each port gets reports=4 only from 8095. The service moved from 8098 to 8095. The fix is to change the port in endpoints.conf.
Outside requests. With ipaddress, four different addresses in access.log are outside 10.20.0.0/22: 10.20.4.8, 192.168.5.5, 198.51.100.23 and 203.0.113.40. The first two are private addresses that belong to other networks, for example a second office or a VPN. The other two are public. The address 203.0.113.40 got 401 twice, and 198.51.100.23 asked for /admin and got 403. Two more lines come from IPv6 clients, 2001:db8:10:1::25 and 2001:db8:10:2::9. A script that reads only dotted IPv4 addresses drops them, and the /admin request with 403 would be missed. Suggest that the orders service is bound to an internal address, or that a firewall limits it to 10.20.0.0/22 for both IPv4 and IPv6.
Clock. python3 ntp.py clock.txt gives an offset of 420000 ms, that is 420 s: the laptop is 7 minutes behind the server. The certificate of the payments service was renewed at 09:05, and the job stopped at 09:10. A laptop that is 7 minutes behind reads 09:03 at that moment, so the start of the certificate is still in its future: "not yet valid". In a real case, openssl x509 -in pay.pem -noout -dates shows a start time a few minutes before the laptop's clock (the certificate that incident.py makes starts when you start the program). Fix the clock with NTP, and do not trust the certificate less.
Neighbours. neigh.txt shows 02:00:00:00:00:66 for both 10.20.0.1 (the gateway) and 10.20.0.77. Two IP addresses with one MAC address is a sign to investigate: a host with two addresses is also normal, but here one of them is the gateway. Compare 02:00:00:00:00:66 with the real MAC of the gateway, and check which port holds it.
A short report.
Summary: The billing job fails on three different layers. Fix the port of reports first, because it is one line in endpoints.conf.
Findings: orders, page level, 401 without a cookie, the job must sign in; payments, certificate level, the name in the certificate is pay.lab.test and the job asks for localhost, add a DNS record and use the right name; reports, port level, connection refused on 8098, the service listens on 8095.
What works: the names localhost and 127.0.0.1 resolve, the orders and reports services answer with the right content, and the payments service answers when the name is right.
Outside requests: four IPv4 addresses outside the company network, two of them public, and two IPv6 clients that an IPv4 filter drops. Limit the access for both families.
Other findings: the laptop clock is 420 s behind, and the gateway address shares a MAC address with 10.20.0.77.
Mark your report
You mark this yourself, or you ask a colleague or a mentor to mark it. The page checks the numbers above; it cannot read your report.
| Criterion | Not yet | Meets the standard | Strong |
|---|---|---|---|
| Findings | A service says only "it does not work". | Layer of each fault: orders 401, payments name mismatch, reports on 8095; outside addresses counted. | Also the clock offset and the shared MAC, each linked to its symptom. |
| Evidence | A claim has no command or output. | Each claim has a command and the line of output that proves it. | A reader re-runs the Evidence section in order and gets the same lines. |
| Judgement | No "What works" section, or a guess shown as fact. | Says what was ruled out, and calls the shared MAC a sign, not proof. | Says what was not tested, such as the real gateway MAC. |
| Fixes | A fix turns off a check, such as -k. | Each fix is specific and names who does it. | Fixes in order, with the reason for the first one. |
| Writing | More than one page, or no summary. | Dated; the summary gives the failure and the first action. | A manager can read it in two minutes and act. |
Meets the standard in every row: you can show this report as a work sample. Remove names and data that are not yours to share.
The report shows your work on this scenario, done on your own machine without a proctor. It does not show work on a real network.
If your report lacks a part, add it and look again at your evidence.