Skip to content
ImagineWorld

Shell Script Security

node_idnode-book_the-ultimate-linux-shell-scripting-guide_chapter-20-shell-script-security
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num20
chapter_titleShell Script Security
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Thus far, we haven’t talked a whole lot about shell scripting security. Frankly, it’s because it’s one of those things that you might never have to worry about. I mean, a lot of times you’ll just be writing scripts for your own use, that you’ll just be running from your own home directory on your own local machine. Even if you’re an administrator who needs to create scripts that perform some sort of administrative task, you might only need to either run them on from your own home directory or share them with other trusted administrators who just run them from their own home directories. In these cases, shell scripting security isn’t necessarily a huge deal.

However, you may at times need to share your script with other users or administrators that you don’t fully trust. In these cases, shell scripting security is extremely important, and should be a major part of your scripting focus. For example, you might have some sort of administrative script that you need to place into a directory that administrators with only limited permissions can access. In those cases, you also need to ensure that nobody can modify it, and that only certain designated administrators can execute it. You’ll also want to design the script in a way that will prevent bad actors from using it to perform command injection attacks.

Topics in this chapter include:

  • Controlling Access to Your Scripts
  • Understanding SUID and SGID Considerations
  • Avoiding Sensitive Data Leakage
  • Understanding Command Injection with eval
  • Understanding Path Security

If you’re ready, let’s get going.

Technical Requirements

I’ll mainly be working with Fedora Server and Ubuntu Server virtual machines. But, the techniques that I’ll show you should work on just about any Linux distro. I’ll also be showing you some things on a FreeBSD 14 virtual machine, and an OpenIndiana virtual machine.

As always, you can grab the scripts by doing:

git clone https://github.com/PacktPublishing/The-Ultimate-Linux-Shell-Scripting-Guide.git

A lot of the scripts that you create might be just for yourself or your co-workers. Or, they might be for general distribution to the public. In all of these cases, you might not need to worry about having any kind of access control on your scripts.

But, there might also be times when you need to create scripts that only certain people can access. The methods that you can use for this include:

  • Assigning sudo privileges
  • Assigning an Access Control List
  • Obfuscating plain-text scripts

We’ll begin by looking using sudo.

sudo is a handy security feature that is installed by default on macOS, OpenIndiana and most Linux distros. It’s also available for installation on most BSD-type distros and any Linux distros on which it isn’t installed by default. The most common way to use sudo is to allow non-privileged users to run programs with root user privileges. (You can also use sudo to allow users to run programs with the privileges of other non-root users, such as a database user. I’m not going to go into all that now though, because I’m trying to keep things simple.)

Now, here’s what makes sudo so cool. Let’s say that you want a particular user to run one particular program with root user privileges. With sudo, you don’t have to give that user the root user password. Instead, just configure the user’s sudo privileges for the program, and then let the user enter his or her own password whenever he or she needs to run the program. Let’s look at how that works.

In this lab, you’ll create a simple script, and configure sudo so that only designated users can run it.

  1. Create the sudo_demo1.sh script, like this:
#!/bin/bash
echo "This is a demo of sudo privileges."
  1. Copy the script to the /usr/local/sbin/ directory:
Terminal window
donnie@ubuntu2404:~$ sudo cp sudo_demo1.sh /usr/local/sbin/
donnie@ubuntu2404:~$ ls -l /usr/local/sbin/sudo_demo1.sh
-rw-r--r-- 1 root root 55 Jul 6 19:24 /usr/local/sbin/sudo_demo1.sh

As you see, copying this script to /user/local/sbin/ caused the ownership of the file to automatically change to the root user.

  1. Set permissions on the sudo_demo1.sh file so that only the root user can access it:
Terminal window
donnie@ubuntu2404:~$ cd /usr/local/sbin/
donnie@ubuntu2404:/usr/local/sbin$ sudo chmod 700 sudo_demo1.sh
donnie@ubuntu2404:/usr/local/sbin$ ls -l sudo_demo1.sh
-rwx------ 1 root root 55 Jul 6 19:24 sudo_demo1.sh
donnie@ubuntu2404:/usr/local/sbin$

Note how in the chmod 700 command, the 7 assigns read, write, and execute privileges to the root user. The two 0s remove all privileges from the group and from others. But, how do we obtain the value of 7 in the user position? Here’s the breakdown on how it works:

The read permission has a value of 4.

The write permission has a value of 2.

The execute permission has a value of 1.

In this case, we want the user to have full read, write, and execute permissions. Adding the values of all three of those permissions gives us a value of 7.

(I know that this is a rather cursory explanation, but for now, please bear with me.)

  1. Create a non-privileged user account for Horatio.

On Debian/Ubuntu, do:

Terminal window
sudo adduser horatio

On Fedora and other Red Hat-type distros:

Terminal window
sudo useradd horatio
sudo passwd horatio
  1. Begin configuring sudo by entering:
Terminal window
sudo visudo

This command will open the /etc/sudoers file in either nano, vi, or vim, depending upon which operating system you’re working with. Beyond that, the directions are the same.

  1. Scroll down until you see this line:
root ALL=(ALL:ALL) ALL

Directly below that line, place this line:

horatio ALL=(ALL:ALL) /usr/local/sbin/sudo_demo1.sh

Save the file as you would with a normal text editor.

I know that the ALL=(ALL:ALL) thing looks confusing, but it’s really quite simple. Here’s the TL;DR of what it means:

ON_HOSTS=(AS_USER:AS_GROUP_MEMBER) ALLOWED_COMMANDS

Now, here’s the more specific breakdown:

The first ALL means that the specified user can run this command on all machines on the local network. Optionally, you can replace this ALL with the hostname of a particular machine or groups of machines on which you want this user to be able to run this command.

The second ALL means that the specified user can run this command as all users, including the root user.

The third ALL means that Horatio can run this command as a member of all groups, including the root user’s group. (Note that this is optional. You can also just omit the group, and set this to ALL=(ALL).)

In the root line, the final ALL means that the root user can run all privileged commands. In the horatio line, the final ALL is replaced by the specific command that we want to allow Horatio to run.

  1. Open another terminal window on your host machine, and log into the virtual machine with Horatio’s account.

  2. Have Horatio test this by first trying to run the script without sudo, and then with sudo, like this:

Terminal window
horatio@ubuntu2404:~$ sudo_demo1.sh
-bash: /usr/local/sbin/sudo_demo1.sh: Permission denied
horatio@ubuntu2404:~$ sudo sudo_demo1.sh
[sudo] password for horatio:
This is a demo of sudo privileges.
horatio@ubuntu2404:~$

You see that Horatio can run the script with his sudo privileges.

  1. Have Horatio attempt to view the script’s source code, like so:
Terminal window
horatio@ubuntu2404:~$ cd /usr/local/sbin
horatio@ubuntu2404:/usr/local/sbin$ less sudo_demo1.sh
sudo_demo1.sh: Permission denied
Terminal window
horatio@ubuntu2404:/usr/local/sbin$ sudo less sudo_demo1.sh
Sorry, user horatio is not allowed to execute '/usr/bin/less sudo_demo1.
sh' as root on ubuntu2404.
horatio@ubuntu2404:/usr/local/sbin$

Horatio can’t view or edit the source code, because he doesn’t have the proper sudo privileges for that. He has sudo privileges to do one thing, and one thing only, as the root user, which is just to execute the sudo_demo1.sh script.

That does it for our introduction to the mysteries of sudo. Let’s move on to our next method of controlling access to scripts.

Using an Access Control List

If you’ve ever been a Windows administrator, you likely know that the NTFS filesystem on Windows allows you to grant really fine-grained permissions settings on files and directories. Sadly, the filesystems on Linux, Unix, and Unix-like systems don’t have such fine-grained access control built into them.

But, we can make up for that deficiency somewhat by using an Access Control List, or ACL. Let’s look at how to do that in this hands-on lab.

Hands-on Lab — Setting an ACL for Horatio on Linux

In this lab, we’ll create another script for which only Horatio will have permission to run. To keep things simple, just use the same virtual machine that you used for the previous lab, so that you won’t have to create another user account.

  1. You’ll find everything you need for setting up ACLs already installed on your Fedora virtual machine. If you’re using a Debian/Ubuntu type of machine, you might have to install the acl package by doing:
Terminal window
sudo apt install acl
  1. Log into your own normal user account, and create the acl_demo.sh script, like this:
#!/bin/bash
echo "This is a demo of ACLs."
  1. Copy the script to the /usr/local/sbin/ directory, and note how the ownership automatically changes to the root user:
Terminal window
donnie@ubuntu2404:~$ vim acl_demo.sh
donnie@ubuntu2404:~$ sudo cp acl_demo.sh /usr/local/sbin/
[sudo] password for donnie:
donnie@ubuntu2404:~$ cd /usr/local/sbin/
donnie@ubuntu2404:/usr/local/sbin$ ls -l acl_demo.sh
-rw-r--r-- 1 root root 44 Jul 24 20:56 acl_demo.sh
donnie@ubuntu2404:/usr/local/sbin$
  1. In order for an ACL to work, you’ll need to remove all permissions from group and others, like this:
Terminal window
donnie@ubuntu2404:/usr/local/sbin$ sudo chmod 700 acl_demo.sh
donnie@ubuntu2404:/usr/local/sbin$ ls -l acl_demo.sh
-rwx------ 1 root root 44 Jul 24 20:56 acl_demo.sh
donnie@ubuntu2404:/usr/local/sbin$

This is because the whole point of using an ACL is to prevent everyone who hasn’t been set up with an ACL from accessing the file.

  1. Use getfacl to verify that no ACL has been set:
acl_demo.sh
donnie@ubuntu2404:/usr/local/sbin$ getfacl acl_demo.sh
# owner: root
# group: root
user::rwx
group::---
other::---
donnie@ubuntu2404:/usr/local/sbin$
  1. In another terminal window, log into Horatio’s account, and try to run the acl_demo.sh script, like so:
Terminal window
horatio@ubuntu2404:~$ acl_demo.sh
-bash: /usr/local/sbin/acl_demo.sh: Permission denied
horatio@ubuntu2404:~$

You see that Horatio has been denied.

  1. Go back to your own terminal window. Create an ACL so that Horatio will have read and execute permissions on the acl_demo.sh script, like so:
Terminal window
donnie@ubuntu2404:/usr/local/sbin$ sudo setfacl -m u:horatio:rx acl_demo.sh
donnie@ubuntu2404:/usr/local/sbin$ ls -l acl_demo.sh
-rwxr-x---+ 1 root root 44 Jul 24 20:56 acl_demo.sh
donnie@ubuntu2404:/usr/local/sbin$

Here’s the breakdown of the command:

  • -m: This means to modify the existing ACL. It will also create an ACL if one hasn’t been created yet.
  • u:horatio: This means that we’re creating an ACL for user horatio.

• rx: This means that we’re granting the read and execute permissions for this file to the specified user.

• Note the + at the end of the permissions settings for this file. This indicates that an ACL has been created.

  1. Use getfacl to verify that the ACL was created properly:
