This project is a demonstration of an in-band SQL Injection attack. The primary goal is to show how an attacker can extract sensitive data or bypass authentication by inserting malicious SQL queries into web input fields.
The application architecture consists of a Node.js web server utilizing the pg module to connect to a PostgreSQL database, I also used pgAdmin as a database management interface. The entire environment is containerized and orchestrated using Docker.
This demo focuses on Authentication Bypass, allowing an attacker to evade login mechanisms. The vulnerability exists because my application builds the SQL query by directly concatenating user input without validation:
SELECT * FROM users WHERE username = '${username}' AND password = '${password}'.
You can simulate three different types of in-band attacks:
- Tautology: By entering
' OR '1'='1in the password field, the attacker creates aWHEREclause that is always true. This returns all rows in the table, effectively violating confidentiality by exposing all records without authorization. - End-of-line Comment: By entering
admin' --in the username field, the SQL comment (--) forces the database to ignore the rest of the query, including the password check. This grants unauthorized access to the admin profile, compromising data confidentiality. - Piggybacked Query: By entering
'; UPDATE users SET password = 'newPass' WHERE username = 'admin'; --in the password field, a second malicious query is appended. This executes both the original query and the update query, changing the admin's password so violating both system integrity and confidentiality. Moreover, by using this same technique, the application includes a button that allows you to maliciously drop the entire table and reveal the database version in use.
- Docker Desktop (required for running the local server and database containers).
- A local code editor (like VS Code).
-
Environment Variables Configuration
- To keep credentials secure, this project uses a
.envfile. - Create a new file named
.envin the root directory. - Copy the structure from
.env.exampleand fill in your desired passwords for PostgreSQL and pgAdmin.
- To keep credentials secure, this project uses a
-
Start the Infrastructure You don't need to manually run
npm installlocally, as the Dockerfile handles dependency installation during the build process. Just open your terminal in the root directory of the project and run:docker-compose up --build -d
-
Access the Web Application Once the containers are running, open your browser and navigate to the login page:
http://localhost:3000
You have two ways to inspect the database depending on your preferred workflow:
Option A: pgAdmin Desktop App (Recommended) If you have the native pgAdmin application installed on your machine:
- Open the pgAdmin app.
- Add a new server connection.
- Set the Host to
localhostand the Port to5432. - Use the username
postgresand the database password you defined in your.envfile.
Option B: pgAdmin via Web Browser If you prefer using the containerized pgAdmin web interface:
- Navigate to
http://localhost:8080in your browser. - Log in using the
PGADMIN_DEFAULT_EMAILandPGADMIN_DEFAULT_PASSWORDyou set in your.envfile (e.g.,users@users.com/root). - Add a new server connection.
- Important: Set the Host to
db(since it is communicating within the Docker network) and the Port to5432. - Use the username
postgresand your database password.
