summaryrefslogtreecommitdiff
path: root/aif/scripts/post/sshsecure.py
blob: aa0d10a82b3edb3da7da288c9d7413ba262d30b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env python3

# Pythonized automated way of running https://sysadministrivia.com/news/hardening-ssh-security
# TODO: check for cryptography module. if it exists, we can do this entirely pythonically
#       without ever needing to use subprocess/ssh-keygen, i think!

# Thanks to https://stackoverflow.com/a/39126754.

# Also, I need to re-write this. It's getting uglier.

# stdlib
import datetime
import glob
import os
import pwd
import re
import signal
import shutil
import subprocess  # REMOVE WHEN SWITCHING TO PURE PYTHON
#### PREP FOR PURE PYTHON IMPLEMENTATION ####
# # non-stdlib - testing and automatic install if necessary.
# # TODO #
# - cryptography module won't generate new-format "openssh-key-v1" keys.
# - See https://github.com/pts/py_ssh_keygen_ed25519 for possible conversion to python 3
# - https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key
# - https://github.com/pyca/cryptography/issues/3509 and https://github.com/paramiko/paramiko/issues/1136
# has_crypto = False
# pure_py = False
# has_pip = False
# pipver = None
# try:
#     import cryptography
#     has_crypto = True
# except ImportError:
#     # We'll try to install it. We set up the logic below.
#     try:
#         import pip
#         has_pip = True
#         # We'll use these to create a temporary lib path and remove it when done.
#         import sys
#         import tempfile
#     except ImportError:
#         # ABSOLUTE LAST fallback, if we got to THIS case, is to use subprocess.
#         has_pip = False
#         import subprocess
#
# # Try installing it then!
# if not all((has_crypto, )):
#     # venv only included after python 3.3.x. We fallback to subprocess if we can't do dis.
#     if sys.hexversion >= 0x30300f0:
#         has_ensurepip = False
#         import venv
#         if not has_pip and sys.hexversion >= 0x30400f0:
#             import ensurepip
#             has_ensurepip = True
#         temppath = tempfile.mkdtemp('_VENV')
#         v = venv.create(temppath)
#         if has_ensurepip and not has_pip:
#             # This SHOULD be unnecessary, but we want to try really hard.
#             ensurepip.bootstrap(root = temppath)
#             import pip
#             has_pip = True
#         if has_pip:
#             pipver = pip.__version__.split('.')
#             # A thousand people are yelling at me for this.
#             if int(pipver[0]) >= 10:
#                 from pip._internal import main as pipinstall
#             else:
#                 pipinstall = pip.main
#             if int(pipver[0]) >= 8:
#                 pipcmd = ['install',
#                           '--prefix={0}'.format(temppath),
#                           '--ignore-installed']
#             else:
#                 pipcmd = ['install',
#                           '--install-option="--prefix={0}"'.format(temppath),
#                           '--ignore-installed']
#             # Get the lib path.
#             libpath = os.path.join(temppath, 'lib')
#             if os.path.exists('{0}64'.format(libpath)) and not os.path.islink('{0}64'.format(libpath)):
#                 libpath += '64'
#             for i in os.listdir(libpath):  # TODO: make this more sane. We cheat a bit here by making assumptions.
#                 if re.search('python([0-9]+(\.[0-9]+)?)?$', i):
#                     libpath = os.path.join(libpath, i)
#                     break
#             libpath = os.path.join(libpath, 'site-packages')
#             sys.prefix = temppath
#             for m in ('cryptography', 'ed25519'):
#                 pipinstall(['install', 'cryptography'])
#             sys.path.append(libpath)
#             try:
#                 import cryptography
#                 has_crypto = True
#             except ImportError:  # All that trouble for nothin'. Shucks.
#                 pass
#
# if all((has_crypto, )):
#     pure_py = True
#
# if pure_py:
#     from cryptography.hazmat.primitives import serialization as crypto_serialization
#     from cryptography.hazmat.primitives.asymmetric import rsa
#     from cryptography.hazmat.backends import default_backend as crypto_default_backend
#

