-- schema.sql — SQLCipher/SQLite schema for the pentest-env toolkit.
-- Applied by lib/db.sh:db_init (after PRAGMA key). Target-centric design:
-- one `targets` row per HTB box / CTF / engagement; everything else FKs to it.
-- The whole DB file is encrypted by SQLCipher, so secret values are stored
-- as plain columns (no per-field crypto needed).
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS targets (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE, -- "Cap", "htb-cap"
platform TEXT DEFAULT 'htb', -- htb|thm|ctf|client
ip TEXT,
hostname TEXT,
os TEXT,
difficulty TEXT, -- easy|medium|hard|insane
status TEXT DEFAULT 'active', -- active|owned|retired
created_at TEXT DEFAULT (datetime('now')),
updated_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS hosts ( -- pivot / internal hosts under a target
id INTEGER PRIMARY KEY,
target_id INTEGER NOT NULL REFERENCES targets(id) ON DELETE CASCADE,
ip TEXT,
hostname TEXT,
os TEXT,
label TEXT,
created_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS services ( -- open ports per host
id INTEGER PRIMARY KEY,
host_id INTEGER NOT NULL REFERENCES hosts(id) ON DELETE CASCADE,
port INTEGER NOT NULL,
proto TEXT DEFAULT 'tcp',
service TEXT,
version TEXT,
notes TEXT
);
CREATE TABLE IF NOT EXISTS credentials ( -- values plain (DB is encrypted)
id INTEGER PRIMARY KEY,
target_id INTEGER NOT NULL REFERENCES targets(id) ON DELETE CASCADE,
host_id INTEGER REFERENCES hosts(id) ON DELETE SET NULL,
username TEXT,
secret TEXT, -- password|hash|key|token material
type TEXT DEFAULT 'password', -- password|hash|ssh-key|token
service TEXT, -- ssh|smb|http|...
source TEXT, -- where it was found
valid INTEGER, -- 1|0|NULL(untested)
created_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS flags (
id INTEGER PRIMARY KEY,
target_id INTEGER NOT NULL REFERENCES targets(id) ON DELETE CASCADE,
type TEXT DEFAULT 'user', -- user|root|web|blood|other
value TEXT NOT NULL,
notes TEXT,
captured_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY,
target_id INTEGER REFERENCES targets(id) ON DELETE CASCADE, -- NULL = global
title TEXT,
body TEXT,
tags TEXT,
created_at TEXT DEFAULT (datetime('now')),
updated_at TEXT DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_hosts_target ON hosts(target_id);
CREATE INDEX IF NOT EXISTS idx_svc_host ON services(host_id);
CREATE INDEX IF NOT EXISTS idx_creds_target ON credentials(target_id);
CREATE INDEX IF NOT EXISTS idx_flags_target ON flags(target_id);
CREATE INDEX IF NOT EXISTS idx_notes_target ON notes(target_id);
CREATE VIEW IF NOT EXISTS v_target_summary AS
SELECT t.id, t.name, t.platform, t.ip, t.status,
(SELECT COUNT(*) FROM flags f WHERE f.target_id = t.id) AS flags,
(SELECT COUNT(*) FROM credentials c WHERE c.target_id = t.id) AS creds,
(SELECT COUNT(*) FROM hosts h WHERE h.target_id = t.id) AS hosts
FROM targets t;