如何使用OWASP ZAP的spiderViewStatus Java API来获取Spider完成工作的状态/百分比?

huangapple go评论112阅读模式
英文:

How to use the spiderViewStatus Java API of OWASP ZAP to get the status/percentage of work done by the Spider?

问题

以下是您提供的内容的翻译:

  1. 我正在遵循 [使用 Spider](https://www.zaproxy.org/docs/api/#using-spider) 的 API 文档。基于 Java 的代码块效果很好,我获得了输出。
  2. - 代码:
  3. import java.util.List;
  4. import org.zaproxy.clientapi.core.ApiResponse;
  5. import org.zaproxy.clientapi.core.ApiResponseElement;
  6. import org.zaproxy.clientapi.core.ApiResponseList;
  7. import org.zaproxy.clientapi.core.ClientApi;
  8. public class SpiderViewStatus {
  9. private static final String ZAP_ADDRESS = "localhost";
  10. private static final int ZAP_PORT = 8080;
  11. // 根据 ZAP 中设置的 API 密钥进行更改,如果 API 密钥已禁用,则使用 NULL
  12. private static final String ZAP_API_KEY = "93tpvc1c5ek2b94arh0e7c8he";
  13. // 要测试的应用程序的 URL
  14. private static final String TARGET = "https://public-firing-range.appspot.com";
  15. //private static final String TARGET = "http://localhost:3000"; //Juice Shop
  16. public static void main(String[] args) {
  17. ClientApi api = new ClientApi(ZAP_ADDRESS, ZAP_PORT, ZAP_API_KEY);
  18. try {
  19. // 开始对目标进行爬行
  20. System.out.println("Spidering target : " + TARGET);
  21. ApiResponse resp = api.spider.scan(TARGET, null, null, null, null);
  22. String scanID;
  23. int progress;
  24. // 扫描返回一个扫描 ID 以支持并发扫描
  25. scanID = ((ApiResponseElement) resp).getValue();
  26. // 轮询状态直到完成
  27. while (true) {
  28. Thread.sleep(1000);
  29. progress = Integer.parseInt(((ApiResponseElement) api.spider.status(scanID)).getValue());
  30. System.out.println("Spider progress : " + progress + "%");
  31. if (progress >= 100) {
  32. break;
  33. }
  34. }
  35. System.out.println("Spider completed");
  36. // 如有必要,对爬虫结果进行后处理
  37. List<ApiResponse> spiderResults = ((ApiResponseList)
  38. api.spider.results(scanID)).getItems();
  39. for (ApiResponse spiderResult : spiderResults)
  40. System.out.println(spiderResult);
  41. // TODO: 使用 Ajax Spider 进一步探索应用程序,或开始扫描应用程序以查找漏洞
  42. } catch (Exception e) {
  43. System.out.println("Exception : " + e.getMessage());
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. - 输出:
  49. Spidering target : https://public-firing-range.appspot.com
  50. Spider progress : 0%
  51. Spider progress : 66%
  52. Spider progress : 100%
  53. Spider completed
  54. https://public-firing-range.appspot.com/sitemap.xml
  55. https://public-firing-range.appspot.com/robots.txt
  56. https://public-firing-range.appspot.com
  57. 在“查看状态”部分中,还提到要执行 [status API](https://www.zaproxy.org/docs/api/#spiderviewstatus) 来获取 Spider 完成的工作状态/百分比。然而,当我附加 [spiderViewStatus](https://www.zaproxy.org/docs/api/#spiderviewstatus) 的代码块:
  58. - 代码块:
  59. System.out.println("Spider completed");
  60. // 如有必要,对爬虫结果进行后处理
  61. //spiderViewStatus: https://www.zaproxy.org/docs/api/#spiderviewstatus
  62. URL obj = new URL("http://zap/JSON/spider/view/status/");
  63. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  64. con.setRequestMethod("GET");
  65. int responseCode = con.getResponseCode();
  66. BufferedReader in = new BufferedReader(
  67. new InputStreamReader(con.getInputStream()));
  68. String inputLine;
  69. StringBuffer response = new StringBuffer();
  70. while ((inputLine = in.readLine()) != null) {
  71. response.append(inputLine);
  72. }
  73. in.close();
  74. System.out.println(response.toString());
  75. // TODO: 使用 Ajax Spider 进一步探索应用程序,或开始扫描应用程序以查找漏洞
  76. 我遇到了 `java.net.UnknownHostException: zap` 错误,如下所示:
  77. - 错误堆栈跟踪:
  78. Spidering target : https://public-firing-range.appspot.com
  79. Spider progress : 66%
  80. Spider progress : 100%
  81. Spider completed
  82. Exception : zap
  83. java.net.UnknownHostException: zap
  84. at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
  85. at java.net.PlainSocketImpl.connect(Unknown Source)
  86. at java.net.SocksSocketImpl.connect(Unknown Source)
  87. at java.net.Socket.connect(Unknown Source)
  88. at java.net.Socket.connect(Unknown Source)
  89. at sun.net.NetworkClient.doConnect(Unknown Source)
  90. at sun.net.www.http.HttpClient.openServer(Unknown Source)
  91. at sun.net.www.http.HttpClient.openServer(Unknown Source)
  92. at sun.net.www.http.HttpClient.<init>(Unknown Source)
  93. at sun.net.www.http.HttpClient.New(Unknown Source)
  94. at sun.net.www.http.HttpClient.New(Unknown Source)
  95. at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
  96. at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
  97. at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
  98. at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
  99. at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
  100. at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
  101. at java.net.HttpURLConnection.getResponseCode(Unknown Source)
  102. at ZAP_tests.SpiderViewStatus.main(SpiderViewStatus.java:52)
  103. 我已尝试将 `http://zap/JSON/spider/view/status/` 替换为 `http://localhost:8080/JSON/spider/view/status/`,但仍然出现相同的错误。
  104. 有谁可以帮帮我吗?
英文:

I was following the API documentation of Using Spider. The Java based code block works great and I get an output.

  • Code:

    1. import java.util.List;
    2. import org.zaproxy.clientapi.core.ApiResponse;
    3. import org.zaproxy.clientapi.core.ApiResponseElement;
    4. import org.zaproxy.clientapi.core.ApiResponseList;
    5. import org.zaproxy.clientapi.core.ClientApi;
    6. public class SpiderViewStatus {
    7. private static final String ZAP_ADDRESS = &quot;localhost&quot;;
    8. private static final int ZAP_PORT = 8080;
    9. // Change to match the API key set in ZAP, or use NULL if the API key is disabled
    10. private static final String ZAP_API_KEY = &quot;93tpvc1c5ek2b94arh0e7c8he&quot;;
    11. // The URL of the application to be tested
    12. private static final String TARGET = &quot;https://public-firing-range.appspot.com&quot;;
    13. //private static final String TARGET = &quot;http://localhost:3000&quot;; //Juice Shop
    14. public static void main(String[] args) {
    15. ClientApi api = new ClientApi(ZAP_ADDRESS, ZAP_PORT, ZAP_API_KEY);
    16. try {
    17. // Start spidering the target
    18. System.out.println(&quot;Spidering target : &quot; + TARGET);
    19. ApiResponse resp = api.spider.scan(TARGET, null, null, null, null);
    20. String scanID;
    21. int progress;
    22. // The scan returns a scan id to support concurrent scanning
    23. scanID = ((ApiResponseElement) resp).getValue();
    24. // Poll the status until it completes
    25. while (true) {
    26. Thread.sleep(1000);
    27. progress = Integer.parseInt(((ApiResponseElement) api.spider.status(scanID)).getValue());
    28. System.out.println(&quot;Spider progress : &quot; + progress + &quot;%&quot;);
    29. if (progress &gt;= 100) {
    30. break;
    31. }
    32. }
    33. System.out.println(&quot;Spider completed&quot;);
    34. // If required post process the spider results
    35. List&lt;ApiResponse&gt; spiderResults = ((ApiResponseList)
    36. api.spider.results(scanID)).getItems(); for (ApiResponse
    37. spiderResult:spiderResults) System.out.println(spiderResult);
    38. // TODO: Explore the Application more with Ajax Spider or Start scanning the application for vulnerabilities
    39. } catch (Exception e) {
    40. System.out.println(&quot;Exception : &quot; + e.getMessage());
    41. e.printStackTrace();
    42. }
    43. }
    44. }
  • Output:

    1. Spidering target : https://public-firing-range.appspot.com
    2. Spider progress : 0%
    3. Spider progress : 66%
    4. Spider progress : 100%
    5. Spider completed
    6. https://public-firing-range.appspot.com/sitemap.xml
    7. https://public-firing-range.appspot.com/robots.txt
    8. https://public-firing-range.appspot.com

Within the View Status section it is also mentions to execute the status API to get the status/percentage of work done by the Spider. However when I append the code block of spiderViewStatus :

  • Code Block:

    1. System.out.println(&quot;Spider completed&quot;);
    2. // If required post process the spider results
    3. //spiderViewStatus: https://www.zaproxy.org/docs/api/#spiderviewstatus
    4. URL obj = new URL(&quot;http://zap/JSON/spider/view/status/&quot;);
    5. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    6. con.setRequestMethod(&quot;GET&quot;);
    7. int responseCode = con.getResponseCode();
    8. BufferedReader in = new BufferedReader(
    9. new InputStreamReader(con.getInputStream()));
    10. String inputLine;
    11. StringBuffer response = new StringBuffer();
    12. while ((inputLine = in.readLine()) != null) {
    13. response.append(inputLine);
    14. }
    15. in.close();
    16. System.out.println(response.toString());
    17. // TODO: Explore the Application more with Ajax Spider or Start scanning the application for vulnerabilities

I am facing java.net.UnknownHostException: zap as follows:

  • Error stacktrace:

    1. Spidering target : https://public-firing-range.appspot.com
    2. Spider progress : 66%
    3. Spider progress : 100%
    4. Spider completed
    5. Exception : zap
    6. java.net.UnknownHostException: zap
    7. at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    8. at java.net.PlainSocketImpl.connect(Unknown Source)
    9. at java.net.SocksSocketImpl.connect(Unknown Source)
    10. at java.net.Socket.connect(Unknown Source)
    11. at java.net.Socket.connect(Unknown Source)
    12. at sun.net.NetworkClient.doConnect(Unknown Source)
    13. at sun.net.www.http.HttpClient.openServer(Unknown Source)
    14. at sun.net.www.http.HttpClient.openServer(Unknown Source)
    15. at sun.net.www.http.HttpClient.&lt;init&gt;(Unknown Source)
    16. at sun.net.www.http.HttpClient.New(Unknown Source)
    17. at sun.net.www.http.HttpClient.New(Unknown Source)
    18. at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    19. at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    20. at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    21. at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    22. at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    23. at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    24. at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    25. at ZAP_tests.SpiderViewStatus.main(SpiderViewStatus.java:52)

I have tried to replace http://zap/JSON/spider/view/status/ with http://localhost:8080/JSON/spider/view/status/ still the same error.

Can anyone help me out please?

答案1

得分: 1

你已经在初始代码中使用api.spider.status(scanID)调用了那个端点。

http://zap/主机只在你通过ZAP代理时才起作用,而在你的第二段代码中似乎没有这样做。

英文:

You are already calling that endpoint in your initial code using api.spider.status(scanID)

The http://zap/ host only works if you are proxying through ZAP, which you don't appear to be in your second section of code.

huangapple
  • 本文由 发表于 2020年5月19日 21:13:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61891946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定