Skip to content

Commit 96d7d01

Browse files
authored
Add logcheck info #12
1 parent 2a46f53 commit 96d7d01

File tree

1 file changed

+106
-39
lines changed

1 file changed

+106
-39
lines changed

linux-1/hardening-and-setup.md

Lines changed: 106 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: >-
1010

1111
The first thing to do after the first boot is to update the system. In most Debian-based flavors of Linux, you achieve this by executing the commands below:
1212

13-
```text
13+
```bash
1414
sudo apt update && sudo apt upgrade -y
1515
```
1616

@@ -29,7 +29,7 @@ Remember that disabling unnecessary services will reduce the attack surface, so
2929

3030
`yum` for RedHat based systems. `apt` for debian based systems.
3131

32-
```text
32+
```bash
3333
# yum erase xinetd ypserv tftp-server telnet-server rsh-server
3434
# apt --purge remove xinetd nis yp-tools tftpd atftpd tftpd-hpa telnetd rsh-server rsh-redone-server
3535
```
@@ -38,14 +38,14 @@ Remember that disabling unnecessary services will reduce the attack surface, so
3838

3939
If you don't want to completely uninstall a service, you can simply disable it until it is needed. A lot of services and daemons are started during system boot and disabling those that are not being used can help with system hardening and can improve boot time. Since most modern distributions use `systemd` instead of init scripts, you can use `systemctl` to list running services.
4040

41-
```text
41+
```bash
4242
sudo systemctl list-unit-files --type=service
4343
sudo systemctl list-dependencies graphical.target
4444
```
4545

4646
These commands will display such service and daemons. You can disable a specific service by using the below commands.
4747

48-
```text
48+
```bash
4949
sudo systemctl disable service
5050
sudo systemctl disable httpd.service
5151
```
@@ -54,7 +54,7 @@ sudo systemctl disable httpd.service
5454

5555
Identifying open connections to the internet is critical to understanding your attack surface. In Kali Linux, use the following commands to identify open ports:
5656

57-
```text
57+
```bash
5858
netstat -tulpn
5959
ss -tulpn
6060
lsof -i
@@ -82,7 +82,7 @@ First of all, if you do not need SSH, disable it. However, if you want to use it
8282

8383
6. Check the manual for SSH to understand all the configurations in `/etc/ssh/sshd_config`. Some other examples of recommended configuration options are:
8484

85-
```text
85+
```bash
8686
AuthorizedKeysFile /etc/ssh/authorized-keys/%u #removes this file from user's folder and puts its in more secure /etc folder
8787
Protocol2
8888
IgnoreRhosts to yes
@@ -121,15 +121,15 @@ The boot directory contains important files related to the Linux kernel, so you
121121

122122
Admins should make sure that users can’t log into their server after a certain number of failed attempts. This increases the overall security of the system by mitigating password attacks. You can use the Linux `faillog` command to see the failed login attempts.
123123

124-
```text
124+
```bash
125125
# faillog
126126
# faillog -m 3
127127
# faillog -l 1800
128128
```
129129

130130
The first command will display the failed login attempts for users from the `/var/log/faillog` database. The second command sets the maximum number of allowed failed login attempts to 3. The third one sets a lock of 1800 seconds or 30 minutes after the allowed number of failed login attempts.
131131

132-
```text
132+
```bash
133133
# faillog -r -u <username>
134134
```
135135

@@ -139,14 +139,14 @@ Use this command to unlock a user once they’re prohibited from login. The max
139139

