forked from zaproxy/community-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowDifferences.js
85 lines (72 loc) · 2.36 KB
/
showDifferences.js
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
//A Fuzzer HTTP Processor script that compares the original Response with the fuzzed Response
//and add the result to the state column!
//To remove all other states from the state column set the variable `removeOtherStatesFromStateColumn` to `true`.
//This might be useful if you want to order the column.
//Script needs Diff add-on
var DiffTool = Java.type("org.zaproxy.zap.extension.diff.diff_match_patch");
var key = "script.showDifferences.js";
var showResultInTable = true;
var removeOtherStatesFromStateColumn = false;
var original = null;
function processMessage(utils, message) {
return message;
}
// Called after receiving the fuzzed message from the server
function processResult(utils, fuzzResult){
if(!original){
original = responseAsString(utils.getOriginalMessage());
}
var fuzzed = responseAsString(fuzzResult.getHttpMessage());
var diffList = createDiff(original, fuzzed);
var aggregatedDiff = aggregateDiff(diffList);
displayToStateColumn(fuzzResult, aggregatedDiff);
return showResultInTable;
}
function responseAsString(httpMessage){
var responseHeader = httpMessage.getResponseHeader().toString();
var responseBody = httpMessage.getResponseBody().toString();
return responseHeader + "\r\n" + responseBody;
}
function createDiff(original, fuzzed){
var diffTool = new DiffTool();
return diffTool.diff_main(original, fuzzed);
}
function displayToStateColumn(fuzzResult, aggregatedDiff){
if(removeOtherStatesFromStateColumn){
removeAllStates(fuzzResult);
}
fuzzResult.addCustomState(key, "Sum: "+padLeft(aggregatedDiff.Sum) + "; Delta:" + aggregatedDiff.Delta);
}
function removeAllStates(fuzzResult){
for each (var key in fuzzResult.getCustomStates().keySet() ) {
fuzzResult.removeCustomState(key);
}
}
function padLeft(value){
var str = value + "";
var pad = "00000000";
return pad.substring(0, pad.length - str.length) + str;
}
function aggregateDiff(diffList){
var sum = 0;
var delta = "";
for each (var diff in diffList) {
if(diff.operation == "INSERT"){
sum += diff.text.length();
delta += "++|" + prepareDiffText(diff.text) + "|";
}
else if(diff.operation == "DELETE"){
sum += diff.text.length();
delta += "--|" + prepareDiffText(diff.text) + "|";
}
}
return {
Sum : sum,
Delta : delta
}
}
function prepareDiffText(text){
text = text.replace("\r", "\\r");
text = text.replace("\n", "\\n");
return text
}