acl_demo.sh
donnie@ubuntu2404:/usr/local/sbin$ getfacl acl_demo.sh
# owner: root
# group: root
user::rwx
user:horatio:r-x
group::---
mask::r-x
other::---
donnie@ubuntu2404:/usr/local/sbin$

The user:horatio:r-x line indicates that the ACL has been created for Horatio.

  1. Go back to Horatio’s terminal window, and have him try to run the script:
Terminal window
horatio@ubuntu2404:~$ acl_demo.sh
This is a demo of ACLs.
horatio@ubuntu2404:~$

This time, Horatio has achieved coolness.

  1. There is one downside to using an ACL instead of sudo. That is, sudo automatically assumes that a user needs to read a shell script in order to execute it. So, sudo allows a user to execute the script without explicitly setting the read permission for that user. This means that the user won’t be able to use any utility such as cat or less to view the contents of the script file. When using an ACL, the read permission and the execute permission both have to be explicitly set on the script to allow someone to run it. So, when using an ACL, you won’t be able to prevent the user from viewing the contents of the file. You can prove that by having Horatio view the file, like so:
horatio@ubuntu2404:~$ cat /usr/local/sbin/acl_demo.sh
#!/bin/bash
echo "This is a demo of ACLs."
horatio@ubuntu2404:~$

The bottom line here is that if you want to prevent users from viewing the contents of your scripts, set them up with the appropriate sudo privileges instead of using an ACL. On the other hand, if you don’t mind that users can view the script’s source code, then using an ACL is definitely an option.

Space doesn’t allow for me to present more than just a cursory coverage of permissions settings, sudo, and ACLs in this chapter. If you need more information about them, I have entire chapters devoted to each of these topics in my Mastering Linux Security and Hardening book.

That’s does it for sudo and ACLs on Linux. Let’s now see how to do it on FreeBSD 14.

Hands-on Lab – Setting an ACL for Horatio on FreeBSD 14

You’ll find everything you need for creating ACLs already installed on FreeBSD 14. Let’s begin.

  1. Create Horatio’s user account by doing:
Terminal window
donnie@freebsd14:~ $ sudo adduser

The FreeBSD adduser command is interactive, similar to the adduser command on Debian and Ubuntu. After you invoke it, you’ll just need to enter Horatio’s information, as prompted. Here’s what it looks like:

Terminal window
donnie@freebsd14:~ $ sudo adduser
Password:
Username: horatio
Full name: Horatio Black Cat
Uid (Leave empty for default):
Login group [horatio]:
Login group is horatio. Invite horatio into other groups? []:
Login class [default]:
Shell (sh csh tcsh bash rbash zsh rzh ksh93 nologin) [sh]:
Home directory [/home/horatio]:
Home directory permissions (Leave empty for default):
Use password-based authentication? [yes]:
Use an empty password? (yes/no) [no]:
Use a random password? (yes/no) [no]:
Enter password:
Enter password again:
Lock out the account after creation? [no]:
Username : horatio
Password : *****
Full Name : Horatio Black Cat
Uid : 1002
Class :
Groups : horatio
Home : /home/horatio
Home Mode :
Shell : /bin/sh
Locked : no
OK? (yes/no) [yes]:
adduser: INFO: Successfully added (horatio) to the user database.
Add another user? (yes/no) [no]:
Goodbye!
donnie@freebsd14:~ $

Figure 20.1: Adding a user account to FreeBSD

In case you’re wondering about the Full Name field, it’s just that Horatio really is a black cat who’s been visiting me lately.

  1. Use the same acl_demo.sh script that you used for the Linux lab. Copy it to the /usr/local/sbin/ directory, and verify that ownership has changed to the root user:
Terminal window
donnie@freebsd14:~ $ sudo cp acl_demo.sh /usr/local/sbin/
Password:
donnie@freebsd14:~ $ cd /usr/local/sbin/
donnie@freebsd14:/usr/local/sbin $ ls -l acl_demo.sh
-rw-r--r-- 1 root wheel 44 Jul 29 15:42 acl_demo.sh
donnie@freebsd14:/usr/local/sbin $
  1. Apply the 700 permissions setting to acl_demo.sh. This will mean that the root user will have read, write, and execute permissions, and that group and others have no permissions.
Terminal window
donnie@freebsd14:/usr/local/sbin $ sudo chmod 700 acl_demo.sh
donnie@freebsd14:/usr/local/sbin $ ls -l acl_demo.sh
-rwx------ 1 root wheel 44 Jul 29 15:42 acl_demo.sh
donnie@freebsd14:/usr/local/sbin $
  1. In another terminal window, log into Horatio’s user account on the FreeBSD machine. Then, have him attempt to run the acl_demo.sh script.
Terminal window
horatio@freebsd14:~ $ acl_demo.sh
-sh: acl_demo.sh: Permission denied
horatio@freebsd14:~ $ sudo acl_demo.sh
Password:
horatio is not in the sudoers file.
This incident has been reported to the administrator.
horatio@freebsd14:~ $
  1. Go back to your own terminal window, and set the ACL for Horatio, like so:
Terminal window
donnie@freebsd14:/usr/local/sbin $ sudo setfacl -m u:horatio:rx:allow acl_demo.sh
Password:
donnie@freebsd14:/usr/local/sbin $

Note that the command is slightly different this time, because Linux uses the Network Filesystem version 4 (NFSv4) style of ACLs, and FreeBSD uses the POSIX style of ACLs. It’s not a big difference, though. It’s just that with FreeBSD, you have to add allow to the setfacl command.

  1. Verify that the ACL has been properly set:
acl_demo.sh
donnie@freebsd14:/usr/local/sbin $ getfacl acl_demo.sh
# owner: root
# group: wheel
user:horatio:r-x---------:---------:allow
owner@:rwxp--aARWcCos:---------:allow
group@:------a-R-c-s:---------:allow
everyone@:------a-R-c-s:---------:allow
donnie@freebsd14:/usr/local/sbin $

You see that indeed, Horatio does have read and execute permissions for this script.

  1. Go back to Horatio’s terminal, and have him try to run the acl_demo.sh command:
Terminal window
horatio@freebsd14:~ $ acl_demo.sh
This is a demo of ACLs.
horatio@freebsd14:~ $

It works, which means that Horatio has now achieved coolness on FreeBSD.

Next, let’s give this a try on OpenIndiana.

Hands-on Lab – Setting an ACL for Horatio on OpenIndiana

Doing this on OpenIndiana will be considerably different, because it doesn’t use setfacl or getfacl to manage ACLs. Instead, it uses chmod for managing both normal permissions settings and ACLs. You’ll once again use the same acl_demo.sh script that you used for the previous labs.

  1. Create Horatio’s user account, like this:
Terminal window
donnie@openindiana:~$ sudo useradd -m horatio
donnie@openindiana:~$ sudo passwd horatio
donnie@openindiana:~$

The -m option tells useradd to create the new user’s home directory. On the OpenIndiana version of useradd, this is normally the only option switch that you’ll need.

  1. There’s no /usr/local/ directory on OpenIndiana, so just copy the acl_demo.sh script to the /usr/sbin/ directory, instead. Then, verify that the ownership has changed to the root user.
Terminal window
donnie@openindiana:~$ sudo cp acl_demo.sh /usr/sbin/
donnie@openindiana:~$ cd /usr/sbin/
donnie@openindiana:/usr/sbin$ ls -l acl_demo.sh
-rw-r--r-- 1 root root 44 Jul 29 16:14 acl_demo.sh
  1. Change the permissions setting of the acl_demo.sh file to 700, as you did in the previous labs:
Terminal window
donnie@openindiana:/usr/sbin$ sudo chmod 700 acl_demo.sh
donnie@openindiana:/usr/sbin$ ls -l acl_demo.sh
-rwx------ 1 root root 44 Jul 29 16:14 acl_demo.sh
donnie@openindiana:/usr/sbin$
  1. In another terminal window, log into Horatio’s account. Have him try to run the acl_demo.sh script:
Terminal window
horatio@openindiana:~$ acl_demo.sh
-bash: /usr/sbin/acl_demo.sh: Permission denied
horatio@openindiana:~$ sudo acl_demo.sh
Password:
horatio is not in the sudoers file.
This incident has been reported to the administrator.
horatio@openindiana:~$
  1. Now, apply an ACL for Horatio, granting him both read and execute privileges for the acl_demo.sh script:
Terminal window
donnie@openindiana:/usr/sbin$ sudo chmod A+user:horatio:rx:allow acl_demo.sh
Password:
donnie@openindiana:/usr/sbin$

Note how instead of using setfacl -m u:horatio, OpenIndiana uses chmod A+user:horatio. The A+ in this case just means that we’re adding an ACL.

  1. Go back to Horatio’s terminal, and have him try to run the script:
Terminal window
horatio@openindiana:~$ acl_demo.sh
This is a demo of ACLs.
horatio@openindiana:~$

Yes, indeed. Even on OpenIndiana, Horatio has achieved coolness.

If you need to learn about OpenIndiana administration, you’ll find that the official documentation at the OpenIndiana website is rather lacking. Fortunately, OpenIndiana is a fork of Oracle’s Solaris operating system, which means that you can use the official Solaris documentation, instead. (I’ve placed a link to the relevant page in the Further Reading section.)

I think that that about covers it for ACLs. Let’s move on to another way to control access to your scripts.

Obfuscating Plain-Text Scripts

You can also hide the contents of your shell scripts and prevent anyone from tampering with them by using the shc utility to convert your scripts into obfuscated executable binary files. And, you can do some things with shc that you can’t do with either sudo or ACLs. Specifically, shc allows you to:

  • Create an executable file that can run on only one machine.
  • Set expiration dates for the executable files.
  • Create executable files that can’t be traced with debugging utilities such as strace or truss. (I’ll explain those in just a bit.)

Let’s begin by installing shc.

Installing shc

It’s easy to install on Linux, FreeBSD, and macOS. Here’s how it’s done:

On Fedora:

Terminal window
sudo dnf install shc gcc

On Debian/Ubuntu:

Terminal window
sudo apt install shc gcc

Note that on Linux, you also need to install the gcc package so that you will have a C compiler that will work with shc. On FreeBSD, the C compiler gets installed as part of the operating system.

On FreeBSD:

Terminal window
sudo pkg install shc

On macOS with Homebrew installed:

brew install shc

Sadly, shc isn’t available for OpenIndiana.

Hands-on Lab — Using shc

For this lab, you’ll create the supersecret .sh script on your Fedora virtual machine. Also, go ahead and boot up your Debian/Ubuntu virtual machine, so that you can test your compiled scripts on it.

  1. Using shc is simple. To demo, let’s create the supersecret .sh script, like this:
#!/bin/bash
cat << secret
This document contains secrets that could impact the entire world!
Please don't let it fall into the wrong hands.
secret

This is just a simple here document that prints out a message, which looks like this:

Terminal window
donnie@fedora:~$ ./supersecret.sh
This document contains secrets that could impact the entire world!
Please don't let it fall into the wrong hands.
donnie@fedora:~$
  1. The next step is to convert the shell script into an obfuscated binary file, like so:
Terminal window
donnie@fedora:~$ shc -f supersecret.sh -o supersecret
donnie@fedora:~$

