Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tests_ssh « include - github.com/CISOfy/lynis.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6823b962a07fbe8f17d5bee2d3428274f9af2b46 (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
#!/bin/sh

#################################################################################
#
#   Lynis
# ------------------
#
# Copyright 2007-2013, Michael Boelen
# Copyright 2007-2017, CISOfy
#
# Website  : https://cisofy.com
# Blog     : http://linux-audit.com
# GitHub   : https://github.com/CISOfy/lynis
#
# Lynis comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
# welcome to redistribute it under the terms of the GNU General Public License.
# See LICENSE file for usage of this software.
#
#################################################################################
#
# SSH
#
#################################################################################
#
    SSH_DAEMON_CONFIG_LOCS="/etc /etc/ssh /usr/local/etc/ssh /opt/csw/etc/ssh"
    SSH_DAEMON_CONFIG=""
    SSH_DAEMON_PORT=""
    SSH_DAEMON_RUNNING=0
    SSH_DAEMON_OPTIONS_FILE=""
#
#################################################################################
#
    InsertSection "SSH Support"
#
#################################################################################
#
    # Test        : SSH-7402
    # Description : Check for a running SSH daemon
    Register --test-no SSH-7402 --weight L --network NO --category security --description "Check for running SSH daemon"
    if [ ${SKIPTEST} -eq 0 ]; then
        LogText "Test: Searching for a SSH daemon"
        IsRunning sshd
        if [ ${RUNNING} -eq 1 ] || PortIsListening "TCP" 22; then
            SSH_DAEMON_RUNNING=1
            Display --indent 2 --text "- Checking running SSH daemon" --result "${STATUS_FOUND}" --color GREEN
            # Store settings in a temporary file
            CreateTempFile
            SSH_DAEMON_OPTIONS_FILE="${TEMP_FILE}"
            ${SSHDBINARY} -T 2> /dev/null > ${SSH_DAEMON_OPTIONS_FILE}
          else
            Display --indent 2 --text "- Checking running SSH daemon" --result "${STATUS_NOT_FOUND}" --color WHITE
        fi
    fi
#
#################################################################################
#
    # Test        : SSH-7404
    # Description : Determine SSH daemon configuration file location
    if [ ${SSH_DAEMON_RUNNING} -eq 1 ]; then PREQS_MET="YES"; else PREQS_MET="NO"; fi
    Register --test-no SSH-7404 --preqs-met ${PREQS_MET} --weight L --network NO --category security --description "Check SSH daemon file location"
    if [ ${SKIPTEST} -eq 0 ]; then
        FOUND=0
        LogText "Test: searching for sshd_config file"
        for I in ${SSH_DAEMON_CONFIG_LOCS}; do
            if [ -f "${I}/sshd_config" ]; then
              LogText "Result: ${I}/sshd_config exists"
              if [ ${FOUND} -eq 1 ]; then
                  ReportException "${TEST_NO}:01"
                  LogText "Result: we already had found another sshd_config file. Using this new file then."
              fi
              FileIsReadable ${I}/sshd_config
              if [ ${CANREAD} -eq 1 ]; then
                  FOUND=1
                  SSH_DAEMON_CONFIG="${I}/sshd_config"
                else
                  LogText "Result: can not read ${I}/sshd_config file (no permission)"
              fi
            fi
        done
        if [ "${SSH_DAEMON_CONFIG}" = "" ]; then
            LogText "Result: No sshd configuration found"
            Display --indent 4 --text "- Searching SSH configuration" --result "${STATUS_NOT_FOUND}" --color YELLOW
            ReportException "${TEST_NO}:1" "SSH daemon is running, but no readable configuration file found"
          else
            LogText "Result: using last found configuration file: ${SSH_DAEMON_CONFIG}"
            Display --indent 4 --text "- Searching SSH configuration" --result "${STATUS_FOUND}" --color GREEN
        fi
    fi
#
#################################################################################
#
    # Test        : SSH-7408
    # Description : Check SSH specific defined options
    # Notes       : Instead of parsing the configuration file, we query the SSH daemon itself
    if [ ${SSH_DAEMON_RUNNING} -eq 1 -a ! "${SSH_DAEMON_OPTIONS_FILE}" = "" ]; then PREQS_MET="YES"; else PREQS_MET="NO"; fi
    Register --test-no SSH-7408 --preqs-met ${PREQS_MET} --weight L --network NO --category security --description "Check SSH specific defined options"
    if [ ${SKIPTEST} -eq 0 ]; then
        LogText "Test: Checking specific defined options in ${SSH_DAEMON_OPTIONS_FILE}"
        ## SSHOPTIONS scheme:
        ##      <OptionName>:<ExpectedValue>,<MediumScoreValue>,<WeakValue>:<TestType>
        ##
        ##      Test types:
        ##      (a) '='         -- equal to is better,
        ##      (b) '<'         -- less or equal is better,
        ##      (c) '>'         -- more or equal is better,
        ##      (d) '!'         -- not equal is better.
        ##
        ##      Example:
        ##      PermitRootLogin:NO,WITHOUT-PASSWORD,YES,:=
        SSHOPS="AllowTcpForwarding:NO,LOCAL,YES:=\
                ClientAliveCountMax:2,4,16:<\
                ClientAliveInterval:300,600,900:<\
                Compression:NO,DELAYED,YES:=\
                FingerprintHash:SHA256,MD5,:=\
                GatewayPorts:NO,,YES:=\
                IgnoreRhosts:YES,,NO:=\
                LoginGraceTime:120,240,480:<\
                LogLevel:VERBOSE,INFO,:=\
                MaxAuthTries:1,3,6:<\
                MaxSessions:2,4,8:<\
                PermitRootLogin:NO,WITHOUT-PASSWORD,YES:=\
                PermitUserEnvironment:NO,,YES:=\
                PermitTunnel:NO,,YES:=\
                Port:,,22:!\
                PrintLastLog:YES,,NO:=\
                Protocol:2,,1:=\
                StrictModes:YES,,NO:=\
                TCPKeepAlive:NO,,YES:=\
                UseDNS:NO,,YES:=\
                UsePrivilegeSeparation:SANDBOX,YES,NO:=\
                VerifyReverseMapping:YES,,NO:=\
                X11Forwarding:NO,,YES:=\
                AllowAgentForwarding:NO,,YES:="

        # Disabled        MaxStartups:4,8,16:<\ (needs fixing)

        # Go through our list of options
        for I in ${SSHOPS}; do
            OPTIONNAME=`echo ${I} | ${CUTBINARY} -d ':' -f1`
            OPTIONNAME_LOWER=`echo ${I} | ${CUTBINARY} -d ':' -f1 | ${AWKBINARY} '{ print tolower($1) }'`
            EXPECTEDVALUE=`echo ${I} | ${CUTBINARY} -d ':' -f2 | ${CUTBINARY} -d',' -f1`
            MEDIUMSCOREDVALUE=`echo ${I} | ${CUTBINARY} -d ':' -f2 | ${CUTBINARY} -d',' -f2`
            WEAKVALUE=`echo ${I} | ${CUTBINARY} -d ':' -f2 | ${CUTBINARY} -d',' -f3`
            TESTTYPE=`echo ${I} | ${CUTBINARY} -d ':' -f3`
            RESULT="NONE"

            if ! SkipAtomicTest "${TEST_NO}:${OPTIONNAME_LOWER}"; then

                # Get value and use the last occurrence
                FOUNDVALUE=`${AWKBINARY} -v OPT="${OPTIONNAME_LOWER}" 'index($0, OPT) == 1 { print toupper($2) }' ${SSH_DAEMON_OPTIONS_FILE} | tail -1`
                LogText "Test: Checking ${OPTIONNAME} in ${SSH_DAEMON_OPTIONS_FILE}"

                if [ ! "${FOUNDVALUE}" = "" ]; then
                    LogText "Result: Option ${OPTIONNAME} found"
                    LogText "Result: Option ${OPTIONNAME} value is ${FOUNDVALUE}"

                    if [ "${TESTTYPE}" = "=" ]; then
                        if [ "${FOUNDVALUE}" = "${EXPECTEDVALUE}" ]; then
                            RESULT="GOOD"
                        elif [ "${FOUNDVALUE}" = "${MEDIUMSCOREDVALUE}" ]; then
                            RESULT="MIDSCORED"
                        elif [ "${FOUNDVALUE}" = "${WEAKVALUE}" ]; then
                            RESULT="WEAK"
                        else
                            RESULT="UNKNOWN"
                        fi

                    elif [ "${TESTTYPE}" = "<" ]; then
                        if [ "${FOUNDVALUE}" -ge "${WEAKVALUE}" -o "${FOUNDVALUE}" -gt "${MEDIUMSCOREDVALUE}" ]; then
                            RESULT="WEAK"
                        elif [ "${FOUNDVALUE}" -le "${MEDIUMSCOREDVALUE}" -a "${FOUNDVALUE}" -gt "${EXPECTEDVALUE}" ]; then
                            RESULT="MIDSCORED"
                        elif [ "${FOUNDVALUE}" -le "${EXPECTEDVALUE}" ]; then
                            RESULT="GOOD"
                        else
                            RESULT="UNKNOWN"
                        fi

                    elif [ "${TESTTYPE}" = ">" ]; then
                        if [ "${FOUNDVALUE}" -le "${WEAKVALUE}" ]; then
                            RESULT="WEAK"
                        elif [ "${FOUNDVALUE}" -le "${WEAKVALUE}" -a "${FOUNDVALUE}" -ge "${MEDIUMSCOREDVALUE}" ]; then
                            RESULT="MIDSCORED"
                        elif [ "${FOUNDVALUE}" -ge "${EXPECTEDVALUE}" ]; then
                            RESULT="GOOD"
                        else
                            RESULT="UNKNOWN"
                        fi

                    elif [ "${TESTTYPE}" = "!" ]; then
                        if [ "${FOUNDVALUE}" = "${WEAKVALUE}" ]; then
                            RESULT="WEAK"
                        elif [ ! "${FOUNDVALUE}" = "${WEAKVALUE}" ]; then
                            RESULT="GOOD"
                        else
                            RESULT="UNKNOWN"
                        fi

                    else
                        RESULT="NONE"
                    fi
                fi

                if [ "${RESULT}" = "GOOD" ]; then
                    LogText "Result: SSH option ${OPTIONNAME} is configured very well"
                    Display --indent 4 --text "- SSH option: ${OPTIONNAME}" --result "${STATUS_OK}" --color GREEN
                    AddHP 3 3
                elif [ "${RESULT}" = "MIDSCORED" ]; then
                    LogText "Result: SSH option ${OPTIONNAME} is configured reasonably"
                    ReportSuggestion ${TEST_NO} "Consider hardening SSH configuration" "${OPTIONNAME} (${FOUNDVALUE} --> ${EXPECTEDVALUE})" "-"
                    ReportDetails --test "${TEST_NO}" --service "sshd" --field "${OPTIONNAME}" --value "${FOUNDVALUE}" --preferredvalue "${EXPECTEDVALUE}" --description "sshd option ${OPTIONNAME}"
                    Display --indent 4 --text "- SSH option: ${OPTIONNAME}" --result "${STATUS_SUGGESTION}" --color YELLOW
                    AddHP 1 3
                elif [ "${RESULT}" = "WEAK" ]; then
                    LogText "Result: SSH option ${OPTIONNAME} is in a weak configuration state and should be fixed"
                    ReportSuggestion ${TEST_NO} "Consider hardening SSH configuration" "${OPTIONNAME} (${FOUNDVALUE} --> ${EXPECTEDVALUE})" "-"
                    ReportDetails --test "${TEST_NO}" --service "sshd" --field "${OPTIONNAME}" --value "${FOUNDVALUE}" --preferredvalue "${EXPECTEDVALUE}" --description "sshd option ${OPTIONNAME}"
                    Display --indent 4 --text "- SSH option: ${OPTIONNAME}" --result "${STATUS_SUGGESTION}" --color YELLOW
                    AddHP 0 3
                elif [ "${RESULT}" = "UNKNOWN" ]; then
                    LogText "Result: Value of SSH option ${OPTIONNAME} is unknown (not defined)"
                    Display --indent 4 --text "- SSH option: ${OPTIONNAME}" --result DEFAULT --color WHITE
                    Report "unknown_config_option[]=ssh|$SSH_DAEMON_CONFIG}|${OPTIONNAME}|"
                else
                    LogText "Result: Option ${OPTIONNAME} not found in output"
                    Display --indent 4 --text "- SSH option: ${OPTIONNAME}" --result "${STATUS_NOT_FOUND}" --color WHITE
                fi
              else
                if IsVerbose; then Display --indent 4 --text "- SSH option: ${OPTIONNAME}" --result "SKIPPED (via config)" --color WHITE; fi
            fi
        done
    fi
#
#################################################################################
#
    # Test        : SSH-7440
    # Description : AllowUsers / AllowGroups
    # Goal        : Check if only a specific amount of users/groups can log in to the system
    if [ ${SSH_DAEMON_RUNNING} -eq 1 -a ! "${SSH_DAEMON_OPTIONS_FILE}" = "" ]; then PREQS_MET="YES"; else PREQS_MET="NO"; fi
    Register --test-no SSH-7440 --preqs-met ${PREQS_MET} --weight L --network NO --category security --description "Check SSH option: AllowUsers and AllowGroups"
    if [ ${SKIPTEST} -eq 0 ]; then
        FOUND=0
        # AllowUsers
        FIND=`${EGREPBINARY} -i "^AllowUsers" ${SSH_DAEMON_OPTIONS_FILE} | ${AWKBINARY} '{ print $2 }'`
        if [ ! "${FIND}" = "" ]; then
            LogText "Result: AllowUsers set, with value ${FIND}"
            Display --indent 4 --text "- SSH option: AllowUsers" --result "${STATUS_FOUND}" --color GREEN
            FOUND=1
          else
            LogText "Result: AllowUsers is not set"
            Display --indent 4 --text "- SSH option: AllowUsers" --result "${STATUS_NOT_FOUND}" --color WHITE
        fi

        # AllowGroups
        FIND=`${EGREPBINARY} -i "^AllowGroups" ${SSH_DAEMON_OPTIONS_FILE} | ${AWKBINARY} '{ print $2 }'`
        if [ ! "${FIND}" = "" ]; then
            LogText "Result: AllowUsers set ${FIND}"
            Display --indent 4 --text "- SSH option: AllowGroups" --result "${STATUS_FOUND}" --color GREEN
            FOUND=1
          else
            LogText "Result: AllowGroups is not set"
            Display --indent 4 --text "- SSH option: AllowGroups" --result "${STATUS_NOT_FOUND}" --color WHITE
        fi

        if [ ${FOUND} -eq 1 ]; then
            LogText "Result: SSH is limited to a specific set of users, which is good"
            AddHP 2 2
          else
            LogText "Result: SSH has no specific user or group limitation. Most likely all valid users can SSH to this machine."
            AddHP 0 1
        fi
    fi
#
#################################################################################
#

Report "ssh_daemon_running=${SSH_DAEMON_RUNNING}"
#Report "ssh_daemon_port=${SSH_DAEMON_PORT}"

WaitForKeyPress

#
#================================================================================
# Lynis - Security Auditing and System Hardening for Linux and UNIX - https://cisofy.com