api.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. # update: 2021-6-28-19
  2. import requests
  3. import logging
  4. import time
  5. import copy
  6. import re
  7. requests.packages.urllib3.disable_warnings()
  8. TEMPLATES = ['PCI Quarterly External Scan', 'Host Discovery', 'WannaCry Ransomware', 'Intel AMT Security Bypass',
  9. 'Basic Network Scan', 'Credentialed Patch Audit', 'Web Application Tests', 'Malware Scan',
  10. 'Mobile Device Scan', 'MDM Config Audit', 'Policy Compliance Auditing', 'Internal PCI Network Scan',
  11. 'Offline Config Audit', 'Audit Cloud Infrastructure', 'SCAP and OVAL Auditing', 'Custom Scan',
  12. 'Bash Shellshock Detection', 'GHOST (glibc) Detection', 'DROWN Detection', 'Badlock Detection',
  13. 'Shadow Brokers Scan', 'Spectre and Meltdown', 'Advanced Scan', 'Advanced Dynamic Scan']
  14. DefaultPolicyForAdvancedScan = {
  15. "uuid": "ad629e16-03b6-8c1d-cef6-ef8c9dd3c658d24bd260ef5f9e66",
  16. "plugins": {
  17. "SMTP problems": {"status": "enabled"},
  18. "Backdoors": {"status": "enabled"},
  19. "Ubuntu Local Security Checks": {"status": "enabled"},
  20. "Gentoo Local Security Checks": {"status": "enabled"},
  21. "Oracle Linux Local Security Checks": {"status": "enabled"},
  22. "RPC": {"status": "enabled"},
  23. "Gain a shell remotely": {"status": "enabled"},
  24. "Service detection": {"status": "enabled"},
  25. "DNS": {"status": "enabled"},
  26. "Mandriva Local Security Checks": {"status": "enabled"},
  27. "Junos Local Security Checks": {"status": "enabled"},
  28. "Misc.": {"status": "enabled"},
  29. "FTP": {"status": "enabled"},
  30. "Slackware Local Security Checks": {"status": "enabled"},
  31. "Default Unix Accounts": {"status": "enabled"},
  32. "AIX Local Security Checks": {"status": "enabled"},
  33. "SNMP": {"status": "enabled"},
  34. "OracleVM Local Security Checks": {"status": "enabled"},
  35. "CGI abuses": {"status": "enabled"},
  36. "Settings": {"status": "enabled"},
  37. "CISCO": {"status": "enabled"},
  38. "Firewalls": {"status": "enabled"},
  39. "Databases": {"status": "enabled"},
  40. "Debian Local Security Checks": {"status": "enabled"},
  41. "Fedora Local Security Checks": {"status": "enabled"},
  42. "Netware": {"status": "enabled"},
  43. "Huawei Local Security Checks": {"status": "enabled"},
  44. "Windows : User management": {"status": "enabled"},
  45. "VMware ESX Local Security Checks": {"status": "enabled"},
  46. "Virtuozzo Local Security Checks": {"status": "enabled"},
  47. "CentOS Local Security Checks": {"status": "enabled"},
  48. "Peer-To-Peer File Sharing": {"status": "enabled"},
  49. "NewStart CGSL Local Security Checks": {"status": "enabled"},
  50. "General": {"status": "enabled"},
  51. "Policy Compliance": {"status": "enabled"},
  52. "Amazon Linux Local Security Checks": {"status": "enabled"},
  53. "Solaris Local Security Checks": {"status": "enabled"},
  54. "F5 Networks Local Security Checks": {"status": "enabled"},
  55. "Denial of Service": {"status": "enabled"},
  56. "Windows : Microsoft Bulletins": {"status": "enabled"},
  57. "SuSE Local Security Checks": {"status": "enabled"},
  58. "Palo Alto Local Security Checks": {"status": "enabled"},
  59. "Red Hat Local Security Checks": {"status": "enabled"},
  60. "PhotonOS Local Security Checks": {"status": "enabled"},
  61. "HP-UX Local Security Checks": {"status": "enabled"},
  62. "CGI abuses : XSS": {"status": "enabled"},
  63. "FreeBSD Local Security Checks": {"status": "enabled"},
  64. "Windows": {"status": "enabled"},
  65. "Scientific Linux Local Security Checks": {"status": "enabled"},
  66. "MacOS X Local Security Checks": {"status": "enabled"},
  67. "Web Servers": {"status": "enabled"},
  68. "SCADA": {"status": "enabled"}
  69. },
  70. "credentials": {"add": {}, "edit": {}, "delete": []},
  71. "settings": {
  72. "patch_audit_over_telnet": "no", "patch_audit_over_rsh": "no", "patch_audit_over_rexec": "no",
  73. "snmp_port": "161", "additional_snmp_port1": "161", "additional_snmp_port2": "161",
  74. "additional_snmp_port3": "161", "http_login_method": "POST", "http_reauth_delay": "",
  75. "http_login_max_redir": "0", "http_login_invert_auth_regex": "no", "http_login_auth_regex_on_headers": "no",
  76. "http_login_auth_regex_nocase": "no", "never_send_win_creds_in_the_clear": "yes",
  77. "dont_use_ntlmv1": "yes", "start_remote_registry": "no", "enable_admin_shares": "no", "ssh_known_hosts": "",
  78. "ssh_port": "22", "ssh_client_banner": "OpenSSH_5.0", "attempt_least_privilege": "no",
  79. "region_dfw_pref_name": "yes", "region_ord_pref_name": "yes", "region_iad_pref_name": "yes",
  80. "region_lon_pref_name": "yes", "region_syd_pref_name": "yes", "region_hkg_pref_name": "yes",
  81. "microsoft_azure_subscriptions_ids": "", "aws_ui_region_type": "Rest of the World",
  82. "aws_us_east_1": "", "aws_us_east_2": "", "aws_us_west_1": "", "aws_us_west_2": "",
  83. "aws_ca_central_1": "", "aws_eu_west_1": "", "aws_eu_west_2": "", "aws_eu_west_3": "",
  84. "aws_eu_central_1": "", "aws_eu_north_1": "", "aws_ap_east_1": "", "aws_ap_northeast_1": "",
  85. "aws_ap_northeast_2": "", "aws_ap_northeast_3": "", "aws_ap_southeast_1": "", "aws_ap_southeast_2": "",
  86. "aws_ap_south_1": "", "aws_me_south_1": "", "aws_sa_east_1": "", "aws_use_https": "yes",
  87. "aws_verify_ssl": "yes", "log_whole_attack": "no", "enable_plugin_debugging": "no",
  88. "audit_trail": "use_scanner_default", "include_kb": "use_scanner_default", "enable_plugin_list": "no",
  89. "custom_find_filepath_exclusions": "", "custom_find_filesystem_exclusions": "",
  90. "reduce_connections_on_congestion": "no", "network_receive_timeout": "5", "max_checks_per_host": "5",
  91. "max_hosts_per_scan": "100", "max_simult_tcp_sessions_per_host": "", "max_simult_tcp_sessions_per_scan": "",
  92. "safe_checks": "yes", "stop_scan_on_disconnect": "no", "slice_network_addresses": "no",
  93. "allow_post_scan_editing": "yes", "reverse_lookup": "no", "log_live_hosts": "no",
  94. "display_unreachable_hosts": "no", "report_verbosity": "Normal", "report_superseded_patches": "yes",
  95. "silent_dependencies": "yes", "scan_malware": "no", "samr_enumeration": "yes", "adsi_query": "yes",
  96. "wmi_query": "yes", "rid_brute_forcing": "no", "request_windows_domain_info": "no",
  97. "scan_webapps": "no", "test_default_oracle_accounts": "no", "provided_creds_only": "yes",
  98. "smtp_domain": "example.com", "smtp_from": "nobody@example.com", "smtp_to": "postmaster@[AUTO_REPLACED_IP]",
  99. "av_grace_period": "0", "report_paranoia": "Normal", "thorough_tests": "no",
  100. "svc_detection_on_all_ports": "yes", "detect_ssl": "yes", "ssl_prob_ports": "Known SSL ports",
  101. "cert_expiry_warning_days": "60", "enumerate_all_ciphers": "yes", "check_crl": "no",
  102. "tcp_scanner": "no", "tcp_firewall_detection": "Automatic (normal)", "syn_scanner": "yes",
  103. "syn_firewall_detection": "Automatic (normal)", "udp_scanner": "no", "ssh_netstat_scanner": "yes",
  104. "wmi_netstat_scanner": "yes", "snmp_scanner": "yes", "only_portscan_if_enum_failed": "yes",
  105. "verify_open_ports": "no", "unscanned_closed": "no", "portscan_range": "default",
  106. "wol_mac_addresses": "", "wol_wait_time": "5", "scan_network_printers": "no", "scan_netware_hosts": "no",
  107. "scan_ot_devices": "no", "ping_the_remote_host": "yes", "arp_ping": "yes", "tcp_ping": "yes",
  108. "tcp_ping_dest_ports": "built-in", "icmp_ping": "yes", "icmp_unreach_means_host_down": "no",
  109. "icmp_ping_retries": "2", "udp_ping": "no", "test_local_nessus_host": "yes",
  110. "fast_network_discovery": "no", "name": "test-1122", "description": ""
  111. }
  112. }
  113. class Api(object):
  114. def __init__(self, service_url='https://47.104.160.37:8001', username='admin', password='admin'):
  115. self.debugs = str()
  116. self.errors = str()
  117. self.urls = list()
  118. self.service_url = service_url
  119. self.headers = dict()
  120. self.headers['content-type'] = 'application/json'
  121. self.headers['X-API-Token'] = self.get_api_token()
  122. self.create_super_user(username, password)
  123. token = self.get_session(username, password)
  124. self.headers['X-Cookie'] = f'token={token}'
  125. def debug(self, **kwargs):
  126. for k, v in kwargs.items():
  127. self.debugs += str(k) + ':' + str(v) + ';'
  128. def error(self, **kwargs):
  129. for k, v in kwargs.items():
  130. self.errors += str(k) + ':' + str(v) + ';'
  131. def get_policy_id_by_name(self, name):
  132. """根据策略名称获取策略id"""
  133. for policy in self.get_policies():
  134. if policy['name'] != name:
  135. continue
  136. return policy['id']
  137. return ''
  138. def get_template_id_by_title(self, title):
  139. """根据策略类型标题获取策略类型id"""
  140. if title not in TEMPLATES:
  141. return ''
  142. for template in self.get_templates():
  143. if template['title'] != title:
  144. continue
  145. return template['uuid']
  146. return ''
  147. def scan_is_completed(self, scan_id):
  148. """检查执行状态"""
  149. return self.get_scan_status(scan_id) == 'completed'
  150. def create_super_user(self, username, password):
  151. """创建超级管理员用户"""
  152. # --- 是否为register ---
  153. if self.server_status() != 'register':
  154. return
  155. self.create_user(username, password)
  156. self.server_restart()
  157. # --- 等待loading结束 ---
  158. while True:
  159. try:
  160. if self.server_status() == 'ready':
  161. break
  162. print(self.server_status())
  163. time.sleep(5)
  164. except Exception as e:
  165. print(e.__class__.__name__)
  166. time.sleep(5)
  167. def create_user(self, username, password):
  168. """
  169. url: https://<ip地址>:8834/users
  170. """
  171. if 'register' != self.server_status():
  172. return 0
  173. url = f'{self.service_url}/users'
  174. data = {
  175. 'username': username,
  176. 'password': password,
  177. 'permissions': 128,
  178. }
  179. print('NessusApiLog:create_user:url:', url)
  180. response = requests.post(url=url, json=data, headers=self.headers, verify=False)
  181. print('NessusApiLog:create_user:result:', response.status_code)
  182. if response.status_code > 300:
  183. self.error(method='create_user', result=response.text)
  184. return 0
  185. else:
  186. self.urls.append(url)
  187. self.debug(method='create_user', result=response.text)
  188. return response.json()['id']
  189. def server_restart(self):
  190. """
  191. url: https://<ip地址>:8834/server/restart
  192. """
  193. url = f'{self.service_url}/server/restart'
  194. print('NessusApiLog:server_restart:url:', url)
  195. response = requests.post(url=url, headers=self.headers, verify=False)
  196. print('NessusApiLog:server_restart:result:', response.status_code)
  197. if response.status_code > 300:
  198. self.error(method='server_restart', result=response.text)
  199. return False
  200. else:
  201. self.debug(method='server_restart', result=response.text)
  202. return True
  203. def server_status(self):
  204. """
  205. url: https://118.190.217.96:8001/server/status
  206. status:
  207. loading
  208. register
  209. ready
  210. """
  211. url = f'{self.service_url}/server/status'
  212. print('NessusApiLog:server_status:url:', url)
  213. response = requests.get(url=url, headers=self.headers, verify=False)
  214. print('NessusApiLog:server_status:result:', response.status_code)
  215. if response.status_code > 300:
  216. self.error(method='restart', result=response.text)
  217. return ''
  218. else:
  219. self.debug(method='restart', result=response.text)
  220. return response.json()['status']
  221. def get_session(self, username, password):
  222. """
  223. url: http://<ip地址>:8834/session
  224. """
  225. url = f'{self.service_url}/session'
  226. data = {
  227. 'username': username,
  228. 'password': password,
  229. }
  230. print('NessusApiLog:get_token:url:', url)
  231. response = requests.post(url=url, json=data, headers=self.headers, verify=False)
  232. print('NessusApiLog:get_token:result:', response.status_code)
  233. if response.status_code > 300:
  234. self.error(method='get_token', result=response.text)
  235. else:
  236. self.urls.append(url)
  237. self.debug(method='get_token', result=response.text)
  238. return response.json()['token']
  239. def get_keys(self):
  240. """
  241. url: http://<ip地址>:8834/session/keys
  242. doc: https://47.104.160.37:8001/api#/resources/session/keys
  243. """
  244. url = f'{self.service_url}/session/keys'
  245. print('NessusApiLog:get_keys:url:', url)
  246. response = requests.put(url=url, headers={'content-type': 'application/json'}, verify=False)
  247. print('NessusApiLog:get_keys:result:', response.status_code)
  248. if response.status_code > 300:
  249. self.error(method='get_keys', result=response.text)
  250. return {}
  251. else:
  252. self.debug(method='get_keys', result=response.text)
  253. return response.json()
  254. def get_policy(self, policy_id):
  255. """
  256. url: http://<ip地址>:8834/policies/<policy_id>
  257. """
  258. url = f'{self.service_url}/policies/{policy_id}'
  259. print('NessusApiLog:get_policy:url:', url)
  260. response = requests.get(url, headers=self.headers, verify=False)
  261. print('NessusApiLog:get_policy:result:', response.status_code)
  262. if response.status_code > 300:
  263. self.error(method='get_policy', result=response.text)
  264. else:
  265. self.urls.append(url)
  266. self.debug(method='get_policy', result=response.text)
  267. return response.json()
  268. def get_policies(self):
  269. """
  270. url: http://<ip地址>:8834/policies
  271. """
  272. url = f'{self.service_url}/policies'
  273. print('NessusApiLog:get_policies:url:', url)
  274. response = requests.get(url, headers=self.headers, verify=False, timeout=300)
  275. print('NessusApiLog:get_policies:result:', response.status_code)
  276. if response.status_code > 300:
  277. self.error(method='get_policies', result=response.text)
  278. else:
  279. self.urls.append(url)
  280. self.debug(method='get_policies', result=response.text)
  281. return response.json()['policies']
  282. def get_scan_status(self, scan_id):
  283. """
  284. url: http://<ip地址>:8834/scans/<scan_id>
  285. """
  286. url = f'{self.service_url}/scans/{scan_id}'
  287. params = {'includeHostDetailsForHostDiscovery': 'true', 'limit': 2500}
  288. print('NessusApiLog:get_scan:url:', url)
  289. response = requests.get(url, headers=self.headers, params=params, verify=False)
  290. print('NessusApiLog:get_scan:result:', response.status_code)
  291. if response.status_code > 300:
  292. self.error(method='get_scan', result=response.text)
  293. else:
  294. self.urls.append(url)
  295. self.debug(method='get_scan', result=response.text)
  296. return response.json()['info']['status']
  297. def get_scan(self, scan_id):
  298. """
  299. url: http://<ip地址>:8834/scans/<scan_id>
  300. """
  301. url = f'{self.service_url}/scans/{scan_id}'
  302. params = {'includeHostDetailsForHostDiscovery': 'true', 'limit': 2500}
  303. print('NessusApiLog:get_scan:url:', url)
  304. response = requests.get(url, headers=self.headers, params=params, verify=False)
  305. print('NessusApiLog:get_scan:result:', response.status_code)
  306. if response.status_code > 300:
  307. self.error(method='get_scan', result=response.text)
  308. else:
  309. self.urls.append(url)
  310. self.debug(method='get_scan', result=response.text)
  311. return response.json()['hosts']
  312. def get_scans(self):
  313. """
  314. url: http://<ip地址>:8834/scans
  315. """
  316. url = f'{self.service_url}/scans'
  317. print('NessusApiLog:get_scans:url:', url)
  318. response = requests.get(url, headers=self.headers, verify=False)
  319. print('NessusApiLog:get_scans:result:', response.status_code)
  320. if response.status_code > 300:
  321. self.error(method='get_scans', result=response.text)
  322. else:
  323. self.urls.append(url)
  324. self.debug(method='get_scans', result=response.text)
  325. return response.json()['scans']
  326. def create_scan(self, template_uuid, scan_name, policy_id, targets):
  327. """
  328. url: http://<ip地址>:8834/scans
  329. doc: https://192.168.20.162:8834/api#/resources/scans/create
  330. """
  331. url = f'{self.service_url}/scans'
  332. data = {
  333. "uuid": template_uuid,
  334. "settings": {
  335. "emails": "",
  336. "filter_type": "and",
  337. "filters": [],
  338. "launch_now": True,
  339. "enabled": False,
  340. "name": scan_name,
  341. "description": "",
  342. # "folder_id": 3,
  343. # "scanner_id": "1",
  344. "policy_id": str(policy_id),
  345. "text_targets": targets,
  346. "file_targets": ""
  347. }
  348. }
  349. print('NessusApiLog:create_scan:url:', url)
  350. response = requests.post(url, json=data, headers=self.headers, verify=False)
  351. print('NessusApiLog:create_scan:result:', response.status_code)
  352. if response.status_code > 300:
  353. self.error(method='create_scan', result=response.text)
  354. else:
  355. self.urls.append(url)
  356. self.debug(method='create_scan', result=response.text)
  357. return response.json()['scan']['id']
  358. def delete_scan(self, scan_id):
  359. """
  360. url: http://<ip地址>:8834/scans/<scan_id>
  361. """
  362. url = f'{self.service_url}/scans/{scan_id}'
  363. print('NessusApiLog:delete_scan:url:', url)
  364. response = requests.delete(url, headers=self.headers, verify=False)
  365. print('NessusApiLog:delete_scan:result:', response.status_code)
  366. if response.status_code > 300:
  367. self.error(method='delete_scan', result=response.text)
  368. return False
  369. else:
  370. self.urls.append(url)
  371. self.debug(method='delete_scan', result=response.text)
  372. return True
  373. def start_scan(self, scan_id):
  374. """
  375. url: http://<ip地址>:8834/scans/<scan_id>/launch
  376. """
  377. url = f'{self.service_url}/scans/{scan_id}/launch'
  378. print('NessusApiLog:start_scan:url:', url)
  379. response = requests.post(url, json={}, headers=self.headers, verify=False)
  380. print('NessusApiLog:start_scan:result:', response.status_code)
  381. if response.status_code > 300:
  382. self.error(method='start_scan', result=response.text)
  383. else:
  384. self.urls.append(url)
  385. self.debug(method='start_scan', result=response.text)
  386. return response.json()
  387. def get_host(self, scan_id, host_id):
  388. """
  389. url: https://192.168.20.162:8834/scans/68/hosts/232
  390. doc: https://192.168.20.162:8834/api#/resources/scans/host-details
  391. """
  392. url = f'{self.service_url}/scans/{scan_id}/hosts/{host_id}'
  393. print('NessusApiLog:get_host:url:', url)
  394. response = requests.get(url, params={}, headers=self.headers, verify=False)
  395. print('NessusApiLog:get_host:result:', response.status_code)
  396. if response.status_code > 300:
  397. self.error(method='get_host', result=response.text)
  398. else:
  399. self.urls.append(url)
  400. self.debug(method='get_host', result=response.text)
  401. return response.json()
  402. def get_plugin(self, scan_id, host_id, plugin_id):
  403. """
  404. url: https://192.168.20.162:8834/scans/68/hosts/232/plugins/100464
  405. """
  406. url = f'{self.service_url}/scans/{scan_id}/hosts/{host_id}/plugins/{plugin_id}'
  407. print('NessusApiLog:get_plugin:url:', url)
  408. response = requests.get(url, params={}, headers=self.headers, verify=False)
  409. print('NessusApiLog:get_plugin:result:', response.status_code)
  410. if response.status_code > 300:
  411. self.error(method='get_plugin', result=response.text)
  412. else:
  413. self.urls.append(url)
  414. self.debug(method='get_plugin', result=response.text)
  415. return response.json()
  416. def get_plugin_info(self, plugin_id):
  417. """
  418. url: https://192.168.20.162:8834/plugins/plugin/{id}
  419. doc: https://192.168.20.162:8834/api#/resources/plugins/plugin-details
  420. """
  421. url = f'{self.service_url}/plugins/plugin/{plugin_id}'
  422. print('NessusApiLog:get_plugin_info:url:', url)
  423. response = requests.get(url, params={}, headers=self.headers, verify=False)
  424. print('NessusApiLog:get_plugin_info:result:', response.status_code)
  425. if response.status_code > 300:
  426. self.error(method='get_plugin_info', result=response.text)
  427. else:
  428. self.urls.append(url)
  429. self.debug(method='get_plugin_info', result=response.text)
  430. return response.json()
  431. def get_templates(self, template_type='policy'):
  432. """
  433. url: https://172.30.2.8:8001/editor/{type}/templates
  434. doc: https://172.30.2.8:8001/api#/resources/editor/list
  435. """
  436. url = f'{self.service_url}/editor/{template_type}/templates'
  437. print('NessusApiLog:get_templates:url:', url)
  438. response = requests.get(url, params={}, headers=self.headers, verify=False)
  439. print('NessusApiLog:get_templates:result:', response.status_code)
  440. if response.status_code > 300:
  441. self.error(method='get_templates', result=response.text)
  442. else:
  443. self.urls.append(url)
  444. self.debug(method='get_templates', result=response.text)
  445. return response.json()['templates']
  446. def create_policy(self, template_uuid, policy_name, settings=None):
  447. """
  448. url: https://192.168.20.162:8834/policies
  449. doc: https://192.168.20.162:8834/api#/resources/policies/create
  450. """
  451. url = f'{self.service_url}/policies/'
  452. data = copy.copy(DefaultPolicyForAdvancedScan)
  453. data['uuid'] = template_uuid
  454. data['settings']['name'] = policy_name
  455. if settings:
  456. data['settings'].update(settings)
  457. print('NessusApiLog:create_policy:url:', url)
  458. response = requests.post(url, json=data, headers=self.headers, verify=False)
  459. print('NessusApiLog:create_policy:result:', response.status_code)
  460. if response.status_code > 300:
  461. self.error(method='create_policy', result=response.text)
  462. return ''
  463. else:
  464. self.urls.append(url)
  465. self.debug(method='create_policy', result=response.text)
  466. return response.json()['policy_id']
  467. def get_api_token(self):
  468. """
  469. url: https://192.168.20.162:8844/nessus6.js?v=1583846805284
  470. """
  471. url = f'{self.service_url}/nessus6.js'
  472. params = {'v': 1583846805284}
  473. print('NessusApiLog:get_api_token:url:', url)
  474. response = requests.get(url=url, params=params, headers=self.headers, verify=False)
  475. print('NessusApiLog:get_api_token:result:', response.status_code)
  476. if response.status_code > 300:
  477. # self.error(method='get_api_token', result=response.text)
  478. return ''
  479. else:
  480. self.urls.append(url)
  481. # self.debug(method='get_api_token', result=response.text)
  482. pattern = r'return"([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})'
  483. return re.findall(pattern, response.text)[0]
  484. if __name__ == '__main__':
  485. # api = Api(service_url='https://118.190.217.96:8001', username='admin', password='admin')
  486. # api = Api(service_url='https://47.104.224.202:8001', username='admin', password='admin')
  487. api = Api(service_url='https://172.30.2.8:8001', username='admin', password='admin')
  488. # --- test api ---
  489. # print(api.server_status())
  490. # print(api.server_restart())
  491. # print(api.server_status())
  492. print(api.get_policies())
  493. print(api.errors)
  494. # print(api.get_templates())
  495. # print(api.get_template_id_by_title('Advanced Scan'))