Podcast
Questions and Answers
What is the main issue with the following PHP code that handles a login form? $username = $_POST['username'];
$password = $_POST['password'];
$query="SELECT * FROM users WHERE username = '$username' AND password='$password'";
$result = mysqli_query($conn, $query);
What is the main issue with the following PHP code that handles a login form? $username = $_POST['username'];
$password = $_POST['password'];
$query="SELECT * FROM users WHERE username = '$username' AND password='$password'";
$result = mysqli_query($conn, $query);
- The code does not connect to the database correctly.
- The code does not handle empty input fields.
- The code uses an outdated version of PHP.
- The code is vulnerable to SQL injection because it directly concatenates user inputs into the SQL query. (correct)
Which of the following inputs could lead to a successful SQL injection attack against the vulnerable code below? $search = $_GET['search'];
$query = "SELECT * FROM products WHERE name LIKE '%$search%'";
$result = mysqli_query($conn, $query);
Which of the following inputs could lead to a successful SQL injection attack against the vulnerable code below? $search = $_GET['search'];
$query = "SELECT * FROM products WHERE name LIKE '%$search%'";
$result = mysqli_query($conn, $query);
- apple
- '; DROP TABLE products; #
- 100 OR 1=1
- All of the above (correct)
What is wrong with the following PHP code, and how can it be mitigated? $id = $_GET['id'];
$new_email = $_POST['email'];
$query = "UPDATE users SET email = '$new_email' WHERE id = $id";
mysqli_query($conn, $query);
What is wrong with the following PHP code, and how can it be mitigated? $id = $_GET['id'];
$new_email = $_POST['email'];
$query = "UPDATE users SET email = '$new_email' WHERE id = $id";
mysqli_query($conn, $query);
- The code will fail because of incorrect SQL syntax.
- The code is vulnerable to SQL injection because it directly includes user inputs in the query. Use parameterized queries to fix it. (correct)
- The code is secure as it is.
- The query should use double quotes instead of single quotes for variables.
What type of injection involves an attacker injecting code in the programming or scripting language used by the application?
What type of injection involves an attacker injecting code in the programming or scripting language used by the application?
Which of the following PHP functions are particularly vulnerable to code injection?
Which of the following PHP functions are particularly vulnerable to code injection?
What is the primary reason to avoid using preg_replace('/e')
in PHP?
What is the primary reason to avoid using preg_replace('/e')
in PHP?
How can you prevent code injection when using the include()
function with user inputs?
How can you prevent code injection when using the include()
function with user inputs?
Which of the following inputs could lead to a successful code injection attack against the vulnerable PHP code below? $command = $_GET['command'];
eval($command);
Which of the following inputs could lead to a successful code injection attack against the vulnerable PHP code below? $command = $_GET['command'];
eval($command);
What type of injection allows an attacker to execute arbitrary system commands on the host OS via the vulnerable application?
What type of injection allows an attacker to execute arbitrary system commands on the host OS via the vulnerable application?
Which of the following inputs would be dangerous if passed to the following PHP code? $filename = $_GET['filename'];
shell_exec("rm " . $filename);
Which of the following inputs would be dangerous if passed to the following PHP code? $filename = $_GET['filename'];
shell_exec("rm " . $filename);
What is the primary risk associated with the following PHP code?$userInput = $_GET['file'];
system("cat " . $userInput);
What is the primary risk associated with the following PHP code?$userInput = $_GET['file'];
system("cat " . $userInput);
Which PHP function
is MOST susceptible to command injection attacks?
Which PHP function
is MOST susceptible to command injection attacks?
What can be a dangerous consequence of using the exec()
function in PHP without proper input validation?
What can be a dangerous consequence of using the exec()
function in PHP without proper input validation?
How can you mitigate command injection vulnerabilities when using functions like system()
, exec()
, or shell_exec()
in PHP?
How can you mitigate command injection vulnerabilities when using functions like system()
, exec()
, or shell_exec()
in PHP?
Flashcards
Code Injection
Code Injection
When an attacker injects malicious code into an application, using the programming language of that application.
eval()
eval()
A PHP function that evaluates a string as PHP code.
include() / require()
include() / require()
PHP functions used to include and execute external files. Attackers can exploit path manipulation.
preg_replace()
preg_replace()
Signup and view all the flashcards
assert()
assert()
Signup and view all the flashcards
Sanitize Inputs
Sanitize Inputs
Signup and view all the flashcards
Input Whitelisting
Input Whitelisting
Signup and view all the flashcards
preg_replace_callback()
preg_replace_callback()
Signup and view all the flashcards
htmlspecialchars()
htmlspecialchars()
Signup and view all the flashcards
Command Injection
Command Injection
Signup and view all the flashcards
exec()
exec()
Signup and view all the flashcards
system()
system()
Signup and view all the flashcards
shell_exec()
shell_exec()
Signup and view all the flashcards
passthru()
passthru()
Signup and view all the flashcards
popen()/proc_open()
popen()/proc_open()
Signup and view all the flashcards
Backticks ``
Backticks ``
Signup and view all the flashcards
escapeshellarg()
escapeshellarg()
Signup and view all the flashcards
escapeshellcmd()
escapeshellcmd()
Signup and view all the flashcards
Input Validation
Input Validation
Signup and view all the flashcards
PHP Built-in Functions
PHP Built-in Functions
Signup and view all the flashcards
Cross-Site Scripting (XSS)
Cross-Site Scripting (XSS)
Signup and view all the flashcards
Non-Persistent (Reflected) XSS
Non-Persistent (Reflected) XSS
Signup and view all the flashcards
Persistent (Stored) XSS
Persistent (Stored) XSS
Signup and view all the flashcards
DOM-Based XSS
DOM-Based XSS
Signup and view all the flashcards
Cross-Site Request Forgery (CSRF)
Cross-Site Request Forgery (CSRF)
Signup and view all the flashcards
Insecure Direct Object References
Insecure Direct Object References
Signup and view all the flashcards
Security Misconfiguration
Security Misconfiguration
Signup and view all the flashcards
File Attacks
File Attacks
Signup and view all the flashcards
Side Channel Attacks
Side Channel Attacks
Signup and view all the flashcards
Common Software Vulnerabilities
Common Software Vulnerabilities
Signup and view all the flashcards
Study Notes
Common Software Vulnerabilities and Controls
- Common software vulnerabilities include:
- Buffer, Stack, and Heap Overflows
- Injection Flaws
- Broken Authentication and Session Management
- Cross-Site Scripting (XSS)
- Non-Persistent or Reflected XSS
- Persistent or Stored XSS
- DOM based XSS
- Cross-Site Request Forgery (CSRF)
- Insecure Direct Object References
- Security Misconfiguration
- File Attacks
- Side Channel Attacks
Defensive Coding Practices
- Defensive coding practices include:
- Input Validation
- Canonicalization
- Sanitization
- Error Handling
- Safe APIs
- Memory Management
- Exception Management
- Session Management
- Configuration Parameters Management
- Secure Startup
- Cryptography
- Concurrency
- Tokenization
- Sandboxing
- Anti-Tampering
SQL Injection Vulnerability Example
- PHP code directly concatenates user inputs which makes the SQL query vulnerable to injection.
SQL Injection Attack Types
- The following inputs lead to a successful SQL injection attack against vulnerable code:
- ' ; DROP TABLE products; #
- 100 OR 1=1
- apple
SQL Injection Mitigation
- PHP code can be fixed by using parameterized queries to avoid direct insertion of user inputs.
Code Injection Definition
- Code injection involves the attacker injecting code in the programming or scripting language used by the application
- Languages commonly injected into are PHP, Python, or JavaScript.
Vulnerable PHP Functions
- PHP functions that are particularly vulnerable to code injection:
- eval()
- include()
- require()
- include_once()
- require_once()
- preg_replace()
- assert()
Code Injection Functions
- eval(): Evaluates a string as PHP code and is rarely needed.
- include(), require(), include_once(), require_once(): Used to include and execute files.
- preg_replace(): Used for regular expression search and replace, evaluates the replacement as PHP code if used with the /e modifier.
- assert(): Evaluates a string as PHP code if a condition is false, used for debugging.
Code Injection Prevention Methods
- Avoid using dangerous PHP functions like eval(), assert(), and preg_replace() with the /e modifier.
- Employ safe methods for dynamic content.
- Avoid the /e modifier in preg_replace(). Use
preg_replace_callback()
instead.
Code Injection Prevention Techniques
htmlspecialchars()
should be used to avoid code injectionspreg_replace_callback()
should be used instead ofpreg_replace()
- It is best to avoid using
preg_replace('/e')
Code Injection Via Directory Traversal
- Attackers use directory traversal (../../) to access a different directory
- This method allows them to upload a file containing harmful PHP code to the server.
Code Injection Prevention Strategy
- Use whitelisting over blacklisting.
- Sanitize and validate inputs
Code Injection Questions
- The primary reason to avoid using
preg_replace('/e')
in PHP is it allows evaluation of PHP code from user input, leading to code injection vulnerabilities. - Code injection using the
include()
function can be prevented by validating that the function only includes safe characters and paths, and restrict it to known filenames.
Command Injection Definition
- Command injection involves the attacker executing arbitrary system commands on the host OS via the vulnerable application
- Often system shell commands are called
Functions Vulnerable to Command Injection in PHP
- Vulnerable functions include:
exec()
system()
shell_exec()
passthru()
popen()
proc_open()
- backticks `` `
Command Injection Vulnerability Example
- Using
shell_exec()
along with thegrep
command, an attacker injected cat /etc/passwd command to view sensitive system files. - Improper handling of user input in an application that performs network diagnostics allows attackers to inject OS commands.
Command Injection Prevention
- Always validate and sanitize inputs using functions like
escapeshellarg()
andescapeshellcmd()
. - Avoid using user input directly in commands
- Apply proper input checks
- Use safer alternatives such as PHP functions.
- Minimize privileges in running commands.
Command Injection Prevention With PHP Built-In Functions
- Use
scandir()
orglob()
instead ofls
for directory listings - Use
unlink()
instead ofrm
for file deletion - Use
copy()
,rename()
, andmove_uploaded_file()
instead ofcp
ormv
for file operations - Employ
file_put_contents()
,file_get_contents()
, andfopen()
instead ofcat
orecho
for reading and writing files - To check server connectivity employ PHP functions like:
fsockopen()
stream_socket_client()
cURL
instead ofsystem("ping $host")
Harmful PHP Code in Command Injections
- Passing the following inputs in PHP code would be dangerous in command injections:
; rm -rf /
Command Injection Attacks
- The most susceptible PHP function to command injection attacks is
exec()
Command Injections Risks
- A dangerous consequence of using the
exec()
function in PHP without proper input validation is an attacker could control the server.
Command Injection Mitigation Tips
- Mitigate command injection vulnerabilities by restricting input to a whitelist of allowed commands and using
escapeshellarg()
orescapeshellcmd()
.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore common software vulnerabilities like XSS and SQL injection. Learn defensive coding practices including input validation and secure APIs to mitigate risks. Understand techniques for secure software development.