Posts

Showing posts with the label secure coding

Stop SQL Injection in .NET

 The Problem: One Input Box Can Expose Your Entire Database SQL Injection is still one of the top threats in .NET applications, especially when developers build SQL queries by string concatenation. Here’s a classic mistake: string query = "SELECT * FROM Users WHERE Email = '" + email + "' AND Password = '" + password + "'"; If an attacker types this into the email field: ' OR 1=1 -- The query turns into: SELECT * FROM Users WHERE Email = '' OR 1=1 --' AND Password = '' Result: The attacker bypasses login or retrieves all users—a full data breach with one input.   ✅ The Solution: Use Parameterized Queries with ADO.NET or Entity Framework The safest and most effective solution in .NET is parameterized queries. They keep SQL logic and data separate, making injection impossible.   🔐 Example with ADO.NET: using (SqlConnection conn = new SqlConnection(connectionString)) {     SqlCommand cmd...

How to Prevent SQL Injection in PHP

 In today’s web-driven world, data breaches often begin with one overlooked flaw— SQL injection . It’s one of the most common attacks against PHP websites, and the worst part? It can happen with just a simple text input.   🚨 The Problem: A Tiny Input Field, a Massive Security Risk Imagine you’re building a PHP login page. A user enters their email and password, and your code does something like this: $email = $_POST['email']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE email = '$email' AND password = '$password'"; Seems harmless, right? Now, a malicious user types this into the email field: ' OR 1=1 -- The resulting SQL becomes: SELECT * FROM users WHERE email = '' OR 1=1 --' AND password = '' The attacker bypasses your login completely—gaining access to any user account.   ✅ The Solution: Use Prepared Statements with PDO Prepared statements prevent SQL injection by...