Facebook Google Plus Twitter LinkedIn YouTube RSS Menu Search Resource - BlogResource - WebinarResource - ReportResource - Eventicons_066 icons_067icons_068icons_069icons_070

Tenable 博客

订阅

Tips on Using the Tenable Python SDK: How to Run Internal Scans, Scan Imports and Exports and More

The Tenable Python SDK was built to provide Tenable.io™ users with the ability to leverage the Tenable.io API by building their own scripts, programs and modules that can seamlessly interact with their data in the Tenable.io platform.

If you’re unfamiliar with how to get started using the Python SDK, refer to my past blog post or see the README for the project in github.

前提条件

The examples used in the post will assume:

  • Python 2.7 or 3.4+ installed
  • An administrator account in Tenable.io with generated API keys
  • A Nessus scanner linked to Tenable.io

Running an internal scan

In this section, you’ll learn how to run an internal scan using the Tenable.io Python SDK.

The code

from tenable_io.client import TenableIOClient
from tenable_io.api.scans import ScanCreateRequest
from tenable_io.api.models import ScanSettings
client = TenableIOClient(access_key='{YOUR ACCESS KEY}', secret_key='{YOUR SECRET KEY}')
scanners = {scanner.name: scanner.id for scanner in client.scanners_api.list().scanners}
template = client.scan_helper.template(name='basic')
scan_id = client.scans_api.create(
 ScanCreateRequest(
 template.uuid,
 ScanSettings(
 ‘{YOUR SCAN NAME}’,
 ‘{YOUR SCAN TARGETS}’,
 scanner_id=scanners['{YOUR SCANNER NAME}']
 )
 )
)
scan = client.scan_helper.id(scan_id)
scan.launch()

注:Be sure to fill in the variables wrapped in curly brackets above with your own information.

The first several lines are importing the Tenable.io SDK client and models for creating your scan.

from tenable_io.client import TenableIOClient
from tenable_io.api.scans import ScanCreateRequest
from tenable_io.api.models import ScanSettings
Next the client needs to be initialized with your API keys.
client = TenableIOClient(access_key='{YOUR ACCESS KEY}', secret_key='{YOUR SECRET KEY}')

The next line will create a dictionary of all linked scanner names with their scanner ID.

scanners = {scanner.name: scanner.id for scanner in client.scanners_api.list().scanners}

The next line will get the policy ID (internally known as the template ID) for the scan you’d like to run. In this example, the ‘Basic’ scan template is used.

template = client.scan_helper.template(name='basic')

Finally, you’ll use all these details to create a “CreateScanRequest” object that can be passed to the API to create your scan.

scan_id = client.scans_api.create(
 ScanCreateRequest(
 template.uuid,
 ScanSettings(
 ‘{YOUR SCAN NAME}’,
 ‘{YOUR SCAN TARGETS}’,
 scanner_id=scanners['{YOUR SCANNER NAME}']
 )
 )
)

注:Scan targets should be defined the same way they would be defined in the User Interface, using commas to separate targets.

With the scan successfully created, all that’s left is to get the “ScanRef” of your scan using its scan ID, which will give you access to all the scan controls, including launching the scan, as shown in the final line.

scan = client.scan_helper.id(scan_id)
scan.launch()

Shortly after running this script, you can confirm it worked by checking the Scans page in Tenable.io. In this case, the scan was named “My Basic Scan” and was set to scan three IPs.

And after it completes.

Exporting a scan report by name

Another use case important to many users is the ability to export a previously run scan to share results with management or other stakeholders. This can also be done with ease using the SDK.

The code

from tenable_io.client import TenableIOClient

client = TenableIOClient(access_key='{YOUR ACCESS KEY}', secret_key='{YOUR SECRET KEY}')
scans = {scan.name: scan.id for scan in client.scans_api.list().scans}
scan = client.scan_helper.id(scans['{YOUR SCAN NAME}'])
scan.download('{YOUR SCAN NAME}.pdf')

As in the example above, first you will import the Tenable.io SDK client and initialize it using your API keys.

from tenable_io.client import TenableIOClient

client = TenableIOClient(access_key='{YOUR ACCESS KEY}', secret_key='{YOUR SECRET KEY}')

Next, you’ll generate a dictionary of your scan names and their associated ID.

scans = {scan.name: scan.id for scan in client.scans_api.list().scans}

Again, similar to the example above, you’ll create a “ScanRef” of your desired scan by supplying the scan’s name.

scan = client.scan_helper.id(scans['{YOUR SCAN NAME}'])

Finally, the last line will download the scan report, which is a PDF by default. Optionally, you can also pass in additional parameters from “ScanExportRequest” to export the report in a different format such as CSV or HTML.

scan.download('{YOUR SCAN NAME}.pdf')

Importing a Nessus scan into Tenable.io

Another solution that may be helpful to some users is the ability to import a Nessus scan from an unlinked scanner into Tenable.io to get a more complete view of their current Cyber Exposure.

The code

import os
from tenable_io.client import TenableIOClient

client = TenableIOClient(access_key='{YOUR ACCESS KEY}', secret_key='{YOUR SECRET KEY}')
dir_path = os.path.dirname(os.path.realpath(__file__))
file = os.path.join(dir_path, '{YOUR NESSUS FILE}')
client.scan_helper.import_scan(file, True)

The first few lines of this example are the same as the last example, with the addition of the Python os module, which will be used to locate the file to upload. In this example, the file should be in the same directory as the script being run.

import os
from tenable_io.client import TenableIOClient

client = TenableIOClient(access_key='{YOUR ACCESS KEY}', secret_key='{YOUR SECRET KEY}')

