Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Testing the running app with a scanner

After this lesson: I can run a baseline scan against my own app and act on what it finds.

55 min lesson8 commandsDevSecOps lab: build a security pipeline and a gate

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

The code passed, the site did not

The shop's code can pass every scan and the live site still send no security headers, or show a crash report to anybody.

Some problems only appear when the app runs. So the pipeline starts it and tests it from outside, as a visitor would.

The one rule

Warning

Scan only your own lab copy. Scanning a site you do not own, without written permission, can be a crime: in the UK under the Computer Misuse Act 1990. Even a gentle scan sends hundreds of requests to someone else's server.

Quick check: the rule

Have a go

Quick check: the rule

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can run a baseline scan against my own app and act on what it finds.

Key words

WordWhat it means
DASTtesting a running app from outside
Crawlfollow every link to find the pages
Passive checkreads replies and sends nothing new
Active checksends a probe to see how the app reacts
Baselinea quick, safe scan run on every change

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

DAST looks at the app from outside

DAST sends real requests and reads the replies. It does not need the code, so it finds what SAST cannot see, such as a missing header.

The usual open-source tool is OWASP ZAP. It is too large for this lab, so the lab has dast.py, a small scanner written to work the same way.

Look at the shop's reply headers

Command
curl -s -D - -o /dev/null http://shop:8000/

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Run a passive baseline scan

Command
docker exec ci python3 dast.py http://shop:8000/

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: check headers yourself

Code: python
headers = {
    "Server": "TuckShop/0.9 Python/3.12.15",
    "Content-Type": "text/html; charset=utf-8",
    "Set-Cookie": "cart=5f2a; Path=/",
}

for wanted in ["Content-Security-Policy", "X-Content-Type-Options", "X-Frame-Options"]:
    print(wanted, "present" if wanted in headers else "MISSING")
print("HttpOnly cookie:", "HttpOnly" in headers["Set-Cookie"])

# Add "X-Frame-Options": "DENY" to the headers and run it again.

Make a free account to save your work and track your progress.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

An active check sends a probe

An active check changes a request and watches what comes back. dast.py --active puts a harmless marker in each query value and looks for it, unescaped, in the reply.

If it comes back as written, the page would run a script put there by an attacker. Run active checks only on your own copy.

Add the active check

Command
docker exec ci python3 dast.py http://shop:8000/ --active

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the hardened shop the same way

Command
docker exec ci python3 dast.py http://shop:8001/ --active

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the hardened shop's headers

Command
curl -s -D - -o /dev/null http://shop:8001/

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think a clean DAST scan means the app is secure. A baseline scan sees replies, not code. The shop's joined SQL, which Semgrep found in lesson 1, never showed in this scan. Use DAST beside SAST, not instead of it.

A rules file turns warnings into failures

In a pipeline, each check is set to IGNORE, WARN or FAIL, with a reason. Anything set to FAIL makes the scan exit 1, and the step fails.

Here a missing policy and a stack trace fail. The server header is ignored, because a proxy in front removes it.

Write the scan's rules file

Command
docker exec -i ci sh -c 'cat > /work/rules/dast.conf' <<'EOF'
1 FAIL every page needs a policy
4 IGNORE the proxy in front removes the header
7 FAIL never show a stack trace
EOF

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Run the scan as a pipeline step

Command
docker exec ci python3 dast.py http://shop:8000/ --config /work/rules/dast.conf --json /work/reports/dast.json; echo "exit $?"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Fix it, then spot a scan

The fix is in the app: set the headers and cookie flags on every reply, and show a plain error page. Port 8001 is that fix.

A scan is easy to spot in a server's log: one address, many pages in seconds, and odd values in the query.

Count the probes in the shop's log

Command
docker logs shop 2>&1 | grep -c "probe"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: a header checker

A scanner sees each reply as a dictionary of headers. Header names are not case-sensitive, so compare them in lower case. Write three functions.

missing(headers) returns the sorted list of names from ["content-security-policy", "x-content-type-options", "x-frame-options"] that the reply lacks, except that x-frame-options is not missing when the policy contains frame-ancestors. cookie_problems(cookie) takes one Set-Cookie value and returns the sorted list of flags it lacks, from HttpOnly, SameSite and Secure, compared in any case. leaks_version(headers) returns True when the Server header contains a digit.

Code: python
def missing(headers):
    return []


def cookie_problems(cookie):
    return []


def leaks_version(headers):
    return False

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.