firewalls 0.1.0__tar.gz

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 (38) hide show
  1. firewalls-0.1.0/PKG-INFO +252 -0
  2. firewalls-0.1.0/README.md +235 -0
  3. firewalls-0.1.0/example/EXAMPLE.md +55 -0
  4. firewalls-0.1.0/example/etc/firewalld/firewalld.conf +33 -0
  5. firewalls-0.1.0/example/etc/firewalld/helpers/.gitkeep +1 -0
  6. firewalls-0.1.0/example/etc/firewalld/icmptypes/.gitkeep +1 -0
  7. firewalls-0.1.0/example/etc/firewalld/ipsets/trusted-dns.xml +16 -0
  8. firewalls-0.1.0/example/etc/firewalld/lockdown-whitelist.xml +8 -0
  9. firewalls-0.1.0/example/etc/firewalld/policies/egress-policy.xml +44 -0
  10. firewalls-0.1.0/example/etc/firewalld/services/.gitkeep +1 -0
  11. firewalls-0.1.0/example/etc/firewalld/services/example-service.xml.example +7 -0
  12. firewalls-0.1.0/example/etc/firewalld/zones/public.xml +9 -0
  13. firewalls-0.1.0/example/etc/logrotate.d/firewalld +19 -0
  14. firewalls-0.1.0/example/etc/sysctl.d/99-firewall-hardening.conf +26 -0
  15. firewalls-0.1.0/example/etc/syslog-ng/conf.d/firewalld.conf +29 -0
  16. firewalls-0.1.0/example/post_deploy.sh.example +19 -0
  17. firewalls-0.1.0/firewalls/__init__.py +25 -0
  18. firewalls-0.1.0/firewalls/cli.py +579 -0
  19. firewalls-0.1.0/firewalls/locale/es/LC_MESSAGES/firewalls.mo +0 -0
  20. firewalls-0.1.0/firewalls/locale/es.po +492 -0
  21. firewalls-0.1.0/firewalls/locale/firewalls.pot +493 -0
  22. firewalls-0.1.0/firewalls/locale/fr/LC_MESSAGES/firewalls.mo +0 -0
  23. firewalls-0.1.0/firewalls/locale/fr.po +492 -0
  24. firewalls-0.1.0/firewalls/locale/hi/LC_MESSAGES/firewalls.mo +0 -0
  25. firewalls-0.1.0/firewalls/locale/hi.po +492 -0
  26. firewalls-0.1.0/firewalls/locale/ro/LC_MESSAGES/firewalls.mo +0 -0
  27. firewalls-0.1.0/firewalls/locale/ro.po +492 -0
  28. firewalls-0.1.0/firewalls/locale/ru/LC_MESSAGES/firewalls.mo +0 -0
  29. firewalls-0.1.0/firewalls/locale/ru.po +492 -0
  30. firewalls-0.1.0/firewalls/locale/zh/LC_MESSAGES/firewalls.mo +0 -0
  31. firewalls-0.1.0/firewalls/locale/zh.po +492 -0
  32. firewalls-0.1.0/firewalls/log_control.py +105 -0
  33. firewalls-0.1.0/firewalls/log_egress.py +243 -0
  34. firewalls-0.1.0/firewalls/log_ingress.py +244 -0
  35. firewalls-0.1.0/firewalls/monitor.py +382 -0
  36. firewalls-0.1.0/firewalls/poethepoet.py +111 -0
  37. firewalls-0.1.0/firewalls/probes.py +376 -0
  38. firewalls-0.1.0/pyproject.toml +30 -0
