@ebowwa/hetzner 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/bootstrap/index.js +1126 -0
  2. package/dist/bootstrap/index.js.map +15 -0
  3. package/dist/index.js +3540 -0
  4. package/dist/index.js.map +31 -0
  5. package/dist/onboarding/index.js +460 -0
  6. package/dist/onboarding/index.js.map +14 -0
  7. package/package.json +53 -16
  8. package/actions.js +0 -1084
  9. package/actions.ts +0 -1053
  10. package/auth.js +0 -39
  11. package/auth.ts +0 -37
  12. package/bootstrap/FIREWALL.md +0 -326
  13. package/bootstrap/KERNEL-HARDENING.md +0 -258
  14. package/bootstrap/SECURITY-INTEGRATION.md +0 -281
  15. package/bootstrap/TESTING.md +0 -301
  16. package/bootstrap/cloud-init.js +0 -323
  17. package/bootstrap/cloud-init.ts +0 -394
  18. package/bootstrap/firewall.js +0 -292
  19. package/bootstrap/firewall.ts +0 -342
  20. package/bootstrap/genesis.js +0 -424
  21. package/bootstrap/genesis.ts +0 -518
  22. package/bootstrap/index.js +0 -59
  23. package/bootstrap/index.ts +0 -71
  24. package/bootstrap/kernel-hardening.js +0 -270
  25. package/bootstrap/kernel-hardening.test.js +0 -182
  26. package/bootstrap/kernel-hardening.test.ts +0 -230
  27. package/bootstrap/kernel-hardening.ts +0 -272
  28. package/bootstrap/security-audit.js +0 -122
  29. package/bootstrap/security-audit.ts +0 -124
  30. package/bootstrap/ssh-hardening.js +0 -186
  31. package/bootstrap/ssh-hardening.ts +0 -192
  32. package/client.js +0 -234
  33. package/client.ts +0 -177
  34. package/config.js +0 -7
  35. package/config.ts +0 -5
  36. package/errors.js +0 -345
  37. package/errors.ts +0 -371
  38. package/index.js +0 -73
  39. package/index.ts +0 -59
  40. package/onboarding/doppler.ts +0 -116
  41. package/onboarding/git.ts +0 -133
  42. package/onboarding/index.ts +0 -18
  43. package/onboarding/onboarding.ts +0 -193
  44. package/onboarding/tailscale.ts +0 -159
  45. package/onboarding/types.ts +0 -115
  46. package/pricing.js +0 -387
  47. package/pricing.ts +0 -422
  48. package/schemas.js +0 -667
  49. package/schemas.ts +0 -765
  50. package/server-status.js +0 -122
  51. package/server-status.ts +0 -81
  52. package/servers.js +0 -667
  53. package/servers.ts +0 -568
  54. package/ssh-keys.js +0 -180
  55. package/ssh-keys.ts +0 -122
  56. package/ssh-setup.js +0 -253
  57. package/ssh-setup.ts +0 -218
  58. package/types.js +0 -99
  59. package/types.ts +0 -389
  60. package/volumes.js +0 -295
  61. package/volumes.ts +0 -229