The next lines use the os module to locate the path of the running script, then get the full path of the scan results file you plan to upload.

dir_path = os.path.dirname(os.path.realpath(__file__))
file = os.path.join(dir_path, '{YOUR NESSUS FILE}')

Finally, you can use the scan_helper “import_scan” function to upload your scan result.

client.scan_helper.import_scan(file, True)

After running the script, you should be able to confirm it worked by checking the Scans page in Tenable.io for your uploaded scan. In this example, the scan was named “offlineScanResults.nessus”.

Tips

One tip that can come in handy when using multiple scripts or deploying your scripts to other machines is to set your API keys in an INI file or as environment variables for the Tenable client to use.

INI example

Create a new file in the same directory that you will execute your script from called “tenable_io.ini”. You can format this file like the example below. Notice you can also easily set the logging level when using this approach. If you have a script that is failing for unknown reasons, setting this to INFO or DEBUG can be helpful.

[tenable_io]
access_key = 1111d58e443e08e080790193e27ae151c16b0415270b738137e50eecbcc08d74
secret_key = 22220bf73a6bcb0cf4bcd9cf5839bff21357f2cd81884e4984e8ed4ecd4b6d83
logging_level = ERROR

Environment variables

If you’d rather not go the route of the INI file, you can also set the TENABLEIO_ACCESS_KEY and TENABLEIO_SECRET_KEY environment variables, which will supply your API keys to the client.

For more information

相关文章

您可加以利用的网络安全新闻

输入您的电子邮件,绝不要错过 Tenable 专家的及时提醒和安全指导。

Tenable Vulnerability Management

可全面访问基于云的现代化漏洞管理平台,从而以无可比拟的精确度发现并追踪所有资产。

Tenable Vulnerability Management 试用版还包含 Tenable Lumin 和 Tenable Web App Scanning。

Tenable Vulnerability Management

可全面访问基于云的现代化漏洞管理平台,从而以无可比拟的精确度发现并追踪所有资产。 立即购买年度订阅。

100 项资产

选择您的订阅选项:

立即购买

Tenable Vulnerability Management

可全面访问基于云的现代化漏洞管理平台,从而以无可比拟的精确度发现并追踪所有资产。

Tenable Vulnerability Management 试用版还包含 Tenable Lumin 和 Tenable Web App Scanning。

Tenable Vulnerability Management

可全面访问基于云的现代化漏洞管理平台,从而以无可比拟的精确度发现并追踪所有资产。 立即购买年度订阅。

100 项资产

选择您的订阅选项:

立即购买

Tenable Vulnerability Management

可全面访问基于云的现代化漏洞管理平台,从而以无可比拟的精确度发现并追踪所有资产。

Tenable Vulnerability Management 试用版还包含 Tenable Lumin 和 Tenable Web App Scanning。

Tenable Vulnerability Management

可全面访问基于云的现代化漏洞管理平台,从而以无可比拟的精确度发现并追踪所有资产。 立即购买年度订阅。

100 项资产

选择您的订阅选项:

立即购买

试用 Tenable Web App Scanning

您可以通过 Tenable One 风险暴露管理平台完全访问我们专为现代应用程序量身打造的最新 Web 应用程序扫描产品。可安全扫描全部在线资产组合的漏洞,具有高度准确性,而且无需繁重的手动操作或中断关键的 Web 应用程序。立即注册。

Tenable Web App Scanning 试用版还包含 Tenable Vulnerability Management 和 Tenable Lumin。

购买 Tenable Web App Scanning

可全面访问基于云的现代化漏洞管理平台,从而以无可比拟的精确度发现并追踪所有资产。 立即购买年度订阅。

5 个 FQDN

$3,578

立即购买

试用 Tenable Lumin

使用 Tenable Lumin 直观呈现及探索您的风险暴露管理,长期追踪风险降低状况,并比照同行业者进行基准衡量。

Tenable Lumin 试用版还包括 Tenable Vulnerability Management 和 Tenable Web App Scanning。

购买 Tenable Lumin

联系销售代表,了解 Tenable Lumin 如何帮助您获取整个企业的洞见并管理网络安全风险。

免费试用 Tenable Nessus Professional

免费试用 7 天

Tenable Nessus 是当今市场上功能最全面的漏洞扫描器。

新 - Tenable Nessus Expert
不可用

Nessus Expert 添加了更多功能,包括外部攻击面扫描,以及添加域和扫描云基础设施的功能。单击此处试用 Nessus Expert。

填写下面的表格可继续试用 Nessus Pro。

购买 Tenable Nessus Professional

Tenable Nessus 是当今市场上功能最全面的漏洞扫描器。Tenable Nessus Professional 可帮助自动化漏洞扫描流程、节省合规周期的时间,并调动起 IT 团队的积极性。

购买多年期许可,即享优惠价格添加高级支持功能,获取一年 365 天、一天 24 小时的电话、社区和聊天支持。

选择您的许可证

购买多年期许可,即享优惠价格

添加支持和培训

免费试用 Tenable Nessus Expert

免费试用 7 天

Nessus Expert 针对现代攻击面而量身打造,可以查看更多信息,保护企业免遭从 IT 到云中漏洞的攻击。

已经有 Tenable Nessus Professional?
升级到 Nessus Expert,免费试用 7 天。

购买 Tenable Nessus Expert

Nessus Expert 针对现代攻击面而量身打造,可以查看更多信息,保护企业免遭从 IT 到云中漏洞的攻击。

选择您的许可证

购买多年许可证,节省幅度更大。

添加支持和培训