Challenge - 5 Problems
Script Security Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
Output of a script with safe variable usage
What is the output of this bash script when run?
#!/bin/bash user_input="; rm -rf /" echo "Hello, $user_input!"
Bash Scripting
#!/bin/bash user_input="; rm -rf /" echo "Hello, $user_input!"
Attempts:
2 left
💡 Hint
Look at how the variable is used inside double quotes.
✗ Incorrect
The variable is inside double quotes, so the shell treats it as a string, not a command. The output prints the exact string including the dangerous characters.
💻 Command Output
intermediate1:30remaining
Effect of unquoted variables in bash scripts
What happens when this script runs?
#!/bin/bash user_input="file.txt; rm -rf /" cat $user_input
Bash Scripting
#!/bin/bash user_input="file.txt; rm -rf /" cat $user_input
Attempts:
2 left
💡 Hint
Think about how the shell interprets unquoted variables with special characters.
✗ Incorrect
Without quotes, the shell splits the variable by spaces and treats ';' as command separator, so it runs 'cat file.txt' then 'rm -rf /'. This is dangerous.
📝 Syntax
advanced1:30remaining
Identify the secure way to read user input in bash
Which option securely reads user input into a variable without allowing command injection?
Attempts:
2 left
💡 Hint
Avoid running user input as code.
✗ Incorrect
Option C safely reads input and prints it as a string. Option C and D execute user input as code, which is unsafe. Option C prints unquoted input, risking word splitting.
🔧 Debug
advanced2:00remaining
Debugging a script vulnerable to command injection
This script is vulnerable to command injection. Which option fixes it?
#!/bin/bash filename=$1 cat $filename
Bash Scripting
#!/bin/bash filename=$1 cat $filename
Attempts:
2 left
💡 Hint
Quoting variables prevents word splitting and command injection.
✗ Incorrect
Option A quotes the variable, preventing injection. Option A is same as unquoted. Option A pipes output but doesn't prevent injection. Option A uses backticks, which executes the content.
🚀 Application
expert2:30remaining
Secure script to safely handle filenames with spaces and special chars
You want to write a bash script that lists details of files given as arguments. Which script correctly handles filenames with spaces and special characters safely?
Attempts:
2 left
💡 Hint
Check how the script iterates over arguments and quotes variables.
✗ Incorrect
Option B uses "$@" which treats each argument separately and quotes variables to handle spaces safely. Options B, C, and D either do not quote properly or iterate incorrectly, causing word splitting or errors.