OWASP Top 10: Injection Attacks
Jeremiah Talamantes
Security @ Podium, Founder @ Compliiant.io, Founder @ Mitigated.io (Sold), Founder @ RedTeam Security (Sold), Author of Building Security Partner Programs, Social Engineer's Playbook and Physical Red Team Operations
Understanding Injection Vulnerabilities: Prevention and Detection Methods
Let's look closely at injection vulnerabilities, a dangerous security hole that might compromise backend systems and even hijack other users' sessions by allowing attackers to sneak harmful code through an application. As the number of these assaults continues to rise, it is essential to be familiar with the most prevalent kinds, such as SQL injections and operating system command injections, which do significant damage.
Keep checking back for more information as we explore injection attack mechanics, system security measures, and the significance of strong input validation. In order to make sure your app is secure, we will also go over how important it is to clean user-provided data.
By the way, if you like my content, please visit Compliiant.io and share it with your LinkedIn network, Medium network, colleagues, and friends. Compliiant.io is a Cybersecurity Services as a Subscription business.
Understanding Injection Vulnerabilities
?Injection vulnerabilities are well-known in the field of application security and pose a serious threat to programmers and end users alike. The improper management of data provided by users is at the heart of these vulnerabilities, which can result in breaches and other severe security risks. Injection vulnerabilities that are commonly encountered include:
This happens when programs don't properly check or clean user input, which opens the door for bad actors to possibly access and change private information. The risk of injection vulnerabilities can be summarized as follows: picture a lock that opens with any key, not only the correct one.?
Now that we know this, we can take the necessary steps to protect our applications against these vulnerabilities..
Detecting Injection Vulnerabilities
An essential part of application security is learning about and finding injection vulnerabilities. Insecure handling of user inputs is a common source of these vulnerabilities, according to HackerOne, although comprehensive code reviews can successfully detect them.
According to OWASP, one of the best ways to find possible injection vulnerabilities is to review the source code. Finding places in the code where user input is not adequately checked or sanitized requires a careful examination. Problems like SQL injection and Cross-Site Scripting can be more easily caught with this approach.
Automated testing can be used in conjunction with manual reviews to find these vulnerabilities. To that end, there are tools available like Pentest-Tools.com that can detect, exploit, and report possible injection flaws.
Finally, detecting and avoiding injection threats relies heavily on ongoing testing. It is possible to prevent the introduction of new vulnerabilities and the prompt resolution of existing ones by doing regular testing of the program.
Prevention Measures for Injection Vulnerabilities
Always present in the realm of digital security are injection vulnerabilities, most notably SQL Injection (SQLi). The good news is that these vulnerabilities can be avoided in a number of ways. Explore some of these approaches.
Sample Vulnerabilities
To illustrate better, here are a few vulnerable blocks of code:
SQL Injection
import MySQLdb
# Database connection settings
hostname = "localhost"
username = "username"
password = "password"
database = "myDatabase"
# Connect to the database
db = MySQLdb.connect(host=hostname, user=username, passwd=password, db=database)
# Create a cursor object
cursor = db.cursor()
# User-provided credentials (normally, you'd get these from a form or other input)
user = input("Enter username: ") # User input
passw = input("Enter password: ") # User input
# SQL query vulnerable to SQL injection
sql = "SELECT id FROM users WHERE username = '%s' AND password = '%s'" % (user, passw)
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
if results:
print("User authenticated.")
else:
print("Invalid username or password.")
except:
print("Error: unable to fetch data")
# disconnect from server
db.close()
Issue
This code above connects to a MySQL database and executes a query to authenticate a user. The user's username and password are directly inserted into the SQL query string, making it susceptible to SQL injection. For instance, if the user inputs a username like anything' OR 'x'='x and any password, it can potentially bypass authentication checks.
领英推荐
Cross-site Scripting
<!DOCTYPE html>
<html>
<head>
<title>Feedback Form</title>
</head>
<body>
<h2>Feedback Form</h2>
<form>
Enter your feedback: <input type="text" id="feedback" name="feedback">
<input type="submit" value="Submit" onclick="displayFeedback(); return false;">
</form>
<p id="feedbackDisplay"></p>
<script>
function displayFeedback() {
var feedback = document.getElementById('feedback').value;
// Vulnerable part: directly inserting user input into the page
document.getElementById('feedbackDisplay').innerHTML = "Your feedback: " + feedback;
}
</script>
</body>
</html>
Issue
In this code, the function displayFeedback takes the user input from the feedback input field and directly sets it as the inner HTML of the feedbackDisplay paragraph. This is vulnerable to XSS because an attacker can enter JavaScript code as part of the feedback, which will be executed when the feedback is displayed. For example, if an attacker enters <script>alert('Hacked!');</script> into the feedback field, it will execute an alert dialog displaying "Hacked!".
This example demonstrates how easy it is to introduce an XSS vulnerability by not sanitizing user inputs before inserting them into the DOM. The correct approach would be to properly escape or sanitize the input to ensure any HTML or JavaScript code does not execute.
OS Commanding
from flask import Flask, request
import os
app = Flask(__name__)
@app.route('/fileinfo', methods=['GET'])
def fileinfo():
filename = request.args.get('filename') # User-supplied filename
command = f"ls -l {filename}" # Vulnerable OS command construction
try:
# Vulnerable part: The command is executed with user input directly included
output = os.popen(command).read()
return output
except Exception as e:
return str(e)
if __name__ == '__main__':
app.run()
Issue
In this application, a user can make a GET request to the /fileinfo endpoint with a filename parameter. The application then constructs a shell command to list details about the file. However, since the filename is directly appended to the command string without any validation or sanitization, it creates a vulnerability.
An attacker could exploit this by providing input like ; cat /etc/passwd as the filename, leading to execution of additional unintended commands (cat /etc/passwd in this case, which could be used to read sensitive data).
To mitigate such vulnerabilities, avoid constructing shell commands with user-supplied input. If it's absolutely necessary, rigorously validate and sanitize the input, and consider using safer alternatives like built-in library functions that don't invoke the shell.
Attack and Consequences?
Having an injection attack is no laughing matter, and it can have devastating effects. Think of a bad guy who, like a sly fox, changes the values of parameters to influence the results of queries. Unauthorized access or even modification of sensitive data might result from this dishonest behavior. It's the same as surrendering control of your castle to an unidentified invader.
An organization's reputation can take a serious harm in the wake of an injection attack, much like a coffee stain on a perfectly white blouse. A ship going down into a sea of red ink would be a good metaphor for the possible financial losses. No company wants to be in this situation.
Believing is seeing. To put these catastrophic results into perspective, think of the 2017 Equifax breach, in which an injection attack exposed the personal information of millions of people.
You may find a wealth of information, including cheat sheets, online to help you strengthen your defenses against injection vulnerabilities. The OWASP Injection Prevention Cheat Sheet stands out because it provides concise, practical advice for avoiding various injection mistakes.
Additional materials and cheat sheets provided by the Open Web Application Security Project (OWASP) shed light on several ways to test for SQL, LDAP, XML, SSI, and XPath injections, among others. Developers and security experts can greatly benefit from these tools, since they keep them informed about the latest prevention techniques.
Remember that these resources shouldn't be relied on exclusively to ward off attacks, although they can help lessen their impact when used correctly. Use them, rather than only reading them.
?So far, we've discussed how injection vulnerabilities are a major risk for web applications. Data theft, data integrity loss, and complete system compromise are all possible outcomes of these attacks, which include providing a program with untrusted input.
Protecting against such assaults requires the use of detection and prevention mechanisms. Among these, you can find methods to validate user inputs, make use of stored processes, and decrease the attack surface of your application (source). Even widely recommended solutions, such as stored procedures or refuse listing, might have significant weaknesses, as SQL injection shows (source).
Last but not least, fixing injection vulnerabilities is critical to system security. To protect yourself from injection assaults, be alert, update your apps often, and learn about new preventive methods.?
If you like my content, please visit Compliiant.io and share it with your friends and colleagues! Cybersecurity services for a low monthly subscription. Pause or cancel at any time. See https://compliiant.io/
Sources:
Security @ Podium, Founder @ Compliiant.io, Founder @ Mitigated.io (Sold), Founder @ RedTeam Security (Sold), Author of Building Security Partner Programs, Social Engineer's Playbook and Physical Red Team Operations
1 年SQL injection is visualized a little better in this video: https://www.youtube.com/watch?v=nH4r6xv-qGg