Skip to content

Commit b66c856

Browse files
committed
Add shell history cleaning feature, clean up in docs, add more elastic cron scheduling
1 parent 04419ce commit b66c856

File tree

7 files changed

+110
-8
lines changed

7 files changed

+110
-8
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.idea
2+
*.pyc
3+
/meta/.galaxy_install_info
4+
!.gitkeep

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ Basic Security
33

44
Just another security role for Ansible.
55

6-
Allows to improve security of:
7-
- SSH
8-
- logs (IPv4 anonymization)
9-
- firewall (blocking incoming connections on unknown ports)
10-
- change root password
6+
**Features:**
7+
- SSH security improvements
8+
- IPv4 addresses anonymization in logs (scheduled every X hours, so the fail2ban can have time to block attackers on time)
9+
- Firewall configuration (blocking incoming connections on unknown ports)
10+
- Change root password
11+
- Scheduled bash, zsh, python (and others) history cleaning, so your passwords will not stay in a history
1112

1213
Does not include fail2ban configuration, as it has a good dedicated role.
1314

@@ -35,9 +36,25 @@ Configuration reference
3536
3637
#
3738
# Logs IP addresses anonymization
38-
# Allows to remove all IPv4 addresses from logs
3939
#
4040
anonymize_logs: true
41+
anonymize_logs_schedule:
42+
minute: 30
43+
hour: "*"
44+
day: "*"
45+
weekday: "*"
46+
month: "*"
47+
48+
#
49+
# Clear bash history
50+
#
51+
clear_shell_history: false
52+
clear_shell_history_schedule:
53+
minute: 30
54+
hour: "*"
55+
day: "*"
56+
weekday: "*"
57+
month: "*"
4158
4259
#
4360
# Firewall

defaults/main.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,31 @@ ssh_gateway_ports: no
99
ssh_permit_tunnel: yes
1010
ssh_password_auth: yes
1111
ssh_permit_root_login: no
12-
ssh_host_title: "International Workers Association server"
12+
ssh_host_title: "iwa-ait.org"
1313
ssh_allow_only_specific_users: "" # type user names here, separated by [space] character
1414
ssh_idle_time: "1800"
1515

1616
#
1717
# Logs IP addresses anonymization
1818
#
1919
anonymize_logs: true
20+
anonymize_logs_schedule:
21+
minute: "00"
22+
hour: "*/6"
23+
day: "*"
24+
weekday: "*"
25+
month: "*"
26+
27+
#
28+
# Clear bash history
29+
#
30+
clear_shell_history: false
31+
clear_shell_history_schedule:
32+
minute: "30"
33+
hour: "*"
34+
day: "*"
35+
weekday: "*"
36+
month: "*"
2037

2138
#
2239
# Firewall
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# Clears history files (from Bash, ZSH, PostgreSQL psql, Python, etc.)
5+
# ====================================================================
6+
# Prevents from leaking passwords typed in shell
7+
# Should be running as ROOT
8+
#
9+
10+
import os
11+
import sys
12+
import pwd
13+
import subprocess
14+
15+
HISTORY_FILES = ['.bash_history', '.sqlite_history', '.python_history',
16+
'.config/psysh/psysh_history', '.psql_history', '.zsh_history']
17+
exit_status = True
18+
19+
for p in pwd.getpwall():
20+
username = p.pw_name
21+
homedir = p.pw_dir
22+
23+
for history_file in HISTORY_FILES:
24+
path = homedir + '/' + history_file
25+
26+
if os.path.isfile(path):
27+
print(' >> Clearing {path}'.format(path=path))
28+
29+
try:
30+
with open(path, 'w') as f:
31+
f.write('')
32+
except Exception as exc:
33+
print(exc)
34+
exit_status = False
35+
36+
subprocess.call(['rm', path + '-*.tmp'], stderr=subprocess.DEVNULL)
37+
38+
39+
sys.exit(0 if exit_status else 1)

tasks/all-tasks.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- include: logs-anonymization.yml
77
when: anonymize_logs == True
88

9+
- include: shell-history-cleaning.yml
10+
when: clear_shell_history == True
11+
912
- include: configure-firewall.yml
1013
when: configure_firewall == True
1114

tasks/logs-anonymization.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@
1313
become: yes
1414
cron:
1515
name: "logs-ipv4-strip"
16-
minute: "30"
16+
minute: "{{ anonymize_logs_schedule['minute'] | default('00') }}"
17+
hour: "{{ anonymize_logs_schedule['hour'] | default('*/6') }}"
18+
day: "{{ anonymize_logs_schedule['day'] | default('*') }}"
19+
weekday: "{{ anonymize_logs_schedule['weekday'] | default('*') }}"
20+
month: "{{ anonymize_logs_schedule['month'] | default('*') }}"
1721
job: "python /usr/local/bin/scan-and-strip-logs.py"

tasks/shell-history-cleaning.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
- name: Copy helper scripts to the bin directory
3+
become: yes
4+
copy:
5+
src: "../files/usr/local/bin/clear-shell-history.py"
6+
dest: "/usr/local/bin/clear-shell-history.py"
7+
mode: a+x
8+
9+
- name: Schedule shell history cleaning
10+
become: yes
11+
cron:
12+
name: "shell-history-cleaning"
13+
minute: "{{ clear_shell_history_schedule['minute'] | default('00') }}"
14+
hour: "{{ clear_shell_history_schedule['hour'] | default('*') }}"
15+
day: "{{ clear_shell_history_schedule['day'] | default('*') }}"
16+
weekday: "{{ clear_shell_history_schedule['weekday'] | default('*') }}"
17+
month: "{{ clear_shell_history_schedule['month'] | default('*') }}"
18+
job: "python3 /usr/local/bin/clear-shell-history.py"

0 commit comments

Comments
 (0)