140140
[Fail2Ban](https://www.fail2ban.org/) is one of the most popular IPS solutions for Unix-like systems. It is written using Python and is available on all POSIX-compliant platforms. It will look for obtrusive network requests all the time and block them as soon as possible. Install Fail2Ban using the below command.
141141

142-
```text
142+
```bash
143143
apt install -y fail2ban
144144
yum install -y fail2ban
145145
```
146146

147147
[DenyHosts](https://github.com/denyhosts/denyhosts) is another popular IPS solution for Linux hardening. It will protect your ssh servers from intrusive brute force attempts. Use the following commands to install in on your Debian or Centos servers.
148148

149-
```text
149+
```bash
150150
apt install -y denyhosts
151151
yum install -y denyhosts
152152
```
@@ -171,22 +171,73 @@ yum install -y denyhosts
171171

172172
If you want to read the contents of the binary files `wtmp`, `utmp` or `btmp`, use the command:
173173

174-
```text
174+
```bash
175175
sudo utmpdump /var/run/utmp
176176
sudo utmpdump /var/log/wtmp
177177
sudo utmpdump /var/log/btmp
178178
```
179179

180180
`who`, `w`, and `last <username>` will also give you information about users logged into your machine.
181181

182-
### with `logcheck`
182+
### `logcheck`
183183

184-
TODO: Clean up description (issue [#12](https://github.com/zweilosec/Infosec-Notes/issues/12))
185-
* Description is a mass block of text. Add formatting,
186-
* Put code into code blocks
187-
* Ensure clarity and brevity
184+
**Logcheck** is a powerful tool for monitoring system logs and identifying potential security threats. It works by monitoring log files for changes (every hour by default) and sends log messages it determines to be 'unusual' to the administrator via email for further analysis. The list of monitored files is stored in `/etc/logcheck/logcheck.logfiles`. The default settings work well unless the `/etc/rsyslog.conf` file has undergone significant customization.
188185

189-
The logcheck program monitors log files every hour by default and sends unusual log messages in emails to the administrator for further analysis. The list of monitored files is stored in /etc/logcheck/logcheck.logfiles. The default values work fine if the /etc/rsyslog.conf file has not been completely overhauled. logcheck can report in various levels of detail: paranoid, server, and workstation. paranoid is very verbose and should probably be restricted to specific servers such as firewalls. server is the default mode and is recommended for most servers. workstation is obviously designed for workstations and is extremely terse, filtering out more messages than the other options. In all three cases, logcheck should probably be customized to exclude some extra messages \(depending on installed services\), unless you really want to receive hourly batches of long uninteresting emails. Since the message selection mechanism is rather complex, /usr/share/doc/logcheck-database/README.logcheck-database.gz is a required—if challenging—read. The applied rules can be split into several types: those that qualify a message as a cracking attempt \(stored in a file in the /etc/logcheck/cracking.d/directory\); ignored cracking attempts \(/etc/logcheck/cracking.ignore.d/\); those classifying a message as a security alert \(/etc/logcheck/violations.d/\); ignored security alerts \(/etc/logcheck/violations.ignore.d/\); finally, those applying to the remaining messages \(considered as system events\). ignore.d files are used to \(obviously\) ignore messages. For example, a message tagged as a cracking attempt or a security alert \(following a rule stored in a /etc/logcheck/violations.d/myfile file\) can only be ignored by a rule in a /etc/logcheck/violations.ignore.d/myfile or /etc/logcheck/violations.ignore.d/myfile-extension file. A system event is always signaled unless a rule in one of the /etc/logcheck/ignore.d.{paranoid,server,workstation}/ directories states the event should be ignored. Of course, the only directories taken into account are those corresponding to verbosity levels equal or greater than the selected operation mode.
186+
**logcheck** provides three reporting levels:
187+
188+
- **Paranoid**: Highly detailed and best suited for security-critical servers like firewalls.
189+
- **Server** (default mode): Recommended for most servers.
190+
- **Workstation**: Designed for workstations, offering minimal reporting by filtering out more messages.
191+
192+
Regardless of the chosen mode, customizing **logcheck** to exclude unnecessary messages is advised. Due to its complex message selection mechanism, reading `/usr/share/doc/logcheck-database/README.logcheck-database.gz` is essential to understanding logcheck's Rule Classifications and how to ingore messages.
193+
194+
Here are some ways to use **logcheck** to harden a Linux system:
195+
196+
#### **Fine-Tune Log Monitoring**
197+
198+
- Customize the list of monitored log files in `/etc/logcheck/logcheck.logfiles` to include critical logs like:
199+
- `/var/log/auth.log` (Authentication logs)
200+
- `/var/log/kern.log` (Kernel logs)
201+
- `/var/log/secure` (Security-related logs)
202+
- `/var/log/btmp` (Failed login attempts)
203+
- Regularly review these logs to detect unauthorized access attempts or system anomalies.
204+
205+
#### **Adjust Filtering Rules**
206+
207+
- Modify rules in `/etc/logcheck/ignore.d.{paranoid,server,workstation}/` to exclude unnecessary messages while ensuring security alerts are not ignored.
208+
- Use `/etc/logcheck/violations.d/` to classify security alerts and `/etc/logcheck/violations.ignore.d/` to filter out false positives.
209+
210+
##### Rule Classification
211+
212+
**logcheck** applies rules to categorize messages into the following types:
213+
214+
- **Cracking attempts** → Stored in `/etc/logcheck/cracking.d/`
215+
- **Ignored cracking attempts** → Stored in `/etc/logcheck/cracking.ignore.d/`
216+
- **Security alerts** → Stored in `/etc/logcheck/violations.d/`
217+
- **Ignored security alerts** → Stored in `/etc/logcheck/violations.ignore.d/`
218+
- **System events** → General logs unless explicitly ignored
219+
220+
##### Ignoring Messages
221+
222+
Messages can only be ignored if rules are defined in specific directories:
223+
224+
- A **cracking attempt** or **security alert** (classified under `/etc/logcheck/violations.d/`) can only be ignored by rules in `/etc/logcheck/violations.ignore.d/`.
225+
- **System events** are always reported unless ignored by rules within `/etc/logcheck/ignore.d.{paranoid,server,workstation}/`, and only those matching or exceeding the selected verbosity level are considered.
226+
227+
#### **Enhance Logcheck with SELinux**
228+
229+
- Enable **SELinux** (`sudo setenforce 1`) to enforce strict access control policies.
230+
- Combine **Logcheck** with SELinux logs (`/var/log/audit/audit.log`) to detect unauthorized access attempts.
231+
232+
#### **Automate Log Analysis**
233+
234+
- Schedule **Logcheck** to run more frequently than the default hourly interval by adjusting the cron job (`/etc/cron.d/logcheck`).
235+
- Set up email alerts to notify administrators immediately of critical security events.
236+
237+
#### **Integrate with Other Security Tools**
238+
239+
- Pair **Logcheck** with **Fail2Ban** to automatically block IPs attempting brute-force attacks.
240+
- Use **Auditd** for deeper system auditing and correlate logs with **Logcheck** for enhanced security insights.
190241

191242
## Enable SELinux
192243

@@ -196,33 +247,47 @@ Security Enhanced Linux is a Kernel security mechanism for supporting access con
196247

197248
[SELinux](https://en.wikipedia.org/wiki/Security-Enhanced_Linux), short for Security Enhanced Linux, is a security mechanism that implements various methods for access control at the kernel level. It was developed by Red Hat but has been added to many [modern Linux distributions.](https://ubuntupit.com/best-linux-distro-top-recommendation-to-boost-up-your-linux-experience/) You can check whether SELinux is enabled in your system or not by using the below command.
198249

199-
```text
250+
```bash
200251
sudo getenforce
201252
```
202253

203-
If it returns `enforcing` , your system is protected by SELinux. If the result says `permissive` your system has SELinux but it’s not enforced. It will return `disabled` for systems where SELinux is completely disabled. You can enforce SELinux by using the below command.
254+
If it returns `enforcing` , your system is protected by SELinux. If the result says `permissive` your system has SELinux but it’s not enforced. It will return `disabled` for systems where SELinux is completely disabled.
255+
256+
You can enforce SELinux by using the below command:
204257

205-
```text
258+
```bash
206259
sudo setenforce 1
207260
```
208261

209-
SELinux has three configuration modes:
262+
Or, using a text editor, open the config file `/etc/selinux/config` and make sure that the policy is enforced by changing the line starting with `SELINUX=`.
263+
264+
```bash
265+
# This file controls the state of SELinux on the system.
266+
# SELINUX can take one of these values:
267+
# enforcing - SELinux security policy is enforced.
268+
# permissive - SELinux prints warnings instead of enforcing.
269+
# disabled - No SELinux policy is loaded.
270+
271+
SELINUX=enforcing
272+
SELINUXTYPE=targeted # Targeted means only selected processes are protected.
273+
```
274+
275+
SELinux has three configuration mode options:
210276

211277
* Disabled: Turned-off
212-
* Permissive: Prints warnings
278+
* Permissive: Prints warnings, but does not block
213279
* Enforcing: Policy is enforced
214280

215-
Using a text editor, open the config file `/etc/selinux/config` and make sure that the policy is enforced by changing the line `SELINUX=enforcing`.
216281

217-
```text
218-
add example of config file - image?
219-
```
282+
### Visualizing the Configuration
283+
If you need an image representation of the configuration file, a simple screenshot of a terminal displaying the file’s contents would be the most practical. You can generate this by running:
284+
220285

221286
## Permissions and verifications
222287

223-
Prepare yourself mentally because this is going to be a long list. But, permissions is one of the most important and critical tasks to achieve the security goal on a Linux host. Set User/Group Owner and Permissions to `root` on `/etc/anacrontab`, `/etc/crontab` and `/etc/cron.*` by executing the following commands: \(as `root`\)
288+
Setting permissions is one of the most important and critical tasks to achieve the security goal on a Linux host. Set User/Group Owner and Permissions to `root` on `/etc/anacrontab`, `/etc/crontab` and `/etc/cron.*` by executing the following commands: \(as `root`\)
224289

225-
```text
290+
```bash
226291
chown root:root /etc/anacrontab
227292
chmod og-rwx /etc/anacrontab
228293
chown root:root /etc/crontab
@@ -241,42 +306,44 @@ chmod og-rwx /etc/cron.d
241306

242307
Set the permissions and owner on `/var/spool/cron` for the `root` crontab.
243308

244-
```text
309+
```bash
245310
chown root:root <crontabfile>
246311
chmod og-rwx <crontabfile>
247312
```
248313

249314
Set permissions and owner on `/etc/passwd` file \(this file must be readable by all users\).
250315

251-
```text
316+
```bash
252317
chmod 644 /etc/passwd
253318
chown root:root /etc/passwd
254319
```
255320

256321
Set permissions and owner on the `/etc/group` file \(this file must be readable by all users\).
257322

258-
```text
323+
```bash
259324
chmod 644 /etc/group
260325
chown root:root /etc/group
261326
```
262327

263328
Set permissions and owner on the `/etc/shadow` file.
264329

265-
```text
330+
```bash
266331
chmod 600 /etc/shadow
267332
chown root:root /etc/shadow
268333
```
269334

270335
Set permissions and owner on the `/etc/gshadow` file.
271336

272-
```text
337+
```bash
273338
chmod 600 /etc/gshadow
274339
chown root:root /etc/gshadow
275340
```
276341

277342

278343

279-
## Misc
344+
## Misc
345+
346+
TODO: expand this
280347

281348
* Use strong passwords
282349
* Set up `fail2ban`, which will make it much harder to brute-force passwords over the network by filtering IP addresses that exceed a limit of failed login attempts. `apt install fail2ban`
@@ -289,7 +356,7 @@ chown root:root /etc/gshadow
289356

290357
Any extra repositories needs to be placed into their own file in the directory `/etc/apt/sources.list.d/` with files named as such: `/etc/apt/sources.list.d/repo-name.list` \(replacing `repo-name` with the mirror name\). This may break things over time so be careful.
291358

292-
```text
359+
```bash
293360
deb http://http.kali.org/kali kali-rolling main non-free contrib
294361
<Archive> <Mirror> <Branch> <Component>
295362
```
@@ -310,7 +377,7 @@ To add kali's repository to another distro use the line: `deb http://http.kali.o
310377

311378
**lynis** - open source security auditing tool. Comes with Kali
312379

313-
```text
380+
```bash
314381
lynis --update
315382
lynis audit system
316383
```
@@ -329,7 +396,7 @@ If you’re not happy with the changes Bastille makes to your computer, you can
329396

330397
When using ssh and scp you can deploy Bastille using the commands:
331398

332-
```text
399+
```bash
333400
scp /etc/Bastille/config root@$targetHost:/etc/Bastille
334401
ssh root@$targetHost "bastille -b"
335402
```
@@ -342,7 +409,7 @@ Tmux can keep alive sessions if you lose ssh sessions etc, can split panes and m
342409

343410
Config from [ippsec](https://www.youtube.com/watch?v=Lqehvpe_djs).
344411

345-
```text
412+
```bash
346413
#set prefix
347414
set -g prefix C-a
348415
bind C-a send-prefix
@@ -396,7 +463,7 @@ Kill pane: `prefix + &`
396463

397464
Here’s a cool and interesting use of iptables. You can turn any computer with a wireless interface into a wireless access point with hostapd. This solution comes from [https://seravo.fi/2014/create-wireless-access-point-hostapd](https://seravo.fi/2014/create-wireless-access-point-hostapd):
398465

399-
```text
466+
```bash
400467
iptables -t nat -F
401468
iptables -F
402469
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

0 commit comments

Comments
 (0)