As you see, I’m using the -f option to point to the shell script that I want to obfuscate, and the -o option to save the obfuscated binary file with the specified filename. This operation also creates a C language source file, as you see here:

Terminal window
donnie@fedora:~$ ls -l supersecret*
-rwxrwxr-x. 1 donnie donnie 15984 Jul 1 16:55 supersecret
-rwxr--r--. 1 donnie donnie 149 Jul 1 16:47 supersecret.sh
-rw-r--r--. 1 donnie donnie 18485 Jul 1 16:55 supersecret.sh.x.c
donnie@fedora:~$

shc works by first creating this C source code file, and then by calling a C compiler to compile the C source code into the binary file. You can prove that this works by trying to open the binary file with cat, like so:

Terminal window
donnie@fedora:~$ cat supersecret
@@@@@?@@@@?
?
@@a
a

If you can read that, then you’re better than I am.

  1. Try running this new supersecret binary, and you’ll see that it works just like the script:
Terminal window
donnie@fedora:~$ ./supersecret
This document contains secrets that could impact the entire world!
Please don't let it fall into the wrong hands.
donnie@fedora:~$

Okay, let’s be real. You’re not going to obfuscate a simple script that does nothing but print out a message. I mean, anyone who can execute this binary file can still see the message. So, let’s just say that your script contains a lot of additional code or data that you want to hide from everyone, including even the authorized users of the script. You also want to ensure that nobody can modify your scripts. In cases like that, shc is definitely a useful tool.

  1. By default, shc creates binary files that will only run on the machine on which they were created. For example, let’s see what happens when I transfer the supersecret binary that I created on this Fedora machine to an Ubuntu machine:
