{"id":6409,"date":"2026-01-03T20:02:28","date_gmt":"2026-01-03T20:02:28","guid":{"rendered":"https:\/\/cybersecurityinfocus.com\/?p=6409"},"modified":"2026-01-03T20:02:28","modified_gmt":"2026-01-03T20:02:28","slug":"zap-script-authentication-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/cybersecurityinfocus.com\/?p=6409","title":{"rendered":"ZAP Script Authentication: A Step-by-Step Guide"},"content":{"rendered":"<h2>TL;DR<\/h2>\n<p>This guide shows you how to authenticate with OWASP ZAP using a script, allowing it to scan protected areas of your web application. We\u2019ll cover setting up the script and testing it.<\/p>\n<h2>Setting Up Authentication in ZAP<\/h2>\n<p>Open Your Script: In ZAP, go to <em>Tools &gt; Options<\/em>.<br \/>\nNavigate to Scripts: Select the \u2018Scripts\u2019 tab.<br \/>\nAdd a New Script: Click \u2018Add\u2019. Give your script a meaningful name (e.g., \u2018MyAuthenticationScript\u2019). Choose a suitable language (JavaScript is common).<\/p>\n<h2>Writing the Authentication Script<\/h2>\n<p>The core of authentication lies in the script itself. Here\u2019s an example using JavaScript to handle basic HTTP authentication:<\/p>\n<p>function scan(helper, ctx) {<br \/>\n  var request = helper.request;<br \/>\n  \/\/ Check if the request needs authentication (e.g., by URL)<br \/>\n  if (request.getURL().toString().startsWith(&#8220;https:\/\/your-protected-app\/admin\/&#8221;)) {<br \/>\n    \/\/ Add Authentication Header<br \/>\n    request.addHeader(&#8220;Authorization&#8221;, &#8220;Basic &#8221; + helper.getAuthenticationToken());<br \/>\n  }<br \/>\n}<\/p>\n<p>Explanation:<\/p>\n<p>scan(helper, ctx): This function is called for each request ZAP intercepts.<br \/>\nhelper.request: Provides access to the current HTTP request object.<br \/>\nrequest.getURL().toString(): Gets the URL of the request as a string.<br \/>\nstartsWith(&#8220;https:\/\/your-protected-app\/admin\/&#8221;): Checks if the URL starts with your protected application\u2019s admin path. <em>Replace this with your actual URL!<\/em><br \/>\nrequest.addHeader(&#8220;Authorization&#8221;, &#8220;Basic &#8221; + helper.getAuthenticationToken()): Adds an \u2018Authorization\u2019 header to the request, including a basic authentication token. The helper.getAuthenticationToken() function is crucial; we\u2019ll define this next.<\/p>\n<h2>Getting the Authentication Token<\/h2>\n<p>You need a way for ZAP to obtain the authentication token (username\/password).  Here\u2019s how you can implement that:<\/p>\n<p>function getAuthenticationToken() {<br \/>\n  \/\/ Prompt user for credentials if not already cached.<br \/>\n  var username = ctx.getOption(&#8220;authentication.username&#8221;);<br \/>\n  var password = ctx.getOption(&#8220;authentication.password&#8221;);<\/p>\n<p>  if (!username || !password) {<br \/>\n    var dialog = new Dialog();<br \/>\n    dialog.setTitle(&#8220;Authentication Required&#8221;);<br \/>\n    dialog.setPromptText(&#8220;Username:&#8221;, username);<br \/>\n    dialog.setPromptText(&#8220;Password:&#8221;, password);<br \/>\n    dialog.showDialog();<br \/>\n    username = dialog.getValue(&#8220;Username&#8221;);<br \/>\n    password = dialog.getValue(&#8220;Password&#8221;);<\/p>\n<p>    ctx.setOption(&#8220;authentication.username&#8221;, username);<br \/>\n    ctx.setOption(&#8220;authentication.password&#8221;, password);<br \/>\n  }<\/p>\n<p>  \/\/ Encode the credentials in Base64.<br \/>\n  var encodedCredentials = btoa(username + &#8216;:&#8217; + password);<br \/>\n  return encodedCredentials;<br \/>\n}<\/p>\n<p>Explanation:<\/p>\n<p>ctx.getOption(&#8220;authentication.username&#8221;) and ctx.getOption(&#8220;authentication.password&#8221;): Attempts to retrieve cached credentials from ZAP\u2019s options.<br \/>\nThe if (!username || !password) block prompts the user for credentials if they aren\u2019t already stored.<br \/>\nbtoa(username + &#8216;:&#8217; + password): Encodes the username and password in Base64, which is required for Basic Authentication.<\/p>\n<h2>Adding the Token Function to Your Script<\/h2>\n<p>Add the getAuthenticationToken() function to your script *before* the scan() function.<\/p>\n<h2>Testing the Script<\/h2>\n<p>Save Your Script: Save the changes to your authentication script.<br \/>\nEnable the Script: In ZAP, ensure the script is enabled (checkbox ticked in the Scripts tab).<br \/>\nBrowse Your Application: Start browsing your protected application.  ZAP will intercept requests and apply the authentication header if it matches your URL condition.<br \/>\nCheck the History Tab: Verify that the \u2018Authorization\u2019 header is being added to requests targeting your protected areas in ZAP\u2019s History tab. Select a request, then look at the \u2018Request\u2019 tab. You should see the header present.<\/p>\n<h2>Troubleshooting<\/h2>\n<p>Incorrect URL: Double-check that the URL condition in your script (startsWith()) is correct.<br \/>\nBase64 Encoding: Ensure the username and password are correctly encoded in Base64.<br \/>\nAuthentication Type: This example uses Basic Authentication. Adjust the script if your application uses a different authentication method (e.g., API keys, OAuth).<\/p>\n<p>The post <a href=\"https:\/\/blog.g5cybersecurity.com\/zap-script-authentication-a-step-by-step-guide\/\">ZAP Script Authentication: A Step-by-Step Guide<\/a> appeared first on <a href=\"https:\/\/blog.g5cybersecurity.com\/\">Blog | G5 Cyber Security<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>TL;DR This guide shows you how to authenticate with OWASP ZAP using a script, allowing it to scan protected areas of your web application. We\u2019ll cover setting up the script and testing it. Setting Up Authentication in ZAP Open Your Script: In ZAP, go to Tools &gt; Options. Navigate to Scripts: Select the \u2018Scripts\u2019 tab. [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6409","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts\/6409"}],"collection":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6409"}],"version-history":[{"count":0,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts\/6409\/revisions"}],"wp:attachment":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}