|
| 1 | +/* |
| 2 | +This is part of a set of scripts which allow you to authenticate to Juice Shop using Selenium. |
| 3 | +
|
| 4 | +These scripts will currently only run in Oracle Nashorn and not Graal.js |
| 5 | +which means you need to run ZAP using Java 11. |
| 6 | +
|
| 7 | +--- |
| 8 | +
|
| 9 | +This script handles authentication for requests that originate from ZAP, |
| 10 | +e.g. from the traditional spider or the active scanner. |
| 11 | +
|
| 12 | +It launches a browser to authenticate to Juice Shop - this is not strictly |
| 13 | +necessary but this is a demonstration of what to do if you need authenticate |
| 14 | +via a browser. |
| 15 | +
|
| 16 | +It also starts and uses a new proxy on a different port. |
| 17 | +If this is not done then the script will hang as it will try to authenticate |
| 18 | +again using the script which is already running. |
| 19 | +
|
| 20 | +The proxy can be stopped via the JuiceShopReset script. |
| 21 | +*/ |
| 22 | + |
| 23 | +var By = Java.type('org.openqa.selenium.By'); |
| 24 | +var Cookie = Java.type("org.openqa.selenium.Cookie"); |
| 25 | +var HttpRequestHeader = Java.type('org.parosproxy.paros.network.HttpRequestHeader'); |
| 26 | +var HttpResponseHeader = Java.type("org.parosproxy.paros.network.HttpResponseHeader"); |
| 27 | +var HttpHeader = Java.type('org.parosproxy.paros.network.HttpHeader'); |
| 28 | +var ScriptVars = Java.type("org.zaproxy.zap.extension.script.ScriptVars"); |
| 29 | +var System = Java.type("java.lang.System"); |
| 30 | +var Thread = Java.type('java.lang.Thread'); |
| 31 | +var URI = Java.type('org.apache.commons.httpclient.URI'); |
| 32 | + |
| 33 | +var extensionNetwork = control.getExtensionLoader().getExtension("ExtensionNetwork"); |
| 34 | + |
| 35 | +var juiceshopAddr = "http://localhost:3000/"; |
| 36 | +var proxyAddress = "127.0.0.1"; |
| 37 | +var proxyPort = 9092; |
| 38 | + |
| 39 | +var count = 0; |
| 40 | +var limit = 2; |
| 41 | + |
| 42 | +function logger() { |
| 43 | + print('[' + this['zap.script.name'] + '] ' + arguments[0]); |
| 44 | +} |
| 45 | + |
| 46 | +function messageHandler(ctx, msg) { |
| 47 | + if (ctx.isFromClient()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + var url = msg.getRequestHeader().getURI().toString(); |
| 51 | + //logger("messageHandler " + url); |
| 52 | + if (url === juiceshopAddr + "rest/user/login" && msg.getRequestHeader().getMethod() === "POST") { |
| 53 | + var json = JSON.parse(msg.getResponseBody().toString()); |
| 54 | + var token = json.authentication.token; |
| 55 | + logger("Saving Juice Shop token"); |
| 56 | + // save the authentication token |
| 57 | + ScriptVars.setGlobalVar("juiceshop.token", token); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function authenticate(helper, _paramsValues, _credentials) { |
| 62 | + // Remove an existing token (if present) - in theory it may now be invalid |
| 63 | + ScriptVars.setGlobalVar("juiceshop.token", null); |
| 64 | + var proxy = ScriptVars.getGlobalCustomVar("auth-proxy"); |
| 65 | + if (!proxy) { |
| 66 | + // We need to start a new proxy so that the request doesn't trigger another login sequence |
| 67 | + logger("Starting proxy"); |
| 68 | + var proxy = extensionNetwork.createHttpProxy(5, messageHandler); |
| 69 | + proxy.start(proxyAddress, proxyPort); |
| 70 | + // Store the proxy in a global script var |
| 71 | + ScriptVars.setGlobalCustomVar("auth-proxy", proxy); |
| 72 | + } |
| 73 | + |
| 74 | + logger("Launching browser to authenticate to Juice Shop"); |
| 75 | + var extSel = control.getSingleton(). |
| 76 | + getExtensionLoader().getExtension( |
| 77 | + org.zaproxy.zap.extension.selenium.ExtensionSelenium.class); |
| 78 | + |
| 79 | + // Change to "firefox" (or "chrome") to see the browsers being launched |
| 80 | + var wd = extSel.getWebDriver(5, "firefox-headless", proxyAddress, proxyPort); |
| 81 | + logger("Got webdriver"); |
| 82 | + |
| 83 | + // Initial request will display a popup that is difficult to get rid of |
| 84 | + wd.get(juiceshopAddr); |
| 85 | + wd.manage().addCookie(new Cookie('cookieconsent_status', 'dismiss')); |
| 86 | + wd.manage().addCookie(new Cookie('welcomebanner_status', 'dismiss')); |
| 87 | + Thread.sleep(1000); |
| 88 | + // This request will get the login page without the pesky popup |
| 89 | + logger("Requesting login page"); |
| 90 | + wd.get(juiceshopAddr + "#/login"); |
| 91 | + Thread.sleep(1000); |
| 92 | + |
| 93 | + // These are standard selenium methods for filling out fields |
| 94 | + // You will need to change them to support different apps |
| 95 | + wd.findElement(By.id("email")).sendKeys(System.getenv("JS_USER")); |
| 96 | + wd.findElement(By.id("password")).sendKeys(System.getenv("JS_PWD")); |
| 97 | + wd.findElement(By.id("loginButton")).click(); |
| 98 | + logger("Submitting form"); |
| 99 | + |
| 100 | + Thread.sleep(500); |
| 101 | + wd.quit(); |
| 102 | + |
| 103 | + Thread.sleep(500); |
| 104 | + logger("Checking verification URL for Juice Shop"); |
| 105 | + token = ScriptVars.getGlobalVar("juiceshop.token"); |
| 106 | + |
| 107 | + // This is the verification URL |
| 108 | + var requestUri = new URI(juiceshopAddr + "rest/user/whoami", false); |
| 109 | + var requestMethod = HttpRequestHeader.GET; |
| 110 | + var requestHeader = new HttpRequestHeader(requestMethod, requestUri, HttpHeader.HTTP11); |
| 111 | + // The auth token and cookie will be added by the httpsender script |
| 112 | + var msg = helper.prepareMessage(); |
| 113 | + msg.setRequestHeader(requestHeader); |
| 114 | + helper.sendAndReceive(msg); |
| 115 | + |
| 116 | + return msg; |
| 117 | +} |
| 118 | + |
| 119 | +function getRequiredParamsNames() { |
| 120 | + return []; |
| 121 | +} |
| 122 | + |
| 123 | +function getOptionalParamsNames() { |
| 124 | + return []; |
| 125 | +} |
| 126 | + |
| 127 | +function getCredentialsParamsNames() { |
| 128 | + return ["username", "password"]; |
| 129 | +} |
| 130 | + |
0 commit comments