This README provides a step-by-step guide for implementing a secure web application for a fictional communication company called Communication_LTD. The project demonstrates common web security vulnerabilities and their mitigations.
The project requires building a web-based information system for Communication_LTD, a company that sells internet browsing packages. The system will store information about customers, browsing packages, and market sectors.
- MySQL Database
- JavaScript-based Web Application (with Node.js and Express)
-
Install Required Software:
- MySQL Server (for database)
- Node.js and npm (for JavaScript backend)
- Visual Studio Code (recommended code editor)
-
Initialize Your Project:
- Create a new folder for your project
- Set up a basic Node.js/Express application
- Install necessary packages (express, mysql2, bcrypt, etc.)
-
Create a Basic Folder Structure:
/config- Configuration files/public- Static files (CSS, client-side JS)/views- HTML templates/routes- Route handlers/models- Database interaction
-
Create Database:
- Create a new database named "communication_ltd"
-
Create Tables:
- Users table (id, username, email, password_hash, salt, login_attempts, is_locked)
- Customers table (id, name, email, sector_id, package_id)
- Packages table (id, name, description, price)
- Sectors table (id, name, description)
- PasswordResets table (id, user_id, reset_token, expiry_date)
- PasswordHistory table (id, user_id, password_hash, created_at)
-
Add Example Data:
- Add a few sample packages and sectors
- Create a configuration file in your project to store password requirements:
- Minimum length (10 characters)
- Required character types (uppercase, lowercase, numbers, special characters)
- History count (3 previous passwords)
- Dictionary words to block
- Maximum login attempts (3)
-
Create a registration form page with:
- Username field
- Email field
- Password field
- Submit button
-
Implement registration logic:
- Validate the password against requirements from config file
- Generate a random salt for the user
- Create an HMAC hash of the password using the salt
- Store username, email, password hash, and salt in the database
- Store the password hash in password history table
- Redirect to login page
-
Create a password change form page with:
- Current password field
- New password field
- Submit button
-
Implement password change logic:
- Verify current password matches stored hash
- Validate new password against requirements
- Check if new password matches any in password history
- Create new salt and hash for the new password
- Update password in database
- Add new password to password history table
- Display success message
-
Create a login form page with:
- Username field
- Password field
- Submit button
- Links to registration and forgot password
-
Implement login logic:
- Check if username exists in database
- Check if account is locked
- Hash the entered password and compare with stored hash
- If match fails, increment login attempts
- If login attempts exceed maximum, lock the account
- If match succeeds, create a session for the user
- Reset login attempts on successful login
- Redirect to dashboard
-
Create a customer form page with:
- Name field
- Email field
- Dropdown for sector
- Dropdown for package
- Submit button
-
Implement customer addition logic:
- Save customer details to database
- Display the newly added customer information on confirmation page
- Include navigation back to dashboard
-
Create a forgot password form page with:
- Username or email field
- Submit button
-
Implement token generation:
- Generate a random value
- Hash it with SHA-1
- Store the token in database with expiration time
- Display message saying token was sent (in a real app, you would email it)
-
Create a reset password page with:
- Token field
- New password field
- Submit button
-
Implement password reset logic:
- Verify token exists and hasn't expired
- Follow the same validation as password change
- Update the user's password
- Redirect to login page
-
Create a version with Stored XSS vulnerability:
- In the customer management system, directly insert user input into HTML without sanitization
- Example attack: Adding a customer with name containing
<script>alert('XSS')</script>
-
Create a version with SQL Injection vulnerability:
- In registration, login, and customer forms, construct SQL queries by concatenating strings
- Example attack: Using
' OR '1'='1in the username field of login form
-
Fix XSS vulnerability:
- Sanitize user input by encoding special characters
- Use built-in template engine features for secure rendering
-
Fix SQL Injection vulnerability:
- Replace string concatenation with parameterized queries
- Use placeholder values in all database queries
-
Test User Registration:
- Try creating a user with various passwords to test complexity rules
- Verify that passwords are properly hashed in database
-
Test Login System:
- Verify that successful login creates a session
- Verify that failed login attempts are tracked
- Verify that account locks after maximum attempts
-
Test Customer Management:
- Add customers and verify they appear in database
- Verify that customer information is displayed correctly
-
Test Vulnerability Demonstrations:
- Test XSS attack in vulnerable version (should work)
- Test XSS attack in secure version (should fail)
- Test SQL injection in vulnerable version (should work)
- Test SQL injection in secure version (should fail)
-
For the Database:
- Create all tables before starting development
- Test queries in MySQL Workbench before implementing in code
-
For Security Features:
- Keep the salt unique for each user
- Use crypto libraries for HMAC and SHA-1 functions
- Never store raw passwords anywhere
-
For Express.js Setup:
- Use express-session for session management
- Use body-parser to handle form data
- Use a template engine like EJS or Handlebars
-
For Vulnerability Demonstrations:
- Keep vulnerable and secure code in separate branches or folders
- Document each vulnerability and how to exploit it