Skip to content

Commit b0d7c8c

Browse files
authored
Merge pull request #276 from psiinon/log-requests
Add scripts to help debugging in docker
2 parents 291d7e1 + a018cef commit b0d7c8c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1313
- README.md - Summary of the script type.
1414
- double-spacer.js - A script that inserts a space after every character in a string.
1515
- standalone/SecurityCrawlMazeScore.js
16+
- scan-hooks/LogMessagesHook.py and httpsender/LogMessages.js to help debugging, especially in docker.
1617

1718
### Changed
1819
- standalone/enableDebugLogging.js > Updated for more recent logging funtionality.

httpsender/LogMessages.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// This script appends the full request and response details to a specified file.
2+
// By default it will print out all messages but you can edit it to only print out the ones
3+
// that you are interested in.
4+
// It is a good option when trying to debug issues encountered when running ZAP in automation.
5+
//
6+
// The sendingRequest and responseReceived functions will be called for all requests/responses sent/received by ZAP,
7+
// including automated tools (e.g. active scanner, fuzzer, ...)
8+
9+
// To use this script in the Docker packaged scans use the scan-hook LogRequestsHook.py
10+
// This script can be used outside of docker but if so change the /zap/wrk/ directory to be a valid local directory.
11+
12+
// 'initiator' is the component the initiated the request:
13+
// 1 PROXY_INITIATOR
14+
// 2 ACTIVE_SCANNER_INITIATOR
15+
// 3 SPIDER_INITIATOR
16+
// 4 FUZZER_INITIATOR
17+
// 5 AUTHENTICATION_INITIATOR
18+
// 6 MANUAL_REQUEST_INITIATOR
19+
// 7 CHECK_FOR_UPDATES_INITIATOR
20+
// 8 BEAN_SHELL_INITIATOR
21+
// 9 ACCESS_CONTROL_SCANNER_INITIATOR
22+
// 10 AJAX_SPIDER_INITIATOR
23+
// For the latest list of values see the HttpSender class:
24+
// https://github.com/zaproxy/zaproxy/blob/main/zap/src/main/java/org/parosproxy/paros/network/HttpSender.java
25+
// 'helper' just has one method at the moment: helper.getHttpSender() which returns the HttpSender
26+
// instance used to send the request.
27+
28+
var SEP = '\n ---------------------------------';
29+
var Files = Java.type('java.nio.file.Files');
30+
var Paths = Java.type('java.nio.file.Paths');
31+
var StandardOpenOption = Java.type('java.nio.file.StandardOpenOption');
32+
33+
// Change this as required - this works well in Docker as long as a suitable local directory has been mapped to it
34+
var f = Paths.get('/zap/wrk/req-resp-log.txt');
35+
36+
function appendToFile(str) {
37+
Files.write(f, str.toString().getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
38+
}
39+
40+
function sendingRequest(msg, initiator, helper) {
41+
// You can change this to print out just the requests you want e.g. by surounding with an 'if' statement like:
42+
// if (msg.getRequestHeader().getURI().toString().startsWith('http://www.example.com'))
43+
// or
44+
// if (initiator == 5)
45+
46+
// Print everything on one line so that threads dont mix the output
47+
appendToFile(SEP + 'ZAP Request Init=' + initiator + '\n' +
48+
msg.getRequestHeader().toString() +
49+
SEP + 'ZAP Request Body\n' +
50+
msg.getRequestBody().toString() +
51+
SEP + 'ZAP Request End');
52+
}
53+
54+
function responseReceived(msg, initiator, helper) {
55+
// Print everything on one line so that threads dont mix the output
56+
appendToFile(SEP + 'ZAP Response Init=' + initiator + '\n' +
57+
msg.getResponseHeader().toString() +
58+
SEP + 'ZAP Response Body\n' +
59+
msg.getResponseBody().toString() +
60+
SEP + 'ZAP Response End');
61+
}

scan-hooks/LogMessagesHook.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# A scan hook (https://www.zaproxy.org/docs/docker/scan-hooks/) which adds a script for logging all requests.
2+
# To use this script copy it and the httpsender/LogRequests.js script to your CWD.
3+
# Then run ZAP like this:
4+
# docker run -v $(pwd):/zap/wrk/:rw -t owasp/zap2docker-stable zap-baseline.py -t https://www.example.com --hook=LogMessagesHook.py
5+
# The requests and responses should be written to a req-resp-log.txt file in the CWD.
6+
7+
def zap_started(zap, target):
8+
zap.script.load('LogMessages.js', 'httpsender', 'Oracle Nashorn', '/zap/wrk/LogMessages.js')
9+
zap.script.enable('LogMessages.js')

0 commit comments

Comments
 (0)