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:\>