Web Security – Grundlagen

Ziel

Allgemeine Grundlagen für ein erstes Verständnis:

Webanwendung

Eine Webanwendung besteht aus zwei Teilen.

Client

Server

HTTP

Browser und Server kommunizieren mit HTTP.

Ein Browser sendet einen Request.

Der Server antwortet mit einer Response.

Beispiel

GET /products?id=5 HTTP/1.1
Host: shop.example

Der Server antwortet beispielsweise mit:

HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 5

Hallo

URL und Parameter

Viele Anwendungen übernehmen Werte direkt aus der URL.

Beispiel

https://shop.example/product?id=5

Parameter

id = 5

Weitere Beispiele

?search=apple
?page=2
?category=books

HTML Formulare

Benutzer senden Daten meist über Formulare.

Beispiel

<form method="POST" action="login.php">
    <input name="email">
    <input name="password" type="password">
    <button type="submit">Login</button>
</form>
<form method="GET" action="login.php">
    <input name="email">
    <input name="password" type="password">
    <button type="submit">Login</button>
</form>

Der Browser sendet anschließend an den Server:

email=max@example.com
password=test123

Grundregel: Alle Eingaben eines Benutzers sind nicht vertrauenswürdig.

<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $email = $_POST["email"];
    $password = $_POST["password"];
} else {
    $email = null;
    $password = null;
}
?>

<form method="POST">
    <input name="email">
    <input name="password">
    <button type="submit">Absenden</button>
</form>

<?php
if ($email !== null) {
    echo "E-Mail: $email<br>";
    echo "Passwort: $password";
} else {
    echo "Noch keine Daten";
}
?>

HTTP Request:

POST /test.php HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 31

email=test%40example.com&password=geheim

GET Methode

<?php
if ($_SERVER["REQUEST_METHOD"] === "GET" &&
    isset($_GET["email"], $_GET["password"])) {

    $email = $_GET["email"];
    $password = $_GET["password"];

    echo "E-Mail: $email<br>";
    echo "Passwort: $password";
} else {
    echo "Noch keine Daten";
}
?>

<form method="GET">
    <input name="email">
    <input name="password">
    <button type="submit">Absenden</button>
</form>

HTTP Request:

GET /test.php?email=test%40example.com&password=geheim HTTP/1.1
Host: localhost

Cookies und Sessions

Nach erfolgreichem Login merkt sich der Server den Benutzer.

Ablauf

Login
    │
    ▼
Server erstellt Session
    │
    ▼
Session-ID im Cookie
    │
    ▼
Browser sendet Cookie bei jedem Request

Wichtig

Der Browser besitzt nicht die Session.

Er besitzt nur die Session-ID.