Adding Pre-Request Scripts

In Acunetix On-Premises, you can add a pre-request script to execute actions before each scanner request. This is commonly used to modify the request, such as adding custom HTTP headers. For example, create a file named PreRequestScript.js with the following content:

/// url:  /

/// script_type: prerequestscript

ax.log(ax.LogLevelInfo,"Pre Request Script Start");

var myhttp = scriptArg.http;

var myrequest = myhttp.request;

var mydate = (Date.now() / 1000).toString();

// add headers to the request before it is sent out

myrequest.setHeader('AcunetixPreRequestScriptEnabled', 'yes');

myrequest.setHeader('AcunetixPreRequestScriptTimeStamp', mydate);

ax.log(ax.LogLevelInfo,"Pre Request Script End");

In this example, each request sent by the scanner will now include two additional headers:

Using scriptArg.http.abort();, you can abort the request before it is executed. This utilizes the abort() function.

Information logged by pre-request scripts via the ax.log() function is available in the debug logs. You can set the log level using one of the log level constants:

ax.log(ax.LogLevelDebug,   'message');

ax.log(ax.LogLevelInfo,    'message');

ax.log(ax.LogLevelWarning, 'message');

ax.log(ax.LogLevelError,   'message');

You can identify the location of the debug logs in the Events tab of the Scan page by expanding the Scan Job Completed event (the logs are in logfile.csv inside the zip file).

For this example, you would find something like the following:

IMPORTANT:

The pre-request script must include two mandatory directives at the beginning of the file:

  • Directive /// url: <path> specifies the URLs the script applies to; in this example, "/" refers to the target's root and all subpaths.
  • Directive /// script_type: prerequestscript identifies the file as a pre-request script; without this directive, it won't be recognized as such.

 

« Back to the Acunetix Support Page