-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathvalidate.py
71 lines (53 loc) · 2 KB
/
validate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from selenium import webdriver
import platform
from selenium.common.exceptions import NoAlertPresentException,UnexpectedAlertPresentException
import time
def is_linux():
return platform.system() == 'Linux'
if is_linux():
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = "/usr/bin/chromium"
else:
options = webdriver.ChromeOptions()
options.add_argument("--headless") # Run Chrome in headless mode
options.add_argument("--disable-gpu") # Disable GPU acceleration (optional)
options.add_argument("--no-sandbox")
# Initialize the WebDriver (assuming Chrome)
def validate_js_alert(url):
driver = webdriver.Chrome(options=options)
try:
# Navigate to the URL
#print(url)
driver.get(url)
# Wait a few seconds to ensure the alert has time to appear
time.sleep(3)
try:
# Switch to the alert
alert = driver.switch_to.alert
# Get the text in the alert
# Accept (close) the alert
alert.accept()
#print(f"Alert detected: {alert_text}\n{url}")
driver.quit()
return {'success':True,'url':url}
except NoAlertPresentException:
# No alert was found
#print("No alert detected")
return {'success':False,'url':url}
except UnexpectedAlertPresentException as e:
# Handle the unexpected alert
return {'success':True,'url':url}
except NoAlertPresentException:
return {'success':False,'url':url}
except Exception as e:
#pass
print(e)
if __name__ == "__main__":
# Example usage
url_to_test = "http://testphp.vulnweb.com/hpp/?pp=test%22%3E%3Cimg%20src=x%20onerror=alert(1)%3El"
result = validate_js_alert(url_to_test)
if result:
print("JavaScript alert was generated.")
else:
print("No JavaScript alert was generated.")