Guest User

Untitled

a guest
Aug 11th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.00 KB | None | 0 0
  1. #!/bin/bash
  2. # unix-privesc-check - Checks Unix system for simple privilege escalations
  3. # Copyright (C) 2008 [email protected]
  4. #
  5. #
  6. # License
  7. # -------
  8. # This tool may be used for legal purposes only. Users take full responsibility
  9. # for any actions performed using this tool. The author accepts no liability
  10. # for damage caused by this tool. If you do not accept these condition then
  11. # you are prohibited from using this tool.
  12. #
  13. # In all other respects the GPL version 2 applies:
  14. #
  15. # This program is free software; you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License version 2 as
  17. # published by the Free Software Foundation.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License along
  25. # with this program; if not, write to the Free Software Foundation, Inc.,
  26. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  27. #
  28. # You are encouraged to send comments, improvements or suggestions to
  29. #
  30. #
  31. # Description
  32. # -----------
  33. # Auditing tool to check for weak file permissions and other problems that
  34. # may allow local attackers to escalate privileges.
  35. #
  36. # It is intended to be run by security auditors and pentetration testers
  37. # against systems they have been engaged to assess, and also by system
  38. # admnisitrators who want to check for "obvious" misconfigurations. It
  39. # can even be run as a cron job so you can check regularly for misconfigurations
  40. # that might be introduced.
  41. #
  42. # Ensure that you have the appropriate legal permission before running it
  43. # someone else's system.
  44. #
  45. # TODO List
  46. # ---------
  47. # There's still plenty that this script doesn't do...
  48. # - Doesn't work for shell scripts! These appear as "/bin/sh my.sh" in the process listing.
  49. # This script only checks the perms of /bin/sh. Not what we're after. :-(
  50. # - Similarly for perl scripts. Probably python, etc. too.
  51. # - Check /proc/pid/cmdline for absolute path names. Check security of these (e.g. /etc/snmp/snmpd.conf)
  52. # - Check everything in root's path - how to find root's path?
  53. # - /proc/pid/maps, smaps are readable and lists some shared objects. We should check these.
  54. # - /proc/pid/fd contain symlinks to all open files (but you can't see other people FDs)
  55. # - check for trust relationships in /etc/hosts.equiv
  56. # - NFS imports / exports / automounter
  57. # - Insecure stuff in /etc/fstab (e.g. allowing users to mount file systems)
  58. # - Inspecting people's PATH. tricky. maybe read from /proc/pid/environ, .bashrc, /etc/profile, .bash_profile
  59. # - Check if /etc/init.d/* scripts are readable. Advise user to audit them if they are.
  60. # - .exrc?
  61. # - X11 trusts, apache passwd files, mysql trusts?
  62. # - Daemons configured in an insecure way: tftpd, sadmind, rexd
  63. # - World writable dirs aren't as bad if the sticky bit is set. Check for this before reporting vulns.
  64. # - Maybe do a strings of binaries (and their .so's?)
  65. # - Do a better job of parsing cron lines - search for full paths
  66. # - Maybe LDPATHs from /etc/env.d
  67. # - Check if ldd, ld.so.conf changes have broken this script on non-linux systems.
  68. # - Avoid check certain paths e.g. /-/_ clearly isn't a real directory.
  69. # - create some sort of readable report
  70. # - indicate when it's likely a result is a false positive and when it's not.
  71. # - Skip pseudo processes e.g. [usb-storage]
  72. # - File permission on kernel modules
  73. # - Replace calls to echo with a my_echo func. Should be passed a string and an "importance" value:
  74. # - my_echo 1 "This is important and should always be printed out"
  75. # - my_echo 2 "This is less important and should only be printed in verbose mode"
  76. # - We check some files / dirs multiple times. Slow. Can we implement a cache?
  77. # - grep for PRIVATE KEY to find private ssh and ssl keys. Where to grep?
  78. # - check SGID programs
  79.  
  80. VERSION="1.4"
  81. HOME_DIR_FILES=".netrc .ssh/id_rsa .ssh/id_dsa .rhosts .shosts .my.cnf .ssh/authorized_keys .bash_history .sh_history .forward"
  82. CONFIG_FILES="/etc/passwd /etc/group /etc/master.passwd /etc/inittab /etc/inetd.conf /etc/xinetd.con /etc/xinetd.d/* /etc/contab /etc/fstab /etc/profile /etc/sudoers"
  83. PGDIRS="/usr/local/pgsql/data ~postgres/postgresql/data ~postgres/data ~pgsql/data ~pgsql/pgsql/data /var/lib/postgresql/data /etc/postgresql/8.2/main /var/lib/pgsql/data"
  84.  
  85. get_owner () {
  86. GET_OWNER_FILE=$1
  87. GET_OWNER_RETURN=`ls -lLd "$GET_OWNER_FILE" | awk '{print $3}'`
  88. }
  89.  
  90. get_group () {
  91. GET_GROUP_FILE=$1
  92. GET_GROUP_RETURN=`ls -lLd "$GET_GROUP_FILE" | awk '{print $4}'`
  93. }
  94.  
  95. usage () {
  96. echo "unix-privesc-check v$VERSION ( http://pentestmonkey.net/tools/unix-privesc-check )"
  97. echo
  98. echo "Usage: unix-privesc-check { standard | detailed }"
  99. echo
  100. echo '"standard" mode: Speed-optimised check of lots of security settings.'
  101. echo
  102. echo '"detailed" mode: Same as standard mode, but also checks perms of open file'
  103. echo ' handles and called files (e.g. parsed from shell scripts,'
  104. echo ' linked .so files). This mode is slow and prone to false '
  105. echo ' positives but might help you find more subtle flaws in 3rd'
  106. echo ' party programs.'
  107. echo
  108. echo "This script checks file permissions and other settings that could allow"
  109. echo "local users to escalate privileges."
  110. echo
  111. echo "Use of this script is only permitted on systems which you have been granted"
  112. echo "legal permission to perform a security assessment of. Apart from this "
  113. echo "condition the GPL v2 applies."
  114. echo
  115. echo "Search the output for the word 'WARNING'. If you don't see it then this"
  116. echo "script didn't find any problems."
  117. echo
  118. }
  119.  
  120. banner () {
  121. echo "Starting unix-privesc-check v$VERSION ( http://pentestmonkey.net/tools/unix-privesc-check )"
  122. echo
  123. echo "This script checks file permissions and other settings that could allow"
  124. echo "local users to escalate privileges."
  125. echo
  126. echo "Use of this script is only permitted on systems which you have been granted"
  127. echo "legal permission to perform a security assessment of. Apart from this "
  128. echo "condition the GPL v2 applies."
  129. echo
  130. echo "Search the output below for the word 'WARNING'. If you don't see it then"
  131. echo "this script didn't find any problems."
  132. echo
  133. }
  134.  
  135. MODE=$1
  136.  
  137. if [ ! "$MODE" = "standard" ] && [ ! "$MODE" = "detailed" ]; then
  138. usage
  139. exit 0
  140. fi
  141.  
  142. # Parse any full paths from $1 (config files, progs, dirs).
  143. # Check the permissions on each of these.
  144. check_called_programs () {
  145. CCP_MESSAGE_STACK=$1
  146. CCP_FILE=$2
  147. CCP_USER=$3
  148. CCP_PATH=$4 # optional
  149.  
  150. # Check the perms of the supplied file regardless
  151. # The caller doesn't want to have to call check_perms as well as check_called_programs
  152. check_perms "$CCP_MESSAGE_STACK" "$CCP_FILE" "$CCP_USER" "$CCP_PATH"
  153.  
  154. # Skip the slow check if we're in quick mode
  155. if [ "$MODE" = "standard" ]; then
  156. return 0;
  157. fi
  158.  
  159. # Check if file is text or not
  160. IS_TEXT=`file "$CCP_FILE" | grep -i text`
  161. IS_DYNBIN=`file "$CCP_FILE" | grep -i 'dynamically linked'`
  162.  
  163. # Process shell scripts (would also work on config files that reference other files)
  164. if [ ! -z "$IS_TEXT" ]; then
  165. # Parse full paths from file - ignoring commented lines
  166. CALLED_FILES=`grep -v '^#' "$CCP_FILE" | sed -e 's/^[^\/]*//' -e 's/["'\'':}$]/\x0a/g' | grep '/' | sed -e 's/[ \*].*//' | grep '^/[a-zA-Z0-9_/-]*$' | sort -u`
  167. for CALLED_FILE in $CALLED_FILES; do
  168. # echo "$CCP_FILE contains a reference to $CALLED_FILE. Checking perms."
  169. check_perms "$CCP_MESSAGE_STACK $CCP_FILE contains the string $CALLED_FILE." "$CALLED_FILE" "$CCP_USER" "$CCP_PATH"
  170. done
  171. else
  172. # Process dynamically linked binaries
  173. if [ ! -z "$IS_DYNBIN" ]; then
  174.  
  175. CALLED_FILES=`ldd "$CCP_FILE" 2>/dev/null | grep '/' | sed 's/[^\/]*\//\//' | cut -f 1 -d ' '`
  176. for CALLED_FILE in $CALLED_FILES; do
  177. check_perms "$CCP_MESSAGE_STACK $CCP_FILE uses the library $CALLED_FILE." "$CALLED_FILE" "$CCP_USER" "$CCP_PATH"
  178. done
  179.  
  180. # Strings binary to look for hard-coded config files
  181. # or other programs that might be called.
  182. for CALLED_FILE in `strings "$CCP_FILE" | sed -e 's/^[^\/]*//' -e 's/["'\'':}$]/\x0a/g' | grep '/' | sed -e 's/[ \*].*//' | grep '^/[a-zA-Z0-9_/-]*$' | sort -u`; do
  183. check_perms "$CCP_MESSAGE_STACK $CCP_FILE contains the string $CALLED_FILE." "$CALLED_FILE" "$CCP_USER" "$CCP_PATH"
  184. done
  185. fi
  186. fi
  187. }
  188.  
  189. # Parse any full paths from $1 (config files, progs, dirs).
  190. # Check the permissions on each of these.
  191. check_called_programs_suid () {
  192. CCP_FILE=$1
  193. CCP_PATH=$2 # optional
  194.  
  195. get_owner $CCP_FILE; CCP_USER=$GET_OWNER_RETURN
  196. CCP_MESSAGE_STACK="$CCP_FILE is SUID $CCP_USER."
  197. LS=`ls -l $CCP_FILE`
  198. echo "Checking SUID-$CCP_USER program $CCP_FILE: $LS"
  199.  
  200. # Don't check perms of executable itself
  201. # check_perms "$CCP_MESSAGE_STACK" "$CCP_FILE" "$CCP_USER" "$CCP_PATH"
  202.  
  203. # Check if file is text or not
  204. IS_TEXT=`file "$CCP_FILE" | grep -i text`
  205. IS_DYNBIN=`file "$CCP_FILE" | grep -i 'dynamically linked'`
  206.  
  207. # Process shell scripts (would also work on config files that reference other files)
  208. if [ ! -z "$IS_TEXT" ]; then
  209. # Skip the slow check if we're in quick mode
  210. if [ "$MODE" = "standard" ]; then
  211. return 0;
  212. fi
  213.  
  214. # Parse full paths from file - ignoring commented lines
  215. CALLED_FILES=`grep -v '^#' "$CCP_FILE" | sed -e 's/^[^\/]*//' -e 's/["'\'':}$]/\x0a/g' | grep '/' | sed -e 's/[ \*].*//' | grep '^/[a-zA-Z0-9_/-]*$' | sort -u`
  216. for CALLED_FILE in $CALLED_FILES; do
  217. # echo "$CCP_FILE contains a reference to $CALLED_FILE. Checking perms."
  218. check_perms "$CCP_MESSAGE_STACK $CCP_FILE contains the string $CALLED_FILE." "$CALLED_FILE" "$CCP_USER" "$CCP_PATH"
  219. done
  220. else
  221. # Process dynamically linked binaries
  222. if [ ! -z "$IS_DYNBIN" ]; then
  223.  
  224. CALLED_FILES=`ldd "$CCP_FILE" 2>/dev/null | grep '/' | sed 's/[^\/]*\//\//' | cut -f 1 -d ' '`
  225. for CALLED_FILE in $CALLED_FILES; do
  226. check_perms "$CCP_MESSAGE_STACK $CCP_FILE uses the library $CALLED_FILE." "$CALLED_FILE" "$CCP_USER" "$CCP_PATH"
  227. done
  228.  
  229. # Skip the slow check if we're in quick mode
  230. if [ "$MODE" = "standard" ]; then
  231. return 0;
  232. fi
  233.  
  234. # Strings binary to look for hard-coded config files
  235. # or other programs that might be called.
  236. for CALLED_FILE in `strings "$CCP_FILE" | sed -e 's/^[^\/]*//' -e 's/["'\'':}$]/\x0a/g' | grep '/' | sed -e 's/[ \*].*//' | grep '^/[a-zA-Z0-9_/-]*$' | sort -u`; do
  237. check_perms "$CCP_MESSAGE_STACK $CCP_FILE contains the string $CALLED_FILE." "$CALLED_FILE" "$CCP_USER" "$CCP_PATH"
  238. done
  239. fi
  240. fi
  241. }
  242.  
  243. # Check if $1 can be changed by users who are not $2
  244. check_perms () {
  245. CP_MESSAGE_STACK=$1
  246. CHECK_PERMS_FILE=$2
  247. CHECK_PERMS_USER=$3
  248. CHECK_PERMS_PATH=$4 # optional
  249.  
  250. if [ ! -f "$CHECK_PERMS_FILE" ] && [ ! -d "$CHECK_PERMS_FILE" ] && [ ! -b "$CHECK_PERMS_FILE" ]; then
  251. CHECK_PERMS_FOUND=0
  252. if [ ! -z "$CHECK_PERMS_PATH" ]; then
  253. # Look for it in the supplied path
  254. for DIR in `echo "$CHECK_PERMS_PATH" | sed 's/:/ /g'`; do
  255. if [ -f "$DIR/$CHECK_PERMS_FILE" ]; then
  256. CHECK_PERMS_FOUND=1
  257. CHECK_PERMS_FILE="$DIR/$CHECK_PERMS_FILE"
  258. break
  259. fi
  260. done
  261. fi
  262.  
  263. #if [ "$CHECK_PERMS_FOUND" = "0" ]; then
  264. # echo "ERROR: File $CHECK_PERMS_FILE doesn't exist. Checking parent path anyway."
  265. # # return 0
  266. # fi
  267. fi
  268.  
  269. C=`echo "$CHECK_PERMS_FILE" | cut -c 1`
  270. if [ ! "$C" = "/" ]; then
  271. echo "ERROR: Can't find absolute path for $CHECK_PERMS_FILE. Skipping."
  272. return 0
  273. fi
  274.  
  275. echo " Checking if anyone except $CHECK_PERMS_USER can change $CHECK_PERMS_FILE"
  276.  
  277. while [ -n "$CHECK_PERMS_FILE" ]; do
  278. perms_secure "$CP_MESSAGE_STACK" $CHECK_PERMS_FILE $CHECK_PERMS_USER
  279. CHECK_PERMS_FILE=`echo $CHECK_PERMS_FILE | sed 's/\/[^\/]*$//'`
  280. done
  281. }
  282.  
  283. # Check if $1 can be read by users who are not $2
  284. check_read_perms () {
  285. CP_MESSAGE_STACK=$1
  286. CHECK_PERMS_FILE=$2
  287. CHECK_PERMS_USER=$3
  288.  
  289. if [ ! -f "$CHECK_PERMS_FILE" ] && [ ! -b "$CHECK_PERMS_FILE" ]; then
  290. echo "ERROR: File $CHECK_PERMS_FILE doesn't exist"
  291. return 0
  292. fi
  293.  
  294. echo " Checking if anyone except $CHECK_PERMS_USER can read file $CHECK_PERMS_FILE"
  295.  
  296. perms_secure_read "$CP_MESSAGE_STACK" "$CHECK_PERMS_FILE" "$CHECK_PERMS_USER"
  297. }
  298.  
  299. perms_secure_read () {
  300. PS_MESSAGE_STACK=$1
  301. PERMS_SECURE_FILE=$2
  302. PERMS_SECURE_USER=$3
  303.  
  304. if [ ! -b "$PERMS_SECURE_FILE" ] && [ ! -f "$PERMS_SECURE_FILE" ] && [ ! -d "$PERMS_SECURE_FILE" ]; then
  305. echo "ERROR: No such file or directory: $PERMS_SECURE_FILE. Skipping."
  306. return 0
  307. fi
  308.  
  309. # Check if owner is different (but ignore root ownership, that's OK)
  310. only_user_can_read "$PS_MESSAGE_STACK" $PERMS_SECURE_FILE $PERMS_SECURE_USER
  311.  
  312. # Check group read perm (but ignore root group, that's OK)
  313. group_can_read "$PS_MESSAGE_STACK" $PERMS_SECURE_FILE $PERMS_SECURE_USER
  314.  
  315. # Check world read perm
  316. world_can_read "$PS_MESSAGE_STACK" $PERMS_SECURE_FILE
  317. }
  318.  
  319. perms_secure () {
  320. PS_MESSAGE_STACK=$1
  321. PERMS_SECURE_FILE=$2
  322. PERMS_SECURE_USER=$3
  323.  
  324. if [ ! -d "$PERMS_SECURE_FILE" ] && [ ! -f "$PERMS_SECURE_FILE" ] && [ ! -b "$PERMS_SECURE_FILE" ]; then
  325. # echo "ERROR: No such file or directory: $PERMS_SECURE_FILE. Skipping."
  326. return 0
  327. fi
  328.  
  329. # Check if owner is different (but ignore root ownership, that's OK)
  330. only_user_can_write "$PS_MESSAGE_STACK" $PERMS_SECURE_FILE $PERMS_SECURE_USER
  331.  
  332. # Check group write perm (but ignore root group, that's OK)
  333. group_can_write "$PS_MESSAGE_STACK" $PERMS_SECURE_FILE $PERMS_SECURE_USER
  334.  
  335. # Check world write perm
  336. world_can_write "$PS_MESSAGE_STACK" $PERMS_SECURE_FILE
  337. }
  338.  
  339. only_user_can_write () {
  340. O_MESSAGE_STACK=$1
  341. O_FILE=$2
  342. O_USER=$3
  343.  
  344. # We just need to check the owner really as the owner
  345. # can always grant themselves write access
  346. get_owner $O_FILE; O_FILE_USER=$GET_OWNER_RETURN
  347. if [ ! "$O_USER" = "$O_FILE_USER" ] && [ ! "$O_FILE_USER" = "root" ]; then
  348. echo "WARNING: $O_MESSAGE_STACK The user $O_FILE_USER can write to $O_FILE"
  349. fi
  350. }
  351.  
  352. group_can_write () {
  353. O_MESSAGE_STACK=$1
  354. O_FILE=$2
  355. O_USER=$3 # ignore group write access $3 is only member of group
  356.  
  357. get_group $O_FILE; O_FILE_GROUP=$GET_GROUP_RETURN
  358. P=`ls -lLd $O_FILE | cut -c 6`
  359. if [ "$P" = "w" ] && [ ! "$O_GROUP" = "root" ]; then
  360. # check the group actually has some members other than $O_USER
  361. group_has_other_members "$O_FILE_GROUP" "$O_USER"; # sets OTHER_MEMBERS to 1 or 0
  362. if [ "$OTHER_MEMBERS" = "1" ]; then
  363. echo "WARNING: $O_MESSAGE_STACK The group $O_FILE_GROUP can write to $O_FILE"
  364. fi
  365. fi
  366. }
  367.  
  368. group_has_other_members () {
  369. G_GROUP=$1
  370. G_USER=$2
  371.  
  372. # If LDAP/NIS is being used this script can't check group memberships
  373. # we therefore assume the worst.
  374. if [ "$EXT_AUTH" = 1 ]; then
  375. OTHER_MEMBERS=1
  376. return 1
  377. fi
  378.  
  379. GROUP_LINE=`grep "^$G_GROUP:" /etc/group`
  380. MEMBERS=`echo "$GROUP_LINE" | cut -f 4 -d : | sed 's/,/ /g'`
  381.  
  382. GID=`echo "$GROUP_LINE" | cut -f 3 -d :`
  383. EXTRA_MEMBERS=`grep "^[^:]*:[^:]*:[0-9]*:$GID:" /etc/passwd | cut -f 1 -d : | xargs echo`
  384.  
  385. for M in $MEMBERS; do
  386. if [ ! "$M" = "$G_USER" ] && [ ! "$M" = "root" ]; then
  387. OTHER_MEMBERS=1
  388. return 1
  389. fi
  390. done
  391.  
  392. for M in $EXTRA_MEMBERS; do
  393. if [ ! "$M" = "$G_USER" ] && [ ! "$M" = "root" ]; then
  394. OTHER_MEMBERS=1
  395. return 1
  396. fi
  397. done
  398.  
  399. OTHER_MEMBERS=0
  400. return 0
  401. }
  402.  
  403. world_can_write () {
  404. O_MESSAGE_STACK=$1
  405. O_FILE=$2
  406.  
  407. P=`ls -lLd $O_FILE | cut -c 9`
  408. S=`ls -lLd $O_FILE | cut -c 10`
  409.  
  410. if [ "$P" = "w" ]; then
  411. if [ "$S" = "t" ]; then
  412. echo "WARNING: $O_MESSAGE_STACK World write is set for $O_FILE (but sticky bit set)"
  413. else
  414. echo "WARNING: $O_MESSAGE_STACK World write is set for $O_FILE"
  415. fi
  416. fi
  417. }
  418.  
  419. only_user_can_read () {
  420. O_MESSAGE_STACK=$1
  421. O_FILE=$2
  422. O_USER=$3
  423.  
  424. # We just need to check the owner really as the owner
  425. # can always grant themselves read access
  426. get_owner $O_FILE; O_FILE_USER=$GET_OWNER_RETURN
  427. if [ ! "$O_USER" = "$O_FILE_USER" ] && [ ! "$O_FILE_USER" = "root" ]; then
  428. echo "WARNING: $O_MESSAGE_STACK The user $O_FILE_USER can read $O_FILE"
  429. fi
  430. }
  431.  
  432. group_can_read () {
  433. O_MESSAGE_STACK=$1
  434. O_FILE=$2
  435. O_USER=$3
  436.  
  437. get_group $O_FILE; O_FILE_GROUP=$GET_GROUP_RETURN
  438. P=`ls -lLd $O_FILE | cut -c 5`
  439. if [ "$P" = "r" ] && [ ! "$O_GROUP" = "root" ]; then
  440. # check the group actually has some members other than $O_USER
  441. group_has_other_members "$O_FILE_GROUP" "$O_USER"; # sets OTHER_MEMBERS to 1 or 0
  442. if [ "$OTHER_MEMBERS" = "1" ]; then
  443. echo "WARNING: $O_MESSAGE_STACK The group $O_FILE_GROUP can read $O_FILE"
  444. fi
  445. fi
  446. }
  447.  
  448. world_can_read () {
  449. O_MESSAGE_STACK=$1
  450. O_FILE=$2
  451.  
  452. P=`ls -lLd $O_FILE | cut -c 8`
  453.  
  454. if [ "$P" = "w" ]; then
  455. echo "WARNING: $O_MESSAGE_STACK World read is set for $O_FILE"
  456. fi
  457. }
  458.  
  459. section () {
  460. echo
  461. echo '############################################'
  462. echo $1
  463. echo '############################################'
  464. }
  465.  
  466. # Guess OS
  467. if [ -x /usr/bin/showrev ]; then
  468. OS="solaris"
  469. SHADOW="/etc/shadow"
  470. elif [ -x /usr/sbin/sam -o -x /usr/bin/sam ]; then
  471. OS="hpux"
  472. SHADOW="/etc/shadow"
  473. elif [ -f /etc/master.passwd ]; then
  474. OS="bsd"
  475. SHADOW="/etc/master.passwd"
  476. else
  477. OS="linux"
  478. SHADOW="/etc/shadow"
  479. fi
  480. echo "Assuming the OS is: $OS"
  481. CONFIG_FILES="$CONFIG_FILES $SHADOW"
  482.  
  483. # Set path so we can access usual directories. HPUX and some linuxes don't have sbin in the path.
  484. PATH=$PATH:/usr/bin:/bin:/sbin:/usr/sbin; export PATH
  485.  
  486. # Check dependent programs are installed
  487. # Assume "which" is installed!
  488. PROGS="ls awk grep cat mount xargs file ldd strings"
  489. for PROG in $PROGS; do
  490. which $PROG 2>&1 > /dev/null
  491. if [ ! $? = "0" ]; then
  492. echo "ERROR: Dependend program '$PROG' is mising. Can't run. Sorry!"
  493. exit 1
  494. fi
  495. done
  496.  
  497. banner
  498.  
  499. section "Recording hostname"
  500. hostname
  501.  
  502. section "Recording uname"
  503. uname -a
  504.  
  505. section "Recording Interface IP addresses"
  506. if [ $OS = 'hpux' ]; then
  507. for IFACE in `lanscan | grep x | awk '{print $5}' 2>/dev/null`; do
  508. ifconfig $IFACE 2>/dev/null
  509. done
  510. else
  511. ifconfig -a
  512. fi
  513.  
  514. section "Checking if external authentication is allowed in /etc/passwd"
  515. FLAG=`grep '^+:' /etc/passwd`
  516. if [ -n "$FLAG" ]; then
  517. echo "WARNING: /etc/passwd allows external authentcation:"
  518. grep '^+:' /etc/passwd
  519. EXT_AUTH=1
  520. else
  521. echo "No +:... line found in /etc/passwd"
  522. fi
  523.  
  524. section "Checking nsswitch.conf for addition authentication methods"
  525. if [ -r "/etc/nsswitch.conf" ]; then
  526. NIS=`grep '^passwd' /etc/nsswitch.conf | grep 'nis'`
  527. if [ -n "$NIS" ]; then
  528. echo "WARNING: NIS is used for authentication on this system"
  529. EXT_AUTH=1
  530. fi
  531. LDAP=`grep '^passwd' /etc/nsswitch.conf | grep 'ldap'`
  532. if [ -n "$LDAP" ]; then
  533. echo "WARNING: LDAP is used for authentication on this system"
  534. EXT_AUTH=1
  535. fi
  536.  
  537. if [ -z "$NIS" ] && [ -z "$LDAP" ]; then
  538. echo "Neither LDAP nor NIS are used for authentication"
  539. fi
  540. else
  541. echo "ERROR: File /etc/nsswitch.conf isn't readable. Skipping checks."
  542. fi
  543.  
  544. # Check important config files aren't writable
  545. section "Checking for writable config files"
  546. for FILE in $CONFIG_FILES; do
  547. if [ -f "$FILE" ]; then
  548. check_perms "$FILE is a critical config file." "$FILE" root
  549. fi
  550. done
  551.  
  552. section "Checking if $SHADOW is readable"
  553. check_read_perms "/etc/shadow holds authentication data" $SHADOW root
  554.  
  555. section "Checking for password hashes in /etc/passwd"
  556. FLAG=`grep -v '^[^:]*:[x\*]*:' /etc/passwd | grep -v '^#'`
  557. if [ -n "$FLAG" ]; then
  558. echo "WARNING: There seem to be some password hashes in /etc/passwd"
  559. grep -v '^[^:]*:[x\*]*:' /etc/passwd | grep -v '^#'
  560. EXT_AUTH=1
  561. else
  562. echo "No password hashes found in /etc/passwd"
  563. fi
  564.  
  565. section "Checking account settings"
  566. # Check for something nasty like r00t::0:0::/:/bin/sh in /etc/passwd
  567. # We only need read access to /etc/passwd to be able to check this.
  568. if [ -r "/etc/passwd" ]; then
  569. OPEN=`grep "^[^:][^:]*::" /etc/passwd | cut -f 1 -d ":"`
  570. if [ -n "$OPEN" ]; then
  571. echo "WARNING: The following accounts have no password:"
  572. grep "^[^:][^:]*::" /etc/passwd | cut -f 1 -d ":"
  573. fi
  574. fi
  575. if [ -r "$SHADOW" ]; then
  576. echo "Checking for accounts with no passwords"
  577. if [ "$OS" = "linux" ]; then
  578. passwd -S -a | while read LINE
  579. do
  580. USER=`echo "$LINE" | awk '{print $1}'`
  581. STATUS=`echo "$LINE" | awk '{print $2}'`
  582. if [ "$STATUS" = "NP" ]; then
  583. echo "WARNING: User $USER doesn't have a password"
  584. fi
  585. done
  586. elif [ "$OS" = "solaris" ]; then
  587. passwd -s -a | while read LINE
  588. do
  589. USER=`echo "$LINE" | awk '{print $1}'`
  590. STATUS=`echo "$LINE" | awk '{print $2}'`
  591. if [ "$STATUS" = "NP" ]; then
  592. echo "WARNING: User $USER doesn't have a password"
  593. fi
  594. done
  595. fi
  596. else
  597. echo "File $SHADOW isn't readable. Skipping some checks."
  598. fi
  599.  
  600. section "Checking library directories from /etc/ld.so.conf"
  601. if [ -f "/etc/ld.so.conf" ] && [ -r "/etc/ld.so.conf" ]; then
  602. for DIR in `grep '^/' /etc/ld.so.conf`; do
  603. check_perms "$DIR is in /etc/ld.so.conf." $DIR root
  604. done
  605.  
  606. #FILES=`grep '^include' /etc/ld.so.conf | sed 's/^include *//'`
  607. #if [ ! -z "$FILES" ]; then
  608. # for DIR in `echo $FILES | xargs cat | sort -u`; do
  609. # done
  610. #fi
  611. else
  612. echo "File /etc/ld.so.conf not present. Skipping checks."
  613. fi
  614.  
  615. # Check sudoers if we have permission - needs root normally
  616. section "Checking sudo configuration"
  617. if [ -f "/etc/sudoers" ] && [ -r "/etc/sudoers" ]; then
  618. echo -----------------
  619. echo "Checking if sudo is configured"
  620. SUDO_USERS=`grep -v '^#' /etc/sudoers | grep -v '^[ \t]*$' | grep -v '^[ \t]*Default' | grep =`
  621. if [ ! -z "$SUDO_USERS" ]; then
  622. echo "WARNING: Sudo is configured. Manually check nothing unsafe is allowed:"
  623. grep -v '^#' /etc/sudoers | grep -v '^[ \t]*$' | grep = | grep -v '^[ \t]*Default'
  624. fi
  625.  
  626. echo -----------------
  627. echo "Checking sudo users need a password"
  628. SUDO_NOPASSWD=`grep -v '^#' /etc/sudoers | grep -v '^[ \t]*$' | grep NOPASSWD`
  629. if [ ! -z "$SUDO_NOPASSWD" ]; then
  630. echo "WARNING: Some users can use sudo without a password:"
  631. grep -v '^#' /etc/sudoers | grep -v '^[ \t]*$' | grep NOPASSWD
  632. fi
  633. else
  634. echo "File /etc/sudoers not present. Skipping checks."
  635. fi
  636.  
  637. section "Checking permissions on swap file(s)"
  638. for SWAP in `swapon -s | grep -v '^Filename' | cut -f 1 -d ' '`; do
  639. check_perms "$SWAP is used for swap space." $SWAP root
  640. check_read_perms "$SWAP is used for swap space." $SWAP root
  641. done
  642.  
  643. section "Checking programs run from inittab"
  644. if [ -f "/etc/inittab" ] && [ -r "/etc/inittab" ]; then
  645. for FILE in `cat /etc/inittab | grep : | grep -v '^#' | cut -f 4 -d : | grep '/' | cut -f 1 -d ' ' | sort -u`; do
  646. check_called_programs "$FILE is run from /etc/inittab as root." $FILE root
  647. done
  648. else
  649. echo "File /etc/inittab not present. Skipping checks."
  650. fi
  651.  
  652. section "Checking postgres trust relationships"
  653. for DIR in $PGDIRS; do
  654. if [ -d "$DIR" ] && [ -r "$DIR/pg_hba.conf" ]; then
  655. grep -v '^#' "$DIR/pg_hba.conf" | grep -v '^[ \t]*$' | while read LINE
  656. do
  657. AUTH=`echo "$LINE" | awk '{print $NF}'`
  658. if [ "$AUTH" = "trust" ]; then
  659. PGTRUST=1
  660. echo "WARNING: Postgres trust configured in $DIR/pg_hba.conf: $LINE"
  661. fi
  662. done
  663. fi
  664. done
  665.  
  666. PGVER1=`psql -U postgres template1 -c 'select version()' 2>/dev/null | grep version`
  667.  
  668. if [ -n "$PGVER1" ]; then
  669. PGTRUST=1
  670. echo "WARNING: Can connect to local postgres database as \"postgres\" without a password"
  671. fi
  672.  
  673. PGVER2=`psql -U pgsql template1 -c 'select version()' 2>/dev/null | grep version`
  674.  
  675. if [ -n "$PGVER2" ]; then
  676. PGTRUST=1
  677. echo "WARNING: Can connect to local postgres database as \"pgsql\" without a password"
  678. fi
  679.  
  680. if [ -z "$PGTRUST" ]; then
  681. echo "No postgres trusts detected"
  682. fi
  683.  
  684. # Check device files for mounted file systems are secure
  685. # cat /proc/mounts | while read LINE # Doesn't work so well when LVM is used - need to be root
  686. section "Checking permissions on device files for mounted partitions"
  687. if [ "$OS" = "linux" ]; then
  688. mount | while read LINE
  689. do
  690. DEVICE=`echo "$LINE" | awk '{print $1}'`
  691. FS=`echo "$LINE" | awk '{print $5}'`
  692. if [ "$FS" = "ext2" ] || [ "$FS" = "ext3" ] ||[ "$FS" = "reiserfs" ]; then
  693. echo "Checking device $DEVICE"
  694. check_perms "$DEVICE is a mounted file system." $DEVICE root
  695. fi
  696. done
  697. elif [ "$OS" = "bsd" ]; then
  698. mount | grep ufs | while read LINE
  699. do
  700. DEVICE=`echo "$LINE" | awk '{print $1}'`
  701. echo "Checking device $DEVICE"
  702. check_perms "$DEVICE is a mounted file system." $DEVICE root
  703. done
  704. elif [ "$OS" = "solaris" ]; then
  705. mount | grep xattr | while read LINE
  706. do
  707. DEVICE=`echo "$LINE" | awk '{print $3}'`
  708. if [ ! "$DEVICE" = "swap" ]; then
  709. echo "Checking device $DEVICE"
  710. check_perms "$DEVICE is a mounted file system." $DEVICE root
  711. fi
  712. done
  713. elif [ "$OS" = "hpux" ]; then
  714. mount | while read LINE
  715. do
  716. DEVICE=`echo "$LINE" | awk '{print $3}'`
  717. C=`echo $DEVICE | cut -c 1`
  718. if [ "$C" = "/" ]; then
  719. echo "Checking device $DEVICE"
  720. check_perms "$DEVICE is a mounted file system." $DEVICE root
  721. fi
  722. done
  723.  
  724. NFS=`mount | grep NFS`
  725. if [ -n "$NFS" ]; then
  726. echo "WARNING: This system is an NFS client. Check for nosuid and nodev options."
  727. mount | grep NFS
  728. fi
  729. fi
  730.  
  731. # Check cron jobs if they're readable
  732. # TODO check that cron is actually running
  733. section "Checking cron job programs aren't writable (/etc/crontab)"
  734. CRONDIRS=""
  735. if [ -f "/etc/crontab" ] && [ -r "/etc/crontab" ]; then
  736. MYPATH=`grep '^PATH=' /etc/crontab | cut -f 2 -d = `
  737. echo Crontab path is $MYPATH
  738.  
  739. # Check if /etc/cron.(hourly|daily|weekly|monthly) are being used
  740. CRONDIRS=`grep -v '^#' /etc/crontab | grep -v '^[ \t]*$' | grep '[ \t][^ \t][^ \t]*[ \t][ \t]*' | grep run-crons`
  741.  
  742. # Process run-parts
  743. grep -v '^#' /etc/crontab | grep -v '^[ \t]*$' | grep '[ \t][^ \t][^ \t]*[ \t][ \t]*' | grep run-parts | while read LINE
  744. do
  745. echo "Processing crontab run-parts entry: $LINE"
  746. USER=`echo "$LINE" | awk '{print $6}'`
  747. DIR=`echo "$LINE" | sed 's/.*run-parts[^()&|;\/]*\(\/[^ ]*\).*/\1/'`
  748. check_perms "$DIR holds cron jobs which are run as $USER." "$DIR" "$USER"
  749. if [ -d "$DIR" ]; then
  750. echo " Checking directory: $DIR"
  751. for FILE in $DIR/*; do
  752. FILENAME=`echo "$FILE" | sed 's/.*\///'`
  753. if [ "$FILENAME" = "*" ]; then
  754. echo " No files in this directory."
  755. continue
  756. fi
  757. check_called_programs "$FILE is run by cron as $USER." "$FILE" "$USER"
  758. done
  759. fi
  760. done
  761.  
  762. # TODO bsd'd periodic:
  763. # 1 3 * * * root periodic daily
  764. # 15 4 * * 6 root periodic weekly
  765. # 30 5 1 * * root periodic monthly
  766.  
  767. grep -v '^#' /etc/crontab | grep -v '^[ ]*$' | grep '[ ][^ ][^ ]*[ ][ ]*' | while read LINE
  768. do
  769. echo "Processing crontab entry: $LINE"
  770. USER=`echo "$LINE" | awk '{print $6}'`
  771. PROG=`echo "$LINE" | awk '{print $7}'`
  772. check_called_programs "$PROG is run from crontab as $USER." $PROG $USER $MYPATH
  773. done
  774. else
  775. echo "File /etc/crontab not present. Skipping checks."
  776. fi
  777.  
  778. # Do this if run-crons is run from /etc/crontab
  779. if [ -n "$CRONDIRS" ]; then
  780. USER=`echo "$CRONDIRS" | awk '{print $6}'`
  781. section "Checking /etc/cron.(hourly|daily|weekly|monthly)"
  782. for DIR in hourly daily weekly monthly; do
  783. if [ -d "/etc/cron.$DIR" ]; then
  784. echo " Checking directory: /etc/cron.$DIR"
  785. for FILE in /etc/cron.$DIR/*; do
  786. FILENAME=`echo "$FILE" | sed 's/.*\///'`
  787. if [ "$FILENAME" = "*" ]; then
  788. echo "No files in this directory."
  789. continue
  790. fi
  791. check_called_programs "$FILE is run via cron as $USER." "$FILE" $USER
  792. done
  793. fi
  794. done
  795. fi
  796.  
  797. section "Checking cron job programs aren't writable (/var/spool/cron/crontabs)"
  798. if [ -d "/var/spool/cron/crontabs" ]; then
  799. for FILE in /var/spool/cron/crontabs/*; do
  800. USER=`echo "$FILE" | sed 's/^.*\///'`
  801. if [ "$USER" = "*" ]; then
  802. echo "No user crontabs found in /var/spool/cron/crontabs. Skipping checks."
  803. continue
  804. fi
  805. echo "Processing crontab for $USER: $FILE"
  806. if [ -r "$FILE" ]; then
  807. MYPATH=`grep '^PATH=' "$FILE" | cut -f 2 -d = `
  808. if [ -n "$MYPATH" ]; then
  809. echo Crontab path is $MYPATH
  810. fi
  811. grep -v '^#' "$FILE" | grep -v '^[ \t]*$' | grep '[ \t][^ \t][^ \t]*[ \t][ \t]*' | while read LINE
  812. do
  813. echo "Processing crontab entry: $LINE"
  814. PROG=`echo "$LINE" | awk '{print $6}'`
  815. check_called_programs "$PROG is run via cron as $USER." "$PROG" $USER
  816. done
  817. else
  818. echo "ERROR: Can't read file $FILE"
  819. fi
  820. done
  821. else
  822. echo "Directory /var/spool/cron/crontabs is not present. Skipping checks."
  823. fi
  824.  
  825. section "Checking cron job programs aren't writable (/var/spool/cron/tabs)"
  826. if [ -d "/var/spool/cron/tabs" ]; then
  827. for FILE in /var/spool/cron/tabs/*; do
  828. USER=`echo "$FILE" | sed 's/^.*\///'`
  829. if [ "$USER" = "*" ]; then
  830. echo "No user crontabs found in /var/spool/cron/crontabs. Skipping checks."
  831. continue
  832. fi
  833. echo "Processing crontab for $USER: $FILE"
  834. if [ -r "$FILE" ]; then
  835. MYPATH=`grep '^PATH=' "$FILE" | cut -f 2 -d = `
  836. if [ -n "$MYPATH" ]; then
  837. echo Crontab path is $MYPATH
  838. fi
  839. grep -v '^#' "$FILE" | grep -v '^[ \t]*$' | grep '[ \t][^ \t][^ \t]*[ \t][ \t]*' | while read LINE
  840. do
  841. echo "Processing crontab entry: $LINE"
  842. PROG=`echo "$LINE" | awk '{print $6}'`
  843. check_called_programs "$PROG is run from cron as $USER." $PROG $USER $MYPATH
  844. done
  845. else
  846. echo "ERROR: Can't read file $FILE"
  847. fi
  848. done
  849. else
  850. echo "Directory /var/spool/cron/tabs is not present. Skipping checks."
  851. fi
  852.  
  853. # Check programs run from /etc/inetd.conf have secure permissions
  854. # TODO: check inetd is actually running
  855. section "Checking inetd programs aren't writable"
  856. if [ -f /etc/inetd.conf ] && [ -r /etc/inetd.conf ]; then
  857. grep -v '^#' /etc/inetd.conf | grep -v '^[ \t]*$' | while read LINE
  858. do
  859. USER=`echo $LINE | awk '{print $5}'`
  860. PROG=`echo $LINE | awk '{print $6}'` # could be tcpwappers ...
  861. PROG2=`echo $LINE | awk '{print $7}'` # ... and this is the real prog
  862. if [ -z "$PROG" ] || [ "$PROG" = "internal" ]; then
  863. # Not calling an external program
  864. continue
  865. fi
  866. echo Processing inetd line: $LINE
  867. if [ -f "$PROG" ]; then
  868. check_called_programs "$PROG is run from inetd as $USER." $PROG $USER
  869. fi
  870. if [ -f "$PROG2" ]; then
  871. check_called_programs "$PROG is run from inetd as $USER." $PROG2 $USER
  872. fi
  873. done
  874. else
  875. echo "File /etc/inetd.conf not present. Skipping checks."
  876. fi
  877.  
  878. # Check programs run from /etc/xinetd.d/*
  879. # TODO: check xinetd is actually running
  880. section "Checking xinetd programs aren't writeable"
  881. if [ -d /etc/xinetd.d ]; then
  882. for FILE in `grep 'disable[ \t]*=[ \t]*no' /etc/xinetd.d/* | cut -f 1 -d :`; do
  883. echo Processing xinetd service file: $FILE
  884. PROG=`grep '^[ \t]*server[ \t]*=[ \t]*' $FILE | sed 's/.*server.*=[ \t]*//'`
  885. USER=`grep '^[ \t]*user[ \t]*=[ \t]*' $FILE | sed 's/.*user.*=[ \t]*//'`
  886. check_called_programs "$PROG is run from xinetd as $USER." $PROG $USER
  887. done
  888. else
  889. echo "Directory /etc/xinetd.d not present. Skipping checks."
  890. fi
  891.  
  892. # Check for writable home directories
  893. section "Checking home directories aren't writable"
  894. cat /etc/passwd | grep -v '^#' | while read LINE
  895. do
  896. echo Processing /etc/passwd line: $LINE
  897. USER=`echo $LINE | cut -f 1 -d :`
  898. DIR=`echo $LINE | cut -f 6 -d :`
  899. SHELL=`echo $LINE | cut -f 7 -d :`
  900. if [ "$SHELL" = "/sbin/nologin" ] || [ "$SHELL" = "/bin/false" ]; then
  901. echo " Skipping user $USER. They don't have a shell."
  902. else
  903. if [ "$DIR" = "/dev/null" ]; then
  904. echo " Skipping /dev/null home directory"
  905. else
  906. check_perms "$DIR is the home directory of $USER." $DIR $USER
  907. fi
  908. fi
  909. done
  910.  
  911. # Check for readable files in home directories
  912. section "Checking for readable sensitive files in home directories"
  913. cat /etc/passwd | while read LINE
  914. do
  915. USER=`echo $LINE | cut -f 1 -d :`
  916. DIR=`echo $LINE | cut -f 6 -d :`
  917. SHELL=`echo $LINE | cut -f 7 -d :`
  918. for FILE in $HOME_DIR_FILES; do
  919. if [ -f "$DIR/$FILE" ]; then
  920. check_read_perms "$DIR/$FILE is in the home directory of $USER." "$DIR/$FILE" $USER
  921. fi
  922. done
  923. done
  924.  
  925. section "Checking SUID programs"
  926. if [ "$MODE" = "detailed" ]; then
  927. for FILE in `find / -type f -perm -04000 2>/dev/null`; do
  928. check_called_programs_suid $FILE
  929. done
  930. else
  931. echo "Skipping checks of SUID programs (it's slow!). Run again in 'detailed' mode."
  932. fi
  933.  
  934. # Check for private SSH keys in home directories
  935. section "Checking for Private SSH Keys home directories"
  936. for HOMEDIR in `cut -f 6 -d : /etc/passwd`; do
  937. if [ -d "$HOMEDIR/.ssh" ]; then
  938. PRIV_KEYS=`grep -l 'BEGIN [RD]SA PRIVATE KEY' $HOMEDIR/.ssh/* 2>/dev/null`
  939. if [ -n "$PRIV_KEYS" ]; then
  940. for KEY in $PRIV_KEYS; do
  941. ENC_KEY=`grep -l 'ENCRYPTED' "$KEY" 2>/dev/null`
  942. if [ -n "$ENC_KEY" ]; then
  943. echo "WARNING: Encrypted Private SSH Key Found in $KEY"
  944. else
  945. echo "WARNING: Unencrypted Private SSH Key Found in $KEY"
  946. fi
  947. done
  948. fi
  949. fi
  950. done
  951.  
  952. # Check for public SSH keys in home directories
  953. section "Checking for Public SSH Keys home directories"
  954. for HOMEDIR in `cut -f 6 -d : /etc/passwd`; do
  955. if [ -r "$HOMEDIR/.ssh/authorized_keys" ]; then
  956. KEYS=`grep '^ssh-' $HOMEDIR/.ssh/authorized_keys 2>/dev/null`
  957. if [ -n "$KEYS" ]; then
  958. echo "WARNING: Public SSH Key Found in $HOMEDIR/.ssh/authorized_keys"
  959. fi
  960. fi
  961. done
  962.  
  963. # Check for any SSH agents running on the box
  964. section "Checking for SSH agents"
  965. AGENTS=`ps -ef | grep ssh-agent | grep -v grep`
  966. if [ -n "$AGENTS" ]; then
  967. echo "WARNING: There are SSH agents running on this system:"
  968. ps -ef | grep ssh-agent | grep -v grep
  969. # for PID in `ps aux | grep ssh-agent | grep -v grep | awk '{print $2}'`; do
  970. for SOCK in `ls /tmp/ssh-*/agent.* 2>/dev/null`; do
  971. SSH_AUTH_SOCK=$SOCK; export SSH_AUTH_SOCK
  972. AGENT_KEYS=`ssh-add -l | grep -v 'agent has no identities.' 2>/dev/null`
  973. if [ -n "$AGENT_KEYS" ]; then
  974. echo "WARNING: SSH Agent has keys loaded [SSH_AUTH_SOCK=$SSH_AUTH_SOCK]"
  975. ssh-add -l
  976. fi
  977. done
  978. else
  979. echo "No SSH agents found"
  980. fi
  981.  
  982. # Check for any GPG agents running on the box
  983. section "Checking for GPG agents"
  984. AGENTS=`ps -ef | grep gpg-agent | grep -v grep`
  985. if [ -n "$AGENTS" ]; then
  986. echo "WARNING: There are GPG agents running on this system:"
  987. ps aux | grep gpg-agent | grep -v grep
  988. else
  989. echo "No GPG agents found"
  990. fi
  991.  
  992. # Check files in /etc/init.d/* can't be modified by non-root users
  993. section "Checking startup files (init.d / rc.d) aren't writable"
  994. for DIR in /etc/init.d /etc/rc.d /usr/local/etc/rc.d; do
  995. if [ -d "$DIR" ]; then
  996. for FILE in $DIR/*; do
  997. F=`echo "$FILE" | sed 's/^.*\///'`
  998. if [ "$F" = "*" ]; then
  999. echo "No user startup script found in $DIR. Skipping checks."
  1000. continue
  1001. fi
  1002. echo Processing startup script $FILE
  1003. check_called_programs "$FILE is run by root at startup." $FILE root
  1004. done
  1005. fi
  1006. done
  1007.  
  1008. section "Checking if running programs are writable"
  1009. if [ $OS = "solaris" ]; then
  1010. # use the output of ps command
  1011. ps -ef -o user,comm | while read LINE
  1012. do
  1013. USER=`echo "$LINE" | awk '{print $1}'`
  1014. PROG=`echo "$LINE" | awk '{print $2}'`
  1015. check_called_programs "$PROG is currently running as $USER." "$PROG" "$USER"
  1016. done
  1017. elif [ $OS = "bsd" ]; then
  1018. # use the output of ps command
  1019. ps aux | while read LINE
  1020. do
  1021. USER=`echo "$LINE" | awk '{print $1}'`
  1022. PROG=`echo "$LINE" | awk '{print $11}'`
  1023. check_called_programs "$PROG is currently running as $USER." "$PROG" "$USER"
  1024. done
  1025. elif [ $OS = "hpux" ]; then
  1026. # use the output of ps command
  1027. ps -ef | while read LINE
  1028. do
  1029. USER=`echo "$LINE" | awk '{print $1}'`
  1030. PROG1=`echo "$LINE" | awk '{print $8}'`
  1031. PROG2=`echo "$LINE" | awk '{print $9}'`
  1032. if [ -f "$PROG1" ]; then
  1033. check_called_programs "$PROG is currently running as $USER." "$PROG1" "$USER"
  1034. fi
  1035. if [ -f "$PROG2" ]; then
  1036. check_called_programs "$PROG is currently running as $USER." "$PROG2" "$USER"
  1037. fi
  1038. done
  1039. elif [ $OS = "linux" ]; then
  1040. # use the /proc file system
  1041. for PROCDIR in /proc/[0-9]*; do
  1042. unset PROGPATH
  1043. PID=`echo $PROCDIR | cut -f 3 -d /`
  1044. echo ------------------------
  1045. echo "PID: $PID"
  1046. if [ -d "$PROCDIR" ]; then
  1047. if [ -r "$PROCDIR/exe" ]; then
  1048. PROGPATH=`ls -l "$PROCDIR/exe" 2>&1 | sed 's/ (deleted)//' | awk '{print $NF}'`
  1049. else
  1050. if [ -r "$PROCDIR/cmdline" ]; then
  1051. P=`cat $PROCDIR/cmdline | tr "\0" = | cut -f 1 -d = | grep '^/'`
  1052. if [ -z "$P" ]; then
  1053. echo "ERROR: Can't find full path of running program: "`cat $PROCDIR/cmdline`
  1054. else
  1055. PROGPATH=$P
  1056. fi
  1057. else
  1058. echo "ERROR: Can't find full path of running program: "`cat $PROCDIR/cmdline`
  1059. continue
  1060. fi
  1061. fi
  1062. get_owner $PROCDIR; OWNER=$GET_OWNER_RETURN
  1063. echo "Owner: $OWNER"
  1064. else
  1065. echo "ERROR: Can't find OWNER. Process has gone."
  1066. continue
  1067. fi
  1068.  
  1069. if [ -n "$PROGPATH" ]; then
  1070. get_owner $PROGPATH; PROGOWNER=$GET_OWNER_RETURN
  1071. echo "Program path: $PROGPATH"
  1072. check_called_programs "$PROGPATH is currently running as $OWNER." $PROGPATH $OWNER
  1073. fi
  1074.  
  1075. if [ "$MODE" = "detailed" ]; then
  1076. for FILE in $PROCDIR/fd/*; do
  1077. F=`echo "$FILE" | sed 's/^.*\///'`
  1078. if [ "$F" = "*" ]; then
  1079. continue
  1080. fi
  1081. check_perms "$FILE is an open file descriptor for process $PID running as $OWNER." $FILE $OWNER
  1082. done
  1083. fi
  1084. done
  1085. fi
Advertisement
Add Comment
Please, Sign In to add comment