Shell script does not run properly

John Scott

I need help running a shell script. At my high school, I am competing in a cyber security competition, and we are given virtual machines of operating systems with security vulnerabilities that we must correct. Ubuntu is one of the operating systems we are given. To assist myself in the competition, I have written a shell script that automates some tasks. It is a normal shell script that is meant to be run with Bash (I use #!/bin/bash in the script instead of the typical #!/bin/sh because it uses some Bash-specific features) but it always acts strange in the virtual machines. I always make the file executable with chmod +x, but that doesn't help anything. Here's what happens. Whenever I try to invoke the script, it's like it prints random bits of the script on the terminal and it executes other portions of the script. It's strange, and I don't know how to troubleshoot it. How should I go about this?

Following is the script:

#!/bin/bash

# Security vulnerability scanner for Ubuntu
# Written by John L. Scott

# TODO
# Add PAM restrictions
# Change all users’ passwords

export UNIXMESSAGE1="The following UNIX program could not be found:"
export UNIXMESSAGE2="Please confirm that your standard Unix utilities are installed."

clear

echo "Starting Ubuntu security script..."
echo ""

# Check if script is running as root
if [ "$UID" != 0 ]
then
    echo "This script must be run as root."
    exit 1
fi

# Start dependency check

if [ "$(which nc)" = "" ]
then
    echo "Netcat is not installed. Install Netcat and try again."
    exit 1
fi

# Temporarily decrease swappiness for this session to make the system faster
if [ "$(which sysctl)" != "" ]
then
    sysctl vm.swappiness=0 > /dev/null
fi

if [ "$(which passwd)" = "" ]
then
    echo "$UNIXMESSAGE1 passwd"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which env)" = "" ]
then
    echo "$UNIXMESSAGE1 env"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which chown)" = "" ]
then
    echo "$UNIXMESSAGE1 chown"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which chmod)" = "" ]
then
    echo "$UNIXMESSAGE1 chmod"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which mkdir)" = "" ]
then
    echo "$UNIXMESSAGE1 mkdir"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which rm)" = "" ]
then
    echo "$UNIXMESSSAGE1 rm"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which touch)" = "" ]
then
    echo "$UNIXMESSAGE1 touch"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which pwd)" = "" ]
then
    echo "$UNIXMESSAGE1 pwd"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which sleep)" = "" ]
then
    echo "$UNIXMESSAGE1 sleep"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which cat)" = "" ]
then
    echo "$UNIXMESSAGE1 cat"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which ls)" = "" ]
then
    echo "$UNIXMESSAGE1 ls"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which mv)" = "" ]
then
    echo "$UNIXMESSAGE1 mv"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which cp)" = "" ]
then
    echo "$UNIXMESSAGE1 mv"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which grep)" = "" ]
then
    echo "$UNIXMESSAGE1 grep"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which deluser)" = "" ]
then
    echo "$UNIXMESSAGE1 deluser"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which sha512sum)" = "" ]
then
    echo "$UNIXMESSAGE1 sha512sum"
    echo "$UNIXMESSAGE2"
    exit 1
fi

if [ "$(which nano)" = "" ]
then
    echo "nano is not installed. Please install nano and try again."
    exit 1
fi

if [ "$(which awk)" = "" ]
then
    echo "No AWK interpreter was found."
    echo "Please install an AWK interpreter and try again."
    exit 1
fi

if [ "$(which uname)" = "" ]
then
    echo "$UNIXMESSAGE1 uname"
    echo "$UNIXMESSAGE2"
    exit 1
fi

# Now that we know uname is installed, we can run a quick kernel/OS check

if [ "$(uname -s)" != "Linux" ]
then
    if [ "$(uname -s)" = "Darwin" ]
    then
        echo "This script is running on an OS with the Darwin kernel, most likely OS X."
        echo "This script should be run on Ubuntu with the Linux kernel instead."
        echo ""
    else
        echo "The kernel currently running is not Linux."
        echo "This script should be run on Ubuntu with the Linux kernel instead."
    fi
    exit 1
fi

if [ "$(which apt-get)" = "" ]
then
    echo "APT is not installed."
    echo "Are you sure you're running this on Ubuntu?"
    echo "Make sure that APT is installed and try again."
    exit 1
fi

if [ "$(which python2)" = "" ]
then
    echo "Python 2 is not installed."
    echo "Please install Python 2 and try again."
    exit 1
fi

# If lsb_release is available, check one last time to make sure this only runs on Ubuntu
# If lsb_release is not available, don't worry about it and move on
if [ "$(which lsb_release)" != "" ]
then
    if [ "$(lsb_release -s -i)" != "Ubuntu" ]
    then
        echo "This script is meant to be run on Ubuntu. Try running this script again on Ubuntu."
        exit 1
    fi
    if [ "$(lsb_release -s -r)" != "12.04" ]
    then
        if [ "$(lsb_release -s -r)" != "14.04" ]
        then
            echo "This script has not yet been tested on Ubuntu $(lsb_release -s -r), but it will probably still work."
        fi
    fi
fi

