利用zap-api-scan.py对Rest Api 进行安全扫描

安全 安全 · systemofdown · 于 1年前 发布 · 1553 次阅读

什么是ZAP

ZAP则是OWASP(Open Web Application Security Project,开放式Web应用程序安全项目组织)里的工具类旗舰项目, 全称是OWASP Zed attack proxy,是一款web application 集成渗透测试和漏洞工具,同样是免费开源跨平台的。

ZAP的官网是: https://www.zaproxy.org , 他有客户端版本, 也有命令行版本. 今天我们这里用的是命令行版本, 进行安全扫描.

扫描思路

通常REST API都会有token之类的无状态的验证,如果直接扫描实际上是没有进入到程序中,所以规划扫描的思路:

  1. 提供API的清单, 现在无论是java还是php 使用OpenApi Spec 这个定义方式是非常主流的
  2. 设置请求的token,及相关必要信息
  3. 扫描的时候验证一下是否是正常的进入到程序中

扫描准备

工欲善其事, 必先利其器

本文中用到的zap是docker版本, 如果没有安装docker的请移步安装教程学习:

CentOS7 下 Docker 升级到最新版本

安装zap

zap docker 分了四个版本:

  • Stable 标准稳定版
  • Bare 最小化版本, 适合CI使用
  • Weekly 周更版
  • Live 最新源码版

脚本 zap-api-scan.py 包含在ZAP Docker 镜像中, 网上有很多教程说是包含在Weekly,Live版本中, 其实早就包含在了Stable版本中了.

我们就选择周更版来做示例. 下载周更版:

docker pull owasp/zap2docker-weekly

配置

token的信息有两种方式加载, 一是直接用命令行的参数中, 二是写在配置文件中使用命令加载配置文件, 由于配置比较多, 我们选择后者.

创建一个本地目录,我本机放在了/Users/mac/Sites/zapapiscan, 新建一个文件options.prop加入配置信息, 其中Bearer token换成有效的

replacer.full_list(0).description=Authorization
replacer.full_list(0).enabled=true
replacer.full_list(0).matchtype=REQ_HEADER
replacer.full_list(0).matchstr=Authorization
replacer.full_list(0).regex=false
replacer.full_list(0).replacement=Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
replacer.full_list(1).description=Accept 
replacer.full_list(1).enabled=true
replacer.full_list(1).matchtype=REQ_HEADER
replacer.full_list(1).matchstr=accept
replacer.full_list(1).regex=false
replacer.full_list(1).replacement=application/json

当发起请求的时候就会带有这两个header信息:

Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9 Accept: application/json

开始扫描

扫描的启动命令:

docker run -v /Users/mac/Sites/zapapiscan:/zap/out:rw  -v /Users/mac/Sites/zapapiscan:/zap/wrk/:rw -t owasp/zap2docker-weekly zap-api-scan.py -t /zap/wrk/api.yaml  -f openapi -z "-configfile /zap/wrk/options.prop" -r /zap/out/report.html

解释一下, 通过-v参数, 把zap docker内部的目录映射到本地/Users/mac/Sites/zapapiscan目录中, 这要就可以调用本地配置文件, 以及输出报告文件. 这一步要根据自己的实际情况配置.

  • -t 参数是接口清单的定义文件, 可以是URL也可以是本地文件(⚠️ 注意:本地文件是docker内部路径, 本机路径)
  • -f 接口清单定义文件的格式, 支持的格式有openapi, soap, 和 graphql

扫描好之后, 就会在本地中生成个测试报告report.html.

zap的参数

Usage: zap-api-scan.py -t <target> -f <format> [options]
    -t target         target API definition, OpenAPI or SOAP, local file or URL, e.g. https://www.example.com/openapi.json
                      or target endpoint URL, GraphQL, e.g. https://www.example.com/graphql
    -f format         openapi, soap, or graphql
Options:
    -h                print this help message
    -c config_file    config file to use to INFO, IGNORE or FAIL warnings
    -u config_url     URL of config file to use to INFO, IGNORE or FAIL warnings
    -g gen_file       generate default config file(all rules set to WARN)
    -r report_html    file to write the full ZAP HTML report
    -w report_md      file to write the full ZAP Wiki(Markdown) report
    -x report_xml     file to write the full ZAP XML report
    -J report_json    file to write the full ZAP JSON document
    -a                include the alpha passive scan rules as well
    -d                show debug messages
    -P                specify listen port
    -D                delay in seconds to wait for passive scanning 
    -i                default rules not in the config file to INFO
    -I                do not return failure on warning
    -l level          minimum level to show: PASS, IGNORE, INFO, WARN or FAIL, use with -s to hide example URLs
    -n context_file   context file which will be loaded prior to scanning the target
    -p progress_file  progress file which specifies issues that are being addressed
    -s                short output format - dont show PASSes or example URLs
    -S                safe mode this will skip the active scan and perform a baseline scan
    -T                max time in minutes to wait for ZAP to start and the passive scan to run
    -U user           username to use for authenticated scans - must be defined in the given context file
    -O                the hostname to override in the (remote) OpenAPI spec
    -z zap_options    ZAP command line options e.g. -z "-config aaa=bbb -config ccc=ddd"
    --hook            path to python file that define your custom hooks
    --schema          GraphQL schema location, URL or file, e.g. https://www.example.com/schema.graphqls

本文由 systemofdown 创作,采用 知识共享署名 3.0 中国大陆许可协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。

共收到 0 条回复
没有找到数据。
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册