Puppet Class: os_hardening::minimize_access

Defined in:
manifests/minimize_access.pp

Overview

Class: os_hardening::minimize_access

Configures profile.conf.

Parameters:

  • allow_change_user (Boolean) (defaults to: false)
  • ignore_max_files_warnings (Boolean) (defaults to: false)
  • manage_home_permissions (Boolean) (defaults to: false)
  • manage_log_permissions (Boolean) (defaults to: false)
  • manage_cron_permissions (Boolean) (defaults to: false)
  • manage_system_users (Boolean) (defaults to: true)
  • always_ignore_users (Array) (defaults to: ['root','sync','shutdown','halt'])
  • ignore_users (Array) (defaults to: [])
  • ignore_home_users (Array) (defaults to: [])
  • ignore_restrict_log_dir (Array) (defaults to: [])
  • ignore_files_in_folder_to_restrict (Array) (defaults to: [])
  • folders_to_restrict (Array) (defaults to: ['/usr/local/games','/usr/local/sbin','/usr/local/bin','/usr/bin','/usr/sbin','/sbin','/bin'])
  • restrict_log_dir (Array) (defaults to: ['/var/log/'])
  • shadowgroup (String) (defaults to: 'root')
  • shadowmode (String) (defaults to: '0600')
  • recurselimit (Integer) (defaults to: 5)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'manifests/minimize_access.pp', line 12

