Java 使用OWASP Zap Api进行扫描
我正在尝试使用脚本扫描目标并执行活动扫描作为概念证明。我已经完成了下面的实现,我无法让它工作我不知道为什么它不会工作?我已经运行了Zap2Docker,可以通过api访问它,我也可以通过gui访问,从gui扫描目标工作正常,但是我的脚本无法在api上工作,请参见下面的内容:Java 使用OWASP Zap Api进行扫描,java,owasp,zap,Java,Owasp,Zap,我正在尝试使用脚本扫描目标并执行活动扫描作为概念证明。我已经完成了下面的实现,我无法让它工作我不知道为什么它不会工作?我已经运行了Zap2Docker,可以通过api访问它,我也可以通过gui访问,从gui扫描目标工作正常,但是我的脚本无法在api上工作,请参见下面的内容: import org.zaproxy.clientapi.core.ApiResponse; import org.zaproxy.clientapi.core.ApiResponseElement; import org.
import org.zaproxy.clientapi.core.ApiResponse;
import org.zaproxy.clientapi.core.ApiResponseElement;
import org.zaproxy.clientapi.core.ApiResponseList;
import org.zaproxy.clientapi.core.ClientApi;
import java.util.List;
public class Spider {
private static String ZAP_ADDRESS;// = "ZAPContainerIp";
private static int ZAP_PORT;// = 8090;
// Change to match the API key set in ZAP, or use NULL if the API key is disabled
private static String ZAP_API_KEY;// = "change me";
// The URL of the application to be tested
private static String TARGET;// = "https://targetip.com";
private static boolean scanComplete;
public static void main(String[] args) {
ZAP_ADDRESS = args[0];
ZAP_PORT = Integer.parseInt(args[1]);
ZAP_API_KEY = args[2];
TARGET = args[3];
ClientApi api = new ClientApi(ZAP_ADDRESS, ZAP_PORT, ZAP_API_KEY);
try {
// Start spidering
System.out.println("Spidering target : " + TARGET);
ApiResponse resp = api.spider.scan(TARGET, null, null, null, null);
String scanID;
int progress;
// The scan returns a scan id to support concurrent scanning
scanID = ((ApiResponseElement) resp).getValue();
// Poll the status until it completes
while (true) {
Thread.sleep(1000);
progress = Integer.parseInt(((ApiResponseElement) api.spider.status(scanID)).getValue());
System.out.println("Spider progress : " + progress + "%");
if (progress >= 100) {
scanComplete = true;
break;
}
}
System.out.println("Spider completed");
// If required post process the spider results
List<ApiResponse> spiderResults = ((ApiResponseList) api.spider.results(scanID)).getItems();
if (scanComplete) {
ActiveScan activeScan = new ActiveScan();
activeScan.attack(ZAP_ADDRESS, ZAP_PORT, ZAP_API_KEY, TARGET);
}
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
e.printStackTrace();
}
}
}
运行时,我得到错误信息:
java -jar WafTestSuite.jar "zapurl" "8090" "change-me-9203935709" "10.10.10.254:3000"; Spidering target : 10.10.8.254:3000
Exception : java.net.SocketException: Unexpected end of file from server
org.zaproxy.clientapi.core.ClientApiException: java.net.SocketException: Unexpected end of file from server
at org.zaproxy.clientapi.core.ClientApi.callApiDom(ClientApi.http://java:366)
at org.zaproxy.clientapi.core.ClientApi.callApi(ClientApi.http://java:350)
at org.zaproxy.clientapi.gen.Spider.scan(Spider.http://java:242)
at Spider.main(Spider.java:28)
Caused by: java.net.SocketException: Unexpected end of file from server
at sun.net.http://www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.http://www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.http://www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.http://www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.http://www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.http://www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at org.zaproxy.clientapi.core.ClientApi.getConnectionInputStream(ClientApi.http://java:399)
at org.zaproxy.clientapi.core.ClientApi.callApiDom(ClientApi.http://java:364)
非常感谢您的帮助。默认情况下,ZAP不接受与API的远程连接。您需要启用它们并设置合适的API密钥(或禁用它)。此常见问题解答中的更多详细信息:此错误消息
java -jar WafTestSuite.jar "zapurl" "8090" "change-me-9203935709" "10.10.10.254:3000"; Spidering target : 10.10.8.254:3000
Exception : java.net.SocketException: Unexpected end of file from server
org.zaproxy.clientapi.core.ClientApiException: java.net.SocketException: Unexpected end of file from server
at org.zaproxy.clientapi.core.ClientApi.callApiDom(ClientApi.http://java:366)
…表示远程服务器在不发送响应的情况下接受并关闭了连接
此错误背后可能有许多原因,其中一些原因如下:
- 可能远程系统太忙,无法处理请求,因此会断开连接
- 请求中可能缺少/无效的头值,这会导致内部错误,服务器会关闭连接,而不是正常发送HTTP错误响应
- 可能存在网络延迟,因此应用程序会随机断开连接
ZAP\u API\u KEY
初始化为字符串,但没有分配任何值。因此出现了错误
因此,配置ZAP的代码块基本上是:
private static final String ZAP_ADDRESS = "localhost";
private static final int ZAP_PORT = 8080;
// Change to match the API key set in ZAP, or use NULL if the API key is disabled
private static final String ZAP_API_KEY = "abcdefghijklmnop123456789";
// The URL of the application to be tested
private static final String TARGET = "http://localhost:3000";
为什么默认情况下需要API密钥? 根据,由于ZAP版本2.4.1的可用性,默认情况下需要配置API密钥,以便调用对ZAP进行更改的API操作。此外,随着ZAP版本2.6.0的可用性,默认情况下需要API密钥才能调用任何API操作。实现此安全功能是为了防止恶意站点调用ZAP API。API安全选项,包括API密钥,可以在API选项屏幕(ZAP Proxy Interface->Tools->options->API)中找到:
更改API密钥 您可以通过以下不同方式更改API密钥:
- 通过单击Generate Random key(生成随机密钥)按钮生成新的API密钥
- 通过使用以下命令行设置API密钥:
-config api.key=change-me-9203935709
-config api.disablekey=true
- 使用以下命令从命令行禁用API密钥:
-config api.key=change-me-9203935709
-config api.disablekey=true