# Let's test if the Internet connection works using Netcat
nc -z 8.8.8.8 53
if [ "$?" != "0" ]
then
    echo "You do not seem to have a working Internet connection."
    echo "Connect to the Internet and try again."
fi

# Warn about any installed server software that we should be concerned about

if [ -d /etc/apache2 ]
then
    echo "The Apache configuration file directory (/etc/apache2) has been found."
    echo "Apache may be installed."
    echo ""
fi

if [ -d /etc/mysql ]
then
    echo "The MySQL configuration file directory (/etc/mysql) has been found."
    echo "MySQL or MariaDB may be installed."
    echo ""
fi

if [ -d /etc/nginx ]
then
    echo "The Nginx configuration file directory (/etc/nginx) has been found."
    echo "Nginx may be installed."
    echo ""
fi

if [ -e /etc/ssh/sshd_config ]
then
    echo "The OpenSSH server configuration file has been found."
    echo “The OpenSSH server may be installed."
    echo "Remember to disable root login with \`PermitRootLogin no\`."
    echo ""
fi

echo "All systems tests have been run successfully. Press any key to start."
read -n 1 -s

# Install updates with APT

echo "Updating APT package cache..."
apt-get -qq update # -qq makes APT as silent as possible while still printing errors
echo "Checking for package upgrades..."
apt-get -qq dist-upgrade && echo "Done."
echo ""

# Warn about any suspicious software

if [ "$(which nmap)" != "" ]
then
    echo "Nmap is installed!"
    echo "Nmap is a program that can be used to scan devices on a network, search for open ports, and do other networking tasks."
    echo "Nmap can be used to perform malicious tasks."
    echo "This will be removed, assuming it has been installed with the package management system."
    apt-get purge nmap
fi

if [ "$(which wireshark)" != "" ]
then
    echo "Wireshark is installed!"
    echo "Wireshark is a packet analyzer that can inspect network traffic."
    echo "Wireshark can be used to perform malicious tasks."
    echo "This will be removed, assuming it has been installed with the package management system."
    apt-get purge wireshark
fi

if [ "$(which john)" != "" ]
then
    echo "John the Ripper is installed!"
    echo "John the Ripper is a password cracking tool that can be used to perform many different kinds of attacks to access users’ passwords."
    echo "John the Ripper can be used to perform malicious tasks."
    echo "This will be removed, assuming it has been installed with the package management system."
    apt-get purge john
fi

if [ "$(which tor)" != "" ]
then
    echo "Tor is installed!"
    echo "Tor is network anonymity software that can be used to get around network restrictions and secretly do activities over the Internet."
    echo "Tor is often associated with the Deep Web and the Dark Web."
    echo "This will be removed, assuming it has been installed with the package management system."
    apt-get purge tor
fi

echo "Here is a list of all users in the \"sudo\" group:"
python2 -c "import grp; print grp.getgrnam('sudo')[3]"
echo ""
echo "If you see anyone in that list that should not have administrator permissions, enter their username now or type \"done\"."

while true
do
    read DELSUDO
    if [ "$DELSUDO" = "done" ]
    then
        break
    fi
    deluser $DELSUDO sudo
    echo "If you must restrict another user from having administrator permisssions, enter their username now or type \"done\"."
done
echo ""

# Apache hardening
if [ -e /etc/apache2/apache2.conf ]
then
    echo "Here is a list of all users in the \"www-data\" group:"
    python2 -c "import grp; print grp.getgrnam('www-data')[3]"
    echo ""
    echo "If you see anyone in that list that should not have access to the Apache web server, enter their username now or type \"done\"."

    while true
    do
        read DELAPACHE
        if [ "$DELAPACHE" = "done" ]
        then
            break
        fi
        deluser $DELAPACHE www-data
        echo "If you must restrict another user from having access to Apache, enter their username now or type \"done\"."
    done
    echo ""
fi

# Let’s delete the root password if there is one
# Ubuntu uses sudo for all administrative tasks, so having a root account is a bad thing
passwd -l root

echo "Here is a list of (almost) all users on the system (check in the GUI later to be sure):"
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
echo ""
echo "If you see any users in that list that should not be on this system, enter their username now or type \"done\"."

while true
do
    read DELUSER
    if [ "$DELUSER" = "done" ]
    then
        break
    fi
    deluser --remove-home $DELUSER
    echo "If you must delete another user, enter their username now or type \"done\"."
done

echo ""

# Set the proper permissions for various files
chown root:root /etc/shadow
chmod 000 /etc/shadow
chown root:root /etc/passwd
chmod 644 /etc/passwd
chown root:root /etc/group
chmod 644 /etc/group
chown root:root /etc/fstab
sudo chmod 664 /etc/fstab

echo "Looking around for media files..."
echo "If any are found, take note and inspect these manually."
find /home -regex ".*\.\(wav\|aif\|mp4\|ogg\|avi\|mp3\|flac\|m4a|oga\|wma\|opus\|wma\|webm\|flv\|gif\|png\|jpg\|mov\|wmv\|mpg\|mpeg\|flv\)"

