Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Learning goals

We will analyze security in industrial automation systems.

Principles for security by design

Based on the preparation:

Principle of least privilege
... requires that every component of a computer system, e.g., program, user, must be able to access only the information and resources that are necessary for its purpose.
Defense in depth
A concept in which multiple layers of defense are placed through a system
Fail-safe

A design feature or practice that, in the event of a failure of the design feature, inherently responds in a way that will cause minimal or no harm to its environment.

KISS principle

A design principle that implies that simplicity should be a design goal.

Separation of duties

The concept of having more than one person required to complete a task.

Open design

A design which is publicly available so it can be built or understood.

Segmentation

Division of a system into multiple isolated subsystems. Different subsystems can have different security requirements.

Usability

Capacity of a system to provide a condition for its users to perform the tasks safely, effectively, and efficiently while enjoying the experience.

E.g., users use post-its if the password requirements are hard to remember.

Attack surface

The sum of a software environment’s different points where an attacker can try to enter data to, extract data from, or control a device or critical software.

We should minimize the attack surface.

Secure by default
All the security features of a system are turned on when a product is delivered.

E: Analyzing attack surface

Example system login interface

Here is an example project that uses:

Figure 1:A system login user interface some of the security principles. Only the admin has access to accounts.

You can use this example in your project. Click for the code.
`system_login.py`
`account_service.py`
`accounts_test_data.csv`
system_login.py
import tkinter as tk
from tkinter import ttk
from account_service import AccountService


class GUI:
    def __init__(self, insert_test_data=False):
        self.account_service = AccountService(insert_test_data=insert_test_data)
        self.root = tk.Tk()
        self.root.title("Account Service Example")

        self.setup_appearance()

    def setup_appearance(self):
        self.mf = ttk.Frame(self.root, padding=10)
        self.mf.grid()
        self.notebook = ttk.Notebook(self.mf)
        self.notebook.grid()
        self.status_line_text = tk.StringVar()
        ttk.Label(self.mf, textvariable=self.status_line_text).grid()
        self.logout = ttk.Button(
            self.mf, text="🚪 Logout", command=self.logout, state="disabled"
        )
        self.logout.grid()

        self.login = ttk.Frame(self.notebook, padding=10)
        self.accounts = ttk.Frame(self.notebook, padding=10)
        self.dashboard = ttk.Frame(self.notebook, padding=10)

        self.notebook.add(self.login, text="🔐 Login")
        self.notebook.add(self.dashboard, text="📊 Dashboard", state="disabled")
        self.notebook.add(self.accounts, text="👥 Accounts", state="disabled")

        self.setup_appearance_login()
        self.setup_appearance_dashboard()
        self.setup_appearance_accounts()

    def setup_appearance_login(self):
        ttk.Label(self.login, text="Username").grid(sticky="w")
        self.login_username_text = tk.StringVar()

        self.login_username_text.set("user")
        self.login_username = ttk.Entry(
            self.login, textvariable=self.login_username_text
        )
        self.login_username.grid()

        ttk.Label(self.login, text="Password").grid(sticky="w")
        self.login_password_text = tk.StringVar()
        self.login_password_text.set("pass")
        ttk.Entry(self.login, textvariable=self.login_password_text, show="*").grid()

        ttk.Button(self.login, text="Login", command=self.check_credentials).grid()
        self.login_username.focus()
        for child in self.login.winfo_children():
            child.grid_configure(padx=5, pady=5)

    def setup_appearance_dashboard(self):
        ttk.Label(self.dashboard, text="Hello! 🌞").grid()

    def setup_appearance_accounts(self):
        ttk.Label(
            self.accounts, text=f"Accounts management could be implemented here."
        ).grid()

    def check_credentials(self, *args):
        if self.account_service.credentials_correct(
            self.login_username_text.get(), self.login_password_text.get()
        ):
            self.notebook.tab(self.login, state="disabled")
            self.notebook.tab(self.dashboard, state="normal")
            if self.account_service.is_admin(self.login_username_text.get()):
                self.notebook.tab(self.accounts, state="normal")
            self.logout.state(["!disabled"])
            self.status_line_text.set(f"{self.login_username.get()} logged in.")
            self.notebook.select(self.dashboard)
        else:
            self.status_line_text.set(
                "❌ Username password pair not present in the database."
            )

    def logout(self, *args):
        self.notebook.tab(self.login, state="normal")
        self.notebook.tab(self.dashboard, state="disabled")
        self.notebook.tab(self.accounts, state="disabled")
        self.logout.state(["disabled"])
        self.status_line_text.set("")

    def run(self):
        self.root.mainloop()


GUI(insert_test_data=True).run()

Protecting a credential database against exploitation

Related concepts:

Hash function

A function that can be used to map data to fixed-size values.

Rainbow table

A precomputed table is a precomputed table for caching the outputs of a cryptographic hash function, usually for cracking password hashes

Salt

Random data fed as an additional input to a one-way function that hashes a password or ...

Salting:

But:

Salting in user registration

Salting in user login

Salting example code

The following code is from the system login interface:

account_service.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
ITERATIONS = 600_000  # makes brute-forcing slow; higher = safer but slower
SALT_LENGTH = 128 // 8  # bytes
SQL_TABLE_ACCOUNTS = """
CREATE TABLE IF NOT EXISTS accounts (
    username TEXT PRIMARY KEY,
    password_salt BLOB NOT NULL,
    password_hash BLOB NOT NULL,
    is_admin INTEGER NOT NULL CHECK (is_admin IN (0, 1))
);"""


def hash_password(password: str, salt: bytes) -> bytes:
    return hashlib.pbkdf2_hmac("sha256", password.encode(), salt, ITERATIONS)


def new_password(password: str) -> tuple[bytes, bytes]:
    """Create a fresh salt + hash for a brand-new account."""
    salt = os.urandom(SALT_LENGTH)
    return salt, hash_password(password, salt)


def password_matches(password: str, salt: bytes, expected_hash: bytes) -> bool:
    """Return True if `password` matches the stored hash."""
    actual_hash = hash_password(password, salt)
    # compare_digest avoids leaking timing info about how much of the hash matched
    return hmac.compare_digest(actual_hash, expected_hash)

E: Analyzing password salting in code