# We need static backup suffixes.
tstamp = int(datetime.datetime.utcnow().timestamp())

# TODO: associate various config directives with version, too.
# For now, we use this for primarily CentOS 6.x, which doesn't support ED25519 and probably some of the MACs.
# Bastards.
# https://ssh-comparison.quendi.de/comparison/cipher.html at some point in the future...
# TODO: maybe implement some parsing of the ssh -Q stuff? https://superuser.com/a/869005/984616
# If you encounter a version incompatibility, please let me know!
# nmap --script ssh2-enum-algos -PN -sV -p22 <host>
magic_ver = 6.5
ssh_ver = subprocess.run(['ssh', '-V'], stderr = subprocess.PIPE).stderr.decode('utf-8').strip().split()[0]
ssh_ver = float(re.sub('^(Open|Sun_)SSH_([0-9\.]+)(p[0-9]+)?,.*$', '\g<2>', ssh_ver))
if ssh_ver >= magic_ver:
    has_ed25519 = True
    supported_keys = ('ed25519', 'rsa')
else:
    has_ed25519 = False
    supported_keys = ('rsa', )


conf_options = {}
conf_options['sshd'] = {'KexAlgorithms': 'diffie-hellman-group-exchange-sha256',
                        'Protocol': '2',
                        'HostKey': ['/etc/ssh/ssh_host_rsa_key'],
                        #'PermitRootLogin': 'prohibit-password',  # older daemons don't like "prohibit-..."
                        'PermitRootLogin': 'without-password',
                        'PasswordAuthentication': 'no',
                        'ChallengeResponseAuthentication': 'no',
                        'PubkeyAuthentication': 'yes',
                        'Ciphers': 'aes256-ctr,aes192-ctr,aes128-ctr',
                        'MACs': 'hmac-sha2-512,hmac-sha2-256'}
if has_ed25519:
    conf_options['sshd']['HostKey'].append('/etc/ssh/ssh_host_ed25519_key')
    conf_options['sshd']['KexAlgorithms'] = ','.join(('curve25519-sha256@libssh.org',
                                                      conf_options['sshd']['KexAlgorithms']))
    conf_options['sshd']['Ciphers'] = ','.join((('chacha20-poly1305@openssh.com,'
                                                 'aes256-gcm@openssh.com,'
                                                 'aes128-gcm@openssh.com'),
                                                conf_options['sshd']['Ciphers']))
    conf_options['sshd']['MACs'] = ','.join((('hmac-sha2-512-etm@openssh.com,'
                                              'hmac-sha2-256-etm@openssh.com,'
                                              'umac-128-etm@openssh.com'),
                                             conf_options['sshd']['MACs'],
                                             'umac-128@openssh.com'))
# Uncomment if this is further configured
#conf_options['sshd']['AllowGroups'] = 'ssh-user'

conf_options['ssh'] = {'Host': {'*': {'KexAlgorithms': 'diffie-hellman-group-exchange-sha256',
                                      'PubkeyAuthentication': 'yes',
                                      'HostKeyAlgorithms': 'ssh-rsa'}}}
if has_ed25519:
    conf_options['ssh']['Host']['*']['KexAlgorithms'] = ','.join(('curve25519-sha256@libssh.org',
                                                                  conf_options['ssh']['Host']['*']['KexAlgorithms']))
    conf_options['ssh']['Host']['*']['HostKeyAlgorithms'] = ','.join(
                                                            (('ssh-ed25519-cert-v01@openssh.com,'
                                                              'ssh-rsa-cert-v01@openssh.com,'
                                                              'ssh-ed25519'),
                                                             conf_options['ssh']['Host']['*']['HostKeyAlgorithms']))