class os_hardening::minimize_access (
  Boolean $allow_change_user                    = false,
  Boolean $ignore_max_files_warnings            = false,
  Boolean $manage_home_permissions              = false,
  Boolean $manage_log_permissions               = false,
  Boolean $manage_cron_permissions              = false,
  Boolean $manage_system_users                  = true,
  Array   $always_ignore_users                  =
    ['root','sync','shutdown','halt'],
  Array   $ignore_users                         = [],
  Array   $ignore_home_users                    = [],
  Array   $ignore_restrict_log_dir              = [],
  Array   $ignore_files_in_folder_to_restrict   = [],
  Array   $folders_to_restrict                  =
    ['/usr/local/games','/usr/local/sbin','/usr/local/bin','/usr/bin','/usr/sbin','/sbin','/bin'],
  Array   $restrict_log_dir                     =
    ['/var/log/'],
  String  $shadowgroup                          = 'root',
  String  $shadowmode                           = '0600',
  Integer $recurselimit                         = 5,
) {

  case $::operatingsystem {
    redhat, fedora: {
      $nologin_path = '/sbin/nologin'
      $shadow_path = ['/etc/shadow', '/etc/gshadow']
    }
    debian, ubuntu, cumuluslinux: {
      $nologin_path = '/usr/sbin/nologin'
      $shadow_path = ['/etc/shadow', '/etc/gshadow']
    }
    default: {
      $nologin_path = '/sbin/nologin'
      $shadow_path = '/etc/shadow'
    }
  }

  # Whether $folders_to_restrict should issue warnings if the puppet max_files
  # file resource exceeds the default soft limit of 1000 on recursive file
  # resources, /bin and /usr/bin can exceed this default limit
  if $ignore_max_files_warnings {
    $use_max_files = -1
  } else {
    $use_max_files = 0
  }

  # remove write permissions from path folders ($PATH) for all regular users
  # this prevents changing any system-wide command from normal users
  ensure_resources ('file',
  { $folders_to_restrict => {
      ensure                  => directory,
      ignore                  => $ignore_files_in_folder_to_restrict,
      links                   => follow,
      mode                    => 'go-w',
      recurse                 => true,
      recurselimit            => $recurselimit,
      selinux_ignore_defaults => true,
      max_files               => $use_max_files,
    }
  })
# Added users with homes
  $homes_users = split($::home_users, ',')

# added ignore these homes
  $target_home_users = difference($homes_users, $ignore_home_users)

# added homes to restrict
if $manage_home_permissions == true {
  ensure_resources ('file',
  { $target_home_users => {
      ensure       => directory,
      links        => follow,
      mode         => 'g-w,o-rwx',
      recurse      => true,
      recurselimit => $recurselimit,
    }
  })
}

# ensure log folders have right permissions
if $manage_log_permissions == true {
  ensure_resources ('file',
  { $restrict_log_dir => {
      ensure       => directory,
      ignore       => $ignore_restrict_log_dir,
      links        => follow,
      mode         => 'g-wx,o-rwx',
      recurse      => true,
      recurselimit => $recurselimit,
    }
  })
}

# ensure crontab have right permissions
if $manage_cron_permissions == true {

  $cronfiles = [ '/etc/anacrontab', '/etc/crontab' ]
  $cronfiles.each |String $cronfile| {
    if ($::existing[$cronfile]) {
      file { $cronfile:
        ensure => file,
        mode   => 'og-rwx',
        owner  => 'root',
        group  => 'root',
      }
    }
  }

# ensure cron hourly have right permissions
  ensure_resources ('file',
  { '/etc/cron.hourly' => {
      ensure       => directory,
      mode         => 'og-rwx',
      owner        => 'root',
      group        => 'root',
      links        => follow,
      recurse      => true,
      recurselimit => $recurselimit,
    }
  })

# ensure cron daily have right permissions
  ensure_resources ('file',
  { '/etc/cron.daily' => {
      ensure       => directory,
      mode         => 'og-rwx',
      owner        => 'root',
      group        => 'root',
      links        => follow,
      recurse      => true,
      recurselimit => $recurselimit,
    }
  })

# ensure cron weekly have right permissions
  ensure_resources ('file',
  { '/etc/cron.weekly' => {
      ensure       => directory,
      mode         => 'og-rwx',
      owner        => 'root',
      group        => 'root',
      links        => follow,
      recurse      => true,
      recurselimit => $recurselimit,
    }
  })

# ensure cron monthly have right permissions
  ensure_resources ('file',
  { '/etc/cron.monthly' => {
      ensure       => directory,
      mode         => 'og-rwx',
      owner        => 'root',
      group        => 'root',
      links        => follow,
      recurse      => true,
      recurselimit => $recurselimit,
    }
  })

# ensure cron.d have right permissions
  ensure_resources ('file',
  { '/etc/cron.d' => {
      ensure       => directory,
      mode         => 'og-rwx',
      owner        => 'root',
      group        => 'root',
      links        => follow,
      recurse      => true,
      recurselimit => $recurselimit,
    }
  })

# ensure cron.deny and at.deny is absent
  file { '/etc/cron.deny':
    ensure => absent,
  }

  file { '/etc/at.deny':
    ensure       => absent,
  }

# ensure cron.allow is there
  file { '/etc/cron.allow':
    ensure => present,
    owner  => 'root',
    group  => 'root',
    mode   => 'og-rwx',
  }

# ensure at.allow is there
  file { '/etc/at.allow':
    ensure => present,
    owner  => 'root',
    group  => 'root',
    mode   => 'og-rwx',
  }
}

  # shadow must only be accessible to user root
  file { $shadow_path:
    ensure => file,
    owner  => 'root',
    group  => $shadowgroup,
    mode   => $shadowmode,
  }

  # su must only be accessible to user and group root
  if $allow_change_user == false {
    file { '/bin/su':
      ensure => file,
      links  => follow,
      owner  => 'root',
      group  => 'root',
      mode   => '0750',
    }
  } else {
    file { '/bin/su':
      ensure => file,
      links  => follow,
      owner  => 'root',
      group  => 'root',
      mode   => '4755',
    }
  }

  if $manage_system_users == true {
    # retrieve system users through custom fact
    $system_users = split($::retrieve_system_users, ',')

    # build array of usernames we need to verify/change
    $ignore_users_arr = union($always_ignore_users, $ignore_users)

    # build a target array with usernames to verify/change
    $target_system_users = difference($system_users, $ignore_users_arr)

    # ensure accounts are locked (no password) and use nologin shell
    user { $target_system_users:
      ensure   => present,
      shell    => $nologin_path,
      password => '*',
    }
  }
}