Terminal window
donnie@ubuntu2404:~$ ./supersecret
./supersecret: 3(???'(??qX5??@??ZBhas expired!
Please contact your provider [email protected]
donnie@ubuntu2404:~$

That’s definitely a handy security feature, because it allows you to control where your program can be run. So, if a malicious hacker were to somehow find your program and download it, he or she wouldn’t be able to run it. The only catch is that we see the default Please contact your provider… message that’s built into shc.

  1. The default Please contact your provider… message doesn’t mean anything to us, because it provides us with a bogus contact address. On the Fedora machine, fix that by adding the -m option and a custom message, like this:
  1. Transfer the new supersecret binary file to the Debian/Ubuntu machine, and try to run it. You should now see your custom message, like this:
Terminal window
donnie@ubuntu2404:~$ ./supersecret
./supersecret: ?!???(??TR??0#50??fo_?7has expired!
You cannot run this program on this machine.
donnie@ubuntu2404:~$

Of course, you could also add your own contact address, if you really wanted to.

  1. Sometimes though, you might want to create binary files that will run on any machine that’s running the same operating system as the machine on which you created them. To do that, just relax the security a bit with the -r option, like so:
Terminal window
donnie@fedora:~$ shc -rf supersecret.sh -o supersecret
donnie@fedora:~$

When I transfer this new binary file to my Ubuntu machine, it will run just fine, as you see here:

Terminal window
donnie@ubuntu2404:~$ ./supersecret
This document contains secrets that could impact the entire world!
Please don't let it fall into the wrong hands.
donnie@ubuntu2404:~$
  1. Also, understand that shc creates binary files that can run on only one type of operating system. To see how that works, let’s transfer the binary that I created on my Fedora machine to a FreeBSD machine. Here’s what happens when I try to run it:
Terminal window
donnie@freebsd14:~$ ./supersecret
ELF binary type "0" not known.
-sh: ./supersecret: Exec format error
donnie@freebsd14:~$

But, if you transfer the original script to the FreeBSD machine and then run shc on it, you’ll get a binary that will run on FreeBSD.

The good news here is that shc works exactly the same on FreeBSD as it does on Linux. So, you won’t even have to modify your shc commands.

The next shc option I’ll cover is the -e option, which allows you to set an expiration date on your program. Just specify the expiration date in the day-month-year (dd/mm/yyyy) format, like so:

Terminal window
donnie@fedora:~$ shc -e 02/07/2024 -rf supersecret.sh -m "This binary file has expired. Contact me at [email protected] if you really need to run it." -o supersecret
donnie@fedora:~$

I’m doing this on 1 July 2024, so I’ll have to come back tomorrow to see if this actually works. So bye for now, I’ll see you tomorrow.

  1. Okay, it’s now 2 July 2024, and I’m back. Let’s see if this supersecret binary file still works:
Terminal window
donnie@fedora:~$ ./supersecret
./supersecret: has expired!
This binary file has expired. Contact me at [email protected] if you really need to run it.
donnie@fedora:~$

Cool, the expiration date option works just fine.

Remember: Even when you obfuscate your scripts with shc, you’ll still need to use either sudo or an ACL to control who can execute them on a particular machine.

Now, let’s see about making our executables untraceable.

Hands-on Lab — Creating Untraceable Executables

As you’ll see in the next chapter, we have several tracing tools available to help programmers debug programs. These tools include strace for Linux systems, truss for FreeBSD, and either dtrace or dtruss for macOS. For Linux, there’s also ltrace, which can trace calls to programming libraries.

The strace, dtrace, truss, and dtruss utilities can trace the system calls that a program makes. The easiest way to explain system calls is that they’re the mechanism that programs use to communicate with the operating system kernel.

The problem for us is that either a malicious actor or an unauthorized non-malicious actor could also use the information that these utilities provide to reverse engineer one of your compiled scripts. This could allow these unauthorized parties to see secrets that you don’t want them to see.

What information would we not want unauthorized people to see? Here are two examples:

The names and locations of sensitive files that your program accesses: These files could reveal secrets to unauthorized actors.

Embedded passwords: Even if you encrypt passwords, using strace, truss, dtrace, or dtruss on your compiled binary file will reveal the plain-text passwords. (You’ll soon see that in the Avoiding Sensitive Data Leakage section.)

Needless to say, either of these cases can represent a serious security problem. So to be sure, just get into the habit of making your shc binaries untraceable, as I’m about to show you in this section.

Here’s how it works:

  1. On the Fedora machine, install strace and ltrace, like this:
Terminal window
donnie@fedora:~$ sudo dnf install strace ltrace
  1. On the Fedora machine, create a new supersecret binary without either the expiration date or the custom message.
Terminal window
donnie@fedora:~$ shc -f supersecret.sh -o supersecret
donnie@fedora:~$
  1. Create a file that contains strace data about the supersecret binary, like this:
Terminal window
donnie@fedora:~$ strace ./supersecret 2> supersecret1_trace.txt
This document contains secrets that could impact the entire world!
Please don't let it fall into the wrong hands.
donnie@fedora:~$

Note that the strace output gets sent to stderr, which is why you have to use the 2> redirector.

  1. Open the supersecret1_trace.txt file in less. What you’ll see is a jumbled up mess that looks something like this:
execve("./supersecret", ["./supersecret"], 0x7ffd6152bd00 /* 58 vars */) = 0
brk(NULL) = 0x14345000
arch_prctl(0x3001 /* ARCH_??? */, 0x7ffe27faa7c0) = -1 EINVAL (Invalid argument)
<!-- page: 531 (printed 534) -->
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
...

For the most part, you would need to be a C language kernel programmer to understand what’s going on with this. However, as we’ll soon see, you might be able to glean some really important information if you know how to look for it.

  • Count how many lines are in the supersecret1_trace.txt file, like this:
Terminal window
donnie@fedora:~$ wc -l supersecret1_trace.txt
297 supersecret1_trace.txt
donnie@fedora:~$

So, there are 297 lines of output in the supersecret1_trace.txt file.

Note that for all of these examples, you might get a different number of output lines on your own machines.

  1. Perform an ltrace operation on the supersecret binary, saving the output to the supersecret1_ltrace.txt file. Then, count the number of lines in the supersecret1_ltrace.txt file, like so:
Terminal window
donnie@fedora:~$ ltrace ./supersecret 2> supersecret1_ltrace.txt
This document contains secrets that could impact the entire world!
Please don't let it fall into the wrong hands.
Terminal window
donnie@fedora:~$ wc -l supersecret1_ltrace.txt
7270 supersecret1_ltrace.txt
donnie@fedora:~

You see that there are 7270 lines in the supersecret1_ltrace file.

  1. Create a new supersecret binary. This time though, use the -U option to make this binary untraceable.
Terminal window
donnie@fedora:~$ shc -Uf supersecret.sh -o supersecret
donnie@fedora:~
  1. Run strace on the supersecret binary again, saving the output to the supersecret2_trace.txt file.
Terminal window
donnie@fedora:~$ strace ./supersecret 2> supersecret2_trace.txt
Killed
donnie@fedora:~$

This time, strace was prevented from doing its job.

  1. Even with the -U option, you’ll still get some output sent to the output file. However, it will be much less than you got without the -U. Verify that by counting the lines in the supersecret2_trace.txt file.
Terminal window
donnie@fedora:~$ wc -l supersecret2_trace.txt
34 supersecret2_trace.txt
donnie@fedora:~$

This time, there are only 34 lines of output, which is way less than the 297 lines that we had without the -U switch. So, this is further proof that the -U option prevents an strace operation from being successful.

  1. Now, do an ltrace on the supersecret binary, and save the output to the supersecret2_ltrace.txt file:
Terminal window
donnie@fedora:~$ ltrace ./supersecret 2> supersecret2_ltrace.txt
donnie@fedora:~$
  1. Count the lines in the supersecret2_ltrace.txt file:
Terminal window
donnie@fedora:~$ wc -l supersecret2_ltrace.txt
4 supersecret2_ltrace.txt
donnie@fedora:~$

Wow! There are only four lines in the output file this time. That’s a far cry from the 7270 lines that we had without the -U option.

On FreeBSD, the shc commands to create the obfuscated binary files are identical to what you’ve seen on Linux. But, to perform system call tracing on FreeBSD binaries, use truss instead of strace, like so:

Terminal window
donnie@freebsd14:~ $ truss ./supersecret 2> supersecret1.truss.txt

As far as I’ve been able to tell, there’s no FreeBSD equivalent to the Linux ltrace command.

On macOS, you have the dtrace and dtruss commands. But to use them, you’ll have to boot your machine into Recovery mode and disable the System Integrity Protection (SIP). I don’t recommend doing that unless you absolutely, positively have to.

You’ll also see that dtrace is available on OpenIndiana. But, since shc isn’t available on OpenIndiana, I won’t bother to cover it.

Next, let’s see if we can decrypt this script.

Many years ago, when I showed shc to the students in my first shell scripting class, it was somewhat easy to crack the shc algorithm in order to convert a binary file back to the original shell script. So at that time, shc wasn’t a very secure option. Now, due to shc developers taking advantage of improvements in modern operating system kernels, it’s a lot harder to crack shc. But, there are a couple of utilities that are supposed to crack shc, or at least that’s what their developers say.

This might be a bit of a spoiler, but I’ll share it anyway.

As you’ll soon see, the binaries that shc creates are currently safe from this type of cracking attack. But, there’s always the possibility that someone could come up with better cracking tools in the future. So, think of this section as a framework for performing your own tests on any cracking tools that might come out in the future.

Let’s take a quick look at these utilities, shall we?

This lab will familiarize you with the UnSHc utility. I’m using a Fedora Server virtual machine, but you can use either another Linux virtual machine or a FreeBSD virtual machine if you desire.

  1. Download UnSHc from Github by doing:
Terminal window
donnie@fedora-server:~$ git clone https://github.com/yannacam/UnSHc.git
  1. Once it’s downloaded, which only takes a few seconds, cd into the UnSHc/latest/ directory, and copy the unshc.sh script to the /usr/local/bin/ directory:
Terminal window
donnie@fedora-server:~$ cd UnSHc/latest/
  1. In the UnSHc/sample/ directory, you’ll see an example shell script, along with the corresponding C source code file and compiled binary file.

Note that this C source code file and binary file were both generated by a very old version of shc. (The reason why I mention this will become apparent in just a moment.)

Copy the binary file, which is test.sh.x, to your home directory:

Terminal window
donnie@fedora-server:~/UnSHc/sample$ ls
test.sh test.sh.x test.sh.x.c
donnie@fedora-server:~/UnSHc/sample$ cp test.sh.x ~
donnie@fedora-server:~/UnSHc/sample$ cd
donnie@fedora-server:~$
  1. Attempt to decrypt the test.sh.x file, like so:
Terminal window
donnie@fedora-server:~$ unshc.sh test.sh.x

The output should look something like this:

  1. Verify that the test.sh.x binary file actually did get decrypted, like this:
Terminal window
donnie@fedora-server:~$ ls -l test*
-rw-r--r--. 1 donnie donnie 193 Jul 5 14:04 test.sh
-rw-r--r--. 1 donnie donnie 11440 Jul 5 14:01 test.sh.x
donnie@fedora-server:~$ cat test.sh
#!/bin/bash
# This script is very critical !
echo "I'm a super critical and private script !"
PASSWORDROOT="SuPeRrOoTpAsSwOrD";
myService --user=root --password=$PASSWORDROOT > /dev/null 2>&1
donnie@fedora-server:~$

Okay, so that works just fine.

  1. Next, try to decrypt a binary file that you created with the current version of shc. (You can use the binary file that you created in the Using shc lab, or you can create a new one.) Do it like this:
Terminal window
donnie@fedora-server:~$ unshc.sh supersecret
...
...
UnSHc is used to decrypt script encrypted with SHc
Original idea from Luiz Octavio Duarte (LOD)
Updated and modernized by Yann CAM
- SHc : [http://www.datsi.fi.upm.es/~frosal/]
- UnSHc : [https://www.asafety.fr/unshc-the-shc-decrypter/]
-------------------------
[*] Input file name to decrypt [supersecret]
[-] Unable to define arc4() call address...
donnie@fedora-server:~$

As you see this time, the operation failed. So, the binary didn’t get decrypted, and the original shell script was not reconstructed.

  1. To see why the operation failed this time, go into the UnSHc/ directory and open the README.md file. Close to the top of the file, you’ll see this paragraph:

Due to the many problems since shc 4.0.3, there seems to be a need for clarification. In shc 4.0.3 many structural changes have been incorporated, so that shc now makes use of various security mechanisms provided by the linux-kernel itself. Therefore, it is now almost impossible to extract the original shell script at all with current UnSHc version, if the new shc version was used. This requires a more in-depth approach, which means that a modified bash or a modified linux-kernel is needed to bypass the security measures.

So you see that the current version of shc is much more secure than the older versions.

I also tested UnSHc on a FreeBSD machine, and got the same results. That tells me that the security enhancements that prevent you from successfully using UnSHc are also in the current FreeBSD kernel.

Also, I tested another shc cracker that’s called deshc-deb. If you’d like to play with it, you can download it by doing:

git clone https://github.com/Rem01Gaming/deshc-deb.git

(Spoiler alert: deshc-deb doesn’t work either.)

Okay, I know, that was a lot of work to show you that something doesn’t work. But, I figured that you might want to see for yourself, instead of just accepting my say-so for it. Seriously though, the bottom line is that shc is a good, secure way to obfuscate your scripts, in case you really need to do that. And, due to improvements in the Linux and FreeBSD kernels, the current decrypting utilities no longer work. However, you still need to take precautions to avoid sensitive data leakage, as you’ll see a bit later in the Avoiding Sensitive Data Leakage section. Next though, let’s look at a pair of permissions setting that could get you into trouble.

Understanding SUID and SGID Considerations

SUID and SGID, which stand for Set User Identity and Set Group Identity, are permissions settings that you can place on executable files. These two permissions settings are not only handy, but are also mandatory on certain executable files that deal with certain functions of Linux, Unix, and Unix-like operating systems. However, if you set SUID or SGID on programs that you create yourself, you could be opening your system up to all kinds of security problems. Before I explain why that is, I need to explain what SUID and SGID actually do and why they’re needed.

First, let’s go into the /bin/ directory, and look at the permissions settings for the rm executable, like so:

Terminal window
donnie@fedora:/bin$ ls -l rm
-rwxr-xr-x. 1 root root 61976 Jan 17 19:00 rm
donnie@fedora:/bin$

Here’s the breakdown of what you’re seeing:

  • The root root portion of this output indicates that this file belongs to the root user, and is associated with the root user’s group.
  • The permissions settings are at the beginning of the line, and are divided into three sections. The rwx settings are for the user of the file, which in this case is the root user. (In the Linux/Unix world, we refer to the owner of the file as the file’s user.) The rwx here means that the root user has read, write, and executable permissions for this file.
  • The next group of settings, which is r-x, is for the group, which in this case is for the root group. This means that members of this group, which in this case consists of only the root user, have read and executable permissions for this file.

  • Finally, we have the third group of settings, which again is r-x. This group of settings is for others. This means that anyone who isn’t the root user or a member of the root group can invoke the rm command, even though the rm executable belongs to the root user. The catch with this is that a normal, unprivileged user can only use rm to remove files and directories with permissions settings that allow the normal user to do so. (Normally, this would mean that the normal user can only remove his or her own files and directories.) Removing any other files and directories with rm requires root privileges.

Now, there are times when a normal user who doesn’t have either root or sudo privileges needs to do something that requires root privileges. The most common task of this sort is when a normal user needs to change his or her own password. Changing a password requires modifying the /etc/shadow file on Linux and OpenIndiana systems, and the /etc/master.passwd file on FreeBSD and most other BSD-type systems. But, look at the permissions settings of the shadow file on my Fedora workstation:

Terminal window
donnie@fedora:/etc$ ls -l shadow
----------. 1 root root 1072 May 10 17:33 shadow
donnie@fedora:/etc$

Okay, this looks very strange, because there are no permissions of any kind on this file for anybody. In reality, the root user does have read and write permissions on this file. It’s just that Red Hat-type operating systems, such as Fedora, use another mechanism to grant those permissions to the root user. (I don’t want to go into what that mechanism is, because it’s beyond the scope of our present topic.)

More typical is what you see on this Debian machine:

Terminal window
donnie@debian12:/etc$ ls -l shadow
-rw-r----- 1 root shadow 1178 Mar 23 13:13 shadow
donnie@debian12:/etc$

Here, we see that only the root user has read and write permissions, and that the shadow group only has read permissions. Either way though, a normal user who needs to change his or her own password needs to modify this file, without invoking root or sudo privileges. How can that be accomplished? Well, with SUID, of course.

Adding the SUID permission to an executable file allows any normal user to execute that file with the same privileges as that file’s user. For example, look at the permissions settings on the passwd executable:

Terminal window
donnie@fedora:/bin$ ls -l passwd
-rwsr-xr-x 1 root root 32416 Jul 19 2023 passwd
donnie@fedora:/bin$

This time, we see rws as the permissions setting for the user, which again in this case is the root user. The lower-case s here means that the executable permission is set for the root user, and that the SUID permission is also set. In the third permissions group, you see r-x, which means that normal, unprivileged users can run this program. The SUID permission is what allows the normal user to change his or her own password, which requires modifying either the /etc/shadow file or the /etc/master.passwd file. So now, let’s see if this works for Frank, who has no root or sudo privileges:

Terminal window
frank@debian12:~$ passwd
Changing password for frank.
Current password:
New password:
Retype new password:
passwd: password updated successfully
frank@debian12:~$

Indeed it does, thanks to the SUID setting on the passwd executable.

I should point out that the SUID setting on the passwd executable only works if a user is setting his or her own password. To set anyone else’s password, a user would still need to have the proper sudo privileges. (And no, I don’t know how the operating system developers can make SUID work selectively like that.)

The SGID setting on executable files works the same way, except for groups. For example, look at the settings for the write executable:

Terminal window
donnie@fedora:/bin$ ls -l write
-rwxr-sr-x. 1 root tty 24288 Apr 7 20:00 write
donnie@fedora:/bin$

In this case, you see the s setting in the group permissions, which means that any user who executes this program has the same privileges as the associated group. In this case, we’re talking about the tty group, which is a system group that allows its members to send output to the terminal. The SGID permission here allows a normal user to use write to send messages to another user who’s logged into another terminal, like so:

Terminal window
donnie@fedora-server:~$ write frank
The server is shutting down in five minutes.
donnie@fedora-server:~$

After entering the write frank command, I hit Enter. I then entered the message and hit Ctrl-d to actually send the message. Thanks to the SGID setting on write, I was able to make that message show up on Frank’s terminal, as you see here:

Terminal window
frank@fedora-server:~$
Message from donnie@fedora-server on pts/1 at 17:01 ...
<!-- page: 539 (printed 542) -->
The server is shutting down in five minutes.
EOF

So, what does all this have to do with shell scripting? Well, it’s just that even though SUID and SGID are mandatory on certain executable files that are part of the operating system, they can be a security hazard if you set them on executable files that you create. By doing so, you can inadvertently allow normal users to do things that they shouldn’t be allowed to do, and you can also allow intruders to invoke malicious code that could affect the entire system.

So, the general rule is to never, but never, set either SUID or SGID on programs of your own creation, unless you really know what you’re doing and can avoid the security problems.

By the way, here’s how to set the SUID permission on files:

Terminal window
donnie@fedora-server:~$ ls -l newscript.sh
-rwxr--r--. 1 donnie donnie 0 Jun 25 17:13 newscript.sh
donnie@fedora-server:~$ chmod u+s newscript.sh
donnie@fedora-server:~$ ls -l newscript.sh
-rwsr--r--. 1 donnie donnie 0 Jun 25 17:13 newscript.sh
donnie@fedora-server:~$

Setting SGID looks like this:

Terminal window
donnie@fedora-server:~$ ls -l newscript2.sh
-rw-r--r--. 1 donnie donnie 0 Jun 25 17:15 newscript2.sh
donnie@fedora-server:~$ chmod g+s newscript2.sh
donnie@fedora-server:~$ ls -l newscript2.sh
-rw-r-Sr--. 1 donnie donnie 0 Jun 25 17:15 newscript2.sh
donnie@fedora-server:~$

But, for shell scripters, there’s a bit of good news. That is, if you set either SUID or SGID on a shell script, it will have no effect at all. This is because the kernels of Linux, Unix, and Unix-like operating systems contain code that cause the operating system to ignore SUID and SGID settings on any executable script file that contains a shebang line. So, contrary to what you might see in other shell scripting tutorials, setting either of these dangerous permissions on your scripts isn’t a problem, because they will have absolutely zero effect. However, they might have an effect on any binary files that you create with shc, so watch out for them.

Unfortunately, I haven’t been able to extensively test the effects of SUID and SGID permissions on the binary files that you create with shc. So, just be aware that you don’t want to see either of these permissions settings on those binary files.

I should also mention that it is possible to mount filesystem partitions with the nosuid option, which would cause the SUID and SGID permissions to be completely ignored on any files that are within that partition. In fact, many modern Linux and Unix distros already mount certain important partitions, such as the /tmp/ filesystem, with the nosuid option by default. Unfortunately, showing you how to set this option on your own partitions is beyond the scope of this book. In fact, I really can’t, because the procedure differs for the various Linux and Unix distros, as well as for the various filesystem formats that they use. If you need to know how to set the nosuid option, your best bet is to consult the documentation for your particular distro.

Okay, let’s talk about data leakage.

Avoiding Sensitive Data Leakage

As a systems administrator, there’s a very good chance that you’ll eventually have to deal with some sort of sensitive data, such as passwords, financial information, or customer information. You’ll always want to ensure that your scripts don’t inadvertently cause any sensitive data to leak out to any unauthorized people. Let’s look at some ways that that could happen, and how to prevent it.

Securing Temporary Files

At some point, you might need to create scripts that store some sort of ephemeral data in a temporary file. Reasons that you might need to do this include:

  • Processing a large amount of data without using excessive system memory.
  • Storing the intermediate results of some sort of complex operation.
  • Storing temporary data for logging debugging information.
  • Allowing different processes or scripts to communicate with each other.

Since the /tmp/ directory is the most common place to store temporary files, let’s begin this topic with and explanation of it.

Understanding the /tmp/ Directory

As I just said, the most common place to store temporary files is in the /tmp/ directory. The good part about this directory is that it is world-readable and world-writable, as you see here:

Terminal window
donnie@fedora-server:~$ ls -ld /tmp
drwxrwxrwt. 17 root root 340 Jun 27 14:27 /tmp
donnie@fedora-server:~$

This is good, because it provides a common, well-known place that different scripts and processes can access. Indeed, this directory isn’t just used by your shell scripts. It’s also used by various operating system processes. Here on the Fedora virtual machine, you see that it’s storing temporary data from various systemd processes:

Terminal window
donnie@fedora-server:/tmp$ ls -l
total 0
drwx------. 3 root root 60 Jun 27 13:44 systemd-private-
d241f12536d0464393f30d8552359053-chronyd.service-RKt6x4
drwx------. 3 root root 60 Jun 27 13:44 systemd-private-
d241f12536d0464393f30d8552359053-dbus-broker.service-plcC5i
drwx------. 3 root root 60 Jun 27 13:45 systemd-private-
d241f12536d0464393f30d8552359053-httpd.service-SB6D6A
...
...
drwx------. 3 root root 60 Jun 27 13:44 systemd-private-
d241f12536d0464393f30d8552359053-systemd-resolved.service-uB13YB
donnie@fedora-server:/tmp$

As you see here, every file or directory that gets created in /tmp/ is set with restrictive permissions, so that only the user of those files or directories can access them. Also, take another look at the permissions settings for the /tmp/ directory itself:

drwxrwxrwt. 17 root root 340 Jun 27 14:27 /tmp

At the end of the permissions string, you see a t in place of an x. This means that the executable permission is set for others, so in that sense it does the same job as the x. So, anybody can enter that directory. But, the t, which is known as the sticky bit, also makes it so that different users can’t delete each other’s files or directories, unless they have root privileges. (This would be true even if the files and directories were set with world-writable permissions.)

I know that this is a rather cursory explanation of the sticky bit, but a fuller explanation is beyond the scope of this book. You’ll find a lot more about it in my Mastering Linux Security and Hardening Book.

Okay, now that you understand the /tmp/ directory, let’s look at how you can create scripts that will create temporary files. Understand though, that there are two ways to do it. First there’s the wrong way, and then there’s the right way.

The Wrong Way to Create Temporary Files

So now, let’s say that you really need to write a script that creates temporary files. How would you do it, and how would it be a security concern? Well, before I show you the right way to create temporary files, let me show you the wrong way, in this tmp_file1.sh script:

#!/bin/bash
echo "This temporary file contains sensitive data." > /tmp/donnie_temp
cat /tmp/donnie_temp

Run this script, and you’ll see this in the /tmp/ directory:

Terminal window
donnie@fedora-server:~$ ls -l /tmp/donnie_temp
-rw-r--r--. 1 donnie donnie 45 Jun 27 16:02 /tmp/donnie_temp
donnie@fedora-server:~$

You see that the read permission is set for the user, group, and others. The write permission is only set for the user. So, I’m the only person who can write to this file, but every unprivileged user who’s logged into the server can read it. That’s not what you want if you’re really dealing with sensitive data. The other problem is that this script is using a predictable naming convention for the temporary file, which makes it easy for attackers to perform sym link attacks.

Trying to explain how sym link attacks work is way beyond the scope of this book. For now, let’s just say that they can cause bad things to happen, such as:

  • Leakage of sensitive data.
  • Injection of false data.
  • Denial of service attacks.

If you want to learn more about sym link attacks, check out the references in the Further Reading section.

Also, bear in mind that security threats don’t always come from unauthorized intruders. Authorized system users who can easily access the /tmp/ directory can also be threats.

Now that you’ve seen the wrong way to do business, let’s look at the right way.

The Right Way to Create Temporary Files

The absolute best way to create temporary files is to use the mktemp utility. Here’s how it works from the command-line:

Terminal window
donnie@fedora-server:~$ mktemp
/tmp/tmp.StfRN1YkBB
donnie@fedora-server:~$ ls -l /tmp/tmp.StfRN1YkBB
-rw-------. 1 donnie donnie 0 Jun 27 16:50 /tmp/tmp.StfRN1YkBB
donnie@fedora-server:~$

This is cool, because it solves both of the problems that we had in the previous demo. First, it automatically sets restrictive permissions on the files that it creates, so that nobody but the person who created them can access them. Secondly, it creates files with random filenames, which makes the files safe from sym link attacks. Let’s see how this works in the tmp_file2.sh script:

#!/bin/bash
temp_file=$(mktemp)
echo "This file contains sensitive data." > "$temp_file"

In the temp_file=$(mktemp) line, I’m using command substitution to assign the output of the mktemp command to the temp_file variable. Since mktemp creates files with random filenames, the name of the temporary file will be different every time you run the script. The echo line is just sending some output into the temporary file. Anyway, let’s see what we have when we run this script:

Terminal window
donnie@fedora-server:~$ ./tmp_file2.sh
donnie@fedora-server:~$ cat /tmp/tmp.sggCBdUd1M
This file contains sensitive data.
donnie@fedora-server:~$

As we’ve already noted above, the file is set with restrictive permissions, so that only I can access it:

Terminal window
donnie@fedora-server:~$ ls -l /tmp/tmp.sggCBdUd1M
-rw-------. 1 donnie donnie 35 Jun 27 17:00 /tmp/tmp.sggCBdUd1M
donnie@fedora-server:~$

Okay, this is all cool, and it looks good. But, we still don’t have a way to automatically delete our temporary files when we no longer need them. I mean, even though they’re all set with restrictive permissions, you still don’t want them hanging around when they’re no longer needed. Now, you might be tempted to just put an rm command at the end of the script, as you see here in the tmp_file3.sh script:

#!/bin/bash
temp_file=$(mktemp)
echo "This file contains sensitive data." > "$temp_file"
rm "$temp_file"

Sure, that works in a simple script like this one. But, if you’re creating a complex script that could possibly exit without running to completion, the rm command might not get invoked. So, your best bet is to use a trap that will delete the temporary file, even if the script exits prematurely. Here’s how that works, in the tmp_file4.sh script:

#!/bin/bash
trap 'rm -f "$temp_file"' EXIT
temp_file=$(mktemp)
echo "This file contains sensitive data." > "$temp_file"
ls -l /tmp/tmp*
cat "$temp_file"

When you execute a script, it always opens in a new child shell. When the script has either run to completion or has exited prematurely, the child shell closes. The second line in this script, the trap line, specifies a command to run when that child shell closes. In this case, we want the rm -f command to run. (The -f switch forces the rm command to delete files without prompting the user.) After the echo line, which sends some text into the temporary file, I’ve added two lines that will prove that the temporary file really does get created. Anyway, here’s what happens when I run the script:

Terminal window
donnie@fedora-server:~$ ./tmp_file4.sh
-rw-------. 1 donnie donnie 35 Jun 28 15:07 /tmp/tmp.6t5Qqx5WqZ
This file contains sensitive data.
donnie@fedora-server:~$ ls -l /tmp/tmp*
ls: cannot access '/tmp/tmp*': No such file or directory
donnie@fedora-server:~$

So, when I ran the script, it showed me the file and then showed me its contents, as I had hoped it would. But when I ran the ls -l /tmp/tmp* command afterwards, it showed that no tmp file exists. This proves that the trap command in the script really did delete the temporary file.

As good as all this looks, there’s still one more modification that I’d like to show you in the tmp_file5.sh script, as you see here:

#!/bin/bash
trap 'rm -f "$temp_file"' EXIT
temp_file=$(mktemp) || exit 1
echo "This file contains sensitive data." > "$temp_file"
ls -l /tmp/tmp*
cat "$temp_file"

The only thing I did here was to add || exit 1 to the temp_file line. This makes it so that if for some weird reason the mktemp command can’t create the temporary file, the script will exit gracefully with exit code 1. In reality, it might not be needed here, because it’s almost a sure bet that mktemp will be able to create the temporary file. But, it’s considered good programming practice to provide a graceful exit mechanism, and it certainly doesn’t hurt anything to have it.

Note that you can also create temporary directories in the /tmp/ directory by using mktemp with the -d option switch.

Okay, you now know how to work with temporary files in a secure manner. Now, let’s move on to something else that can be a bit of a bugaboo. That is, how to securely use passwords in scripts.

Using Passwords in Shell Scripts

The information in this section can help you out with two different scenarios:

  • You need to create a script that a certain administrator or group of administrators will use, and place it where they can all access it. These administrators all have limited privileges, and what they need to do requires the password to some remote server. But, you don’t want them to know that password, because you don’t want to allow them to actually log into that server. All you want them to do is just one specific job, such as copying files to that server.

  • You need to create a script for your own use that requires a password to a remote server. You want to set the script up to automatically run at a regularly scheduled time, as either a cron job or as a systemd timer job. If the script has to prompt you for a password, the job will be interrupted and won’t run to completion.

In both cases, you’ll need to embed a password into your script. But, there’s a danger that your password might leak out to unauthorized parties. Naturally, you’ll want to prevent that. So, let’s see how.

Hands-on Lab — Encrypting Passwords

For this scenario, I’ll show you a solution that I borrowed from the How-to Geek website. It’s a good solution as far as it goes, but it’s only a partial solution, as you’ll soon see. First though, let’s encrypt a password and create a script that uses it. Then, I’ll show you the complete solution.

I’ve placed the link to the original How-to Geek article in the Further Reading section. Also, to avoid re-inventing the proverbial wheel, I won’t repeat the extensive explanations that the author of this article has already provided.

  1. To begin, you’ll need a system with both the openssl and the sshpass packages installed. The openssl package is normally installed on pretty much every Linux, Unix, or Unix-like operating system, so you won’t have to worry about that. You’ll only need to worry about installing sshpass.

You’ll use openssl to encrypt your password, and sshpass to automatically pass your password to the ssh client.

The sshpass package is in the normal repositories for Fedora, Debian/Ubuntu, and FreeBSD. The package name is the same in all cases, so just use your normal package manager to install it.

Just for fun, I’ll be using the FreeBSD virtual machine for this lab, but you can use either of your Linux virtual machines if you desire. (The procedure will be the same, regardless.)

  1. Create the .secret_vault.txt file that contains the encrypted password for the remote server that you want to access, like this:
Terminal window
donnie@freebsd14:~ $ echo 'Chicken&&Lips' | openssl enc -aes-256-cbc -md sha512
-a -pbkdf2 -iter 100000 -salt -pass pass:'Turkey&&Lips' > .secret_vault.txt
donnie@freebsd14:~ $

In this command, Chicken&&Lips is the password for the remote server that you want to access, and Turkey&&Lips is the password that you’ll need to decrypt the password. (Why you need this decryption password will become clear in a few moments.) Chicken&&Lips is the password that you’ll encrypt.

  1. Use the chmod 600 .secret_vault.txt command to set the read and write permissions for yourself, and to remove all permissions from everyone else:
Terminal window
donnie@freebsd14:~ $ ls -l .secret_vault.txt
-rw-r--r-- 1 donnie donnie 45 Jul 31 16:31 .secret_vault.txt
donnie@freebsd14:~ $ chmod 600 .secret_vault.txt
donnie@freebsd14:~ $ ls -l .secret_vault.txt
-rw------- 1 donnie donnie 45 Jul 31 16:31 .secret_vault.txt
donnie@freebsd14:~ $
  1. Look in the .secret_vault.txt file, and you’ll see the sha512 hash value of your remote server password:
Terminal window
donnie@freebsd14:~ $ cat .secret_vault.txt
U2FsGdGVkX18eeWfcaGbr0/4Fd70vTC3vIjVgymXwfCM=
donnie@freebsd14:~ $

Create the go-remote.sh script, using your own information for the Remote_User, Remote_Password, and Remote_Server:

#!/bin/bash
# name of the remote account
Remote_User=horatio
# password for the remote account
Remote_Password=$(openssl enc -aes-256-cbc -md sha512 -a -d -pbkdf2 -iter 100000 -salt -pass pass:'Turkey&Lips' < .secret_vault.txt)
# remote computer
Remote_Server=192.168.0.20
# connect to the remote computer and put a timestamp in a file called script.log
sshpass -p $Remote_Password ssh -T $Remote_User@$Remote_Server << _
remote_commands
echo $USER "-" $(date) >> /home/$Remote_User/script.log
_remote_commands

As you’ll see explained in the original How-to Geek article, the password for decrypting the Remote_Password has to be in the script, in plain-text. The author of the article doesn’t consider this a problem, because the .secret_vault.txt file is a so-called hidden file that’s in your own home directory, and because the permissions on it are set so that only the file owner can access it. However, that explanation only makes sense if the plain-text script is in a location that nobody else can access. (I’ll explain more about that in just a bit.)

Instead of hard-coding the information for the remote user, remote server, and remote server password into the body of the script, I’m assigning the values for this information to the Remote_User, Remote_Password, and Remote_Server variables. To obtain the decrypted password that I need to assign to the Remote_Password variable, I’m using the stdin (<) redirctor to read the password hash from the .secret_vault.txt file into the openssl command.

The sshpass -p command passes Horatio’s password, from the Remote_Password variable, to the ssh client. Since we don’t need for Horatio to open a remote terminal, we’ll disable that with the -T option for ssh. The _remote_commands here document contains a single command that will be executed on the remote server. That is, it will create a script.log file that contains a timestamp in Horatio’s home directory.

  1. In order for this script to work, you’ll need to have Horatio’s public SSH key for the remote server in your .ssh/known_hosts file. So, before you try to run the script, have Horatio log into the remote server in the normal manner, and then have him log back out, like so:
Terminal window
donnie@freebsd14:~ ssh [email protected]
...
...
horatio@ubuntu2404:~$ exit
donnie@freebsd14:~
  1. Execute the go-remote.sh script. You should see the login information from the remote server come up momentarily, and then be returned to the command prompt of your local machine.

  2. Log into Horatio’s account on the remote server. You should see the script.log file that contains a timestamp. It should look something like this:

Terminal window
horatio@ubuntu2404:~$ ls -l script.log
-rw-rw-r-- 1 horatio horatio 38 Jul 31 21:34 script.log
horatio@ubuntu2404:~$ cat script.log
donnie - Wed Jul 31 17:34:44 EDT 2024
horatio@ubuntu2404:~$

If you see the script.log file in Horatio’s home directory, you have achieved coolness. But, is this really a complete solution? Well, no. Let’s look at some potential problems that the author of this How-to Geek article doesn’t address.

Understanding the Problems with this Solution

Again I ask, is the solution that I’ve just presented a good one? Well, that depends upon a few factors. I mean, if you have the script and the hashed password in your own home directory where only you can access it, it might be okay. But, here’s an important consideration. Some operating systems, such as FreeBSD and older implementations of Linux, have users’ home directories open to other users by default. Anyone who can get into your home directory can read the encryption password in your plain-text script, and see the name of the file that contains the encrypted password. In fact, let’s take a look at that.

Home Directory Permissions on FreeBSD

Here’s how it looks on FreeBSD:

Terminal window
donnie@freebsd14:~ $ cd ..
donnie@freebsd14:/home $ ls -l
total 17
drwxr-xr-x 9 donnie donnie 68 Jul 31 17:52 donnie
drwxr-xr-x 2 horatio horatio 10 Jul 30 17:38 horatio
donnie@freebsd14:/home $ cd horatio/
donnie@freebsd14:/home/horatio $ ls
donnie@freebsd14:/home/horatio $ ls -a
.
.cshrc
.login_conf
.mailrc
.sh_history
..
.login
.mail_aliases
.profile
.sshrc
donnie@freebsd14:/home/horatio $

As you see, by default, FreeBSD sets the read and execute permissions for others on users’ home directories. In this case, it means that Horatio can see what’s in my directory, and I can see what’s in his directory, without having to use any kind of administrative privileges. Fortunately, I can easily fix that for myself, like this:

Terminal window
donnie@freebsd14:/home $ chmod 700 donnie/
donnie@freebsd14:/home $ ls -ld donnie/
drwx------ 9 donnie donnie 68 Jul 31 18:01 donnie/
donnie@freebsd14:/home $

The chmod 700 command preserves the read, write, and execute permissions that I have for myself, and removes all permissions for everyone else. To ensure that any future users have this restrictive permissions settings on their FreeBSD home directories, create the /etc/adduser.conf file with that setting, like this:

Terminal window
donnie@freebsd14:~ $ sudo adduser -C

This is an interactive utility that prompts you for a lot of information. Accept the defaults for everything except for the Home directory permissions (Leave empty for default): line. For it, enter 700 as the value. Test your setup by creating another user account. You should see that the new user will have restrictive permissions set on his or her home directory, as you see here for the account that I just created for Vicky:

Terminal window
donnie@freebsd14:/home $ ls -ld vicky/
drwx------ 2 vicky vicky 9 Jul 31 18:04 vicky/
donnie@freebsd14:/home $

Also, keep in mind that when you install FreeBSD, the user account that the installer creates for you will have the more open permissions settings for your home directory. So, after you complete the installation, be sure to set your home directory permissions to the more restrictive value. Then, run the sudo adduser -C command to create the /etc/adduser.conf file, as I’ve just shown you.

Home Directories on Linux

Home directory permissions on Linux aren’t as much of a problem, because many modern Linux distros create home directories with restrictive permissions by default. For example, here’s how they look on any Red Hat-type system, such as Fedora, and on Debian 12:

Terminal window
donnie@fedora:/home$ ls -l
total 0
drwx------. 1 donnie donnie 25094 Jul 31 13:46 donnie
donnie@fedora:/home$

And, here’s how they look on Ubuntu 22.04 and newer:

Terminal window
donnie@ubuntu2404:/home$ ls -l
total 12
drwxr-x--- 13 donnie donnie 4096 Jul 31 22:22 donnie
drwxr-x--- 3 horatio horatio 4096 Jul 31 21:34 horatio
drwxr-x--- 2 vicky vicky 4096 Jul 6 19:05 vicky
donnie@ubuntu2404:/home$

The only difference with Ubuntu is that it has read and execute permissions for the user’s private group, where the Red Hat distros and Debian don’t. That’s okay because either way, nobody can access the home directories except for their respective owners.

Home Directories on Other Linux or Unix Distros

Other Linux or Unix distros might have other ways of doing business with their users’ home directories. If you work with any of them, be sure to check the home directory permissions settings, and make any necessary changes.

Okay, that does it for home directory permissions. But, what if you need to place a script somewhere else where other administrators can access it? Let’s see what we can do about that.

Hands-on Lab: Making an Untraceable Binary

If other administrators need to access a script that requires an embedded password, you might have to place both it and the encrypted password file into some other directory, such as /usr/local/bin/ or /usr/local/sbin/. In those cases, you’ll want to ensure that nobody can read or modify the script, and that nobody can trace it. That’s because anyone who can read your script, the go-remote.sh script in this case, can see the password that you need to decrypt the remote server password, as well as the name of the file that contains the remote server password. And of course, anyone who can edit this file can add extra commands to do possibly nasty things on the remote server.

Fortunately, you can easily solve this problem by using shc to turn your scripts into executable binary files, as I showed you a few pages back in the Controlling Access to Your Scripts section. But, when you do this, you absolutely must use shc with the -U option to make the binary untraceable.

If you don’t make your binary files untraceable, anyone who can access the files can use debugging utilities such as strace, truss, dtrace, or dtruss to obtain the plain-text password, even if it has been encrypted with the strongest algorithm known to mankind.

  1. To demonstrate, go back to the same virtual machine that you used to create the go-remote.sh script. Turn the script into an executable binary, using shc without the -U option:
Terminal window
donnie@freebsd14:~ $ shc -f go-remote.sh -o go-remote
donnie@freebsd14:~ $
  1. Using truss on FreeBSD or strace on Linux, create the trace1.txt trace file of the go-remote executable.

On FreeBSD:

Terminal window
donnie@freebsd14:~ $ truss ./go-remote 2> trace1.txt

On Linux:

Terminal window
donnie@fedora-server:~$ strace ./go-remote 2> trace1.txt
  1. Open the trace1.txt file, and scroll down until you see Horatio’s password for the remote server. In my own file, it was on line 191, and it looks like this:
read(3,"Chicken&&Lips\n",4096) = 14 (0xe)

Yes indeed, Chicken&&Lips really is Horatio’s password for the remote server.

  1. Re-create the go-remote binary, using the -U option to make it untraceable:
Terminal window
donnie@freebsd14:~ $ shc -Uf go-remote.sh -o go-remote
donnie@freebsd14:~ $
  1. Repeat Step 2, except this time save the output to the trace2.txt file:

  2. Open the trace2.txt file and search for Horatio’s password. (Spoiler alert: This time, you won’t find it.)

Now, after all this, I need to let you in on a little secret. That is, that neither Horatio’s username nor the address of the remote server shows up in the truss or strace output. So, anyone who were to trace your binaries will have the password, but no username or server address. But, don’t think that you’re all good because of this. Remember, you need to think like a malicious hacker. Any malicious hacker worth his or her salt would already have used other means to map the servers on your network, and would have found a way to enumerate a list of possible usernames. So, even though this procedure only gives hackers one piece of the puzzle, it’s still an important piece that can be combined with the other pieces.

All right, I think we’re about through with this topic. Let’s now take a quick look at secure coding practices.

Understanding Command Injection with eval

Another major problem with shell script security involves scripts that accept input from untrusted users or untrusted sources. If the script is coded incorrectly, an attacker could use it to inject malicious commands as the script’s input. Before we look at examples of that, let’s look the eval command, which facilitates passing data or commands into a script.

Using eval on the Command-line

The eval command is a shell builtin that’s available on most shells. It’s very handy when used properly, but dangerous when used improperly. Before we get into that, let’s look at how eval works on the command-line.

Okay, eval is one of those commands that can be really complex to fully understand. So, to keep things simple, I’ll be presenting some rather simplistic eval demos in this section. Even though they’ll demonstrate things that you’ll never do in real life, they’ll serve the purpose of demonstrating the concepts.

For anyone who would like to see more in-depth coverage of eval, I’ll drop some links into the Further Reading section.

The best way to think of eval is that you can use it to dynamically process commands that the shell would normally treat as meaningless text strings. For example, let’s say that we want to find the date from three weeks ago. We would use date with the —date= option, like this:

Terminal window
donnie@fedora-server:~$ date
Wed Aug 7 02:48:23 PM EDT 2024
donnie@fedora-server:~$ date --date="3 weeks ago"
Wed Jul 17 02:48:25 PM EDT 2024
donnie@fedora-server:~$

As you see, I just used the “3 weeks ago” text string as the argument for the —date option, which works just fine. Now, let’s assign this date command to the threeweeksago variable and echo back the results, like this:

Terminal window
donnie@fedora-server:~$ threeweeksago='date --date="3 weeks ago"'
donnie@fedora-server:~$ echo $threeweeksago
date --date="3 weeks ago"
donnie@fedora-server:~$

As you see, the echo command just returned the text string that I assigned to the variable. For some real magic, watch what happens when I replace echo with eval:

Terminal window
donnie@fedora-server:~$ eval $threeweeksago
Thu Jul 18 04:02:14 PM EDT 2024
donnie@fedora-server:~$

Now, let’s wait a few minutes and run this again.

Terminal window
donnie@fedora-server:~$ eval $threeweeksago
Thu Jul 18 04:05:10 PM EDT 2024
donnie@fedora-server:~$

Look carefully, and you’ll see that the second eval command has updated the time value. This sort of thing can be handy in scripting, because it allows you to assign a command to a variable, and then use that variable throughout the rest of the script. This allows you to execute the command in various parts of the script, without having to type out the entire command multiple times. But, as I’ve already mentioned, there’s both a safe way and a dangerous way to use eval. Let’s first look at the safe way.

Using eval Safely

Let’s take a look at the eval-test1.sh script to see a simple example of safe eval usage:

#!/bin/bash
threeweeksago='date --date="3 weeks ago"'
eval $threeweeksago

Running the script looks like this:

Terminal window
donnie@fedora-server:~$ ./eval-test1.sh
Thu Jul 18 04:19:25 PM EDT 2024
donnie@fedora-server:~$

The reason this is safe is because we’re feeding eval a text string that comes from within the script itself. As long as you have the permissions on this script locked down so that nobody can modify it, it’s perfectly sane and safe. Using eval in this way makes it impossible for attackers to insert their own malicious commands.

Of course, this script is rather pointless, because it doesn’t do anything at all useful. So, let’s look at a more practical example in the eval-test2.sh script:

#!/bin/bash
desiredDate='date --date="1 month ago"'
datestamp=$(eval "$desiredDate")
echo "I'm creating a report with the $datestamp timestamp in the filename." > somefile_"$datestamp".txt

Here, I’m using eval within a command substitution construct to create the value for the datestamp variable. I’ll then use this datestamp variable to insert a datestamp into both the text file, and the name of the text file. Here’s how it looks when I run the script:

Terminal window
donnie@fedora-server:~$ ./eval-test2.sh
donnie@fedora-server:~$ ls -l somefile_Thu*
-rw-r--r--. 1 donnie donnie 90 Aug 8 16:42 'somefile_Thu Jul 18 04:42:31 PM
EDT 2024.txt'
Terminal window
donnie@fedora-server:~$ cat "somefile_Thu Jul 18 04:42:31 PM EDT 2024.txt"
I'm creating a report with the Thu Jul 18 04:42:31 PM EDT 2024 timestamp in the
filename.
donnie@fedora-server:~$

As you see, it works just fine. And, if I were to run this command a few moments later, I would see a new file with a new timestamp. And of course, if I were to decide to change the —date value to something else, such as “1 month ago”, I would only have to make the change in the desiredDate= line, instead of having to change it multiple times throughout the script.

So, now that I’ve shown you the safe way to use eval, let’s look at the dangerous way.

Using eval Dangerously

The eval-test3.sh script shows a very simple example of what you never want to do:

#!/bin/bash
eval $1

Yeah, I know what you’re thinking. Nobody would ever create such a simplistic script that uses eval in this manner. That’s okay though, because it serves the purpose of demonstrating the concept. Let’s try it out to see what happens.

Terminal window
donnie@fedora-server:~$ ./eval-test3.sh 'date --date="1 day ago"'
Wed Aug 7 05:14:48 PM EDT 2024
donnie@fedora-server:~$

As you see, the date command gets passed into eval as positional parameter $1. This seems safe enough, but is it really? The key here is that anyone who runs this script can insert any command that he or she wants to run. For example, let’s say that this script is in the /usr/local/bin/ directory. The permissions are set so that nobody can modify it, but also so that every non-privileged user can execute it. So, what could a malicious hacker accomplish with this? Let’s see.

Terminal window
donnie@fedora-server:~$ eval-test3.sh 'cat /etc/passwd'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
...
charlie:x:1009:1009::/home/charlie:/usr/bin/zsh
horatio:x:1010:1010::/home/horatio:/bin/bash
donnie@fedora-server:~$

This particular example might not seem like a big deal, because the passwd file is world-readable anyway. But then, it could be a big deal under certain circumstances. For example, let’s say that you’re running a web server, and you have a shell script set up as a CGI script.

CGI stands for Common Gateway Interface. You can create CGI scripts from various programming languages, including shell scripting. These CGI scripts can perform various functions on web servers, such as counting users, or even serving out content.

A full explanation of setting up web servers and how to hack them is way beyond the scope of this book. For now, let’s just say that there are some clever hackers out there who can find and exploit poorly designed CGI scripts on web servers. That’s especially true if the web server security isn’t set up properly.

A malicious hacker who hasn’t been able to log into the web server wouldn’t be able to view the passwd file. But, if he or she were to find that this poorly-designed script is performing some sort of CGI function, he or she might be able to view the passwd file to enumerate the server’s users. (But then, maybe not. Proper web server security measures might prevent this from happening, even if the script is insecure.)

For another example that might be more probable, let’s say that you’ve placed the eval-test3.sh script in the /usr/local/sbin/ directory, and have it set up so that only the root user has executable permissions, like this:

Terminal window
donnie@fedora-server:/usr/local/sbin$ ls -l eval-test3.sh
-rwx------. 1 root root 21 Aug 8 17:20 eval-test3.sh
donnie@fedora-server:/usr/local/sbin$

You then invoke the sudo visudo command to set up Charlie so that he has root privileges to run the eval-test3.sh script, but nothing else. Here’s the line that does that:

charlie ALL=(ALL) /usr/local/sbin/eval-test3.sh

Let’s see how that works.

Terminal window
charlie@fedora-server:~$ eval-test3.sh 'cat /etc/passwd'
bash: /usr/local/sbin/eval-test3.sh: Permission denied
charlie@fedora-server:~$ sudo eval-test3.sh 'cat /etc/passwd'
[sudo] password for charlie:
root:x:0:0:root:/root:/bin/bash
...
...
charlie:x:1009:1009::/home/charlie:/usr/bin/zsh
horatio:x:1010:1010::/home/horatio:/bin/bash
charlie@fedora-server:~$

Charlie didn’t do any harm this time, but what about next time? Let’s see:

Terminal window
charlie@fedora-server:~$ sudo eval-test3.sh 'systemctl stop httpd'
charlie@fedora-server:~$

This time, Charlie did some real damage. He shut down the web server, causing a Denial-of-Service attack. Ordinarily, Charlie wouldn’t have the power to do this. But, because he has sudo privileges to run the eval-test3.sh script, and that script contains the eval $1 line, Charlie can now run any command he wants to run, including system administrative commands.

As I said at the beginning of this section, I’ve tried to keep the explanation of eval simple. To see more complex and somewhat more realistic eval scenarios, I would like to draw your attention to two really good articles that I’ve found. The first one is on Medium.com, and is the absolute best write-up I’ve found that covers a real-world way to exploit poorly-written scripts with eval:

The perils of Bash eval:

https://medium.com/dot-debug/the-perils-of-bash-eval-cc5f9e309cae

The second one, at the Earthly.dev site, shows how to perform a reverse shell attack with an eval script. (If you don’t know what a reverse shell attack is, you’ll find the explanation in the article.) Anyway, you can find it here:

Bash eval: Understanding and (Safely) Using the Power of Dynamic Code Evaluation:

https://earthly.dev/blog/safely-using-bash-eval/

Also, there has been at least one instance of a real-life shell script vulnerability that involved an incorrect usage of eval. This involved the installation script for Gradle, which is an automated build tool for software development. You can read about it here:

CVE-2021-32751:

https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-32751

Now that you understand the dangers of eval, let’s consider whether or not we even have to use it.

Using Alternatives to eval

Many times, you’ll be able to use a safe alternative to eval. Let’s look at a few examples.

Using Command Substitution

In the eval-test1.sh script that I showed you at the beginning of this section, you could have replaced eval with a command substitution construct.

Here’s how it looks in the eval-test1-alternative.sh script:

#!/bin/bash
threeweeksago=$(date --date="3 weeks ago")
echo $threeweeksago

Run the script, and you’ll see that it behaves exactly the same as the eval version:

Terminal window
donnie@fedora-server:~$ ./eval-test1-alternative.sh
Mon Jul 22 04:36:14 PM EDT 2024
donnie@fedora-server:~$ ./eval-test1-alternative.sh
Mon Jul 22 04:37:23 PM EDT 2024
donnie@fedora-server:~$

Granted, it doesn’t really matter in this case, because the script doesn’t accept external input. So, it’s perfectly safe to use eval with it. But, if you create a script that does accept external input and you have the choice between using eval and using command substitution, your best bet is to go with command substitution.

Remember: Always go with the safest option for your scripts.

In some cases, using eval doesn’t even do anything that we can’t do without it. For example, look at the eval-test4.sh script:

#!/bin/bash
a=$1
eval echo $(( a + 1 ))

When you run this script, you’ll pass in a number as positional parameter $1. The arithmetic operator in the eval line will increment the number by 1, and display the result. Here’s how that looks:

Terminal window
donnie@fedora-server:~$ ./eval-test4.sh 36
37
donnie@fedora-server:~$

Now, let’s remove the eval command, so that the script looks like this:

#!/bin/bash
a=$1
echo $(( a + 1 ))

Running the script without eval gives us this:

Terminal window
donnie@fedora-server:~$ ./eval-test4.sh 36
37
donnie@fedora-server:~$

The output is identical to what it was when we used eval, which tells me that eval isn’t even needed here. So, the only thing that eval does here is to add an attack vector that we definitely don’t need.

Remember: Don’t use eval when it isn’t necessary.

A similar scenario involves assigning a command to a variable, as we see here in the eval-test5.sh script:

#!/bin/bash
checkWebserver="systemctl status httpd"
eval $checkWebserver

Running this script will indeed show us the status of the web server service:

Terminal window
donnie@fedora-server:~$ ./eval-test5.sh
● httpd.service - The Apache HTTP Server
...

As before, we can accomplish the same thing without using eval at all, as you see here in the eval-test6.sh script:

#!/bin/bash
checkWebserver="systemctl status httpd"
$checkWebserver

Run this script, and the results will be identical to what they were when you used eval. So again, eval isn’t needed.

Another example of this is the value5.sh script that I showed you in Chapter 10—Understanding Functions. To refresh your memory, here’s what it looks like:

#!/bin/bash
valuepass() {
local __internalvar=$1
local myresult='Shell scripting is cool!'
eval $__internalvar="$myresult"
}
<!-- page: 559 (printed 562) -->
valuepass result
echo $result

I’ve already explained this in Chapter 10, so for now I’ll just say that I’m using eval here as a mechanism to help pass values in and out of the valuepass function. But, if you refer back to Chapter 10, you’ll see that this is only one of several methods that I showed you for passing values in and out of a function. Using eval here is perfectly safe, since this script is only passing values internally. But, if you ever need to create a function that accepts external values from either the script’s user or an external text file, you’ll want to use one of the alternative methods.

I think that covers it for eval. Let’s look at one last possible coding problem, and then move on to the next chapter.

Understanding Path Security

The possibility—however remote it may be—exists that someone could plant a trojaned version of a Unix system utility on your system, and then manipulate a user’s PATH setting so that the trojaned utility would be invoked, instead of the real one. The trojaned utility could do a variety of nasty things, such as exfiltrating sensitive data or performing a ransomware attack by encrypting important files. Before I show you a script, let’s see how this looks on the command-line. Let’s begin by showing you where the executable file for the normal ls command is located:

Terminal window
donnie@fedora-server:~$ which ls
alias ls='ls --color=auto'
/usr/bin/ls
donnie@fedora-server:~$

We see that it’s located in the /usr/bin/ directory, as it should be. Now, let’s create a bogus ls, in the form of a script, and place it into the /tmp/ directory. Here’s the ls script:

#!/bin/bash
echo "This is a trojaned ls file. This command does something nasty."
/usr/bin/ls

Of course, the echo command is harmless, but a real malicious hacker would replace that with something that isn’t harmless. After running the malicious command, the script will then invoke the normal ls command.

Next, let’s manipulate the user’s PATH setting so that this script will get invoked instead of the real ls.

Terminal window
donnie@fedora-server:~$ PATH=/tmp:$PATH
donnie@fedora-server:~$

Finally, we’ll invoke ls:

Terminal window
donnie@fedora-server:~$ ls
This is a trojaned ls file. This command does something nasty.
04:26:53 Jul
18
link.txt
...
...
ip_addresses.csv variables.txt
ipaddress_list_2024-06-03.txt
donnie@fedora-server:~$

This time, the bogus ls was invoked, because the /tmp/ directory is the first directory in the user’s PATH setting.

By default, the shell scripts that you create will use your normal PATH settings to find the utilities and programs that the script invokes. For example, when you want to invoke the ls command in a script, just add a line that says ls instead of /usr/bin/ls. By doing that though, any attacker who can manipulate a user’s PATH setting could cause the script to run a bogus, trojaned program. There are three things you can do to prevent that.

  • Method 1: Make the script unreadable and untraceable, as I showed you in the first portion of this chapter. That way, attackers won’t be able to see which utilities that the script is invoking, which means that they won’t know which utilities to replace with a trojaned version.

  • Method 2: Explicitly set a new PATH at the top of the script. Here’s how that would look, in the path-test1.sh script:

#!/bin/bash
PATH=/bin:/sbin:/usr/local/bin:/usr/local/sbin
ls -l
  • Method 3: Use the entire path to every command that you place in the script. So, for example, when you want to invoke awk, have the line say /bin/awk instead of just awk.

One problem with the third method is that it could make your scripts less portable. That’s because some operating systems, such as FreeBSD, might have the executable files for certain utilities stored in directories that are different from what you’re used to. For example, FreeBSD has the executable files for many of its utilities in the /usr/local/bin/ directory, instead of in the /usr/bin/ as we’re used to seeing on Linux operating systems. So, if you want to ensure that your scripts will run on as many operating systems as possible, your best bet is to forget about Method 3. Using Method 2 will be much easier and just as effective.

And now, you’re wondering how an attacker could possibly manipulate someone’s PATH setting like this. Well, I can think of two possible scenarios.

Attack Scenario 1: Compromising the User’s Account

One way that an attacker can manipulate a user’s PATH setting is to first gain access to the user’s account. If the attacker has already accomplished that, then it’s game over anyway and path security would be the least of the user’s problems. So, your best bet would be to ensure that users’ accounts are configured securely, to prevent anyone from breaking in.

Attack Scenario 2: Social Engineering

An attacker could also possibly use social engineering to trick a user into running a program that would alter the user’s shell configuration files.

In the world of cybersecurity, social engineering can take many forms. For example, it can come by way of a scam email with a link to a malicious file, or it could come by way of a face-to-face encounter. In any case, the goal is to convince the victim to perform some sort of action that could cause a breach of security.

For this scenario, I’m using the Fedora Server virtual machine.

  1. First, let’s look at the harmless-program.sh script:

    #!/bin/bash
    echo "This program is harmless."
    echo "Trust me!"
    echo "PATH=/tmp:$PATH" >> /home/"$USER"/.bashrc
    echo "export PATH" >> /home/"$USER"/.bashrc
  2. Run the script, and note that it places an extra PATH directive at the end of the .bashrc file. This extra directive overrides the PATH directive that’s at the top of the file. This new setting won’t take effect right away, but it will the next time that the user either logs into the system or opens another terminal window.

  3. Log out and then log back in again. Prepare to be amazed at what happens when you try to do a directory listing:

    donnie@fedora-server:~$ ls
    You've just been pwned!
    You really should learn to protect yourself
    against social engineering attacks.
    acl_demo.sh link.txt
    . . .
    . . .
    ipaddress_count.sh user_activity_original.sh
    ip_addresses.csv user_agent.tsv
    ipaddress_list_2024-06-03.txt variables.txt
    donnie@fedora-server:~$

Of course, in real life the script would have done something a bit more nasty. Also, distributing this sort of malicious program as a script would be a bit obvious.

  1. So, let’s use shc to convert this to an untraceable executable binary. Also, use the -r option so that the binary will execute on other Linux machines.
Terminal window
donnie@fedora-server:~$ shc -rUf harmless-program.sh -o harmless-program
donnie@fedora-server:~$
  1. Before you try to execute the binary, open the .bashrc file in your text editor, and delete the two lines that were added by the script.

  2. Log out and then log back in.

  3. Execute the new harmless-program binary.

  4. Log out and then log back in. Then, execute the ls command. You should see the same results that you saw in Step 3.

  5. Finally, open the .bashrc file in your text editor and delete the two lines that were added by the harmless-program program.

If you do a web search for articles related to shell scripting security, you’ll find several that talk about path security attacks. Curiously though, I’ve never seen a single one that explains how someone might execute such an attack.

Also, this scenario provides a prime example of why all personnel who use a computer should learn to recognize and avoid social engineering attacks.

Okay, I think that this about covers things for this topic. So, let’s wrap up and move on.

In this chapter, we covered topics that are important to a security-conscious Linux or Unix administrator. We began with a discussion of how to control access to your important scripts, and showed various methods of doing so. Next, we looked at considerations about the SUID and SGID permissions settings, and then we looked at a few different ways to prevent your scripts from leaking sensitive data. We then looked at how using the eval command in scripts can be quite dangerous, and wrapped up with a discussion of path security.

In the next chapter, we’ll talk about debugging buggy scripts. I’ll see you there.

  1. Which of the following is true about the eval command?

    a. It’s always safe to use eval in your scripts.

    b. It’s always dangerous to use eval in your scripts.

    c. It’s only safe to use eval in your scripts if it only takes input from and external source.

    d. It’s only safe to use eval in your scripts if it only takes input from within your script.

  1. Which two of the following statements are true about the /tmp/ directory? (Choose two.) a. It’s completely secure, because only administrative users can enter it. b. Anybody can create files in it. c. Anybody can read files that mktemp creates in the /tmp/ directory. d. Any files that mktemp creates in the /tmp/ directory can only be read by the user who created them.

  2. How would you prevent an attacker from obtaining information from a binary that you’ve created with shc? a. It’s never possible to obtain information from an shc binary. b. Use shc with the -U option. c. Use shc with the -u option. d. There’s nothing you can do to prevent this.

  3. Before you set an Access Control List on a file, what must you do to that file? a. Remove all permissions from group and others. b. There’s nothing that you need to do. c. Ensure that user, group, and others all have read, write, and execute permissions. d. Change ownership of the file to the root user.

  4. What’s the best way to ensure that any temporary files that your script creates in the /tmp/ directory always get deleted? a. After you run the script, go into the /tmp/ directory and delete the temporary files yourself. b. Automatically delete the files with an rm command at the end of the script. c. Automatically delete the files with a trap command at the beginning of the script. d. No action is needed, because your script will always automatically delete its temporary files.

Further Reading

Answers

  1. d
  2. b and d
  3. b
  4. a
  5. c

Get This Book’s PDF Version and Exclusive Extras Scan the QR code (or go to packtpub.com/unlock). Search for this book by name, confirm the edition, and then follow the steps on the page.

Note: Keep your invoice handy. Purchases made directly from Packt don’t require one.