Acunetix 安装:使用 Python 进行 API 调用
除了几个现成的集成之外,Acunetix还提供了一个API,使您可以将扫描仪与任何环境集成在一起。Acunetix API允许您使用任何扫描仪功能,而无需访问扫描仪UI。作为示例,我们将向您展示如何使用Python调用Acunetix API。
我们将讨论两个使用Python进行API调用的选项:
- 选项1:使用Swagger客户端进行API调用
- 选项2:使用基本HTTP进行API调用
出于本文的目的,我们还将假定您在Windows 64位环境中工作。
选项1:使用Swagger客户端进行API调用
对于此选项,我们将执行必要的步骤,以使用Python和Swagger Client进行首次Acunetix API调用:
1.安装Python
- 单击此处下载适用于Python 3.8.4的Windows 64位可执行文件安装程序,如果需要,]也可以浏览其他版本。
- 通过双击安装程序来安装Python:
- 确保启用“ 将Python添加到PATH”复选框。
- 单击立即安装按钮。
- 安装程序完成后,单击“ 关闭”按钮。
2.安装JAVA JDK
单击此处,下载用于JAVA SE JDK 8的Windows 64位安装程序。
通过双击安装程序来安装JAVA JDK:
- 在安装程序中不需要配置任何选项,因此您可以简单地接受所有默认设置并继续进行安装。
- 安装程序完成后,单击“ 关闭”按钮。
在管理员命令提示符下,运行以下命令来设置
JAVA_HOME
环境变量:setx JAVA_HOME -m "c:\Program Files\Java\jdk1.8.0_261"
3.安装Git
- 单击此处下载适用于Git 2.27.0的Windows 64位安装程序,或查找其他发行版。
- 通过双击安装程序来安装Git:
- 在安装程序中不需要配置任何选项,因此您可以简单地接受所有默认设置并继续进行安装。
- 安装程序完成后,单击完成按钮。
4.安装Swagger Codegen
在常规命令提示符下,移至根文件夹,克隆swagger-codegen存储库,然后移至存储库文件夹:
cd c: git clone https://github.com/swagger-api/swagger-codegen.git cd c:\swagger-codegen
构建swagger-codegen工具:
从文件夹中运行以下命令:
mvnw package
成功完成后,结果应类似于以下内容:
[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] swagger-codegen-project ............................ SUCCESS [ 43.643 s] [INFO] swagger-codegen (core library) ..................... SUCCESS [03:28 min] [INFO] swagger-codegen (executable) ....................... SUCCESS [ 27.892 s] [INFO] swagger-codegen (maven-plugin) ..................... SUCCESS [ 56.018 s] [INFO] swagger-generator .................................. SUCCESS [02:43 min] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 08:30 min [INFO] Finished at: 2020-07-17T16:06:07+01:00 [INFO] Final Memory: 62M/629M [INFO] ------------------------------------------------------------------------
5.生成用于Python的Acunetix API库
为swagger- **codegen创建acunetix子文件夹:
md c:\swagger-codegen\acunetix cd c:\swagger-codegen\acunetix
将Acunetix提供的swagger.yaml文件复制到c:\ swagger-codegen \ acunetix文件夹中。
使用以下命令创建批处理文件:
5555notepad c:\swagger-codegen\acunetix\py-ax.bat
将以下内容插入py-ax.bat文件:
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar set params=generate -i acunetix\swagger.yaml -l python -o acunetix\python -DpackageName=swagger_client java %JAVA_OPTS% -jar %executable% %params%
退出记事本,保存py-ax.bat文件。
使用以下命令生成库:
cd c:\swagger-codegen acunetix\py-ax.bat
这将在c:\ swagger-codegen \ acunetix \ python内部生成库。
使用以下命令安装库:
cd c:\swagger-codegen\acunetix\python pip install .
6.创建Python脚本以进行API调用
创建您的Python脚本
创建新的脚本文件:
notepad c:\swagger-codegen\acunetix\apitest.py
将以下命令插入到apitest.py文件中:
import swagger_client from swagger_client.rest import ApiException from pprint import pprint api_config = swagger_client.Configuration() api_config.ssl_ca_cert = 'C:\ProgramData\Acunetix\certs\ca.cer' api_config.host = 'https://localhost:3443/api/v1' api_config.api_key['X-Auth'] = '1986abcd1986abcd1986abcd1986abcd1986abcd1986abcd1986abcd1986abcd' api_client = swagger_client.ApiClient(configuration=api_config) api_instance = swagger_client.TargetsApi(api_client=api_client) print(api_instance) try: # Targets # api_response = api_instance.get_targets(c=c, l=l, q=q, s=s) api_response = api_instance.get_targets() pprint(api_response) except ApiException as e: print("Exception when calling TargetsApi->getTargets: %s\n" % e)
请记住,您需要用自己的Acunetix安装中的API密钥替换API密钥。
运行您的API测试脚本:
cd c:\swagger-codegen\acunetix python apitest.py
查看结果
成功执行后,脚本的结果将类似于以下内容:
c:\swagger-codegen\acunetix>apitest.py
{'pagination': {'count': 3,
'cursor_hash': '8f629dd49f910b9202eb0da5d51fdb6e',
'cursors': [None],
'sort': None},
'targets': [{'continuous_mode': False,
'last_scan_date': datetime.date(2020, 7, 10),
'last_scan_id': 'ec27a320-2351-4b27-b6dd-34313dff9c1f',
'last_scan_session_id': 'bb3ad61a-2615-47ac-bcfb-4cb269521746',
'last_scan_session_status': 'completed',
'links': None,
'manual_intervention': False,
'scan_authorization': None,
'severity_counts': None,
'target_id': 'e7260534-cfb7-4d30-bc18-a2ed0b0eead8',
'threat': None,
'verification': None},
{'continuous_mode': False,
'last_scan_date': None,
'last_scan_id': None,
'last_scan_session_id': None,
'last_scan_session_status': None,
'links': None,
'manual_intervention': None,
'scan_authorization': None,
'severity_counts': None,
'target_id': '94c1fa22-8f0c-471e-9737-c199e57ad1fa',
'threat': None,
'verification': 'demo'},
{'continuous_mode': False,
'last_scan_date': None,
'last_scan_id': None,
'last_scan_session_id': None,
'last_scan_session_status': None,
'links': None,
'manual_intervention': None,
'scan_authorization': None,
'severity_counts': None,
'target_id': 'a67df446-dda1-4715-91f5-471add517fea',
'threat': None,
'verification': None}]}
c:\swagger-codegen\acunetix>
选项2:使用基本HTTP进行API调用
安装Python
使用Python脚本进行API调用
安装Python必备软件包
安装Python 请求包:
python -m pip install requests
创建您的Python脚本
创建新的脚本文件:
md c:\acunetixapi notepad c:\acunetixapi\apitest.py
将以下命令插入到apitest.py文件中:
# importing libraries import json, requests, urllib3 # api-endpoint URL = "https://localhost:3443/api/v1/targets" # defining a params dict for the parameters to be sent to the API #PARAMS = {'c':'', 'l':'', 's':''} PARAMS = {} #defining the headers to send headers = {'content-type': 'application/json', 'X-Auth':'1986abcd1986abcd1986abcd1986abcd1986abcd1986abcd1986abcd1986abcd'} #create connection pool pool = urllib3.PoolManager(cert_reqs='REQUIRED', assert_hostname='localhost',) #send request resp = pool.request('GET', URL, '', headers) #decode response data = json.loads(resp.data.decode('utf-8')) #dump decoded response print(data)
请记住,您需要用自己的Acunetix安装中的API密钥替换API密钥。
运行您的API测试脚本:
c:\acunetixapi\apitest.py
查看结果
成功执行后,脚本的结果将类似于以下内容:
c:\>acunetixapi\apitest.py
{'targets': [{'address': 'https://mytesting.net', 'continuous_mode': False, 'criticality': 10, 'deleted_at': None, 'description': 'Test OAuth2 Site', 'last_scan_date': '2020-07-10T12:02:58.153112+00:00', 'last_scan_id': 'ec27a320-2351-4b27-b6dd-34313dff9c1f', 'last_scan_session_id': 'bb3ad61a-2615-47ac-bcfb-4cb269521746', 'last_scan_session_status': 'completed', 'manual_intervention': False, 'severity_counts': None, 'target_id': 'e7260534-cfb7-4d30-bc18-a2ed0b0eead8', 'threat': None, 'type': None, 'verification': None}, {'address': 'http://testphp.vulnweb.com', 'continuous_mode': False, 'criticality': 10, 'deleted_at': None, 'description': 'test2', 'last_scan_date': None, 'last_scan_id': None, 'last_scan_session_id': None, 'last_scan_session_status': None, 'manual_intervention': None, 'severity_counts': None, 'target_id': '94c1fa22-8f0c-471e-9737-c199e57ad1fa', 'threat': None, 'type': 'demo', 'verification': 'demo'}, {'address': 'https://mytesting.net', 'continuous_mode': False, 'criticality': 10, 'deleted_at': None, 'description': 'Testing LSR only', 'last_scan_date': None, 'last_scan_id': None, 'last_scan_session_id': None, 'last_scan_session_status': None, 'manual_intervention': None, 'severity_counts': None, 'target_id': 'a67df446-dda1-4715-91f5-471add517fea', 'threat': None, 'type': None, 'verification': None}], 'pagination': {'count': 3, 'cursor_hash': '8f629dd49f910b9202eb0da5d51fdb6e', 'cursors': [None], 'sort': None}}
c:\>
