Day 3 of 760 minCipher CadetHands-on lab

Day 3: Collecting Data With curl and a Small Python Script

Taught by Mr Elite Updated

By the end of today you can

  • Start a local test target of your own and confirm it is running
  • Fetch response headers and page content with curl
  • Run a short Python script that saves headers and body to a raw folder
  • Explain why raw output is never edited and how to label each file

Warm-up

Answer from memory first, then tap to check. Recalling yesterday is what makes it stick.

What two things make up a single page load, and why does one page cause many of them?

A request and a response. The HTML points to other files (images, scripts, styles), and each one needs its own request.

Which browser tab shows you the requests and headers a page triggers?

The Network tab in developer tools.

Yesterday you read a web app by clicking through developer tools. It worked, but try repeating it on ten pages. You would be clicking, squinting and copying by hand, and next week you could not prove exactly what you saw.

Today we hand that job to tools. A tool makes the same requests your browser makes, but it can do it the same way every time and write the result straight to a file. That is the first stage of a pipeline: collect, and keep what you collected exactly as it arrived.

The same requests, made by a tool

Your browser sends a request and gets a response. Nothing magic happens in between. A command-line tool can send the very same request and show you the response without drawing the page. That makes it ideal for recording evidence, because there is no styling or scripts running to confuse the picture.

We use two tools today. curl is a free command that fetches a URL and prints what comes back. Python is a language, and a short Python script lets us repeat the fetch and save files automatically.

curl
A command-line program that sends a request to a URL and shows the response.
header
A label and value sent with a response, such as Server or Content-Type.
raw output
The response exactly as it came back, with nothing cleaned, edited or reformatted.
local target
A test web server running on your own computer, so you are only ever touching your own system.

Quick check

Why do we collect with a tool instead of only reading pages in the browser?

Watch me do it: build a target, then collect from it

First I make a safe place to practise. I create a project folder, a tiny website, and start a server for it on your own computer. Then I fetch from it with curl, and then with a script. Open your terminal and follow along in order.

bash
cd recon-course
mkdir raw site
echo '<h1>Test target</h1><form action="/login"><input name="user"></form>' > site/index.html
ls
Makes the two folders and a one-line test page with a form, so there is something to observe.
bash
cd site
python3 -m http.server 8080
Starts a basic web server for the site folder on port 8080. On Windows without WSL, use python instead of python3.

Now the first real collection. In your new terminal, from the recon-course folder, ask curl for headers only. The capital I option sends a HEAD request, which asks for headers without the body.

bash
curl -I http://localhost:8080
Fetches only the response headers from your local target.

You should see something close to this. Version numbers will differ on your machine.

text
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.12.1
Date: Mon, 01 Jan 2024 10:00:00 GMT
Content-type: text/html
Content-Length: 77
Typical headers. The Server line is a technology hint, exactly the kind of thing you grouped in Day 2.

Now save output to files. The greater-than sign sends what would print on screen into a file instead. The -s option hides curl's progress meter so the file holds only the response.

bash
curl -sI http://localhost:8080 > raw/home.headers.txt
curl -s http://localhost:8080 > raw/home.body.html
ls raw
cat raw/home.headers.txt
Saves headers and body into two files, then lists the folder and prints one file to confirm it worked.

Quick check

You run curl -s http://localhost:8080 > raw/home.body.html and the file is empty. What is the most likely cause?

A 10-line Python collector

Typing two curl commands per page gets old quickly, and file names drift. A script does both fetches and names the files consistently. Create collect.py in your recon-course folder and paste the code below. It uses only Python's built-in tools, so there is nothing to install.

python
import sys, urllib.request
url = sys.argv[1]
name = sys.argv[2]
with urllib.request.urlopen(url, timeout=10) as r:
    status = r.status
    headers = ''.join(f'{k}: {v}\n' for k, v in r.getheaders())
    body = r.read()
open(f'raw/{name}.headers.txt', 'w').write(f'{status}\n{headers}')
open(f'raw/{name}.body.html', 'wb').write(body)
print('saved', name, status, len(body), 'bytes')
Takes a URL and a short name from the command line, fetches the page, and writes headers and body to raw/. The wb mode writes the body bytes untouched.