@@ -1,230 +0,0 @@
1
- /**
2
- * Kernel Hardening Module Tests
3
- *
4
- * Tests for kernel hardening cloud-init components.
5
- */
6
-
7
- import { test, expect } from "bun:test";
8
- import {
9
- kernelHardeningPackages,
10
- kernelHardeningWriteFiles,
11
- kernelHardeningRunCmd,
12
- } from "./kernel-hardening";
13
-
14
- test("kernelHardeningPackages returns empty array (no packages needed)", () => {
15
- const packages = kernelHardeningPackages();
16
- expect(packages).toBeArray();
17
- expect(packages).toHaveLength(0);
18
- });
19
-
20
- test("kernelHardeningWriteFiles returns sysctl configuration", () => {
21
- const writeFiles = kernelHardeningWriteFiles();
22
- expect(writeFiles).toBeArray();
23
- expect(writeFiles.length).toBeGreaterThan(0);
24
-
25
- // Check for key file path
26
- const filesContent = writeFiles.join("\n");
27
- expect(filesContent).toContain("/etc/sysctl.d/99-security-hardening.conf");
28
- expect(filesContent).toContain("owner: root:root");
29
- expect(filesContent).toContain("permissions: '0644'");
30
-
31
- // Check for key security settings
32
- expect(filesContent).toContain("net.ipv4.conf.all.rp_filter = 1");
33
- expect(filesContent).toContain("net.ipv4.tcp_syncookies = 1");
34
- expect(filesContent).toContain("kernel.randomize_va_space = 2");
35
- expect(filesContent).toContain("fs.suid_dumpable = 0");
36
- expect(filesContent).toContain("fs.protected_hardlinks = 1");
37
- expect(filesContent).toContain("fs.protected_symlinks = 1");
38
-
39
- // Check for 2026 best practices
40
- expect(filesContent).toContain("kernel.dmesg_restrict = 1");
41
- expect(filesContent).toContain("kernel.yama.ptrace_scope = 2");
42
- expect(filesContent).toContain("kernel.unprivileged_bpf_disabled = 1");
43
- });
44
-
45
- test("kernelHardeningWriteFiles includes all security categories", () => {
46
- const writeFiles = kernelHardeningWriteFiles();
47
- const filesContent = writeFiles.join("\n");
48
-
49
- // 1. IP Spoofing Protection
50
- expect(filesContent).toContain("rp_filter");
51
- expect(filesContent).toContain("log_martians");
52
- expect(filesContent).toContain("accept_redirects");
53
- expect(filesContent).toContain("secure_redirects");
54
- expect(filesContent).toContain("send_redirects");
55
-
56
- // 2. SYN Flood Protection
57
- expect(filesContent).toContain("tcp_syncookies");
58
- expect(filesContent).toContain("tcp_tw_reuse");
59
- expect(filesContent).toContain("tcp_max_syn_backlog");
60
- expect(filesContent).toContain("tcp_synack_retries");
61
- expect(filesContent).toContain("tcp_syn_retries");
62
-
63
- // 3. Network Stack Hardening
64
- expect(filesContent).toContain("icmp_echo_ignore_broadcasts");
65
- expect(filesContent).toContain("icmp_ignore_bogus_error_responses");
66
- expect(filesContent).toContain("tcp_timestamps");
67
- expect(filesContent).toContain("tcp_sack");
68
-
69
- // 4. Core Dump Restrictions
70
- expect(filesContent).toContain("suid_dumpable");
71
- expect(filesContent).toContain("core_pattern");
72
-
73
- // 5. Memory Protection (ASLR)
74
- expect(filesContent).toContain("randomize_va_space");
75
-
76
- // 6. Filesystem Protection
77
- expect(filesContent).toContain("protected_hardlinks");
78
- expect(filesContent).toContain("protected_symlinks");
79
- expect(filesContent).toContain("protected_fifos");
80
- expect(filesContent).toContain("protected_regular");
81
-
82
- // 7. Network Behavior Tuning
83
- expect(filesContent).toContain("tcp_fastopen");
84
- expect(filesContent).toContain("accept_source_route");
85
- expect(filesContent).toContain("tcp_window_scaling");
86
-
87
- // 8. Security-Related Kernel Parameters
88
- expect(filesContent).toContain("kernel.sysrq");
89
- expect(filesContent).toContain("kernel.kexec_load");
90
- expect(filesContent).toContain("user.max_user_namespaces");
91
- expect(filesContent).toContain("kernel.unprivileged_bpf_disabled");
92
-
93
- // 9. Additional Hardening (2026)
94
- expect(filesContent).toContain("kernel.dmesg_restrict");
95
- expect(filesContent).toContain("kernel.yama.ptrace_scope");
96
-
97
- // 10. Performance Tuning
98
- expect(filesContent).toContain("nf_conntrack_max");
99
- expect(filesContent).toContain("tcp_keepalive_time");
100
- expect(filesContent).toContain("tcp_keepalive_intvl");
101
- expect(filesContent).toContain("tcp_keepalive_probes");
102
- });
103
-
104
- test("kernelHardeningRunCmd returns activation commands", () => {
105
- const runCmd = kernelHardeningRunCmd();
106
- expect(runCmd).toBeArray();
107
- expect(runCmd.length).toBeGreaterThan(0);
108
-
109
- const cmdContent = runCmd.join("\n");
110
-
111
- // Check for sysctl application
112
- expect(cmdContent).toContain("sysctl --system");
113
-
114
- // Check for logging
115
- expect(cmdContent).toContain("/var/log/kernel-hardening.log");
116
-
117
- // Check for summary display
118
- expect(cmdContent).toContain("Kernel Hardening Applied (2026)");
119
- expect(cmdContent).toContain("IP Spoof Protection:");
120
- expect(cmdContent).toContain("SYN Cookies:");
121
- expect(cmdContent).toContain("ASLR Level:");
122
- expect(cmdContent).toContain("SUID Core Dumps:");
123
- expect(cmdContent).toContain("Hard Links Protected:");
124
- expect(cmdContent).toContain("Ptrace Scope:");
125
- });
126
-
127
- test("kernelHardeningRunCmd includes verification commands", () => {
128
- const runCmd = kernelHardeningRunCmd();
129
- const cmdContent = runCmd.join("\n");
130
-
131
- // Check for sysctl commands to verify settings
132
- expect(cmdContent).toContain("sysctl -n net.ipv4.conf.all.rp_filter");
133
- expect(cmdContent).toContain("sysctl -n net.ipv4.tcp_syncookies");
134
- expect(cmdContent).toContain("sysctl -n kernel.randomize_va_space");
135
- expect(cmdContent).toContain("sysctl -n fs.suid_dumpable");
136
- expect(cmdContent).toContain("sysctl -n fs.protected_hardlinks");
137
- expect(cmdContent).toContain("sysctl -n kernel.yama.ptrace_scope");
138
- });
139
-
140
- test("kernel hardening settings match CIS benchmarks", () => {
141
- const writeFiles = kernelHardeningWriteFiles();
142
- const filesContent = writeFiles.join("\n");
143
-
144
- // CIS Benchmark 1.5.1: Ensure core dumps are restricted
145
- expect(filesContent).toContain("fs.suid_dumpable = 0");
146
-
147
- // CIS Benchmark 3.3.1: Ensure IP forwarding is disabled (not set by default)
148
- // We don't set this as it may be needed for container workloads
149
-
150
- // CIS Benchmark 3.3.2: Ensure send redirects is disabled
151
- expect(filesContent).toContain("net.ipv4.conf.all.send_redirects = 0");
152
-
153
- // CIS Benchmark 3.3.3: Ensure ICMP redirects are not accepted
154
- expect(filesContent).toContain("net.ipv4.conf.all.accept_redirects = 0");
155
-
156
- // CIS Benchmark 3.3.4: Ensure secure ICMP redirects are not accepted
157
- expect(filesContent).toContain("net.ipv4.conf.all.secure_redirects = 0");
158
-
159
- // CIS Benchmark 3.3.5: Ensure suspicious packets are logged
160
- expect(filesContent).toContain("net.ipv4.conf.all.log_martians = 1");
161
-
162
- // CIS Benchmark 3.3.6: Ensure broadcast ICMP requests are ignored
163
- expect(filesContent).toContain("net.ipv4.icmp_echo_ignore_broadcasts = 1");
164
-
165
- // CIS Benchmark 3.3.7: Ensure bogus ICMP responses are ignored
166
- expect(filesContent).toContain("net.ipv4.icmp_ignore_bogus_error_responses = 1");
167
-
168
- // CIS Benchmark 3.3.8: Ensure Reverse Path Filtering is enabled
169
- expect(filesContent).toContain("net.ipv4.conf.all.rp_filter = 1");
170
-
171
- // CIS Benchmark 3.3.9: Ensure TCP SYN Cookies is enabled
172
- expect(filesContent).toContain("net.ipv4.tcp_syncookies = 1");
173
-
174
- // CIS Benchmark 3.3.10: Ensure IPv6 is disabled (optional, commented out)
175
- // We don't disable IPv6 by default as it may be needed
176
-
177
- // CIS Benchmark 1.5.2: Ensure address space layout randomization (ASLR) is enabled
178
- expect(filesContent).toContain("kernel.randomize_va_space = 2");
179
-
180
- // CIS Benchmark 1.5.3: Ensure prelink is disabled (package removal, not in sysctl)
181
- // Not applicable to sysctl configuration
182
-
183
- // CIS Benchmark 1.5.4: Ensure core dump backtraces are disabled
184
- expect(filesContent).toContain("fs.suid_dumpable = 0");
185
- });
186
-
187
- test("kernel hardening includes 2026 best practices", () => {
188
- const writeFiles = kernelHardeningWriteFiles();
189
- const filesContent = writeFiles.join("\n");
190
-
191
- // Modern kernel hardening (2026)
192
- expect(filesContent).toContain("kernel.dmesg_restrict = 1");
193
- expect(filesContent).toContain("kernel.yama.ptrace_scope = 2");
194
- expect(filesContent).toContain("kernel.unprivileged_bpf_disabled = 1");
195
- expect(filesContent).toContain("user.max_user_namespaces = 0");
196
- expect(filesContent).toContain("kernel.kexec_load = 0");
197
-
198
- // Filesystem hard links/symlinks protection (TOCTOU prevention)
199
- expect(filesContent).toContain("fs.protected_hardlinks = 1");
200
- expect(filesContent).toContain("fs.protected_symlinks = 1");
201
- expect(filesContent).toContain("fs.protected_fifos = 2");
202
- expect(filesContent).toContain("fs.protected_regular = 2");
203
-
204
- // Performance tuning with security in mind
205
- expect(filesContent).toContain("net.netfilter.nf_conntrack_max = 262144");
206
- expect(filesContent).toContain("net.ipv4.tcp_keepalive_time = 600");
207
- });
208
-
209
- test("kernel hardening has proper documentation headers", () => {
210
- const writeFiles = kernelHardeningWriteFiles();
211
- const filesContent = writeFiles.join("\n");
212
-
213
- // Check for documentation and headers
214
- expect(filesContent).toContain("Kernel Security Hardening Configuration");
215
- expect(filesContent).toContain("2026 best practices");
216
- expect(filesContent).toContain("CIS Benchmark");
217
- expect(filesContent).toContain("NIST");
218
-
219
- // Check for section headers
220
- expect(filesContent).toContain("IP SPOOFING PROTECTION");
221
- expect(filesContent).toContain("SYN FLOOD PROTECTION");
222
- expect(filesContent).toContain("NETWORK STACK HARDENING");
223
- expect(filesContent).toContain("CORE DUMP RESTRICTIONS");
224
- expect(filesContent).toContain("MEMORY PROTECTION (ASLR)");
225
- expect(filesContent).toContain("FILESYSTEM PROTECTION");
226
- expect(filesContent).toContain("NETWORK BEHAVIOR TUNING");
227
- expect(filesContent).toContain("SECURITY-RELATED KERNEL PARAMETERS");
228
- expect(filesContent).toContain("ADDITIONAL HARDENING (2026)");
229
- expect(filesContent).toContain("PERFORMANCE TUNING");
230
- });
@@ -1,272 +0,0 @@
1
- /**
2
- * Kernel Hardening Cloud-Init Components
3
- *
4
- * Composable cloud-init blocks for securing the Linux kernel on new servers.
5
- * Implements 2026 best practices for network stack hardening, IP spoofing
6
- * protection, SYN flood mitigation, and secure core dump policies.
7
- *
8
- * Background: Public-facing VPS servers are constantly probed and attacked.
9
- * Default Linux kernel settings prioritize compatibility over security. This
10
- * module applies CIS Benchmark-aligned hardening via /etc/sysctl.d/ which
11
- * persists across reboots and overrides defaults.
12
- *
13
- * Three composable functions return cloud-init line arrays for splicing into
14
- * the appropriate YAML sections:
15
- * - kernelHardeningPackages() → packages: section (currently empty, reserved)
16
- * - kernelHardeningWriteFiles() → write_files: section (drops sysctl config)
17
- * - kernelHardeningRunCmd() → runcmd: section (applies settings immediately)
18
- *
19
- * Security Measures Implemented:
20
- * 1. Network Stack Hardening: SYN cookies, ICMP rate limits, martian packet logging
21
- * 2. IP Spoofing Protection: Reverse path filtering, source address verification
22
- * 3. SYN Flood Protection: TCP SYN cookies, reuse time_wait connections
23
- * 4. Core Dump Restrictions: Disable setuid dumps, limit core dump size to 0
24
- * 5. File Permissions: Hard links, symlinks, FIFO protection
25
- * 6. Memory Protection: ASLR, randomize_va_space
26
- *
27
- * References:
28
- * - CIS Benchmark for Ubuntu Linux 24.04
29
- * - NIST SP 800-53 Revision 5
30
- * - https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt
31
- */
32
-
33
- /**
34
- * Packages required for kernel hardening.
35
- * Returns cloud-init YAML lines for the `packages:` section.
36
- *
37
- * Note: All kernel hardening is done via sysctl configuration, which uses
38
- * built-in kernel functionality. No additional packages are required.
39
- * This function is reserved for future expansion (e.g., auditd, kexec-tools).
40
- */
41
- export function kernelHardeningPackages(): string[] {
42
- return [
43
- // Reserved for future packages (auditd, kexec-tools, etc.)
44
- // Currently empty - all hardening via sysctl
45
- ];
46
- }
47
-
48
- /**
49
- * Kernel sysctl configuration file for comprehensive hardening.
50
- * Returns cloud-init YAML lines for the `write_files:` section.
51
- *
52
- * Drops /etc/sysctl.d/99-security-hardening.conf which:
53
- * - Takes precedence over /etc/sysctl.conf (99- prefix ensures last load)
54
- * - Persists across reboots (sysctl.d files are applied on boot)
55
- * - Can be applied immediately via `sysctl --system` (see runcmd)
56
- *
57
- * Settings organized by category:
58
- * 1. IP Spoofing Protection: rp_filter, secure redirects
59
- * 2. SYN Flood Protection: syncookies, tcp_tw_reuse
60
- * 3. Network Stack: ICMP rate limits, martian logging, ignore broadcasts
61
- * 4. Core Dumps: Disabled for setuid programs, limited for all processes
62
- * 5. Memory Protection: ASLR, randomize_va_space
63
- * 6. Filesystem: Hard link/symlink protection
64
- */
65
- export function kernelHardeningWriteFiles(): string[] {
66
- const lines: string[] = [];
67
-
68
- lines.push(" # Kernel hardening: sysctl.d configuration for 2026 best practices");
69
- lines.push(" # This file persists across reboots and overrides /etc/sysctl.conf");
70
- lines.push(" - path: /etc/sysctl.d/99-security-hardening.conf");
71
- lines.push(" owner: root:root");
72
- lines.push(" permissions: '0644'");
73
- lines.push(" content: |");
74
- lines.push(" # =================================================================");
75
- lines.push(" # Kernel Security Hardening Configuration");
76
- lines.push(" # =================================================================");
77
- lines.push(" # Applied via cloud-init for com.hetzner.codespaces");
78
- lines.push(" # Version: 1.0.0 (2026 best practices)");
79
- lines.push(" #");
80
- lines.push(" # This configuration follows CIS Benchmark and NIST guidelines");
81
- lines.push(" # See: /usr/share/doc/linux-doc/sysctl/ for parameter documentation");
82
- lines.push("");
83
- lines.push(" # =================================================================");
84
- lines.push(" # 1. IP SPOOFING PROTECTION");
85
- lines.push(" # =================================================================");
86
- lines.push(" # Enable reverse path filtering (validates source addresses)");
87
- lines.push(" # Prevents IP spoofing attacks by dropping packets with invalid sources");
88
- lines.push(" net.ipv4.conf.all.rp_filter = 1");
89
- lines.push(" net.ipv4.conf.default.rp_filter = 1");
90
- lines.push("");
91
- lines.push(" # Log martian packets (packets with impossible addresses)");
92
- lines.push(" # Helps detect spoofing attempts and network misconfigurations");
93
- lines.push(" net.ipv4.conf.all.log_martians = 1");
94
- lines.push("");
95
- lines.push(" # Disable ICMP redirect acceptance (prevent MITM attacks)");
96
- lines.push(" net.ipv4.conf.all.accept_redirects = 0");
97
- lines.push(" net.ipv4.conf.default.accept_redirects = 0");
98
- lines.push(" net.ipv4.conf.all.secure_redirects = 0");
99
- lines.push(" net.ipv4.conf.default.secure_redirects = 0");
100
- lines.push("");
101
- lines.push(" # Disable sending ICMP redirects");
102
- lines.push(" net.ipv4.conf.all.send_redirects = 0");
103
- lines.push(" net.ipv4.conf.default.send_redirects = 0");
104
- lines.push("");
105
- lines.push(" # =================================================================");
106
- lines.push(" # 2. SYN FLOOD PROTECTION");
107
- lines.push(" # =================================================================");
108
- lines.push(" # Enable SYN cookies (protects against SYN flood attacks)");
109
- lines.push(" # Allows server to continue accepting connections under SYN flood");
110
- lines.push(" net.ipv4.tcp_syncookies = 1");
111
- lines.push("");
112
- lines.push(" # Reuse TIME_WAIT sockets for new connections (safer, faster)");
113
- lines.push(" # Reduces connection table exhaustion under high load");
114
- lines.push(" net.ipv4.tcp_tw_reuse = 1");
115
- lines.push("");
116
- lines.push(" # Reduce SYN backlog and timeouts for faster detection");
117
- lines.push(" net.ipv4.tcp_max_syn_backlog = 2048");
118
- lines.push(" net.ipv4.tcp_synack_retries = 2");
119
- lines.push(" net.ipv4.tcp_syn_retries = 5");
120
- lines.push("");
121
- lines.push(" # =================================================================");
122
- lines.push(" # 3. NETWORK STACK HARDENING");
123
- lines.push(" # =================================================================");
124
- lines.push(" # Disable ICMP redirect acceptance (IPv6)");
125
- lines.push(" net.ipv6.conf.all.accept_redirects = 0");
126
- lines.push(" net.ipv6.conf.default.accept_redirects = 0");
127
- lines.push("");
128
- lines.push(" # Ignore ICMP broadcasts (prevent smurf attacks)");
129
- lines.push(" net.ipv4.icmp_echo_ignore_broadcasts = 1");
130
- lines.push("");
131
- lines.push(" # Ignore bogus ICMP error responses (prevent ICMP attacks)");
132
- lines.push(" net.ipv4.icmp_ignore_bogus_error_responses = 1");
133
- lines.push("");
134
- lines.push(" # Enable TCP timestamps (RFC 1323) for better sequence handling");
135
- lines.push(" # Also protects against wrapped sequence number attacks");
136
- lines.push(" net.ipv4.tcp_timestamps = 1");
137
- lines.push("");
138
- lines.push(" # Enable TCP selective acknowledgments (better performance)");
139
- lines.push(" net.ipv4.tcp_sack = 1");
140
- lines.push("");
141
- lines.push(" # =================================================================");
142
- lines.push(" # 4. CORE DUMP RESTRICTIONS");
143
- lines.push(" # =================================================================");
144
- lines.push(" # Disable core dumps for setuid programs (prevent privilege escalation)");
145
- lines.push(" fs.suid_dumpable = 0");
146
- lines.push("");
147
- lines.push(" # Limit core dump size to 0 (disable core dumps)");
148
- lines.push(" # Override in /etc/security/limits.conf if needed for debugging");
149
- lines.push(" kernel.core_pattern = |/bin/false");
150
- lines.push("");
151
- lines.push(" # =================================================================");
152
- lines.push(" # 5. MEMORY PROTECTION (ASLR)");
153
- lines.push(" # =================================================================");
154
- lines.push(" # Enable Address Space Layout Randomization (full)");
155
- lines.push(" # Makes exploitation of memory corruption vulnerabilities harder");
156
- lines.push(" # 0: Disabled, 1: Conservative, 2: Full (default)");
157
- lines.push(" kernel.randomize_va_space = 2");
158
- lines.push("");
159
- lines.push(" # =================================================================");
160
- lines.push(" # 6. FILESYSTEM PROTECTION");
161
- lines.push(" # =================================================================");
162
- lines.push(" # Hard link/symlink protection (prevent time-of-check time-of-use)");
163
- lines.push(" fs.protected_hardlinks = 1");
164
- lines.push(" fs.protected_symlinks = 1");
165
- lines.push("");
166
- lines.push(" # FIFO protection (prevent FIFO attacks on world-writable directories)");
167
- lines.push(" fs.protected_fifos = 2");
168
- lines.push("");
169
- lines.push(" # Regular file protection (prevent file overwrite attacks)");
170
- lines.push(" fs.protected_regular = 2");
171
- lines.push("");
172
- lines.push(" # =================================================================");
173
- lines.push(" # 7. NETWORK BEHAVIOR TUNING");
174
- lines.push(" # =================================================================");
175
- lines.push(" # Enable TCP Fast Open (TFO) for reduced latency");
176
- lines.push(" net.ipv4.tcp_fastopen = 3");
177
- lines.push("");
178
- lines.push(" # Disable source routing (prevent packet routing manipulation)");
179
- lines.push(" net.ipv4.conf.all.accept_source_route = 0");
180
- lines.push(" net.ipv4.conf.default.accept_source_route = 0");
181
- lines.push(" net.ipv6.conf.all.accept_source_route = 0");
182
- lines.push(" net.ipv6.conf.default.accept_source_route = 0");
183
- lines.push("");
184
- lines.push(" # Enable TCP window scaling (RFC 7323) for high-bandwidth links");
185
- lines.push(" net.ipv4.tcp_window_scaling = 1");
186
- lines.push("");
187
- lines.push(" # =================================================================");
188
- lines.push(" # 8. SECURITY-RELATED KERNEL PARAMETERS");
189
- lines.push(" # =================================================================");
190
- lines.push(" # Disable magic sysrq key (prevent console-based attacks)");
191
- lines.push(" # 0: Disabled, 1: Enable (for debugging only)");
192
- lines.push(" kernel.sysrq = 0");
193
- lines.push("");
194
- lines.push(" # Disable kexec system call (prevent kernel replacement)");
195
- lines.push(" # 0: Disabled, 1: Enabled");
196
- lines.push(" kernel.kexec_load = 0");
197
- lines.push("");
198
- lines.push(" # Disable user namespaces (prevent container breakouts)");
199
- lines.push(" # 0: Disabled, 1: Enabled");
200
- lines.push(" user.max_user_namespaces = 0");
201
- lines.push("");
202
- lines.push(" # Enable unprivileged bpf disabled (prevent eBPF-based exploits)");
203
- lines.push(" # 0: Disabled, 1: Enabled");
204
- lines.push(" kernel.unprivileged_bpf_disabled = 1");
205
- lines.push("");
206
- lines.push(" # =================================================================");
207
- lines.push(" # 9. ADDITIONAL HARDENING (2026)");
208
- lines.push(" # =================================================================");
209
- lines.push(" # Disable IPv6 if not needed (uncomment if IPv6 is disabled)");
210
- lines.push(" # net.ipv6.conf.all.disable_ipv6 = 1");
211
- lines.push(" # net.ipv6.conf.default.disable_ipv6 = 1");
212
- lines.push("");
213
- lines.push(" # Enable dmesg restriction (prevent kernel info leaks)");
214
- lines.push(" kernel.dmesg_restrict = 1");
215
- lines.push("");
216
- lines.push(" # Restrict ptrace scope (prevent process tracing by non-parent)");
217
- lines.push(" # 0: Traditional, 1: Restricted, 2: Admin-only, 3: No attach");
218
- lines.push(" kernel.yama.ptrace_scope = 2");
219
- lines.push("");
220
- lines.push(" # =================================================================");
221
- lines.push(" # 10. PERFORMANCE TUNING (safe defaults)");
222
- lines.push(" # =================================================================");
223
- lines.push(" # Increase connection tracking table size (for stateful firewalls)");
224
- lines.push(" net.netfilter.nf_conntrack_max = 262144");
225
- lines.push("");
226
- lines.push(" # Reduce TCP keepalive timeouts for faster dead peer detection");
227
- lines.push(" net.ipv4.tcp_keepalive_time = 600");
228
- lines.push(" net.ipv4.tcp_keepalive_intvl = 30");
229
- lines.push(" net.ipv4.tcp_keepalive_probes = 3");
230
- lines.push("");
231
-
232
- return lines;
233
- }
234
-
235
- /**
236
- * Commands to apply kernel hardening settings immediately at first boot.
237
- * Returns cloud-init YAML lines for the `runcmd:` section.
238
- *
239
- * Order matters:
240
- * 1. Load all sysctl settings from /etc/sysctl.d/*.conf
241
- * 2. Apply settings immediately (don't wait for reboot)
242
- * 3. Log applied settings for audit trail
243
- * 4. Display summary for cloud-init output verification
244
- */
245
- export function kernelHardeningRunCmd(): string[] {
246
- const lines: string[] = [];
247
-
248
- lines.push(" # Kernel hardening: apply sysctl settings immediately");
249
- lines.push(" # Settings are already in /etc/sysctl.d/99-security-hardening.conf");
250
- lines.push("");
251
- lines.push(" # Apply all sysctl settings (overrides defaults immediately)");
252
- lines.push(" - sysctl --system");
253
- lines.push("");
254
- lines.push(" # Log applied settings for audit trail");
255
- lines.push(" - sysctl -a | grep -E '(rp_filter|syncookies|randomize_va|suid_dump)' > /var/log/kernel-hardening.log 2>&1 || true");
256
- lines.push("");
257
- lines.push(" # Display summary of critical hardening settings");
258
- lines.push(" - |");
259
- lines.push(" echo '========================================'");
260
- lines.push(" echo 'Kernel Hardening Applied (2026)'");
261
- lines.push(" echo '========================================'");
262
- lines.push(" echo 'IP Spoof Protection: '$(sysctl -n net.ipv4.conf.all.rp_filter)");
263
- lines.push(" echo 'SYN Cookies: '$(sysctl -n net.ipv4.tcp_syncookies)");
264
- lines.push(" echo 'ASLR Level: '$(sysctl -n kernel.randomize_va_space)");
265
- lines.push(" echo 'SUID Core Dumps: '$(sysctl -n fs.suid_dumpable)");
266
- lines.push(" echo 'Hard Links Protected: '$(sysctl -n fs.protected_hardlinks)");
267
- lines.push(" echo 'Ptrace Scope: '$(sysctl -n kernel.yama.ptrace_scope)");
268
- lines.push(" echo '========================================'");
269
- lines.push("");
270
-
271
- return lines;
272
- }
@@ -1,122 +0,0 @@
1
- "use strict";
2
- /**
3
- * Security Audit Cloud-Init Components
4
- *
5
- * Composable cloud-init blocks for running post-bootstrap security audits.
6
- * Generates comprehensive security reports for verification and compliance.
7
- *
8
- * This module runs LAST in the bootstrap sequence, after all other security
9
- * hardening is applied. It captures the state of the system for verification.
10
- *
11
- * Three composable functions return cloud-init line arrays for splicing into
12
- * the appropriate YAML sections:
13
- * - securityAuditPackages() → packages: section
14
- * - securityAuditWriteFiles() → write_files: section
15
- * - securityAuditRunCmd() → runcmd: section
16
- */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.securityAuditPackages = securityAuditPackages;
19
- exports.securityAuditWriteFiles = securityAuditWriteFiles;
20
- exports.securityAuditRunCmd = securityAuditRunCmd;
21
- /**
22
- * Packages required for security auditing.
23
- * Returns cloud-init YAML lines for the `packages:` section.
24
- *
25
- * - lynis: Comprehensive security auditing tool
26
- */
27
- function securityAuditPackages() {
28
- return [
29
- " - lynis",
30
- ];
31
- }
32
- /**
33
- * Files to write at first boot for security auditing.
34
- * Returns cloud-init YAML lines for the `write_files:` section.
35
- *
36
- * Drops 1 file onto the server:
37
- *
38
- * 1. /opt/monitoring/security-audit.sh
39
- * - Collects security metrics (UFW, fail2ban, sshd, sysctl)
40
- * - Runs Lynis audit with warnings-only output
41
- * - Generates JSON report at /var/log/security-audit.json
42
- * - Logs summary to /var/log/security-audit.log
43
- */
44
- function securityAuditWriteFiles() {
45
- var lines = [];
46
- // 1. Security audit script - comprehensive security check
47
- lines.push(" # Security audit script - runs after all hardening");
48
- lines.push(" - path: /opt/monitoring/security-audit.sh");
49
- lines.push(" owner: root:root");
50
- lines.push(" permissions: '0755'");
51
- lines.push(" content: |");
52
- lines.push(" #!/bin/bash");
53
- lines.push(" set -euo pipefail");
54
- lines.push(" LOGFILE=\"/var/log/security-audit.log\"");
55
- lines.push(" REPORTFILE=\"/var/log/security-audit.json\"");
56
- lines.push(" echo \"Security Audit started at $(date -Iseconds)\" | tee \"$LOGFILE\"");
57
- lines.push("");
58
- lines.push(" # Firewall status");
59
- lines.push(" echo \"\" | tee -a \"$LOGFILE\"");
60
- lines.push(" echo \"=== UFW Firewall Status ===\" | tee -a \"$LOGFILE\"");
61
- lines.push(" ufw status verbose | tee -a \"$LOGFILE\" || true");
62
- lines.push("");
63
- lines.push(" # Fail2ban status");
64
- lines.push(" echo \"\" | tee -a \"$LOGFILE\"");
65
- lines.push(" echo \"=== Fail2ban Status ===\" | tee -a \"$LOGFILE\"");
66
- lines.push(" systemctl status fail2ban --no-pager | tee -a \"$LOGFILE\" || true");
67
- lines.push(" fail2ban-client status sshd | tee -a \"$LOGFILE\" || true");
68
- lines.push("");
69
- lines.push(" # SSHd status");
70
- lines.push(" echo \"\" | tee -a \"$LOGFILE\"");
71
- lines.push(" echo \"=== SSHd Status ===\" | tee -a \"$LOGFILE\"");
72
- lines.push(" systemctl status ssh --no-pager | tee -a \"$LOGFILE\" || true");
73
- lines.push(" sshd -T | grep -E '(PasswordAuthentication|PermitRootLogin|MaxStartups)' | tee -a \"$LOGFILE\" || true");
74
- lines.push("");
75
- lines.push(" # Kernel hardening status");
76
- lines.push(" echo \"\" | tee -a \"$LOGFILE\"");
77
- lines.push(" echo \"=== Kernel Hardening Status ===\" | tee -a \"$LOGFILE\"");
78
- lines.push(" sysctl randomize_va_space kptr_restrict tcp_syncookies | tee -a \"$LOGFILE\" || true");
79
- lines.push("");
80
- lines.push(" # Lynis audit (warnings only, non-interactive)");
81
- lines.push(" echo \"\" | tee -a \"$LOGFILE\"");
82
- lines.push(" echo \"=== Lynis Security Audit ===\" | tee -a \"$LOGFILE\"");
83
- lines.push(" lynis audit system --warnings-only --quiet 2>&1 | tee -a \"$LOGFILE\" || true");
84
- lines.push("");
85
- lines.push(" # Generate JSON report");
86
- lines.push(" timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)");
87
- lines.push(" ufw_active=$(ufw status | grep -c \"Status: active\" || echo \"0\")");
88
- lines.push(" fail2ban_active=$(systemctl is-active fail2ban 2>/dev/null || echo \"inactive\")");
89
- lines.push(" sshd_active=$(systemctl is-active ssh 2>/dev/null || echo \"inactive\")");
90
- lines.push(" aslr_enabled=$(cat /proc/sys/kernel/randomize_va_space 2>/dev/null || echo \"0\")");
91
- lines.push(" cat > \"$REPORTFILE\" <<AUDEOF");
92
- lines.push(" {");
93
- lines.push(" \"timestamp\": \"$timestamp\",");
94
- lines.push(" \"firewall\": { \"active\": $ufw_active },");
95
- lines.push(" \"fail2ban\": { \"active\": \"$fail2ban_active\" },");
96
- lines.push(" \"sshd\": { \"active\": \"$sshd_active\" },");
97
- lines.push(" \"kernel_hardening\": { \"aslr_enabled\": $aslr_enabled }");
98
- lines.push(" }");
99
- lines.push(" AUDEOF");
100
- lines.push(" echo \"Security Audit completed at $(date -Iseconds)\" | tee -a \"$LOGFILE\"");
101
- lines.push(" echo \"Report saved to $REPORTFILE\" | tee -a \"$LOGFILE\"");
102
- lines.push("");
103
- return lines;
104
- }
105
- /**
106
- * Commands to run security audit at first boot.
107
- * Returns cloud-init YAML lines for the `runcmd:` section.
108
- *
109
- * Order matters:
110
- * 1. Create /opt/monitoring directory (audit script target)
111
- * 2. Run security audit script (captures state after all hardening)
112
- * 3. Log audit completion for verification
113
- */
114
- function securityAuditRunCmd() {
115
- var lines = [];
116
- lines.push(" # Security audit: run comprehensive security check");
117
- lines.push(" - mkdir -p /opt/monitoring");
118
- lines.push(" - /opt/monitoring/security-audit.sh");
119
- lines.push(" - echo \"Security audit completed at $(date -Iseconds)\" | tee -a /root/.bootstrap-status");
120
- lines.push("");
121
- return lines;
122
- }