forked from zaproxy/community-scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgreenbone-maintain-auth.js
More file actions
141 lines (113 loc) · 3.57 KB
/
Copy pathgreenbone-maintain-auth.js
File metadata and controls
141 lines (113 loc) · 3.57 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*exported sendingRequest, responseReceived*/
// Greenbone auth & sessions are a bit flaky with scans ...
// ... this helps make auth less flaky
// Logging with the script name is super helpful!
function logger() {
print("[" + this["zap.script.name"] + "] " + arguments[0]);
}
function isStaticUrl(url) {
if (url.indexOf(".xml") !== -1) {
return true;
}
if (url.indexOf(".css") !== -1) {
return true;
}
if (url.indexOf(".gif") !== -1) {
return true;
}
if (url.indexOf(".js") !== -1) {
return true;
}
if (url.indexOf(".txt") !== -1) {
return true;
}
if (url.indexOf(".htm") !== -1) {
return true;
}
return false;
}
var HtmlParameterType = Java.type(
"org.parosproxy.paros.network.HtmlParameter.Type"
);
var COOKIE_TYPE = HtmlParameterType.cookie;
var ScriptVars = Java.type("org.zaproxy.zap.extension.script.ScriptVars");
var HtmlParameter = Java.type("org.parosproxy.paros.network.HtmlParameter");
var HttpSender = Java.type("org.parosproxy.paros.network.HttpSender");
// Rewrite requests to include correct query token param
function sendingRequest(msg, initiator, helper) {
var reqbody = msg.getRequestBody().toString();
var headers = msg.getRequestHeader();
var url = headers.getURI().toString();
var qry = headers.getURI().getQuery();
var cookies = headers.getCookieParams();
if (initiator === HttpSender.SPIDER_INITIATOR) {
}
if (isStaticUrl(url)) {
return;
}
var token = ScriptVars.getGlobalVar("openvas.token");
var gsad_id = ScriptVars.getGlobalVar("openvas.gsad_id");
if (gsad_id === null || gsad_id === "0" || gsad_id == 0) {
logger("No valid gsad_id");
return;
}
if (token === null) {
return;
}
// Already logged in, so move on
if (
(headers.getMethod() === "POST" && reqbody.indexOf("cmd=login") !== -1) ||
url.indexOf("login") !== -1
) {
return;
}
var cookieParam = new HtmlParameter(COOKIE_TYPE, "GSAD_SID", gsad_id);
// https://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/URI.html
if (qry !== null && qry.toString().indexOf(token) !== -1) {
logger("Already has token, no need to rewrite");
return;
}
// If already a cookie, remove to reset
if (!cookies.isEmpty()) {
var existing = cookies.first();
cookies.remove(existing);
}
cookies.add(cookieParam);
msg.getRequestHeader().setCookieParams(cookies);
var newqry = "token=" + token;
if (qry !== null) {
newqry = qry.replace(
/[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}/,
token
);
}
// @todo add token to post data
headers.getURI().setQuery(newqry);
}
// Monitor responses to look for successful login to update session info
function responseReceived(msg, initiator, helper) {
var reqbody = msg.getRequestBody().toString();
var resbody = msg.getResponseBody().toString();
var headers = msg.getRequestHeader();
var resheaders = msg.getResponseHeader();
// Login is only via POST
if (headers.getMethod() !== "POST") {
return;
}
// Login has specific items in post body
if (reqbody.indexOf("cmd=login") === -1) {
return;
}
var cookie = resheaders.getHeader("Set-Cookie").toString();
var gsad_id = cookie.split(";")[0].split("=")[1];
var tokenIdx = resbody.indexOf("&token=");
var token = resbody.substring(tokenIdx + 7, tokenIdx + 43);
// Ignore bad session id
if (gsad_id == "0") {
return;
}
ScriptVars.setGlobalVar("openvas.token", token);
ScriptVars.setGlobalVar("openvas.gsad_id", gsad_id);
logger("New greenbone session tokens " + token + " - " + gsad_id);
// @todo set active session
}