Sub topic 8
Script risks and safe handling
This chapter assumes you can read a short batch, PowerShell, Bash or Python script and that you know basic file paths, permissions and networking terms. Scripts save time, but they run with the permissions of whoever starts them. A mistake or a malicious line can do damage quickly and at scale.
The three main risks
The A+ Core 2 exam groups script risks into three areas.
- Malware. A script is plain text, so an attacker can hide harmful commands in one. Common examples are a
.vbsor.ps1file attached to an email, or a script copied from a forum. Running it executes whatever it contains. - Unintended system or browser changes. A script can change registry values, firewall rules, startup items, the default browser, or the browser's home page and extensions. A typo or a wrong assumption can change settings on many machines at once.
- Mishandling of resources. A script can overwrite or delete files, open files or network connections and never close them, or loop forever and use all the CPU or memory. A bad path in a delete command can remove the wrong folder.
#!/bin/bash
# Dangerous: if $dir is empty, this becomes "rm -rf /*"
dir=""
rm -rf "$dir"/*If the variable is empty, the command points at the root of the file system. A script that builds a path from a variable must check that the variable has a value first.
How a risk becomes an incident
Safe handling practices
- Check the source. Use scripts from your own team or a vendor you trust. Do not run code you cannot explain.
- Read before you run. Look for commands that delete files, download content, change the registry or send data out.
- Test first. Run the script on a virtual machine or a non-production computer.
- Use least privilege. Do not run a script as an administrator or root unless the task needs it.
- Validate variables. Confirm that a path or value is not empty before you use it in a destructive command.
- Keep backups. Make sure a restore point or backup exists before a script changes settings or data.
- Control the script file. Store scripts in a protected location with limited write access, so an attacker cannot edit them.
Windows may block unsigned PowerShell scripts through its execution policy. Treat that setting as a safety control. Do not turn it off only to make one script run.
# Check the current execution policy before running a downloaded script
Get-ExecutionPolicyCommon errors
- Running a script from an email attachment because it "looks like a normal text file".
- Testing on a production server.
- Hard-coding passwords in a script, where anyone who can read the file can see them.
- Skipping the backup step because the change looked small.
Exercise
A technician downloads a cleanup script from a forum and plans to run it as administrator on a user's workstation. Name two safe handling steps to take first.
Show answer
Read the script to see what it deletes or changes, and test it on a non-production machine or virtual machine. Also acceptable: confirm the source is trusted, make a backup, or run it with a standard account if the task allows.
Exercise
Why is rm -rf "$dir"/* risky when $dir has not been set?
Show answer
With an empty variable the path becomes /*, so the command tries to delete everything under the root of the file system. This is a resource mishandling risk. The fix is to check that the variable has a value before the command runs.