Reading it top to bottom: the first two lines grab the URL and the name you type after the script name. The with block makes the request and keeps the status code, the headers and the body. The last three lines write two files and print a one-line receipt.

bash
python3 collect.py http://localhost:8080 home
ls raw
Runs the collector with the name home. Expect home.headers.txt and home.body.html to appear.

Keep raw output untouched

Here is the habit that matters most today. Whatever lands in the raw folder, you never edit it. Not to tidy it, not to fix a typo, not to remove a line you think is boring. If you change it, you can no longer say what the target actually sent.

Cleaning happens tomorrow, and it will write to a different folder. Raw stays as the evidence. If your cleaning script has a bug, you can always go back to raw and run it again.

Give every file a clear name: the page, then what it holds. Then keep a one-line note for each, for example in raw/NOTES.txt: home.headers.txt: status line and headers from the local test page. That note is part of today's mission.

Lab: collecting from the DocuShare lab

Your local server is plain. The lab gives you a more realistic app to collect from. It is a SecurityElites practice lab, so it is safe and in scope. You will use it only for collection today, not to attack anything.

Your turn

IDOR

DocuShare is a small document app that uses numeric IDs in its URLs. Today we are not solving it, we are practising observation and collection on the pages that belong to you. That means the evidence you save is accurate and you have a clean record of how the app behaves, which any later testing depends on.

Open the lab in a new tab

What to try

What to notice

  • How the document is identified in the URL, and whether that identifier looks predictable.
  • Which request carries your login state, and where it travels in the request.
  • Whether the saved raw file matches what you saw in the browser.

If it did not work

Editing files in raw to make them tidier.

Leave raw alone. Write any cleaned version into a different folder tomorrow.

Closing the server terminal and then wondering why curl fails.

The server only runs while its window is open. Restart it from the site folder with python3 -m http.server 8080.

Running python collect.py from the wrong folder, so raw/ cannot be found.

Run ls first. You should see collect.py and raw in the same folder. Use cd recon-course if not.

Pasting a copied cURL command that includes your login cookie into notes you share.

Treat cookies like passwords. Keep those files private and do not post them anywhere.

Recap

  • A tool makes the same requests as your browser and can save the result to a file.
  • curl -I fetches headers only, and curl -s hides the progress meter.
  • A local server on localhost gives you a target that is entirely yours.
  • A short Python script gives consistent names and repeatable collection.
  • Raw output is evidence and is never edited. Cleaning goes in a separate folder.
  • Every raw file deserves a one-line note saying what it contains.

Cheat sheet

curl -I URL
Fetch response headers only
curl -s URL > file
Fetch quietly and save the body to a file
python3 -m http.server 8080
Serve the current folder on localhost port 8080
python3 collect.py URL name
Run your collector and save raw/name.headers.txt and raw/name.body.html
ls raw
List what you have collected
Ctrl+C
Stop a running server

Before the next day

Mission: Three raw files with notes

Run your collection against your local target or the lab and build your first evidence folder. This feeds Day 4, where you will turn these raw files into structured data.

About 30 min

You are done when: A raw folder holding at least three untouched output files plus NOTES.txt with a one-line description of each.

Want more? Add a third argument to collect.py for a timestamp in the file name, so repeated runs never overwrite each other.

Quiz

4 questions. Your score is saved with your progress.

1. Why does the script write the body with mode wb?

2. Your cleaning script mangles a file. What does keeping raw untouched let you do?

3. Which target is appropriate for today's commands?

4. curl -sI shows a Server header naming the software and version. Why note it?

Finish the day

Mark the day complete to save your progress.

Go to Day 4

Next: On Day 4 we take the raw files you saved today and turn them into a tidy CSV or JSON file with a Python script. Good raw output makes that job much easier.

Tell someone you are doing this:WhatsAppXLinkedIn

Your notes

Private to this device. Write what you want to remember from today.