@@ -0,0 +1,252 @@
1
+ Metadata-Version: 2.4
2
+ Name: firewalls
3
+ Version: 0.1.0
4
+ Summary: Centralized firewalld configuration and deployment manager for Gentoo Linux
5
+ Author: worky
6
+ Requires-Python: >=3.10,<4.0
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3.10
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Classifier: Programming Language :: Python :: 3.14
13
+ Requires-Dist: colorama (>=0.4.6,<0.5.0)
14
+ Requires-Dist: poethepoet (>=0.49.0,<0.50.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ # Firewalls Configuration
18
+
19
+ Centralized management and deployment of `firewalld` configurations for Gentoo Linux systems, packaged and managed with **Poetry**.
20
+
21
+ ---
22
+
23
+ ## 🗂️ Project Structure
24
+
25
+ The public repository contains the CLI deployment tool, log streaming monitors, connectivity probes, and a clean template configuration (`example/`). Machine-specific configurations (private workstations, production servers) can be maintained in a separate private repository or external directory:
26
+
27
+ ```text
28
+ firewalls/
29
+ ├── pyproject.toml # Poetry package configuration and dependencies
30
+ ├── firewalls/ # Python package containing CLI tools
31
+ │ ├── __init__.py
32
+ │ ├── cli.py # Deployment and service control
33
+ │ ├── log_control.py # Dynamic runtime LogDenied manager
34
+ │ ├── monitor.py # Live real-time dashboard
35
+ │ ├── log_ingress.py # Inbound blocked traffic monitor
36
+ │ ├── log_egress.py # Outbound blocked traffic monitor
37
+ │ └── probes.py # Network connectivity probes
38
+ ├── AI_PROMPT.md # Master prompt and guide for generating configurations with AI
39
+ ├── README.md
40
+ └── example/ # Clean machine configuration template
41
+ ├── EXAMPLE.md # Functional documentation of rules and services
42
+ ├── post_deploy.sh.example # Optional post-deployment script template
43
+ └── etc/
44
+ ├── firewalld/
45
+ │ ├── firewalld.conf # Main firewalld configuration
46
+ │ ├── lockdown-whitelist.xml
47
+ │ ├── zones/ # Zone definitions (e.g. public.xml)
48
+ │ ├── services/ # Custom service definitions
49
+ │ ├── ipsets/ # IPSet definitions (e.g. trusted-dns.xml)
50
+ │ ├── icmptypes/ # ICMP type definitions
51
+ │ ├── helpers/ # Connection helpers
52
+ │ └── policies/ # Egress and inter-zone policies (egress-policy.xml)
53
+ ├── syslog-ng/conf.d/ # Log routing into /var/log/firewalld/
54
+ ├── logrotate.d/ # Log rotation configuration
55
+ └── sysctl.d/ # Kernel network hardening (rp_filter, anti-spoofing)
56
+ ```
57
+
58
+ ---
59
+
60
+ ## 🤖 Generating Machine Configurations with AI
61
+
62
+ To easily generate a customized firewall configuration using an AI assistant (Claude, Gemini, ChatGPT, Antigravity, etc.), refer to **[AI_PROMPT.md](AI_PROMPT.md)**. It provides a master prompt ready to customize with your machine's role, network interfaces, and allowed services.
63
+
64
+ ---
65
+
66
+ ## 🚀 Usage
67
+
68
+ All commands are managed through **Poetry**:
69
+
70
+ ### 1. Deploying a Machine Configuration
71
+
72
+ Machine configurations can reside in any local directory or private git repository:
73
+
74
+ 1. **Direct path** (absolute or relative):
75
+ ```bash
76
+ poetry run firewalls_deploy /path/to/my-firewalls/my_machine
77
+ ```
78
+
79
+ 2. **Using the `--configs-dir` (`-d`) option**:
80
+ ```bash
81
+ poetry run firewalls_deploy my_machine --configs-dir /path/to/my-firewalls
82
+ ```
83
+
84
+ 3. **Using the environment variable** (Recommended):
85
+ ```bash
86
+ export FIREWALLS_DIR="$HOME/my-firewalls"
87
+ poetry run firewalls_deploy my_machine
88
+ ```
89
+
90
+ 4. **Template or repository machines** (also checks `~/.config/firewalls/machines/`):
91
+ ```bash
92
+ poetry run firewalls_deploy example
93
+ ```
94
+
95
+ ### 2. Stopping the `firewalld` Service
96
+
97
+ ```bash
98
+ poetry run firewalls_deploy stop
99
+ ```
100
+
101
+ ### What `firewalls_deploy` Does:
102
+ 1. Resolves and validates the machine configuration across search paths (`--configs-dir`, `$FIREWALLS_DIR`, `~/.config/firewalls/machines/`, direct path, or local directory).
103
+ 2. Verifies `root` privileges.
104
+ 3. Synchronizes `<machine>/etc/` into system `/etc/` preserving permissions, symlinks, and directory structure.
105
+ 4. Enforces `root:root` ownership and secure permissions (`0750` directories, `0644` files) strictly on files written under `/etc/firewalld`, without modifying parent directory permissions of `/etc`.
106
+ 5. Re-applies kernel network security parameters from `/etc/sysctl.d/` if present.
107
+ 6. Configures and restarts/reloads `syslog-ng` to isolate firewall logs into `/var/log/firewalld/`.
108
+ 7. Reloads `firewalld` rules via `firewall-cmd --reload` (or starts the service with OpenRC / systemd if stopped).
109
+ 8. Automatically executes the optional `post_deploy.sh` script if present in the machine folder.
110
+
111
+ ---
112
+
113
+ ## 📊 Real-Time Monitoring and Log Analysis
114
+
115
+ You can monitor, tally, or summarize blocked events with the built-in CLI tools:
116
+
117
+ ```bash
118
+ # Monitor blocked incoming connections (Ingress)
119
+ poetry run firewalls_log_ingress
120
+ poetry run firewalls_log_ingress --summary # Displays ranked summary
121
+
122
+ # Monitor blocked outgoing connections (Egress)
123
+ poetry run firewalls_log_egress
124
+ poetry run firewalls_log_egress --summary # Displays ranked summary
125
+
126
+ # Live interactive dashboard (Ingress + Egress with reverse DNS PTR)
127
+ poetry run firewalls_monitor # Default refresh (3s), top 20 rankings, DNS resolution
128
+ poetry run firewalls_monitor -n 2 -p 5 # Refresh every 2s showing top 5 entries
129
+ poetry run firewalls_monitor --no-resolve # Show raw IP addresses without reverse DNS
130
+ ```
131
+
132
+ ---
133
+
134
+ ## 🧪 Connectivity Probes (`firewalls_probes`)
135
+
136
+ The `firewalls_probes` command tests connectivity for both **outgoing (egress)** and **incoming (ingress / listen)** traffic to validate that firewall policies work as expected.
137
+
138
+ ### Command Options
139
+
140
+ | Parameter | Short | Description | Default |
141
+ | :--- | :--- | :--- | :--- |
142
+ | `--host` | `-H` | Target hostname or IP address for outbound probes | `kde.org` |
143
+ | `--port` | `-p` | Port number to probe | `1555` |
144
+ | `--protocol` | `-proto` | Transport protocol (`tcp` or `udp`) | `tcp` |
145
+ | `--timeout` | `-t` | Maximum socket timeout in seconds | `5.0` |
146
+ | `--count` | `-c` | Number of probe iterations | `1` |
147
+ | `--listen` | `-l` | **Listen Mode (Ingress)**: Opens port locally to wait for incoming connections | Disabled (Outbound) |
148
+ | `--bind` | `-b` | Local interface IP address to bind in listen mode | `0.0.0.0` |
149
+
150
+ ---
151
+
152
+ ### Recommended Verification Test Suite
153
+
154
+ #### 1. Outgoing Permitted Traffic (Egress - Expected Success)
155
+
156
+ - **Standard Web Traffic (HTTP/HTTPS)**:
157
+ ```bash
158
+ poetry run firewalls_probes -H kde.org -p 80 -proto tcp
159
+ poetry run firewalls_probes -H kde.org -p 443 -proto tcp
160
+ ```
161
+ - **Gentoo Portage Sync (rsync)**:
162
+ ```bash
163
+ poetry run firewalls_probes -H rsync.gentoo.org -p 873 -proto tcp
164
+ ```
165
+ - **DNS Resolution and Time Sync (NTP)**:
166
+ ```bash
167
+ poetry run firewalls_probes -H 1.1.1.1 -p 53 -proto udp
168
+ poetry run firewalls_probes -H pool.ntp.org -p 123 -proto udp
169
+ ```
170
+
171
+ #### 2. Outgoing Blocked Traffic (Zero Trust - Expected Drop)
172
+
173
+ - **Unauthorized outbound port (e.g., TCP 1555)**:
174
+ ```bash
175
+ poetry run firewalls_probes -H kde.org -p 1555 -proto tcp -t 3.0
176
+ ```
177
+ *Expected result*: `[FAILED]` due to `Connection timed out`.
178
+
179
+ - **Outbound SSH to external host (TCP 22)**:
180
+ ```bash
181
+ poetry run firewalls_probes -H github.com -p 22 -proto tcp -t 3.0
182
+ ```
183
+ *Expected result*: `[FAILED]` (if outbound SSH is not explicitly allowed).
184
+
185
+ #### 3. Incoming Traffic (Ingress / Listen Mode)
186
+
187
+ - **Test if a local port accepts incoming connections**:
188
+ ```bash
189
+ # Listens on port 8080 locally for up to 10 seconds waiting for an incoming connection
190
+ poetry run firewalls_probes -l -p 8080 -t 10.0
191
+ ```
192
+ - **Listen on local UDP port**:
193
+ ```bash
194
+ poetry run firewalls_probes -l -p 51820 -proto udp -t 10.0
195
+ ```
196
+
197
+ #### 4. Integrated Verification with Live Monitor
198
+
199
+ To visually confirm that packet drops are recorded accurately:
200
+
201
+ 1. In one terminal, launch the live dashboard:
202
+ ```bash
203
+ poetry run firewalls_monitor -a
204
+ ```
205
+ 2. In a second terminal, execute a blocked probe:
206
+ ```bash
207
+ poetry run firewalls_probes -H kde.org -p 1555
208
+ ```
209
+ 3. Observe the denial registered in real time under the **DENIED OUTGOING (EGRESS)** panel, complete with destination IP and reverse DNS PTR hostname.
210
+
211
+ ---
212
+
213
+ ## 🔒 Log Architecture and `dmesg` Protection
214
+
215
+ To prevent firewall drop logs from flooding the kernel ring buffer (`dmesg`) and `/var/log/messages`:
216
+
217
+ 1. **On-Demand Drop Logging (`LogDenied=off` by default)**:
218
+ - Traditionally, setting `LogDenied=all` in `firewalld` triggers `printk()` calls at the kernel level via `nf_log_syslog`, polluting `dmesg` with local network broadcast and drop packets.
219
+ - **Dynamic Solution**: In `firewalld.conf`, `LogDenied` is set to `off` permanently. During normal operation, the firewall operates completely silently.
220
+ - **Automatic CLI Activation**:
221
+ - When starting `firewalls_log_ingress`, `firewalls_log_egress`, or `firewalls_monitor` (run as `root`), they dynamically invoke:
222
+ ```bash
223
+ firewall-cmd --set-log-denied=all
224
+ ```
225
+ - Upon exiting or pressing **Ctrl+C**, the signal handler immediately restores:
226
+ ```bash
227
+ firewall-cmd --set-log-denied=off
228
+ ```
229
+ - Kernel drop logging only takes place while an administrator is actively monitoring traffic.
230
+
231
+ 2. **syslog-ng Routing**:
232
+ - `syslog-ng` captures firewall drops and routes them exclusively to `/var/log/firewalld/denied.log`.
233
+ - Includes a `throttle(30)` directive to prevent disk exhaustion from log flooding attacks.
234
+ - `firewalls_deploy` automatically configures `@include "/etc/syslog-ng/conf.d/*.conf"` before catch-all rules, guaranteeing with `flags(final);` that firewall drops do not leak into `/var/log/messages`.
235
+
236
+ ---
237
+
238
+ ## ➕ Adding a New Machine
239
+
240
+ 1. Use the instructions and master prompt in **[AI_PROMPT.md](AI_PROMPT.md)** with an AI assistant, or copy the `example/` template:
241
+ ```bash
242
+ mkdir -p ~/my-firewalls/server1
243
+ cp -r example/* ~/my-firewalls/server1/
244
+ ```
245
+ 2. Customize the XML files in `~/my-firewalls/server1/etc/firewalld/` for your host.
246
+ 3. Deploy with:
247
+ ```bash
248
+ poetry run firewalls_deploy ~/my-firewalls/server1
249
+ # Or with FIREWALLS_DIR exported:
250
+ poetry run firewalls_deploy server1
251
+ ```
252
+
@@ -0,0 +1,235 @@
1
+ # Firewalls Configuration
2
+
3
+ Centralized management and deployment of `firewalld` configurations for Gentoo Linux systems, packaged and managed with **Poetry**.
4
+
5
+ ---
6
+
7
+ ## 🗂️ Project Structure
8
+
9
+ The public repository contains the CLI deployment tool, log streaming monitors, connectivity probes, and a clean template configuration (`example/`). Machine-specific configurations (private workstations, production servers) can be maintained in a separate private repository or external directory:
10
+
11
+ ```text
12
+ firewalls/
13
+ ├── pyproject.toml # Poetry package configuration and dependencies
14
+ ├── firewalls/ # Python package containing CLI tools
15
+ │ ├── __init__.py
16
+ │ ├── cli.py # Deployment and service control
17
+ │ ├── log_control.py # Dynamic runtime LogDenied manager
18
+ │ ├── monitor.py # Live real-time dashboard
19
+ │ ├── log_ingress.py # Inbound blocked traffic monitor
20
+ │ ├── log_egress.py # Outbound blocked traffic monitor
21
+ │ └── probes.py # Network connectivity probes
22
+ ├── AI_PROMPT.md # Master prompt and guide for generating configurations with AI
23
+ ├── README.md
24
+ └── example/ # Clean machine configuration template
25
+ ├── EXAMPLE.md # Functional documentation of rules and services
26
+ ├── post_deploy.sh.example # Optional post-deployment script template
27
+ └── etc/
28
+ ├── firewalld/
29
+ │ ├── firewalld.conf # Main firewalld configuration
30
+ │ ├── lockdown-whitelist.xml
31
+ │ ├── zones/ # Zone definitions (e.g. public.xml)
32
+ │ ├── services/ # Custom service definitions
33
+ │ ├── ipsets/ # IPSet definitions (e.g. trusted-dns.xml)
34
+ │ ├── icmptypes/ # ICMP type definitions
35
+ │ ├── helpers/ # Connection helpers
36
+ │ └── policies/ # Egress and inter-zone policies (egress-policy.xml)
37
+ ├── syslog-ng/conf.d/ # Log routing into /var/log/firewalld/
38
+ ├── logrotate.d/ # Log rotation configuration
39
+ └── sysctl.d/ # Kernel network hardening (rp_filter, anti-spoofing)
40
+ ```
41
+
42
+ ---
43
+
44
+ ## 🤖 Generating Machine Configurations with AI
45
+
46
+ To easily generate a customized firewall configuration using an AI assistant (Claude, Gemini, ChatGPT, Antigravity, etc.), refer to **[AI_PROMPT.md](AI_PROMPT.md)**. It provides a master prompt ready to customize with your machine's role, network interfaces, and allowed services.
47
+
48
+ ---
49
+
50
+ ## 🚀 Usage
51
+
52
+ All commands are managed through **Poetry**:
53
+
54
+ ### 1. Deploying a Machine Configuration
55
+
56
+ Machine configurations can reside in any local directory or private git repository:
57
+
58
+ 1. **Direct path** (absolute or relative):
59
+ ```bash
60
+ poetry run firewalls_deploy /path/to/my-firewalls/my_machine
61
+ ```
62
+
63
+ 2. **Using the `--configs-dir` (`-d`) option**:
64
+ ```bash
65
+ poetry run firewalls_deploy my_machine --configs-dir /path/to/my-firewalls
66
+ ```
67
+
68
+ 3. **Using the environment variable** (Recommended):
69
+ ```bash
70
+ export FIREWALLS_DIR="$HOME/my-firewalls"
71
+ poetry run firewalls_deploy my_machine
72
+ ```
73
+
74
+ 4. **Template or repository machines** (also checks `~/.config/firewalls/machines/`):
75
+ ```bash
76
+ poetry run firewalls_deploy example
77
+ ```
78
+
79
+ ### 2. Stopping the `firewalld` Service
80
+
81
+ ```bash
82
+ poetry run firewalls_deploy stop
83
+ ```
84
+
85
+ ### What `firewalls_deploy` Does:
86
+ 1. Resolves and validates the machine configuration across search paths (`--configs-dir`, `$FIREWALLS_DIR`, `~/.config/firewalls/machines/`, direct path, or local directory).
87
+ 2. Verifies `root` privileges.
88
+ 3. Synchronizes `<machine>/etc/` into system `/etc/` preserving permissions, symlinks, and directory structure.
89
+ 4. Enforces `root:root` ownership and secure permissions (`0750` directories, `0644` files) strictly on files written under `/etc/firewalld`, without modifying parent directory permissions of `/etc`.
90
+ 5. Re-applies kernel network security parameters from `/etc/sysctl.d/` if present.
91
+ 6. Configures and restarts/reloads `syslog-ng` to isolate firewall logs into `/var/log/firewalld/`.
92
+ 7. Reloads `firewalld` rules via `firewall-cmd --reload` (or starts the service with OpenRC / systemd if stopped).
93
+ 8. Automatically executes the optional `post_deploy.sh` script if present in the machine folder.
94
+
95
+ ---
96
+
97
+ ## 📊 Real-Time Monitoring and Log Analysis
98
+
99
+ You can monitor, tally, or summarize blocked events with the built-in CLI tools:
100
+
101
+ ```bash
102
+ # Monitor blocked incoming connections (Ingress)
103
+ poetry run firewalls_log_ingress
104
+ poetry run firewalls_log_ingress --summary # Displays ranked summary
105
+
106
+ # Monitor blocked outgoing connections (Egress)
107
+ poetry run firewalls_log_egress
108
+ poetry run firewalls_log_egress --summary # Displays ranked summary
109
+
110
+ # Live interactive dashboard (Ingress + Egress with reverse DNS PTR)
111
+ poetry run firewalls_monitor # Default refresh (3s), top 20 rankings, DNS resolution
112
+ poetry run firewalls_monitor -n 2 -p 5 # Refresh every 2s showing top 5 entries
113
+ poetry run firewalls_monitor --no-resolve # Show raw IP addresses without reverse DNS
114
+ ```
115
+
116
+ ---
117
+
118
+ ## 🧪 Connectivity Probes (`firewalls_probes`)
119
+
120
+ The `firewalls_probes` command tests connectivity for both **outgoing (egress)** and **incoming (ingress / listen)** traffic to validate that firewall policies work as expected.
121
+
122
+ ### Command Options
123
+
124
+ | Parameter | Short | Description | Default |
125
+ | :--- | :--- | :--- | :--- |
126
+ | `--host` | `-H` | Target hostname or IP address for outbound probes | `kde.org` |
127
+ | `--port` | `-p` | Port number to probe | `1555` |
128
+ | `--protocol` | `-proto` | Transport protocol (`tcp` or `udp`) | `tcp` |
129
+ | `--timeout` | `-t` | Maximum socket timeout in seconds | `5.0` |
130
+ | `--count` | `-c` | Number of probe iterations | `1` |
131
+ | `--listen` | `-l` | **Listen Mode (Ingress)**: Opens port locally to wait for incoming connections | Disabled (Outbound) |
132
+ | `--bind` | `-b` | Local interface IP address to bind in listen mode | `0.0.0.0` |
133
+
134
+ ---
135
+
136
+ ### Recommended Verification Test Suite
137
+
138
+ #### 1. Outgoing Permitted Traffic (Egress - Expected Success)
139
+
140
+ - **Standard Web Traffic (HTTP/HTTPS)**:
141
+ ```bash
142
+ poetry run firewalls_probes -H kde.org -p 80 -proto tcp
143
+ poetry run firewalls_probes -H kde.org -p 443 -proto tcp
144
+ ```
145
+ - **Gentoo Portage Sync (rsync)**:
146
+ ```bash
147
+ poetry run firewalls_probes -H rsync.gentoo.org -p 873 -proto tcp
148
+ ```
149
+ - **DNS Resolution and Time Sync (NTP)**:
150
+ ```bash
151
+ poetry run firewalls_probes -H 1.1.1.1 -p 53 -proto udp
152
+ poetry run firewalls_probes -H pool.ntp.org -p 123 -proto udp
153
+ ```
154
+
155
+ #### 2. Outgoing Blocked Traffic (Zero Trust - Expected Drop)
156
+
157
+ - **Unauthorized outbound port (e.g., TCP 1555)**:
158
+ ```bash
159
+ poetry run firewalls_probes -H kde.org -p 1555 -proto tcp -t 3.0
160
+ ```
161
+ *Expected result*: `[FAILED]` due to `Connection timed out`.
162
+
163
+ - **Outbound SSH to external host (TCP 22)**:
164
+ ```bash
165
+ poetry run firewalls_probes -H github.com -p 22 -proto tcp -t 3.0
166
+ ```
167
+ *Expected result*: `[FAILED]` (if outbound SSH is not explicitly allowed).
168
+
169
+ #### 3. Incoming Traffic (Ingress / Listen Mode)
170
+
171
+ - **Test if a local port accepts incoming connections**:
172
+ ```bash
173
+ # Listens on port 8080 locally for up to 10 seconds waiting for an incoming connection
174
+ poetry run firewalls_probes -l -p 8080 -t 10.0
175
+ ```
176
+ - **Listen on local UDP port**:
177
+ ```bash
178
+ poetry run firewalls_probes -l -p 51820 -proto udp -t 10.0
179
+ ```
180
+
181
+ #### 4. Integrated Verification with Live Monitor
182
+
183
+ To visually confirm that packet drops are recorded accurately:
184
+
185
+ 1. In one terminal, launch the live dashboard:
186
+ ```bash
187
+ poetry run firewalls_monitor -a
188
+ ```
189
+ 2. In a second terminal, execute a blocked probe:
190
+ ```bash
191
+ poetry run firewalls_probes -H kde.org -p 1555
192
+ ```
193
+ 3. Observe the denial registered in real time under the **DENIED OUTGOING (EGRESS)** panel, complete with destination IP and reverse DNS PTR hostname.
194
+
195
+ ---
196
+
197
+ ## 🔒 Log Architecture and `dmesg` Protection
198
+
199
+ To prevent firewall drop logs from flooding the kernel ring buffer (`dmesg`) and `/var/log/messages`:
200
+
201
+ 1. **On-Demand Drop Logging (`LogDenied=off` by default)**:
202
+ - Traditionally, setting `LogDenied=all` in `firewalld` triggers `printk()` calls at the kernel level via `nf_log_syslog`, polluting `dmesg` with local network broadcast and drop packets.
203
+ - **Dynamic Solution**: In `firewalld.conf`, `LogDenied` is set to `off` permanently. During normal operation, the firewall operates completely silently.
204
+ - **Automatic CLI Activation**:
205
+ - When starting `firewalls_log_ingress`, `firewalls_log_egress`, or `firewalls_monitor` (run as `root`), they dynamically invoke:
206
+ ```bash
207
+ firewall-cmd --set-log-denied=all
208
+ ```
209
+ - Upon exiting or pressing **Ctrl+C**, the signal handler immediately restores:
210
+ ```bash
211
+ firewall-cmd --set-log-denied=off
212
+ ```
213
+ - Kernel drop logging only takes place while an administrator is actively monitoring traffic.
214
+
215
+ 2. **syslog-ng Routing**:
216
+ - `syslog-ng` captures firewall drops and routes them exclusively to `/var/log/firewalld/denied.log`.
217
+ - Includes a `throttle(30)` directive to prevent disk exhaustion from log flooding attacks.
218
+ - `firewalls_deploy` automatically configures `@include "/etc/syslog-ng/conf.d/*.conf"` before catch-all rules, guaranteeing with `flags(final);` that firewall drops do not leak into `/var/log/messages`.
219
+
220
+ ---
221
+
222
+ ## ➕ Adding a New Machine
223
+
224
+ 1. Use the instructions and master prompt in **[AI_PROMPT.md](AI_PROMPT.md)** with an AI assistant, or copy the `example/` template:
225
+ ```bash
226
+ mkdir -p ~/my-firewalls/server1
227
+ cp -r example/* ~/my-firewalls/server1/
228
+ ```
229
+ 2. Customize the XML files in `~/my-firewalls/server1/etc/firewalld/` for your host.
230
+ 3. Deploy with:
231
+ ```bash
232
+ poetry run firewalls_deploy ~/my-firewalls/server1
233
+ # Or with FIREWALLS_DIR exported:
234
+ poetry run firewalls_deploy server1
235
+ ```
@@ -0,0 +1,55 @@
1
+ # Firewall Configuration: [MACHINE_NAME]
2
+
3
+ ## 📋 Machine Overview
4
+ - **Hostname / Identifier**: `[MACHINE_NAME]` (e.g., `web-server`, `dev-laptop`)
5
+ - **Role**: [Workstation / Web Server / NAS / Local Router]
6
+ - **Network Interface**: `[INTERFACE]` (e.g., `eth0`, `eno1`, `wlan0`)
7
+ - **Operating System**: Gentoo Linux
8
+ - **Firewall Backend**: `nftables`
9
+ - **Default Zone**: `public` (bound to `[INTERFACE]`, target `DROP`)
10
+ - **Egress Policy**: `egress-policy` (target `DROP`, `HOST` -> `ANY`)
11
+ - **Security Model**: **Dual Default Deny (Bidirectional Zero Trust)**.
12
+ - **Ingress**: No open ports by default unless explicitly permitted. Unsolicited incoming packets are discarded (`DROP`).
13
+ - **Egress**: Only explicitly authorized outgoing connections are allowed (strict DNS, NTP, HTTP/HTTPS, etc.). Any other outbound traffic is blocked and dropped (`DROP`).
14
+
15
+ ---
16
+
17
+ ## 🛡️ Active Rules and Functional Behavior
18
+
19
+ ### 1. Outgoing Traffic (*Egress Filtering* - `policies/egress-policy.xml`)
20
+ - **Policy**: **DEFAULT DENY (Target `DROP`)**.
21
+ - **Logging**: Dropped connections logged to `/var/log/firewalld/denied.log`.
22
+
23
+ | Service / Port | Protocol | Status | Functional Justification |
24
+ | :--- | :--- | :--- | :--- |
25
+ | `http` | TCP (80) | **Allowed** | Standard web browsing and package repository downloads. |
26
+ | `https` | TCP (443) / UDP (443) | **Allowed** | Secure web browsing and HTTP/3 (QUIC). |
27
+ | `dns` (Restricted) | UDP / TCP (53) | **Allowed (IPSet)** | DNS queries restricted to the `trusted-dns` IPSet to prevent DNS tunneling and data exfiltration. |
28
+ | `ntp` | UDP (123) | **Allowed** | System time synchronization. |
29
+ | `dhcp` | UDP (67, 68) | **Allowed** | Local network IPv4 DHCP lease renewal (if applicable). |
30
+ | `rsync` | TCP (873) | **Optional** | Gentoo Portage tree synchronization (`emerge --sync`). |
31
+ | **Anti-Spoofing** | `127.0.0.0/8` outbound | **DENIED (DROP)** | Immediate drop for packets with spoofed loopback source addresses. |
32
+ | **Any other outbound** | All | **DENIED (DROP)** | Strict drop for uncatalogued outbound traffic. |
33
+
34
+ ### 2. Incoming Traffic (*Ingress Filtering* - `zones/public.xml`)
35
+ - **Zone**: `public` (Target: `DROP`, Interface: `[INTERFACE]`)
36
+ - **Policy**: **DEFAULT DENY (Target `DROP`)**.
37
+
38
+ | Service / Port | Protocol | Status | Functional Justification |
39
+ | :--- | :--- | :--- | :--- |
40
+ | `ssh` | TCP (22) | **Configurable** | Disabled by default on workstations; restrict to administration IPs on servers. |
41
+ | **Any other inbound** | All | **DENIED (DROP)** | Absolute drop of all unsolicited incoming traffic. |
42
+
43
+ ---
44
+
45
+ ## 🔒 Security Mechanisms and System Hardening
46
+
47
+ 1. **D-Bus Lockdown (`Lockdown=yes`)**: Protects `firewalld` so non-privileged processes cannot alter rules via D-Bus.
48
+ 2. **Strict DNS Filtering (`trusted-dns`)**: Prevents malware from querying arbitrary rogue DNS resolvers.
49
+ 3. **Kernel Hardening (`sysctl.d/99-firewall-hardening.conf`)**:
50
+ - `rp_filter = 1` (Strict IPv4 reverse path filtering / anti-spoofing).
51
+ - `accept_redirects = 0` (Prevention of ICMP redirect attacks).
52
+ - `log_martians = 1` (Kernel logging of martian / impossible source addresses).
53
+ 4. **Dedicated Logging (`syslog-ng` + `logrotate`)**:
54
+ - Denied events routed to `/var/log/firewalld/denied.log` with rate limiting (`throttle(30)`).
55
+ - Keeps `/var/log/messages` and `dmesg` clean and noise-free.
@@ -0,0 +1,33 @@
1
+ # Default configuration for firewalld on Gentoo Linux
2
+ # Documentation: man firewalld.conf
3
+
4
+ # Default zone
5
+ DefaultZone=public
6
+
7
+ # Clean up on exit or stop
8
+ CleanupOnExit=yes
9
+
10
+ # Lockdown (limits D-Bus interface changes to users/commands in lockdown-whitelist.xml)
11
+ Lockdown=yes
12
+
13
+ # Reverse Path Filter check on IPv6
14
+ IPv6_rpfilter=yes
15
+
16
+ # Rule individual calls or combined restore
17
+ IndividualCalls=no
18
+
19
+ # Log denied packets. Options: 'all', 'unicast', 'broadcast', 'multicast', 'off'.
20
+ # 'off' by default to keep dmesg clean; CLI monitors enable it on demand.
21
+ LogDenied=off
22
+
23
+ # Automatic helper assignment
24
+ AutomaticHelpers=system
25
+
26
+ # Firewall backend implementation (nftables recommended)
27
+ FirewallBackend=nftables
28
+
29
+ # Flush all runtime rules on reload
30
+ FlushAllOnReload=yes
31
+
32
+ # Filter IPv6-in-IPv4 packets as per RFC 3964
33
+ RFC3964_IPv4=yes
@@ -0,0 +1 @@
1
+ # Placeholder to maintain directory structure
@@ -0,0 +1 @@
1
+ # Placeholder to maintain directory structure
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <ipset type="hash:ip">
3
+ <short>trusted-dns</short>
4
+ <description>IPSet of trusted secure DNS resolvers (Cloudflare, Quad9, Google DNS, and local router)</description>
5
+ <!-- Example local gateway or DNS resolver -->
6
+ <entry>192.168.1.1</entry>
7
+ <!-- Cloudflare DNS -->
8
+ <entry>1.1.1.1</entry>
9
+ <entry>1.0.0.1</entry>
10
+ <!-- Quad9 DNS -->
11
+ <entry>9.9.9.9</entry>
12
+ <entry>149.112.112.112</entry>
13
+ <!-- Google DNS -->
14
+ <entry>8.8.8.8</entry>
15
+ <entry>8.8.4.4</entry>
16
+ </ipset>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <whitelist>
3
+ <short>Lockdown Whitelist Template</short>
4
+ <description>Restricts D-Bus firewall manipulation strictly to root (UID 0) and authorized commands.</description>
5
+ <user id="0"/>
6
+ <command name="/usr/bin/python*"/>
7
+ <command name="/usr/bin/firewall-cmd"/>
8
+ </whitelist>
@@ -0,0 +1,44 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <policy target="DROP">
3
+ <short>Egress-Policy-Template</short>
4
+ <description>Strict egress control: default drop (target DROP). Only explicitly authorized outgoing connections are permitted.</description>
5
+ <ingress-zone name="HOST"/>
6
+ <egress-zone name="ANY"/>
7
+
8
+ <!-- Anti-Spoofing: Immediate drop of outgoing packets with spoofed loopback addresses -->
9
+ <rule family="ipv4">
10
+ <source address="127.0.0.0/8"/>
11
+ <drop/>
12
+ </rule>
13
+
14
+ <!-- DNS restricted strictly to trusted resolvers (trusted-dns IPSet) -->
15
+ <rule family="ipv4">
16
+ <destination ipset="trusted-dns"/>
17
+ <service name="dns"/>
18
+ <accept/>
19
+ </rule>
20
+
21
+ <!-- Standard and secure web browsing -->
22
+ <service name="http"/>
23
+ <service name="https"/>
24
+ <!-- HTTP/3 / QUIC -->
25
+ <port port="443" protocol="udp"/>
26
+
27
+ <!-- Time synchronization -->
28
+ <service name="ntp"/>
29
+
30
+ <!-- DHCP client for local network IP renewal -->
31
+ <service name="dhcp"/>
32
+
33
+ <!-- Uncomment if machine synchronizes Gentoo Portage packages via rsync -->
34
+ <!-- <service name="rsync"/> -->
35
+
36
+ <!-- Example rich rule for a specific destination -->
37
+ <!--
38
+ <rule family="ipv4">
39
+ <destination address="192.168.1.50"/>
40
+ <service name="ssh"/>
41
+ <accept/>
42
+ </rule>
43
+ -->
44
+ </policy>
@@ -0,0 +1 @@
1
+ # Placeholder to maintain directory structure
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!-- Example custom service definition -->
3
+ <service>
4
+ <short>custom-app</short>
5
+ <description>Example custom application service</description>
6
+ <port protocol="tcp" port="8080"/>
7
+ </service>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <zone target="DROP">
3
+ <short>Public</short>
4
+ <description>Public zone with strict default-drop policy (target DROP). Unsolicited incoming packets are dropped unless explicitly authorized.</description>
5
+ <!-- Replace 'eth0' with the machine's actual network interface (e.g., eno1, enp3s0, wlan0) -->
6
+ <interface name="eth0"/>
7
+ <!-- Example: Enable SSH only if necessary (or restrict with a rich rule by source IP) -->
8
+ <!-- <service name="ssh"/> -->
9
+ </zone>