Repository for a set up of a vulnerable web environment to simulate SQL Injection attacks and detect them using IDS/IPS tools, scripts, and log analysis. Infrastructure is provisioned using Terraform and configured with Ansible on AWS Free Tier resources*
-
Install Required Tools
-
Set Up AWS Credentials and SSH Keys
- Create an AWS account and generate an Access Key for CLI usage.
- Run
aws configureto set up your credentials. - Create a SSH
key_pairfor your VMs
-
Configure Variables
- Edit
variables.tfvarsand set yourkey_pairand IP address. - You can find your IP address with:
curl https://ipinfo.io/ip
- Edit
-
Prepare Deployment Script
- Make the deployment script executable:
chmod +x deploy.sh
- Make the deployment script executable:
-
Deploy Infrastructure
- First initialize the Terraform project:
terraform init - Run the deployment script, passing the path to your private key:
./deploy.sh path-to-private-key
- The script runs
terraform apply -auto-approveandansible-playbook -i hosts playbook.yaml.
You can also run these commands manually if needed and for terraform runterraform applywithout-auto-approveto be asked to perform the actions.
- First initialize the Terraform project:
-
Access the Virtual Machines
- SSH into the web server VM:
ssh ubuntu@<IP_VM> -i path-to-private-key
- On the database VM, you can test the database setup:
psql -U postgres -d web_server_db
- SSH into the web server VM:
NOTE: Whenever you need a public or private IP, you can simply run the next command in your terminal:
terraform output
-
Troubleshooting
- If you see an error like:
This can rarely happen when running
fatal: [<IP>]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host <IP> port 22: Connection timed out", "unreachable": true}deploy.sh. If it does, simply run the script again.
- If you see an error like:
-
Simulate and Detect SQL Injection Attacks
-
On the siem VM, run:
sudo tail -f /var/ossec/logs/alerts/alerts.log
-
On the webserver VM, run:
sudo tail -f /var/ossec/logs/active-responses.log
-
On the attacker VM, run (substituting the
<PRIVATE_IP_WEB>with the actual private IP address of the vm_web_server):sqlmap -u "http://<PRIVATE_IP_WEB>/get_login.php?id=1" --batch --level=2 --risk=2 -
The attack will be mitigated by blocking the source IP after a while, adding it to the
iptablesblocked IPs. You can see this by running on the webserver VM:sudo iptables -L INPUT -n --line-numbers
-
You can also test attacks with logins using the webpage of the Apache server. Put this URL in your browser:
http://<PUBLIC_IP_WEBSERVER_VM>/get_login.php
-
-
Destroy and Clean Up the Environment
- To remove all provisioned resources and avoid unnecessary charges, run:
terraform destroy
- To remove all provisioned resources and avoid unnecessary charges, run:
- WARNING: If you do NOT destroy the infrastructure after use, AWS may CHARGE YOU for active resources if you exceed the AWS Free Tier limits!
NOTE: While this setup is designed to stay within AWS Free Tier limits, you may incur small charges (a few cents) for data transfer.