-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase_schema.sql
More file actions
187 lines (172 loc) · 6.61 KB
/
Copy pathdatabase_schema.sql
File metadata and controls
187 lines (172 loc) · 6.61 KB
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
-- 实时攻防平台数据库设计
-- 主题:红黑色调攻防平台
-- 删除数据库
DROP DATABASE IF EXISTS Red_Game;
-- 创建数据库
CREATE DATABASE IF NOT EXISTS Red_Game CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE Red_Game;
-- 首先创建不依赖其他表的队伍表
CREATE TABLE teams (
id INT PRIMARY KEY AUTO_INCREMENT,
team_name VARCHAR(100) UNIQUE NOT NULL,
team_icon VARCHAR(255),
total_score INT DEFAULT 0,
member_count INT DEFAULT 0,
max_members INT DEFAULT 3,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- 然后创建用户表(引用队伍表)
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(100),
role ENUM('admin', 'red_team') DEFAULT 'red_team',
team_id INT,
nickname VARCHAR(100),
avatar VARCHAR(255), -- 确保有这个字段
total_score INT DEFAULT 0,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE SET NULL
);
-- 比赛表(引用用户表)
CREATE TABLE competitions (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(200) NOT NULL,
description TEXT,
background_story TEXT,
theme_image VARCHAR(255),
start_time DATETIME,
end_time DATETIME,
is_active BOOLEAN DEFAULT FALSE,
is_ended BOOLEAN DEFAULT FALSE,
created_by INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
);
-- 靶标表(引用比赛表)
CREATE TABLE targets (
id INT PRIMARY KEY AUTO_INCREMENT,
competition_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
ip_address VARCHAR(45) NOT NULL,
flag VARCHAR(255) NOT NULL,
points INT NOT NULL DEFAULT 100,
description TEXT,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (competition_id) REFERENCES competitions(id) ON DELETE CASCADE
);
-- Flag提交记录表(引用用户表和靶标表)
-- 修改Flag提交记录表,确保target_id不为NULL
CREATE TABLE flag_submissions (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
target_id INT NOT NULL, -- 改为 NOT NULL
submitted_flag VARCHAR(255) NOT NULL,
is_correct BOOLEAN DEFAULT FALSE,
points_earned INT DEFAULT 0,
submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (target_id) REFERENCES targets(id) ON DELETE CASCADE
);
-- 积分记录表(引用用户表和队伍表)
CREATE TABLE score_logs (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
team_id INT,
action_type ENUM('flag_submission', 'bonus', 'penalty') DEFAULT 'flag_submission',
points INT NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE SET NULL
);
-- 系统日志表(引用队伍表和用户表)
CREATE TABLE system_logs (
id INT PRIMARY KEY AUTO_INCREMENT,
log_type ENUM('login', 'attack', 'system', 'error', 'success', 'warning', 'network', 'file_integrity', 'malware_detection') DEFAULT 'system',
source_ip VARCHAR(45),
target_ip VARCHAR(45),
message TEXT NOT NULL,
severity ENUM('low', 'medium', 'high', 'critical') DEFAULT 'medium',
team_id INT,
user_id INT,
raw_data JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE SET NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
);
-- 攻击流量日志表(引用队伍表)
CREATE TABLE attack_logs (
id INT PRIMARY KEY AUTO_INCREMENT,
team_id INT NOT NULL,
source_ip VARCHAR(45) NOT NULL,
target_ip VARCHAR(45) NOT NULL,
attack_type VARCHAR(50),
traffic_volume INT DEFAULT 0,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE
);
-- 靶标状态表(引用靶标表)
CREATE TABLE target_status (
id INT PRIMARY KEY AUTO_INCREMENT,
target_id INT,
is_online BOOLEAN DEFAULT TRUE,
is_compromised BOOLEAN DEFAULT FALSE,
last_check TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (target_id) REFERENCES targets(id) ON DELETE CASCADE
);
-- 创建索引
CREATE INDEX idx_users_username ON users(username);
CREATE INDEX idx_users_team_id ON users(team_id);
CREATE INDEX idx_users_role ON users(role);
CREATE INDEX idx_flag_submissions_user_id ON flag_submissions(user_id);
CREATE INDEX idx_flag_submissions_target_id ON flag_submissions(target_id);
CREATE INDEX idx_score_logs_user_id ON score_logs(user_id);
CREATE INDEX idx_score_logs_team_id ON score_logs(team_id);
CREATE INDEX idx_system_logs_created_at ON system_logs(created_at);
CREATE INDEX idx_system_logs_log_type ON system_logs(log_type);
CREATE INDEX idx_attack_logs_team_id ON attack_logs(team_id);
CREATE INDEX idx_attack_logs_timestamp ON attack_logs(timestamp);
-- 插入默认管理员账户(密码:godxing)
INSERT INTO users (username, password, email, role, nickname) VALUES
('admin', 'godxing', '1848210202@qq.com', 'admin', '系统管理员');
-- 创建视图:实时积分榜
CREATE VIEW real_time_rankings AS
SELECT
t.id,
t.team_name,
t.team_icon,
t.total_score,
COUNT(DISTINCT u.id) as member_count,
MAX(fs.submitted_at) as last_submission,
RANK() OVER (ORDER BY t.total_score DESC) as rank_position
FROM teams t
LEFT JOIN users u ON t.id = u.team_id AND u.role = 'red_team'
LEFT JOIN flag_submissions fs ON fs.user_id IN (SELECT id FROM users WHERE team_id = t.id)
WHERE t.member_count > 0
GROUP BY t.id, t.team_name, t.team_icon, t.total_score
ORDER BY t.total_score DESC;
-- 创建视图:用户积分详情
CREATE VIEW user_score_details AS
SELECT
u.id,
u.username,
u.nickname,
u.avatar,
u.total_score,
t.team_name,
t.team_icon,
COUNT(DISTINCT fs.target_id) as targets_completed,
MAX(fs.submitted_at) as last_submission
FROM users u
LEFT JOIN teams t ON u.team_id = t.id
LEFT JOIN flag_submissions fs ON u.id = fs.user_id AND fs.is_correct = TRUE
WHERE u.role = 'red_team' AND u.is_active = TRUE
GROUP BY u.id, u.username, u.nickname, u.avatar, u.total_score, t.team_name, t.team_icon
ORDER BY u.total_score DESC;