echo "Installing Gufw..."
echo "Use this to set up a firewall."
sudo apt-get -qq install gufw

echo "The security scan is complete."
exit 0
kos

Here's the problem:

 254      echo “The OpenSSH server may be installed."
               ^––SC1015 This is a unicode double quote. Delete and retype it.

Other things worth noticing:

  76      echo "$UNIXMESSSAGE1 rm"
                ^––SC2153 Possible misspelling: UNIXMESSSAGE1 may not be assigned, but UNIXMESSAGE1 is.
 260  read -n 1 -s
      ^––SC2162 read without -r will mangle backslashes.
 315      read DELSUDO
          ^––SC2162 read without -r will mangle backslashes.
 320      deluser $DELSUDO sudo
                  ^––SC2086 Double quote to prevent globbing and word splitting.
 335          read DELAPACHE
              ^––SC2162 read without -r will mangle backslashes.
 340          deluser $DELAPACHE www-data
                      ^––SC2086 Double quote to prevent globbing and word splitting.
 357      read DELUSER
          ^––SC2162 read without -r will mangle backslashes.
 362      deluser --remove-home $DELUSER
                                ^––SC2086 Double quote to prevent globbing and word splitting.

I found the problem using shellcheck.net; in general consider using this or an equivalent tool to quickly catch syntax errors / possible issues with your scripts.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

What does "Run command as a login shell" do?

분류에서Dev

Is there an easy way to log all activity that a shell script does?

분류에서Dev

Shell script file (.sh) does not run, and throws an error

분류에서Dev

Call shell script "inline" in another shell script

분류에서Dev

Run shell script on windows using Cygwin

분류에서Dev

Run script in a non interactive shell?

분류에서Dev

Can a shell script running in a ssh continue to run if the SSH instance closes?

분류에서Dev

how to run a shell script on git commit

분류에서Dev

How to run Bash shell script on Windows?

분류에서Dev

Getting error ": command not found" when trying to run shell script

분류에서Dev

Debugger does not attach itself to remote target if shell script is run before in IntelliJ

분류에서Dev

How do run MYSQL scripts from a shell script with bash variables?

분류에서Dev

Why does my cronjob not execute my shell-script?

분류에서Dev

How to do string comparison properly in shell script?

분류에서Dev

Button does not work properly

분류에서Dev

How to schedule run shell script that opens Firefox

분류에서Dev

Why does `exec 2>&1` fail in this bourne shell script?

분류에서Dev

Run two shell commands

분류에서Dev

How do I run a windows executable in linux shell script?

분류에서Dev

Shell script to run Linux command on multiple servers command by command

분류에서Dev

can not run the command from shell script but it's fine when typing directly into terminal

분류에서Dev

Why doesn't "sudo su" in a shell script run the rest of the script as root?

분류에서Dev

How to start a gnome shell and run multiple commands from a script

분류에서Dev

Long PHP script does not run completely

분류에서Dev

Run Android Studio on reboot using shell script

분류에서Dev

File globbing pattern, !(*example), behaves differently in bash script than it does in bash shell

분류에서Dev

Command not working properly in shell script

분류에서Dev

Shell script: run screen, open several screens and run a command in each

분류에서Dev

How to run a shell script in background?

Related 관련 기사

  1. 1

    What does "Run command as a login shell" do?

  2. 2

    Is there an easy way to log all activity that a shell script does?

  3. 3

    Shell script file (.sh) does not run, and throws an error

  4. 4

    Call shell script "inline" in another shell script

  5. 5

    Run shell script on windows using Cygwin

  6. 6

    Run script in a non interactive shell?

  7. 7

    Can a shell script running in a ssh continue to run if the SSH instance closes?

  8. 8

    how to run a shell script on git commit

  9. 9

    How to run Bash shell script on Windows?

  10. 10

    Getting error ": command not found" when trying to run shell script

  11. 11

    Debugger does not attach itself to remote target if shell script is run before in IntelliJ

  12. 12

    How do run MYSQL scripts from a shell script with bash variables?

  13. 13

    Why does my cronjob not execute my shell-script?

  14. 14

    How to do string comparison properly in shell script?

  15. 15

    Button does not work properly

  16. 16

    How to schedule run shell script that opens Firefox

  17. 17

    Why does `exec 2>&1` fail in this bourne shell script?

  18. 18

    Run two shell commands

  19. 19

    How do I run a windows executable in linux shell script?

  20. 20

    Shell script to run Linux command on multiple servers command by command

  21. 21

    can not run the command from shell script but it's fine when typing directly into terminal

  22. 22

    Why doesn't "sudo su" in a shell script run the rest of the script as root?

  23. 23

    How to start a gnome shell and run multiple commands from a script

  24. 24

    Long PHP script does not run completely

  25. 25

    Run Android Studio on reboot using shell script

  26. 26

    File globbing pattern, !(*example), behaves differently in bash script than it does in bash shell

  27. 27

    Command not working properly in shell script

  28. 28

    Shell script: run screen, open several screens and run a command in each

  29. 29

    How to run a shell script in background?

뜨겁다태그

보관