check-ssh.sh 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/bin/bash
  2. case $1 in
  3. Protocol)
  4. if [ "$(sed -e '/^#/d' -e '/^[ \t][ \t]*#/d' -e 's/#.*$//' -e '/^$/d' /etc/ssh/sshd_config | grep -w "^Protocol" | awk '{print $2}')" -ne 2 ];then
  5. exit 1
  6. fi
  7. ;;
  8. rhosts)
  9. if [ "$(sed -e '/^#/d' -e '/^[ \t][ \t]*#/d' -e 's/#.*$//' -e '/^$/d' /etc/ssh/sshd_config | grep -i IgnoreRhosts | awk '{print $2}')" != "yes" ];then
  10. exit 1
  11. fi
  12. ;;
  13. hostauth)
  14. if [ "$(sed -e '/^#/d' -e '/^[ \t][ \t]*#/d' -e 's/#.*$//' -e '/^$/d' /etc/ssh/sshd_config | grep -i HostbasedAuthentication | awk '{print $2}')" != "no" ];then
  15. exit 1
  16. fi
  17. ;;
  18. permitroot)
  19. if [ "$(sed -e '/^#/d' -e '/^[ \t][ \t]*#/d' -e 's/#.*$//' -e '/^$/d' /etc/ssh/sshd_config | grep -i PermitRootLogin | awk '{print $2}')" != "no" ];then
  20. exit 1
  21. fi
  22. ;;
  23. emptypassword)
  24. if [ "$(sed -e '/^#/d' -e '/^[ \t][ \t]*#/d' -e 's/#.*$//' -e '/^$/d' /etc/ssh/sshd_config | grep -i PermitEmptyPasswords | awk '{print $2}')" != "no" ];then
  25. exit 1
  26. fi
  27. ;;
  28. emptypasswordenvironment)
  29. if [ "$(sed -e '/^#/d' -e '/^[ \t][ \t]*#/d' -e 's/#.*$//' -e '/^$/d' /etc/ssh/sshd_config | grep -i PermitEmptyPasswords | awk '{print $2}')" != "no" ];then
  30. exit 1
  31. fi
  32. ;;
  33. ciphers)
  34. if grep -i "Ciphers.*aes128-ctr\|Ciphers.*aes256-ctr\|Ciphers.*aes192-ctr" /etc/ssh/sshd_config;then
  35. :
  36. else
  37. exit 1
  38. fi
  39. ;;
  40. banner)
  41. if grep -i banner /etc/ssh/sshd_config | grep -v "^#";then
  42. :
  43. else
  44. exit 1
  45. fi
  46. ;;
  47. installed)
  48. OPENSSH_SERVER=$(dpkg -s openssh-server | grep -i "Status:.*install.*ok.*installed" | wc -l)
  49. OPENSSH_CLIENT=$(dpkg -s openssh-client | grep -i "Status:.*install.*ok.*installed" | wc -l)
  50. if [ ${OPENSSH_SERVER} -eq 1 ]; then
  51. if [ ${OPENSSH_CLIENT} -eq 1 ]; then
  52. :
  53. else
  54. echo "OPENSSH_CLIENT ${OPENSSH_CLIENT} != 1"
  55. exit 1
  56. fi
  57. else
  58. echo "OPENSSH_SERVER ${OPENSSH_SERVER} != 1"
  59. exit 1
  60. fi
  61. ;;
  62. sshd_status)
  63. if systemctl status sshd | grep "Active:.*(running)";then
  64. :
  65. else
  66. exit 1
  67. fi
  68. ;;
  69. ClientAliveInterval)
  70. if grep ClientAliveInterval /etc/ssh/sshd_config | grep -v "^#";then
  71. INTERVAL=`grep ClientAliveInterval /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  72. if [ ${INTERVAL} -lt 60 ];then
  73. exit 1
  74. fi
  75. else
  76. exit 1
  77. fi
  78. ;;
  79. RhostsRSAAuthentication)
  80. if grep RhostsRSAAuthentication /etc/ssh/sshd_config | grep -v "^#";then
  81. SETVALUE=`grep RhostsRSAAuthentication /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  82. if [ "${SETVALUE}" == "no" ];then
  83. exit 1
  84. fi
  85. else
  86. exit 1
  87. fi
  88. ;;
  89. ClientAliveCountMax)
  90. if grep ClientAliveCountMax /etc/ssh/sshd_config | grep -v "^#";then
  91. SETVALUE=`grep ClientAliveCountMax /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  92. if [ ${SETVALUE} -gt 3 ];then
  93. exit 1
  94. fi
  95. else
  96. exit 1
  97. fi
  98. ;;
  99. IgnoreRhosts)
  100. if grep IgnoreRhosts /etc/ssh/sshd_config | grep -v "^#";then
  101. SETVALUE=`grep IgnoreRhosts /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  102. if [ "${SETVALUE}" == "no" ];then
  103. exit 1
  104. fi
  105. else
  106. exit 1
  107. fi
  108. ;;
  109. PrintLastLog)
  110. if grep PrintLastLog /etc/ssh/sshd_config | grep -v "^#";then
  111. SETVALUE=`grep PrintLastLog /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  112. if [ "${SETVALUE}" != "yes" ];then
  113. exit 1
  114. fi
  115. else
  116. exit 1
  117. fi
  118. ;;
  119. IgnoreUserKnownHosts)
  120. if grep IgnoreUserKnownHosts /etc/ssh/sshd_config | grep -v "^#";then
  121. SETVALUE=`grep IgnoreUserKnownHosts /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  122. if [ "${SETVALUE}" != "yes" ];then
  123. exit 1
  124. fi
  125. else
  126. exit 1
  127. fi
  128. ;;
  129. macs)
  130. if grep -i "MACs.*hmac-sha2-256\|MACs.*hmac-sha2-512" /etc/ssh/sshd_config;then
  131. :
  132. else
  133. exit 1
  134. fi
  135. ;;
  136. pubkeypermissive)
  137. COUNT=`find /etc/ssh/ -type f -name "*.pub" -perm /133 -exec ls -l {} \; | wc -l`
  138. if [ ${COUNT} -eq 0 ];then
  139. :
  140. else
  141. exit 1
  142. fi
  143. ;;
  144. hostkeypermissive)
  145. COUNT=`find /etc/ssh/ -type f -name "*ssh_host*key" -perm /177 -exec ls -l {} \; | wc -l`
  146. if [ ${COUNT} -eq 0 ];then
  147. :
  148. else
  149. exit 1
  150. fi
  151. ;;
  152. GSSAPIAuthentication)
  153. if grep GSSAPIAuthentication /etc/ssh/sshd_config | grep -v "^#";then
  154. SETVALUE=`grep GSSAPIAuthentication /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  155. if [ "${SETVALUE}" != "no" ];then
  156. exit 1
  157. fi
  158. else
  159. exit 1
  160. fi
  161. ;;
  162. KerberosAuthentication)
  163. if grep KerberosAuthentication /etc/ssh/sshd_config | grep -v "^#";then
  164. SETVALUE=`grep KerberosAuthentication /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  165. if [ "${SETVALUE}" != "no" ];then
  166. exit 1
  167. fi
  168. else
  169. exit 1
  170. fi
  171. ;;
  172. StrictModes)
  173. if grep StrictModes /etc/ssh/sshd_config | grep -v "^#";then
  174. SETVALUE=`grep StrictModes /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  175. if [ "${SETVALUE}" != "yes" ];then
  176. exit 1
  177. fi
  178. else
  179. exit 1
  180. fi
  181. ;;
  182. UsePrivilegeSeparation)
  183. if grep UsePrivilegeSeparation /etc/ssh/sshd_config | grep -v "^#";then
  184. SETVALUE=`grep UsePrivilegeSeparation /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  185. if [ "${SETVALUE}" != "yes" -a "${SETVALUE}" != "sandbox" ];then
  186. exit 1
  187. fi
  188. else
  189. exit 1
  190. fi
  191. ;;
  192. Compression)
  193. if grep Compression /etc/ssh/sshd_config | grep -v "^#";then
  194. SETVALUE=`grep Compression /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  195. if [ "${SETVALUE}" != "no" -a "${SETVALUE}" != "delayed" ];then
  196. exit 1
  197. fi
  198. else
  199. exit 1
  200. fi
  201. ;;
  202. X11Forwarding)
  203. if grep X11Forwarding /etc/ssh/sshd_config | grep -v "^#";then
  204. SETVALUE=`grep X11Forwarding /etc/ssh/sshd_config | grep -v "^#" | awk '{printf $2}'`
  205. if [ "${SETVALUE}" != "no" ];then
  206. exit 1
  207. fi
  208. else
  209. exit 1
  210. fi
  211. ;;
  212. pam_python)
  213. if grep -q 'pam_python' /etc/pam.d/sshd; then
  214. exit 1
  215. fi
  216. ;;
  217. esac