UnixDroid

Lesson 7 of 12 · 8 min

Subdomain and directory enumeration with Python requests

Before attacking an app, testers map its attack surface. Here's how to enumerate subdomains and hidden directories with the requests library.

Before testing an application, you map its attack surface — every subdomain and hidden page that exists. This recon is mostly guessing from a wordlist and checking what responds, and it's a perfect job for Python's requests library. Only ever run it against targets you own or are authorised to test.

Enumeration is just wordlist + check

You take a list of likely names, build a URL for each, and see which ones answer. No magic — just speed and a good wordlist.

Finding subdomains

import requests

domain = 'example.com'
for sub in ['www', 'mail', 'dev', 'admin', 'api']:
    url = f'https://{sub}.{domain}'
    try:
        r = requests.get(url, timeout=3)
        print(f'[+] {url} -> {r.status_code}')
    except requests.RequestException:
        pass   # didn't resolve or connect

Each name that answers is another way in — and forgotten dev or staging subdomains are often the unpatched ones.

Finding hidden directories

Same idea, one level down — guess paths on a site you're allowed to test.

base = 'http://localhost:8000'
for path in ['admin', 'login', 'backup', 'config', 'uploads']:
    r = requests.get(f'{base}/{path}')
    if r.status_code != 404:
        print(f'[+] /{path} -> {r.status_code}')
Status codeRough meaning
200Page exists and loads
301 / 302Redirect — something's there
403Exists but forbidden (interesting!)
404Not found — skip it

A 403 is often more exciting than a 200: the page exists but someone tried to hide it.

Tip: Real wordlists (like SecLists) have tens of thousands of entries. Always set a timeout and go gently against a target you own — hammering a server with thousands of fast requests is noisy and can look like a denial-of-service attack.

The ethics line, again

Subdomain and directory enumeration touches real infrastructure. Run it only against domains you own or are explicitly authorised to test — a written scope agreement exists for exactly this reason.

Try it yourself
  1. Start a local server with python3 -m http.server 8000 and enumerate paths against it.
  2. Add two more words to the directory list and re-run.
  3. Print only the paths that return a 200 status.
No Linux machine? Practise in the free online terminal

Quick check

Which HTTP status code means a page exists but access is forbidden - often the most interesting result in enumeration?

Need a hint?

It is in the 400s and means forbidden.

Finished this lesson?

Done lessons keep your daily streak going 🔥

0 day streak

0 lessons done

best 0 days

Log in to keep your streak on every device →