Mada za sehemu hiiPractising safe programming practicesMada 2
- Performing sanitisation and validation of user inputs
- Performing prevention of injections attacks
Injection Attack Prevention
Injection attacks are one of the most dangerous security threats to computer applications. They occur when an attacker sends harmful data to a program, tricking it into executing unintended commands. As a programmer, you must learn to protect your applications from these attacks.
An injection attack happens when untrusted data is sent to an application as part of a command or query. The attacker's goal is to make the application do something it was not designed to do, such as stealing data, deleting information, or bypassing security checks.
Why Injection Attacks Happen
Many programmers combine user input directly with code or commands without checking the input first. When the application runs, the attacker's malicious input gets executed as part of the legitimate command.
1. SQL Injection (SQLi)
SQL injection occurs when attackers insert harmful SQL commands into input fields that interact with a database. This can allow them to view, modify, or delete database records without authorization.
2. Cross-Site Scripting (XSS)
XSS attacks inject malicious scripts into web pages viewed by other users. These scripts can steal session cookies, redirect users to harmful sites, or perform actions on behalf of the victim.
Input Validation
Always check that user input matches expected formats before using it. Reject any input that contains suspicious characters.
Key validation rules:
- Check data type (numbers, letters, symbols)
- Verify length limits
- Use allowlists (accept only known good values) instead of blocklists
Input Sanitisation
Clean user input by removing or encoding special characters that could be interpreted as code.
Parameterized Queries
Use parameterized queries (also called prepared statements) when accessing databases. This keeps the query structure separate from user data, preventing attackers from changing the query's meaning.
Output Encoding
When displaying user input on web pages, encode special characters so they appear as text rather than executable code.
Vulnerable Code (PHP)
$username = $_POST['username'];
$password = $_POST['password'];
// DANGEROUS: Direct concatenation
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);
An attacker could enter ' OR '1'='1 as the username, and the query would become:
SELECT * FROM users WHERE username='' OR '1'='1' AND password='$password'
Since '1'='1' is always true, the attacker gains access without knowing a real password.
Secure Code (PHP)
$username = $_POST['username'];
$password = $_POST['password'];
// SAFE: Parameterized query
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();
The ? placeholders ensure user input is treated as data, not SQL code.
- Validate all input — never trust data from users
- Use parameterized queries — separate code from data
- Escape special characters — convert dangerous symbols to safe equivalents
- Limit database permissions — give applications only the access they need
- Test your code — try entering malicious input to check for vulnerabilities
Injection attacks exploit applications that do not properly handle user input. By validating and sanitising all input, using parameterized queries, and following secure coding practices, you can protect your applications and the data they handle.
In Tanzania, many small businesses use simple web applications for inventory management or customer orders. For example, a duka in Dar es Salaam might use a PHP system to track stock. If that system does not prevent SQL injection, an attacker could steal customer phone numbers used for M-Pesa transactions or delete sales records. By applying the secure coding practices learned in this topic, a student could help local businesses protect their data and maintain customer trust.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
e student should be le to: Identify injection attack risks Implement suitable preventive measures against injection attacks. 132 133
Tathmini ya matokeo
Applications are protected against injection attacks and perform securely under various input conditions.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Use parameterized queries and stored procedures to secure database interactions.
- Validate and sanitize user inputs to prevent injection vulnerabilities.
Principles: The student should explain the principle of:
- Input validation to prevent malicious code execution.
Theories: The trainee should explain: The trainee should explain:
- How injection attacks exploit poorly coded applications and unsecured input fields.
Detailed knowledge about:
- SQL injection, XSS, LDAP injection, and other types of injection vulnerabilities.
Swali
What is an injection attack in the context of computer security?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado