Java 使用OWASP Zap 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.

我正在尝试使用脚本扫描目标并执行活动扫描作为概念证明。我已经完成了下面的实现,我无法让它工作我不知道为什么它不会工作?我已经运行了Zap2Docker,可以通过api访问它,我也可以通过gui访问,从gui扫描目标工作正常,但是我的脚本无法在api上工作,请参见下面的内容:

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
    

感谢您抽出时间来了解这一点,我尝试了这一点,因为我试图消除任何可能的问题,我已经完成了这两项工作,配置API密钥并传递它,禁用API密钥并传递NULL,但是,我仍然在两个场合收到相同的错误。在这种情况下,请查看zap.log文件-这应该会给出更详细的信息。您好,我可以看出您有很多经验,我将尝试查看日志文件,但是从脚本看,您是否可以看到进程和API调用有任何明显错误?这行代码:SocketException:来自服务器的意外文件结尾似乎让我感到困惑。非常感谢你的帮助,西蒙。谢谢,我是ZAP项目负责人:P“意外的文件结尾”几乎总是一个配置问题-ZAP会默默地拒绝您的请求,因为它认为这是不允许的。调试这个的最简单方法是查看zap.log文件-如果这发生在我身上,我会这么做。zap Royalty!,感谢yes配置错误端点未配置为允许我从Ip访问。我很确定这就是解决问题的原因。非常感谢你的帮助。现在只需确保所有其他配置都是正确的。如果你知道的话,可以说是一个检查表,列出需要在zap端配置什么来扫描你的QWASP果汁店,这将是上帝派来的?i、 电子认证、会话管理?如果不是,我肯定会找到办法的。谢谢你,西蒙。