Learning goals¶
Explain the importance of security in operational technology
Identify potential attack vectors in industrial automation systems
Evaluate the consequences of security breaches
Apply security principles to an industrial control system implementation (in the project)
Locate basic security principles in software, e.g., salting
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:
principle of least privilege: only admin can see the accounts
defense in depth: multiple security layers, e.g., password hashing with salting, role-based access control
attack surface minimization, e.g., hiding tabs that a user does not have privileges to see.
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.
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()
"""
Account system: create accounts, check passwords, check admin status.
We NEVER store the plain password. Instead we store a random "salt" plus
a hash of (salt + password). To check a login, we redo the hash and
compare it to what's stored instead of comparing the password directly.
"""
import hashlib
import hmac
import os
import sqlite3
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)
class AccountService:
def __init__(self, db_path: str = "db.sqlite", insert_test_data: bool = False):
self.db_path = db_path
self.con = sqlite3.connect(db_path, autocommit=True)
_ = self.con.execute(SQL_TABLE_ACCOUNTS)
if insert_test_data:
self.insert_test_data()
def create_account(
self,
username: str,
password: str,
is_admin: bool = False,
insert_or_ignore: bool = False,
):
salt, password_hash = new_password(password)
_ = self.con.execute(
f"INSERT {'OR IGNORE' if insert_or_ignore else ''} INTO accounts (username, password_salt, password_hash, is_admin) VALUES (?, ?, ?, ?)",
(username, salt, password_hash, is_admin),
)
def username_exists(self, username: str) -> bool:
cursor = self.con.execute(
"SELECT 1 FROM accounts WHERE username = ?", (username,)
)
return cursor.fetchone() is not None
def credentials_correct(self, username: str, password: str) -> bool:
cursor = self.con.execute(
"SELECT password_salt, password_hash FROM accounts WHERE username = ?",
(username,),
)
try:
password_salt, password_hash = cursor.fetchone()
except TypeError:
return False
return password_matches(password, password_salt, password_hash)
def is_admin(self, username: str) -> bool:
cursor = self.con.execute(
"SELECT 1 FROM accounts WHERE username = ? AND is_admin = 1",
(username,),
)
return cursor.fetchone() is not None
def insert_test_data(self, filename: str = "accounts_test_data.csv"):
from csv import reader
with open(filename) as f:
for username, password, is_admin in reader(f):
self.create_account(
username.strip(),
password.strip(),
bool(int(is_admin.strip())),
insert_or_ignore=True,
)
user, pass, 0
admin, pass, 1
Protecting a credential database against exploitation¶
Related concepts:
A function that can be used to map data to fixed-size values.
A precomputed table is a precomputed table for caching the outputs of a cryptographic hash function, usually for cracking password hashes
Random data fed as an additional input to a one-way function that hashes a password or ...
Salting:
prevents precomputed attacks like rainbow tables, so that the attackers must crack each password individually
ensures that identical passwords have different hashes
But:
it does not prevent brute-force attacks against individual passwords, so weak passwords like password123 can be relatively quickly found.
Salting in user registration¶
Salting in user login¶
Salting example code¶
The following code is from the system login interface:
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 40ITERATIONS = 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)