0
if [ "$(stat -c "%a" /etc/crontab)" == "644" ]
then
  echo "Is there a vulnerability: No, Permission set on /etc/crontab file is correct."
else
  echo "Is there a vulnerability: Yes, Permission set on /etc/crontab file is incorrect. echo awk -F: $(stat -c "%a" /etc/crontab)"
fi

This is an audit script i am working on, and I would like to output the current Permissions of the /etc/crontab file, if it is not equals to chmod 644. I've tried many methods to no avail. I am doing this in a RHEL 7 server if it matters.

  • 1
    Where exactly did you notice a problem? – Cyrus Nov 13 '19 at 08:33
  • 1
    `echo "Is there a vulnerability: Yes, Permission set on /etc/crontab file is incorrect. echo awk -F: $(stat -c "%a" /etc/crontab)"`. Should it be `echo "Is there a vulnerability: Yes, Permission set on /etc/crontab file is incorrect : $(stat -c "%a" /etc/crontab)"` ? (without `awk -F:`) – anishsane Nov 13 '19 at 08:36
  • 1
    The code looks fine and works for me. Beside this you should considered using the recommended arithmetic context in `Bash`: `if (( "$(stat -c "%a" /etc/crontab)" == 644 )); then echo "Vulnerability"; fi`. – stephanmg Nov 13 '19 at 08:38
  • 1
    See also https://stackoverflow.com/questions/18668556/comparing-numbers-in-bash. – stephanmg Nov 13 '19 at 08:38
  • 1
    Can you share the output of `stat -c "%a" /etc/crontab` ? Given that this is audit script, might be that the file has more restricted permission than 0644, which should be OK for the audit purpose (e.g., 0600, or even 0400). – dash-o Nov 13 '19 at 09:24

2 Answers2

1

This should be the most correct way to do what you want

#!/bin/bash

if (( $(stat -c "%a" /etc/crontab) == 644 ))
then
  echo "Is there a vulnerability: No, Permission set on /etc/crontab file is correct."
else
  echo "Is there a vulnerability: Yes, Permission set on /etc/crontab file is incorrect: $(stat -c "%a" /etc/crontab)"
fi
Francesco Gasparetto
  • 1,819
  • 16
  • 20
0

When you compare numbers in bash you should use -eq instead of ==. So your if from this:

if [ "$(stat -c "%a" /etc/crontab)" == "644" ]

must be

if [ "$(stat -c "%a" /etc/crontab)" -eq "644" ]

And in this line

 echo "Is there a vulnerability: Yes, Permission set on /etc/crontab file is incorrect. echo awk -F: $(stat -c "%a" /etc/crontab)"

you should remove quotes around %a

Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31