This project demonstrates the difference between a web application that is vulnerable to SQL Injection attacks and one that is protected using parameterized queries.
This application consists of two different websites:
- Vulnerable Website: Allows SQL injection attacks because it directly concatenates user input into SQL queries.
- Secure Website: Prevents SQL injection attacks by using parameterized queries.
Both websites have:
- Login pages that require correct user ID and password
- User registration pages
- Dashboard pages that display sensitive data after successful authentication
- User authentication with MD5 hashed passwords
- Demonstration of SQL injection vulnerabilities
- Comparison of vulnerable vs. secure coding practices
- PostgreSQL database integration
- Node.js (v18 or higher)
- PostgreSQL database
- npm or yarn
-
Clone the repository:
git clone [repository-url] cd [repository-directory] -
Install dependencies:
npm install -
Configure your PostgreSQL database:
- Create a PostgreSQL database named
authdb - Update the database connection details in
src/env.tsif necessary
- Create a PostgreSQL database named
-
Set up the database tables and seed data:
npm run setup-dbThis will create the necessary tables and add sample users:
- Username:
admin, Password:admin123 - Username:
user, Password:user123
- Username:
-
Start the development server:
npm run dev -
Access the application: Open your browser and navigate to
http://localhost:3000
Here are some examples of SQL injection attacks you can try on the vulnerable app:
-
Login Bypass:
- Username:
admin' -- - Password: (anything)
- Username:
-
Always True Condition:
- Username:
' OR '1'='1 - Password:
' OR '1'='1
- Username:
-
Dangerous Operations (prevented in this demo):
- Username:
admin'; DROP TABLE users; -- - Password: (anything)
- Username:
The vulnerable version builds SQL queries by directly concatenating user input:
const sql = `SELECT * FROM users WHERE username = '${username}' AND password = '${hashedPassword}'`;The secure version uses parameterized queries to prevent SQL injection:
const sql = 'SELECT * FROM users WHERE username = $1 AND password = $2';
const result = await pool.query(sql, [username, hashedPassword]);This project is for educational purposes only.
This application intentionally contains security vulnerabilities for demonstration purposes. Do not use this code in production environments.