def hostKeys(buildmoduli):
    # Starting haveged should help lessen the time load a non-negligible amount, especially on virtual platforms.
    if os.path.lexists('/usr/bin/haveged'):
        # We could use psutil here, but then that's a python dependency we don't need.
        # We could parse the /proc directory, but that's quite unnecessary. pgrep's installed by default on
        # most distros.
        with open(os.devnull, 'wb') as devnull:
            if subprocess.run(['pgrep', 'haveged'], stdout = devnull).returncode != 0:
                subprocess.run(['haveged'], stdout = devnull)
    #Warning: The moduli stuff takes a LONG time to run. Hours.
    if buildmoduli:
        subprocess.run(['ssh-keygen',
                        '-G', '/etc/ssh/moduli.all',
                        '-b', '4096',
                        '-q'])
        subprocess.run(['ssh-keygen',
                        '-T', '/etc/ssh/moduli.safe',
                        '-f', '/etc/ssh/moduli.all',
                        '-q'])
        if os.path.lexists('/etc/ssh/moduli'):
            os.rename('/etc/ssh/moduli', '/etc/ssh/moduli.old')
        os.rename('/etc/ssh/moduli.safe', '/etc/ssh/moduli')
        os.remove('/etc/ssh/moduli.all')
    for suffix in ('', '.pub'):
        for k in glob.glob('/etc/ssh/ssh_host_*key{0}'.format(suffix)):
            os.rename(k, '{0}.old.{1}'.format(k, tstamp))
    if has_ed25519:
        subprocess.run(['ssh-keygen',
                        '-t', 'ed25519',
                        '-f', '/etc/ssh/ssh_host_ed25519_key',
                        '-q',
                        '-N', ''])
    subprocess.run(['ssh-keygen',
                    '-t', 'rsa',
                    '-b', '4096',
                    '-f', '/etc/ssh/ssh_host_rsa_key',
                    '-q',
                    '-N', ''])
    # We currently don't use this, but for simplicity's sake let's return the host keys.
    hostkeys = {}
    for k in supported_keys:
        with open('/etc/ssh/ssh_host_{0}_key.pub'.format(k), 'r') as f:
            hostkeys[k] = f.read()
    return(hostkeys)

