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

Free lesson

Ship a web server with safe defaults

After this lesson: I can review a web server's settings against a checklist.

55 min lesson5 commandsWeb server hardening lab: fix a weak nginx configuration and check it with curl

The bakery that told strangers too much

A bakery puts its website online. Nobody breaks in. But anyone can see which software it runs, read a backup file and list a folder of supplier names.

The settings were the ones the software came with. Safe defaults are the first job of anyone who ships a server.

By the end of this lesson

Note

I can review a web server's settings against a checklist.

Key words

WordWhat it meansExample
Hardeningchanging settings so a system gives away and allows less
Headera line of facts that travels with a web reply
Status codethe number that says how a request went404
Defaultthe setting used when nobody chose one
Attack surfaceeverything that a stranger can reach
Reloadmake a server read its settings again, without stopping

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

A server answers with more than a page

Every reply from a web server carries a status code and some headers. The code says how it went: 200 worked, 404 not found, 405 method not allowed.

Headers can say too much. Each fact a stranger learns makes your attack surface a little bigger.

Your lab has a bakery's web server called web. These three Runs only look at what it says.

Read the headers of a reply

Command
curl -s -I http://web

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask for a page that is not there

Command
curl -s -o /dev/null -w "%{http_code}\n" http://web/nothing-here

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Try a method a static site refuses

Command
curl -s -o /dev/null -w "%{http_code}\n" -X DELETE http://web/

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A review compares settings with a checklist

To harden a server, read its settings line by line and ask the same questions each time. Does it say too much? Does it allow too much? Does it keep a record?

A default is written to make a server easy to start, not safe to leave. The table lists some common risks.

Worked example: a review checklist

SettingRiskSafer choice
Old protocol versionsKnown weaknesses can be usedAllow only the newer ones
No limit on upload sizeOne user fills the diskSet a limit that fits
Logging switched offNobody can see what happenedKeep the logs
Any site may call the APIOther sites read your dataName the sites allowed

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

A shop's settings to review

Code: bash
server {
    listen 443 ssl;
    server_name books.example.com;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    client_max_body_size 0;
    access_log off;
    add_header Access-Control-Allow-Origin *;
    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

A common mistake

Warning

Many people think hiding the version number makes a server secure. It only hides one clue. A stranger can still try every known fault. Hide the clue and also keep the software up to date and remove what should not be there.

Test a change, then reload

nginx refuses a reload with a bad file and keeps its old settings, but a restart fails. So you test the file first. nginx has a test that reads the settings and says whether they are valid.

Only then do you reload, which makes the running server read the new settings without dropping a visitor.

A check script turns each rule of the checklist into a pass or a fail, so a machine can repeat the review. The lab has one, and it is yours to use after the lesson.

Test the settings, change nothing

Command
docker exec web nginx -t

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask which version is running

Command
docker exec web nginx -v

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further

Add a seventh rule to your lint_config after the assignment: client_max_body_size with a size above 100m is big-upload-limit. Sizes end in k or m.

Write two tests of your own, one for client_max_body_size 500m; and one for client_max_body_size 2m;, and check that only the first gives a finding.

About this lab and its source: Web server hardening lab: fix a weak nginx configuration and check it with curl

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