Learn · Tier 3 · Module 7 of 10
Scripting the boring parts
Python and curl in a loop: replay requests, follow chains and decode in bulk.
The idea
By hand, one request is quick. Thirty requests in order, each using a value from the last, or a thousand strings to decode, is slow and error-prone. A short script does it in seconds, and keeps an exact record of what was sent.
When to script
- Chains. Each response says where to go next (a path, a token, a page number).
- Repetition. The same request with one value changed: IDs 1 to 500, every task in a queue.
- Bulk decoding. Every string in a table, every message in a capture.
- Anything you have done three times by hand.
Python and requests
requests is the usual Python library for HTTP. The pieces map one-to-one to curl:
import requests
r = requests.post(
"https://c2.example.test/gate", # the URL
headers={"User-Agent": "Pebble/2.4"}, # -H
data={"id": "7", "st": "idle"}, # --data, form-encoded
params={"v": "2"}, # ?v=2 in the URL
)
print(r.status_code) # 200
print(r.headers) # response headers
print(r.text) # body as text
print(r.json()) # body parsed as JSON
json= instead of data= sends a JSON body. r.content is the body as bytes.
A chain
path = "/api/export"
while path:
r = requests.get(BASE + path, headers={"X-Api-Key": key})
page = r.json()
print(page["page"])
path = page["next"] # None on the last page ends the loop
curl in a loop
For simple repetition a shell loop is enough:
for id in $(seq 1 50); do
curl -s "https://c2.example.test/task?id=$id"; echo
done
-s hides the progress meter. When each request needs a value from the last response, switch to Python: parsing JSON in shell is possible (jq) but gets awkward fast.
Good habits
- Print as you go, so you see where it stops.
- Stop on an error status instead of carrying on:
if r.status_code != 200: print(r.text); break. - Do not hammer a server: a live instance here answers 100 requests per 10 seconds. A real C2 may notice you.
Walkthrough: in the browser
The workspace's Notebook runs Python in your browser. Its necropsy module has two helpers: file(name) returns a puzzle file as bytes, and request(method, path, headers=..., data=...) sends a request to the instance through the same connection as the console. Use await in front of both.
- Sign in, open the practice puzzle's workspace and press Spin up instance.
- Open the Notebook tab. Python loads once (about 10 MB). The first cell shows the helpers.
- In a new cell, ask for the start of the export:
from necropsy import request
r = await request("GET", "/api/export")
print(r.status, r.text)
start = r.json()
- The response names the key and the first page. Fetch page 1 by hand, with the header:
key = start["auth"].split("X-Api-Key: ")[1].split()[0]
page = (await request("GET", start["first"], headers={"X-Api-Key": key})).json()
print(page)
- Each page links to the next. Turn it into a loop that stops when
nextisNone, printing each page number. The last page hasexport_id. - Shift+Enter runs a cell. Cells are kept in this browser, per puzzle.
The Terminal line shows how this maps to Python on your own computer: request() is requests.request(), and file() is open(name, "rb").read().
Practice
Checkpoint
Answers from either path count.