@ebowwa/hetzner 0.1.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.
- package/actions.js +802 -0
- package/actions.ts +1053 -0
- package/auth.js +35 -0
- package/auth.ts +37 -0
- package/bootstrap/FIREWALL.md +326 -0
- package/bootstrap/KERNEL-HARDENING.md +258 -0
- package/bootstrap/SECURITY-INTEGRATION.md +281 -0
- package/bootstrap/TESTING.md +301 -0
- package/bootstrap/cloud-init.js +279 -0
- package/bootstrap/cloud-init.ts +394 -0
- package/bootstrap/firewall.js +279 -0
- package/bootstrap/firewall.ts +342 -0
- package/bootstrap/genesis.js +406 -0
- package/bootstrap/genesis.ts +518 -0
- package/bootstrap/index.js +35 -0
- package/bootstrap/index.ts +71 -0
- package/bootstrap/kernel-hardening.js +266 -0
- package/bootstrap/kernel-hardening.test.ts +230 -0
- package/bootstrap/kernel-hardening.ts +272 -0
- package/bootstrap/security-audit.js +118 -0
- package/bootstrap/security-audit.ts +124 -0
- package/bootstrap/ssh-hardening.js +182 -0
- package/bootstrap/ssh-hardening.ts +192 -0
- package/client.js +137 -0
- package/client.ts +177 -0
- package/config.js +5 -0
- package/config.ts +5 -0
- package/errors.js +270 -0
- package/errors.ts +371 -0
- package/index.js +28 -0
- package/index.ts +55 -0
- package/package.json +56 -0
- package/pricing.js +284 -0
- package/pricing.ts +422 -0
- package/schemas.js +660 -0
- package/schemas.ts +765 -0
- package/server-status.ts +81 -0
- package/servers.js +424 -0
- package/servers.ts +568 -0
- package/ssh-keys.js +90 -0
- package/ssh-keys.ts +122 -0
- package/ssh-setup.ts +218 -0
- package/types.js +96 -0
- package/types.ts +389 -0
- package/volumes.js +172 -0
- package/volumes.ts +229 -0
|
@@ -0,0 +1,266 @@
|
|
|
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
|
+
* Packages required for kernel hardening.
|
|
34
|
+
* Returns cloud-init YAML lines for the `packages:` section.
|
|
35
|
+
*
|
|
36
|
+
* Note: All kernel hardening is done via sysctl configuration, which uses
|
|
37
|
+
* built-in kernel functionality. No additional packages are required.
|
|
38
|
+
* This function is reserved for future expansion (e.g., auditd, kexec-tools).
|
|
39
|
+
*/
|
|
40
|
+
export function kernelHardeningPackages() {
|
|
41
|
+
return [
|
|
42
|
+
// Reserved for future packages (auditd, kexec-tools, etc.)
|
|
43
|
+
// Currently empty - all hardening via sysctl
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Kernel sysctl configuration file for comprehensive hardening.
|
|
48
|
+
* Returns cloud-init YAML lines for the `write_files:` section.
|
|
49
|
+
*
|
|
50
|
+
* Drops /etc/sysctl.d/99-security-hardening.conf which:
|
|
51
|
+
* - Takes precedence over /etc/sysctl.conf (99- prefix ensures last load)
|
|
52
|
+
* - Persists across reboots (sysctl.d files are applied on boot)
|
|
53
|
+
* - Can be applied immediately via `sysctl --system` (see runcmd)
|
|
54
|
+
*
|
|
55
|
+
* Settings organized by category:
|
|
56
|
+
* 1. IP Spoofing Protection: rp_filter, secure redirects
|
|
57
|
+
* 2. SYN Flood Protection: syncookies, tcp_tw_reuse
|
|
58
|
+
* 3. Network Stack: ICMP rate limits, martian logging, ignore broadcasts
|
|
59
|
+
* 4. Core Dumps: Disabled for setuid programs, limited for all processes
|
|
60
|
+
* 5. Memory Protection: ASLR, randomize_va_space
|
|
61
|
+
* 6. Filesystem: Hard link/symlink protection
|
|
62
|
+
*/
|
|
63
|
+
export function kernelHardeningWriteFiles() {
|
|
64
|
+
const lines = [];
|
|
65
|
+
lines.push(" # Kernel hardening: sysctl.d configuration for 2026 best practices");
|
|
66
|
+
lines.push(" # This file persists across reboots and overrides /etc/sysctl.conf");
|
|
67
|
+
lines.push(" - path: /etc/sysctl.d/99-security-hardening.conf");
|
|
68
|
+
lines.push(" owner: root:root");
|
|
69
|
+
lines.push(" permissions: '0644'");
|
|
70
|
+
lines.push(" content: |");
|
|
71
|
+
lines.push(" # =================================================================");
|
|
72
|
+
lines.push(" # Kernel Security Hardening Configuration");
|
|
73
|
+
lines.push(" # =================================================================");
|
|
74
|
+
lines.push(" # Applied via cloud-init for com.hetzner.codespaces");
|
|
75
|
+
lines.push(" # Version: 1.0.0 (2026 best practices)");
|
|
76
|
+
lines.push(" #");
|
|
77
|
+
lines.push(" # This configuration follows CIS Benchmark and NIST guidelines");
|
|
78
|
+
lines.push(" # See: /usr/share/doc/linux-doc/sysctl/ for parameter documentation");
|
|
79
|
+
lines.push("");
|
|
80
|
+
lines.push(" # =================================================================");
|
|
81
|
+
lines.push(" # 1. IP SPOOFING PROTECTION");
|
|
82
|
+
lines.push(" # =================================================================");
|
|
83
|
+
lines.push(" # Enable reverse path filtering (validates source addresses)");
|
|
84
|
+
lines.push(" # Prevents IP spoofing attacks by dropping packets with invalid sources");
|
|
85
|
+
lines.push(" net.ipv4.conf.all.rp_filter = 1");
|
|
86
|
+
lines.push(" net.ipv4.conf.default.rp_filter = 1");
|
|
87
|
+
lines.push("");
|
|
88
|
+
lines.push(" # Log martian packets (packets with impossible addresses)");
|
|
89
|
+
lines.push(" # Helps detect spoofing attempts and network misconfigurations");
|
|
90
|
+
lines.push(" net.ipv4.conf.all.log_martians = 1");
|
|
91
|
+
lines.push("");
|
|
92
|
+
lines.push(" # Disable ICMP redirect acceptance (prevent MITM attacks)");
|
|
93
|
+
lines.push(" net.ipv4.conf.all.accept_redirects = 0");
|
|
94
|
+
lines.push(" net.ipv4.conf.default.accept_redirects = 0");
|
|
95
|
+
lines.push(" net.ipv4.conf.all.secure_redirects = 0");
|
|
96
|
+
lines.push(" net.ipv4.conf.default.secure_redirects = 0");
|
|
97
|
+
lines.push("");
|
|
98
|
+
lines.push(" # Disable sending ICMP redirects");
|
|
99
|
+
lines.push(" net.ipv4.conf.all.send_redirects = 0");
|
|
100
|
+
lines.push(" net.ipv4.conf.default.send_redirects = 0");
|
|
101
|
+
lines.push("");
|
|
102
|
+
lines.push(" # =================================================================");
|
|
103
|
+
lines.push(" # 2. SYN FLOOD PROTECTION");
|
|
104
|
+
lines.push(" # =================================================================");
|
|
105
|
+
lines.push(" # Enable SYN cookies (protects against SYN flood attacks)");
|
|
106
|
+
lines.push(" # Allows server to continue accepting connections under SYN flood");
|
|
107
|
+
lines.push(" net.ipv4.tcp_syncookies = 1");
|
|
108
|
+
lines.push("");
|
|
109
|
+
lines.push(" # Reuse TIME_WAIT sockets for new connections (safer, faster)");
|
|
110
|
+
lines.push(" # Reduces connection table exhaustion under high load");
|
|
111
|
+
lines.push(" net.ipv4.tcp_tw_reuse = 1");
|
|
112
|
+
lines.push("");
|
|
113
|
+
lines.push(" # Reduce SYN backlog and timeouts for faster detection");
|
|
114
|
+
lines.push(" net.ipv4.tcp_max_syn_backlog = 2048");
|
|
115
|
+
lines.push(" net.ipv4.tcp_synack_retries = 2");
|
|
116
|
+
lines.push(" net.ipv4.tcp_syn_retries = 5");
|
|
117
|
+
lines.push("");
|
|
118
|
+
lines.push(" # =================================================================");
|
|
119
|
+
lines.push(" # 3. NETWORK STACK HARDENING");
|
|
120
|
+
lines.push(" # =================================================================");
|
|
121
|
+
lines.push(" # Disable ICMP redirect acceptance (IPv6)");
|
|
122
|
+
lines.push(" net.ipv6.conf.all.accept_redirects = 0");
|
|
123
|
+
lines.push(" net.ipv6.conf.default.accept_redirects = 0");
|
|
124
|
+
lines.push("");
|
|
125
|
+
lines.push(" # Ignore ICMP broadcasts (prevent smurf attacks)");
|
|
126
|
+
lines.push(" net.ipv4.icmp_echo_ignore_broadcasts = 1");
|
|
127
|
+
lines.push("");
|
|
128
|
+
lines.push(" # Ignore bogus ICMP error responses (prevent ICMP attacks)");
|
|
129
|
+
lines.push(" net.ipv4.icmp_ignore_bogus_error_responses = 1");
|
|
130
|
+
lines.push("");
|
|
131
|
+
lines.push(" # Enable TCP timestamps (RFC 1323) for better sequence handling");
|
|
132
|
+
lines.push(" # Also protects against wrapped sequence number attacks");
|
|
133
|
+
lines.push(" net.ipv4.tcp_timestamps = 1");
|
|
134
|
+
lines.push("");
|
|
135
|
+
lines.push(" # Enable TCP selective acknowledgments (better performance)");
|
|
136
|
+
lines.push(" net.ipv4.tcp_sack = 1");
|
|
137
|
+
lines.push("");
|
|
138
|
+
lines.push(" # =================================================================");
|
|
139
|
+
lines.push(" # 4. CORE DUMP RESTRICTIONS");
|
|
140
|
+
lines.push(" # =================================================================");
|
|
141
|
+
lines.push(" # Disable core dumps for setuid programs (prevent privilege escalation)");
|
|
142
|
+
lines.push(" fs.suid_dumpable = 0");
|
|
143
|
+
lines.push("");
|
|
144
|
+
lines.push(" # Limit core dump size to 0 (disable core dumps)");
|
|
145
|
+
lines.push(" # Override in /etc/security/limits.conf if needed for debugging");
|
|
146
|
+
lines.push(" kernel.core_pattern = |/bin/false");
|
|
147
|
+
lines.push("");
|
|
148
|
+
lines.push(" # =================================================================");
|
|
149
|
+
lines.push(" # 5. MEMORY PROTECTION (ASLR)");
|
|
150
|
+
lines.push(" # =================================================================");
|
|
151
|
+
lines.push(" # Enable Address Space Layout Randomization (full)");
|
|
152
|
+
lines.push(" # Makes exploitation of memory corruption vulnerabilities harder");
|
|
153
|
+
lines.push(" # 0: Disabled, 1: Conservative, 2: Full (default)");
|
|
154
|
+
lines.push(" kernel.randomize_va_space = 2");
|
|
155
|
+
lines.push("");
|
|
156
|
+
lines.push(" # =================================================================");
|
|
157
|
+
lines.push(" # 6. FILESYSTEM PROTECTION");
|
|
158
|
+
lines.push(" # =================================================================");
|
|
159
|
+
lines.push(" # Hard link/symlink protection (prevent time-of-check time-of-use)");
|
|
160
|
+
lines.push(" fs.protected_hardlinks = 1");
|
|
161
|
+
lines.push(" fs.protected_symlinks = 1");
|
|
162
|
+
lines.push("");
|
|
163
|
+
lines.push(" # FIFO protection (prevent FIFO attacks on world-writable directories)");
|
|
164
|
+
lines.push(" fs.protected_fifos = 2");
|
|
165
|
+
lines.push("");
|
|
166
|
+
lines.push(" # Regular file protection (prevent file overwrite attacks)");
|
|
167
|
+
lines.push(" fs.protected_regular = 2");
|
|
168
|
+
lines.push("");
|
|
169
|
+
lines.push(" # =================================================================");
|
|
170
|
+
lines.push(" # 7. NETWORK BEHAVIOR TUNING");
|
|
171
|
+
lines.push(" # =================================================================");
|
|
172
|
+
lines.push(" # Enable TCP Fast Open (TFO) for reduced latency");
|
|
173
|
+
lines.push(" net.ipv4.tcp_fastopen = 3");
|
|
174
|
+
lines.push("");
|
|
175
|
+
lines.push(" # Disable source routing (prevent packet routing manipulation)");
|
|
176
|
+
lines.push(" net.ipv4.conf.all.accept_source_route = 0");
|
|
177
|
+
lines.push(" net.ipv4.conf.default.accept_source_route = 0");
|
|
178
|
+
lines.push(" net.ipv6.conf.all.accept_source_route = 0");
|
|
179
|
+
lines.push(" net.ipv6.conf.default.accept_source_route = 0");
|
|
180
|
+
lines.push("");
|
|
181
|
+
lines.push(" # Enable TCP window scaling (RFC 7323) for high-bandwidth links");
|
|
182
|
+
lines.push(" net.ipv4.tcp_window_scaling = 1");
|
|
183
|
+
lines.push("");
|
|
184
|
+
lines.push(" # =================================================================");
|
|
185
|
+
lines.push(" # 8. SECURITY-RELATED KERNEL PARAMETERS");
|
|
186
|
+
lines.push(" # =================================================================");
|
|
187
|
+
lines.push(" # Disable magic sysrq key (prevent console-based attacks)");
|
|
188
|
+
lines.push(" # 0: Disabled, 1: Enable (for debugging only)");
|
|
189
|
+
lines.push(" kernel.sysrq = 0");
|
|
190
|
+
lines.push("");
|
|
191
|
+
lines.push(" # Disable kexec system call (prevent kernel replacement)");
|
|
192
|
+
lines.push(" # 0: Disabled, 1: Enabled");
|
|
193
|
+
lines.push(" kernel.kexec_load = 0");
|
|
194
|
+
lines.push("");
|
|
195
|
+
lines.push(" # Disable user namespaces (prevent container breakouts)");
|
|
196
|
+
lines.push(" # 0: Disabled, 1: Enabled");
|
|
197
|
+
lines.push(" user.max_user_namespaces = 0");
|
|
198
|
+
lines.push("");
|
|
199
|
+
lines.push(" # Enable unprivileged bpf disabled (prevent eBPF-based exploits)");
|
|
200
|
+
lines.push(" # 0: Disabled, 1: Enabled");
|
|
201
|
+
lines.push(" kernel.unprivileged_bpf_disabled = 1");
|
|
202
|
+
lines.push("");
|
|
203
|
+
lines.push(" # =================================================================");
|
|
204
|
+
lines.push(" # 9. ADDITIONAL HARDENING (2026)");
|
|
205
|
+
lines.push(" # =================================================================");
|
|
206
|
+
lines.push(" # Disable IPv6 if not needed (uncomment if IPv6 is disabled)");
|
|
207
|
+
lines.push(" # net.ipv6.conf.all.disable_ipv6 = 1");
|
|
208
|
+
lines.push(" # net.ipv6.conf.default.disable_ipv6 = 1");
|
|
209
|
+
lines.push("");
|
|
210
|
+
lines.push(" # Enable dmesg restriction (prevent kernel info leaks)");
|
|
211
|
+
lines.push(" kernel.dmesg_restrict = 1");
|
|
212
|
+
lines.push("");
|
|
213
|
+
lines.push(" # Restrict ptrace scope (prevent process tracing by non-parent)");
|
|
214
|
+
lines.push(" # 0: Traditional, 1: Restricted, 2: Admin-only, 3: No attach");
|
|
215
|
+
lines.push(" kernel.yama.ptrace_scope = 2");
|
|
216
|
+
lines.push("");
|
|
217
|
+
lines.push(" # =================================================================");
|
|
218
|
+
lines.push(" # 10. PERFORMANCE TUNING (safe defaults)");
|
|
219
|
+
lines.push(" # =================================================================");
|
|
220
|
+
lines.push(" # Increase connection tracking table size (for stateful firewalls)");
|
|
221
|
+
lines.push(" net.netfilter.nf_conntrack_max = 262144");
|
|
222
|
+
lines.push("");
|
|
223
|
+
lines.push(" # Reduce TCP keepalive timeouts for faster dead peer detection");
|
|
224
|
+
lines.push(" net.ipv4.tcp_keepalive_time = 600");
|
|
225
|
+
lines.push(" net.ipv4.tcp_keepalive_intvl = 30");
|
|
226
|
+
lines.push(" net.ipv4.tcp_keepalive_probes = 3");
|
|
227
|
+
lines.push("");
|
|
228
|
+
return lines;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Commands to apply kernel hardening settings immediately at first boot.
|
|
232
|
+
* Returns cloud-init YAML lines for the `runcmd:` section.
|
|
233
|
+
*
|
|
234
|
+
* Order matters:
|
|
235
|
+
* 1. Load all sysctl settings from /etc/sysctl.d/*.conf
|
|
236
|
+
* 2. Apply settings immediately (don't wait for reboot)
|
|
237
|
+
* 3. Log applied settings for audit trail
|
|
238
|
+
* 4. Display summary for cloud-init output verification
|
|
239
|
+
*/
|
|
240
|
+
export function kernelHardeningRunCmd() {
|
|
241
|
+
const lines = [];
|
|
242
|
+
lines.push(" # Kernel hardening: apply sysctl settings immediately");
|
|
243
|
+
lines.push(" # Settings are already in /etc/sysctl.d/99-security-hardening.conf");
|
|
244
|
+
lines.push("");
|
|
245
|
+
lines.push(" # Apply all sysctl settings (overrides defaults immediately)");
|
|
246
|
+
lines.push(" - sysctl --system");
|
|
247
|
+
lines.push("");
|
|
248
|
+
lines.push(" # Log applied settings for audit trail");
|
|
249
|
+
lines.push(" - sysctl -a | grep -E '(rp_filter|syncookies|randomize_va|suid_dump)' > /var/log/kernel-hardening.log 2>&1 || true");
|
|
250
|
+
lines.push("");
|
|
251
|
+
lines.push(" # Display summary of critical hardening settings");
|
|
252
|
+
lines.push(" - |");
|
|
253
|
+
lines.push(" echo '========================================'");
|
|
254
|
+
lines.push(" echo 'Kernel Hardening Applied (2026)'");
|
|
255
|
+
lines.push(" echo '========================================'");
|
|
256
|
+
lines.push(" echo 'IP Spoof Protection: '$(sysctl -n net.ipv4.conf.all.rp_filter)");
|
|
257
|
+
lines.push(" echo 'SYN Cookies: '$(sysctl -n net.ipv4.tcp_syncookies)");
|
|
258
|
+
lines.push(" echo 'ASLR Level: '$(sysctl -n kernel.randomize_va_space)");
|
|
259
|
+
lines.push(" echo 'SUID Core Dumps: '$(sysctl -n fs.suid_dumpable)");
|
|
260
|
+
lines.push(" echo 'Hard Links Protected: '$(sysctl -n fs.protected_hardlinks)");
|
|
261
|
+
lines.push(" echo 'Ptrace Scope: '$(sysctl -n kernel.yama.ptrace_scope)");
|
|
262
|
+
lines.push(" echo '========================================'");
|
|
263
|
+
lines.push("");
|
|
264
|
+
return lines;
|
|
265
|
+
}
|
|
266
|
+
//# sourceMappingURL=kernel-hardening.js.map
|
|
@@ -0,0 +1,230 @@
|
|
|
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
|
+
});
|