def config(opts, t):
    special = {'sshd': {}, 'ssh': {}}
    # We need to handle these directives a little differently...
    special['sshd']['opts'] = ['Match']
    special['sshd']['filters'] = ['User', 'Group', 'Host', 'LocalAddress', 'LocalPort', 'Address']
    # These are arguments supported by each of the special options. We'll use this to verify entries.
    special['sshd']['args'] = ['AcceptEnv', 'AllowAgentForwarding', 'AllowGroups', 'AllowStreamLocalForwarding',
                               'AllowTcpForwarding', 'AllowUsers', 'AuthenticationMethods', 'AuthorizedKeysCommand',
                               'AuthorizedKeysCommandUser', 'AuthorizedKeysFile', 'AuthorizedPrincipalsCommand',
                               'AuthorizedPrincipalsCommandUser', 'AuthorizedPrincipalsFile', 'Banner',
                               'ChrootDirectory', 'ClientAliveCountMax', 'ClientAliveInterval', 'DenyGroups',
                               'DenyUsers', 'ForceCommand', 'GatewayPorts', 'GSSAPIAuthentication',
                               'HostbasedAcceptedKeyTypes', 'HostbasedAuthentication',
                               'HostbasedUsesNameFromPacketOnly', 'IPQoS', 'KbdInteractiveAuthentication',
                               'KerberosAuthentication', 'MaxAuthTries', 'MaxSessions', 'PasswordAuthentication',
                               'PermitEmptyPasswords', 'PermitOpen', 'PermitRootLogin', 'PermitTTY', 'PermitTunnel',
                               'PermitUserRC', 'PubkeyAcceptedKeyTypes', 'PubkeyAuthentication', 'RekeyLimit',
                               'RevokedKeys', 'StreamLocalBindMask', 'StreamLocalBindUnlink', 'TrustedUserCAKeys',
                               'X11DisplayOffset', 'X11Forwarding', 'X11UseLocalHost']
    special['ssh']['opts'] = ['Host', 'Match']
    special['ssh']['args'] = ['canonical', 'exec', 'host', 'originalhost', 'user', 'localuser']
    cf = '/etc/ssh/{0}_config'.format(t)
    shutil.copy2(cf, '{0}.bak.{1}'.format(cf, tstamp))
    with open(cf, 'r') as f:
        conf = f.readlines()
    conf.append('\n\n# Added per https://sysadministrivia.com/news/hardening-ssh-security\n\n')
    confopts = []
    # Get an index of directives pre-existing in the config file.
    for line in conf[:]:
        opt = line.split()
        if opt:
            if not re.match('^(#.*|\s+.*)$', opt[0]):
                confopts.append(opt[0])
    # We also need to modify the config file- comment out starting with the first occurrence of the 
    # specopts, if it exists. This is why we make a backup.
    commentidx = None
    for idx, i in enumerate(conf):
        if re.match('^({0})\s+.*$'.format('|'.join(special[t]['opts'])), i):
            commentidx = idx
            break
    if commentidx is not None:
        idx = commentidx
        while idx <= (len(conf) - 1):
            conf[idx] = '#{0}'.format(conf[idx])
            idx += 1
    # Now we actually start replacing/adding some major configuration.
    for o in opts.keys():
        if o in special[t]['opts'] or isinstance(opts[o], dict):
            # We need to put these at the bottom of the file due to how they're handled by sshd's config parsing.
            continue
        # We handle these a little specially too- they're for multiple lines sharing the same directive.
        # Since the config should be explicit, we remove any existing entries specified that we find.
        else:
            if o in confopts:
                # If I was more worried about recursion, or if I was appending here, I should use conf[:].
                # But I'm not. So I won't.
                for idx, opt in enumerate(conf):
                    if re.match('^{0}(\s.*)?\n$'.format(o), opt):
                        conf[idx] = '#{0}'.format(opt)
            # Here we handle the "multiple-specifying" options- notably, HostKey.
            if isinstance(opts[o], list):
                for l in opts[o]:
                    if l is not None:
                        conf.append('{0} {1}\n'.format(o, l))
                    else:
                        conf.append('{0}\n'.format(o))
            else:
                # So it isn't something we explicitly save until the end (such as a Match or Host),
                # and it isn't something that's specified multiple times.
                if opts[o] is not None:
                    conf.append('{0} {1}\n'.format(o, opts[o]))
                else:
                    conf.append('{0}\n'.format(o))
    # NOW we can add the Host/Match/etc. directives.
    for o in opts.keys():
        if isinstance(opts[o], dict):
            for k in opts[o].keys():
                conf.append('{0} {1}\n'.format(o, k))
                for l in opts[o][k].keys():
                    if opts[o][k][l] is not None:
                        conf.append('\t{0} {1}\n'.format(l, opts[o][k][l]))
                    else:
                        conf.append('\t{0}\n'.format(l))
    with open(cf, 'w') as f:
        f.write(''.join(conf))
    return()

