File size: 6,264 Bytes
c8f640d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
-- CyberShield AI Database Schema (PostgreSQL for Supabase)

CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    username TEXT UNIQUE NOT NULL,
    password_hash TEXT NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS domains (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    domain_name TEXT NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(user_id, domain_name)
);

CREATE TABLE IF NOT EXISTS scan_results (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    domain_name TEXT NOT NULL,
    scan_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    ssl_status TEXT NOT NULL,
    ssl_issuer TEXT,
    ssl_cn TEXT,
    ssl_issue_date TEXT,
    ssl_expiry_date TEXT,
    ssl_days_remaining INTEGER,
    ssl_raw_info TEXT,
    headers_csp TEXT,
    headers_hsts TEXT,
    headers_xframe TEXT,
    headers_xcontent TEXT,
    headers_referrer TEXT,
    security_score INTEGER NOT NULL,
    risk_level TEXT NOT NULL,
    recommendations_json TEXT
);

CREATE TABLE IF NOT EXISTS reports (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    scan_id INTEGER NOT NULL REFERENCES scan_results(id) ON DELETE CASCADE,
    report_name TEXT NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS threat_intel_results (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    scan_id INTEGER,
    domain_name TEXT NOT NULL,
    ip_address TEXT,
    country TEXT,
    isp TEXT,
    threat_level TEXT DEFAULT 'Low',
    reputation_score INTEGER DEFAULT 100,
    blacklist_status TEXT DEFAULT 'Safe',
    blacklisted_on TEXT,
    phishing_detected INTEGER DEFAULT 0,
    malware_detected INTEGER DEFAULT 0,
    suspicious_tld INTEGER DEFAULT 0,
    raw_data TEXT,
    scan_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS cve_cache (
    id SERIAL PRIMARY KEY,
    cve_id TEXT UNIQUE NOT NULL,
    description TEXT,
    severity TEXT,
    cvss_score REAL,
    published_date TEXT,
    affected_tech TEXT,
    mitigation TEXT,
    cached_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS defacement_baselines (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    domain_name TEXT NOT NULL,
    content_hash TEXT NOT NULL,
    content_length INTEGER,
    captured_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(user_id, domain_name)
);

CREATE TABLE IF NOT EXISTS alerts (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    domain_name TEXT,
    alert_type TEXT NOT NULL,
    severity TEXT DEFAULT 'Medium',
    title TEXT NOT NULL,
    message TEXT NOT NULL,
    is_dismissed INTEGER DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS email_settings (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE,
    email_address TEXT,
    smtp_host TEXT DEFAULT 'smtp.gmail.com',
    smtp_port INTEGER DEFAULT 587,
    smtp_username TEXT,
    smtp_password TEXT,
    notify_ssl_expiry INTEGER DEFAULT 1,
    notify_defacement INTEGER DEFAULT 1,
    notify_high_risk INTEGER DEFAULT 1,
    notify_blacklist INTEGER DEFAULT 1
);

-- ═══════════════════════════════════════════════════════════════
-- CyberShield AI 3.1 Migration Schema
-- ═══════════════════════════════════════════════════════════════

-- Attack Surface Monitoring Table
CREATE TABLE IF NOT EXISTS attack_surface (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    domain_name TEXT NOT NULL,
    subdomains TEXT, -- JSON array of discovered subdomains
    open_ports TEXT, -- JSON array of discovered open ports
    dns_records TEXT, -- JSON object of DNS records (A, MX, CNAME, TXT, NS)
    ssl_status TEXT,
    attack_surface_score INTEGER DEFAULT 100,
    scan_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(user_id, domain_name)
);

-- Audit Logging Table
CREATE TABLE IF NOT EXISTS audit_logs (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    username TEXT,
    action TEXT NOT NULL,
    details TEXT,
    ip_address TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Security Timeline Table
CREATE TABLE IF NOT EXISTS security_timeline (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    domain_name TEXT,
    event_type TEXT NOT NULL, -- 'SSL_EXPIRY_ALERT', 'THREAT_INTEL_ALERT', 'DEFACEMENT_ALERT', 'NEW_CVE', 'USER_SCAN'
    severity TEXT DEFAULT 'Medium',
    title TEXT NOT NULL,
    message TEXT NOT NULL,
    event_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Scheduled Automated Scans Table
CREATE TABLE IF NOT EXISTS scheduled_scans (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    domain_name TEXT NOT NULL,
    interval_hours INTEGER NOT NULL, -- 1, 6, 24, 168
    last_run TIMESTAMP WITH TIME ZONE,
    next_run TIMESTAMP WITH TIME ZONE,
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(user_id, domain_name)
);

-- Real-time Visitor Geolocation Table
CREATE TABLE IF NOT EXISTS visitor_locations (
    id SERIAL PRIMARY KEY,
    ip_address TEXT,
    country TEXT,
    country_code TEXT,
    city TEXT,
    suburb TEXT,
    road TEXT,
    postcode TEXT,
    lat REAL,
    lon REAL,
    accuracy_m REAL,
    location_mode TEXT,
    isp TEXT,
    org TEXT,
    asn TEXT,
    user_agent TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);