def clientKeys(user = 'root'):
    uid = pwd.getpwnam(user).pw_uid
    gid = pwd.getpwnam(user).pw_gid
    homedir = os.path.expanduser('~{0}'.format(user))
    sshdir = '{0}/.ssh'.format(homedir)
    os.makedirs(sshdir, mode = 0o700, exist_ok = True)
    if has_ed25519:
        if not os.path.lexists('{0}/id_ed25519'.format(sshdir)) \
                and not os.path.lexists('{0}/id_ed25519.pub'.format(sshdir)):
            subprocess.run(['ssh-keygen',
                            '-t', 'ed25519',
                            '-o',
                            '-a', '100',
                            '-f', '{0}/id_ed25519'.format(sshdir),
                            '-q',
                            '-N', ''])
    if not os.path.lexists('{0}/id_rsa'.format(sshdir)) and not os.path.lexists('{0}/id_rsa.pub'.format(sshdir)):
        if has_ed25519:
            subprocess.run(['ssh-keygen',
                            '-t', 'rsa',
                            '-b', '4096',
                            '-o',
                            '-a', '100',
                            '-f', '{0}/id_rsa'.format(sshdir),
                            '-q',
                            '-N', ''])
        else:
            subprocess.run(['ssh-keygen',
                            '-t', 'rsa',
                            '-b', '4096',
                            '-a', '100',
                            '-f', '{0}/id_rsa'.format(sshdir),
                            '-q',
                            '-N', ''])
    for basedir, dirs, files in os.walk(sshdir):
        os.chown(basedir, uid, gid)
        os.chmod(basedir, 0o700)
        for f in files:
            os.chown(os.path.join(basedir, f), uid, gid)
            os.chmod(os.path.join(basedir, f), 0o600)
    if 'pubkeys' not in globals():
        pubkeys = {}
    pubkeys[user] = {}
    for k in supported_keys:
        with open('{0}/id_{1}.pub'.format(sshdir, k), 'r') as f:
            pubkeys[user][k] = f.read()
    return(pubkeys)

def daemonMgr():
    # In case the script is running without sshd running.
    pidfile = '/var/run/sshd.pid'
    if not os.path.isfile(pidfile):
        return()
    # We're about to do somethin' stupid. Let's make it a teeny bit less stupid.
    with open(os.devnull, 'w') as devnull:
        confchk = subprocess.run(['sshd', '-T'], stdout = devnull)
    if confchk.returncode != 0:
        for suffix in ('', '.pub'):
            for k in glob.glob('/etc/ssh/ssh_host_*key{0}'.format(suffix)):
                os.rename('{0}.old.{1}'.format(k, tstamp), k)
            for conf in ('', 'd'):
                cf = '/etc/ssh/ssh{0}_config'.format(conf)
                os.rename('{0}.{1}'.format(cf, tstamp),
                          cf)
        exit('OOPS. We goofed. Backup restored and bailing out.')
    # We need to restart sshd once we're done. I feel dirty doing this, but this is the most cross-platform way I can
    # do it. First, we need the path to the PID file.
    # TODO: do some kind of better way of doing this.
    with open('/etc/ssh/sshd_config', 'r') as f:
        for line in f.readlines():
            if re.search('^\s*PidFile\s+.*', line):
                pidfile = re.sub('^\s*PidFile\s+(.*)(#.*)?$', '\g<1>', line)
                break
    with open(pidfile, 'r') as f:
        pid = int(f.read().strip())
    os.kill(pid, signal.SIGHUP)
    return()

def main():
    self_pidfile = '/tmp/sshsecure.pid'
    is_running = False
    # First, check to see if we're already running.
    # This is where I'd put a psutil call... IF I HAD ONE.
    if os.path.isfile(self_pidfile):
        is_running = subprocess.run(['pgrep', '-F', self_pidfile], stdout = subprocess.PIPE)
        if is_running.stdout.decode('utf-8').strip() != '':
            # We're still running. Exit gracefully.
            print('We seem to still be running from a past execution; exiting')
            exit(0)
        else:
            # It's a stale PID file.
            os.remove(self_pidfile)
    else:
        with open(self_pidfile, 'w') as f:
            f.write(str(os.getpid()) + '\n')
    _chkfile = '/etc/ssh/.aif-generated'
    if not os.path.isfile(_chkfile):
        # Warning: The moduli stuff can take a LONG time to run. Hours.
        buildmoduli = True
        hostKeys(buildmoduli)
    for t in ('sshd', 'ssh'):
        config(conf_options[t], t)
    clientKeys()
    with open(_chkfile, 'w') as f:
        f.write(('ssh, sshd, and hostkey configurations/keys have been modified by sshsecure.py from OpTools.\n'
                 'https://git.square-r00t.net/OpTools/\n'))
    daemonMgr()
    os.remove(self_pidfile)
    return()

if __name__ == '__main__':
    main()