Skip to content

Commit 4e50d01

Browse files
committed
adding custom columns script
1 parent e2fb4a4 commit 4e50d01

File tree

3 files changed

+153
-6
lines changed

3 files changed

+153
-6
lines changed

articles/defender-for-iot/organizations/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@
274274
items:
275275
- name: OT network pre-deployment checklist
276276
href: pre-deployment-checklist.md
277+
- name: Custom columns sample script
278+
href: custom-columns-sample-script.md
277279
- name: Microsoft Defender for IoT ninja training
278280
href: resources-training-sessions.md
279281
- name: Regional availability
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
title: Sample automation script for custom columns on on-premises management consoles - Microsoft Defender for IoT
3+
description: Learn how to Learn how to view and manage OT devices (assets) from the Device inventory page on an on-premises management console.
4+
ms.topic: how-to
5+
ms.date: 07/12/2022
6+
7+
---
8+
9+
# Sample automation script for custom columns on on-premises management consoles
10+
11+
This article shows a sample script to use when adding custom columns to your on-premises management console **Device inventory** page.
12+
13+
For more information, see [[Add data using automation](#tab/automation)](how-to-investigate-all-enterprise-sensor-detections-in-a-device-inventory.md#add-data-using-automationtabautomation).
14+
15+
## Sample script for custom columns
16+
17+
Copy the following code to a local file and then modify it as needed to create your sample columns.
18+
19+
```python
20+
#!/usr/local/bin/python
21+
# coding: utf8
22+
23+
from cyberx.custom_columns.custom_column import CustomColumnCommand
24+
from cyberx.custom_columns.utils import TimeoutError
25+
import requests
26+
VA_SCORE = '0'
27+
score = 'Secure Device'
28+
29+
30+
class Impl(CustomColumnCommand):
31+
""" Here you can define global script-wise variables
32+
For example:
33+
name = ""
34+
In order to access those variable you should prefix it with "self." (self.name). """
35+
36+
""" This method runs only once, before traversing all the assets in the inventory.
37+
You should use it to fetch global script-wise data from an external resource and store it in memory
38+
in order to prevent from the script to perform costly operation for each asset in the inventory. """
39+
40+
41+
42+
def pre_calculation(self):
43+
self.log_info ("Start Pre-Calc")
44+
AccessToken = '27b2b023d6924a9d8885c07eace30478'
45+
self.VA_SCORE = requests.get(url = 'https://10.10.3.11/api/v1/reports/vulnerabilities/devices', headers = {'Authorization':AccessToken}, verify = False).json()
46+
self.log_info ("End Pre-Calc")
47+
pass
48+
49+
""" This method runs only once, after traversing all the assets in the inventory.
50+
You should use it to clean resources created or opened in the pre_calculation method.
51+
Such resources could be temporary files or db connections for example. """
52+
def post_calculation(self):
53+
pass
54+
55+
""" This method runs for each asset in the inventory.
56+
Here you should compute the requested value and return it using the valid_result or error_result utility methods (explained below).
57+
In order to access the asset data use the following list:
58+
59+
asset inventory column name - data key (data type)
60+
=========================== - ====================
61+
Appliances - 'xsenses' (array of strings)
62+
Business Units - 'businessUnits' (array of strings)
63+
Discovered - 'discovered' (date)
64+
Firmware Version - 'firmwareVersion' (string)
65+
IP Address - 'ipAddress' (string)
66+
Is Authorized - 'isAuthorized' (boolean)
67+
Is Known as Scanner - 'isScanner' (boolean)
68+
Is Programming Asset - 'isProgramming' (boolean)
69+
Last Activity - 'lastActivity' (date)
70+
MAC Address - 'macAddress' (string)
71+
Model - 'model' (string)
72+
Module Address - 'moduleAddress' (string)
73+
Name - 'name' (string)
74+
Operating System - 'operatingSystem' (string)
75+
Protocols - 'protocols' (array of strings)
76+
Rack - 'rack' (string)
77+
Region - 'region' (string), 'regionId' (integer)
78+
Serial - 'serial' (string)
79+
Site - 'site' (string), 'siteId' (integer)
80+
Slot - 'slot' (string)
81+
Type - 'type' (string)
82+
Unhandled Alerts - 'unhandledAlerts' (integer)
83+
Vendor - 'vendor' (string)
84+
Zone - 'zone' (string), 'zoneId' (integer)
85+
86+
For example, in order to get the asset's IP address you should use asset['ipAddress'] and you will get it as a string. """
87+
def calculate(self, asset):
88+
self.log_info ("Start Calculate")
89+
90+
ipAddress = asset['ipAddress']
91+
score = 'Secure Device'
92+
93+
94+
for device in self.VA_SCORE:
95+
if ipAddress in device['ipAddresses']:
96+
score = device['securityScore']
97+
98+
self.log_info ("End Calculate")
99+
return self.valid_result(score)
100+
101+
""" This method is for testing the script functionality.
102+
You should use it in order to test that you are able to access an external resource or perform a complex computation.
103+
A good practice will be to at least run the pre_calculation and post_calculation methods and validate they work as expected.
104+
You should use the valid_result or error_result utility methods (explained below) when returning the test result. """
105+
def test(self):
106+
return self.valid_result(score)
107+
108+
""" This method return TCP ports to open for outgoing communication (if needed).
109+
It should just return an array of port numbers, for example [234, 334, 3562]. """
110+
def get_outgoing_tcp_ports(self):
111+
return []
112+
113+
""" This method return TCP ports to open for incoming communication (if needed).
114+
It should just return an array of port numbers, for example [234, 334, 3562]. """
115+
def get_incoming_tcp_ports(self):
116+
return []
117+
118+
""" This method return UDP ports to open for outgoing communication (if needed).
119+
It should just return an array of port numbers, for example [234, 334, 3562]. """
120+
def get_outgoing_udp_ports(self):
121+
return []
122+
123+
""" This method return UDP ports to open for incoming communication (if needed).
124+
It should just return an array of port numbers, for example [234, 334, 3562]. """
125+
def get_incoming_udp_ports(self):
126+
return []
127+
128+
""" Utility methods at your disposal:
129+
130+
self.valid_result(result):
131+
This method receives the result and indicates that the computation went well.
132+
133+
self.error_result(error_message):
134+
This method receives an error message and indicates that the computation did not went well.
135+
136+
self.log_info(message):
137+
This method will log the message in the dedicated custom columns log file named '/var/cyberx/logs/custom-columns.log'
138+
139+
self.log_error(error_message):
140+
This method will log the error message as an error in the dedicated custom columns log file named '/var/cyberx/logs/custom-columns.log' """
141+
```
142+
143+
## Next steps
144+
145+
For more information, see [Manage your OT device inventory from an on-premises management console](how-to-investigate-all-enterprise-sensor-detections-in-a-device-inventory.md).

articles/defender-for-iot/organizations/how-to-investigate-all-enterprise-sensor-detections-in-a-device-inventory.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ To export device inventory data, select the **Import/Export file** :::image type
4747

4848
Save the exported file locally.
4949

50-
## Enhance device inventory data
50+
## Add to and enhance device inventory data
5151

52-
Enhance the data in your device inventory with information from other sources, such as CMDBs, DNS, firewalls, and Web APIs. Use enhanced data to learn things such as:
52+
Use information from other sources, such as CMDBs, DNS, firewalls, and Web APIs, to enhance the data shown in your device inventory. For example, use enhanced data to present information about the following items:
5353

5454
- Device purchase dates and end-of-warranty dates
5555
- Users responsible for each device
@@ -59,9 +59,9 @@ Enhance the data in your device inventory with information from other sources, s
5959
- Devices running active antivirus applications
6060
- Users signed in to devices
6161

62-
Enhancement data is shown as extra columns in the on-premises management console **Device inventory** page.
62+
Added and enhancement data is shown as extra columns, in addition to the existing columns available in the on-premises management console **Device inventory** page.
6363

64-
Enhance data by adding it manually or by running customized scripts from Defender for IoT. You can also work with Defender for IoT support to set up your system to receive Web API queries.
64+
Enhance data by adding it manually or by running a customized version of our [automation script sample](custom-columns-sample-script.md). You can also open a support ticket to set up your system to receive Web API queries.
6565

6666
For example, the following image shows an example of how you might use enhanced data in the device inventory:
6767

@@ -95,7 +95,7 @@ The new data appears in the **Device Inventory** grid.
9595

9696
To enhance your data using automation scripts:
9797

98-
1. Contact [Microsoft Support](https://support.serviceshub.microsoft.com/supportforbusiness/create?sapId=82c88f35-1b8e-f274-ec11-c6efdd6dd099) to obtain the relevant scripts.
98+
1. Copy the [sample automation script](custom-columns-sample-script.md) to a local file and modify it as needed.
9999

100100
1. Sign in to your on-premises management console, and select **Device inventory**.
101101

@@ -105,7 +105,7 @@ To enhance your data using automation scripts:
105105

106106
1. In the **Add Custom Column** dialog box, add the new column name using up to 250 UTF characters.
107107

108-
1. Select **Automatic**. When the **UPLOAD SCRIPT** and **TEST SCRIPT** buttons appear, upload and then test the script you'd received from [Microsoft Support](https://support.serviceshub.microsoft.com/supportforbusiness/create?sapId=82c88f35-1b8e-f274-ec11-c6efdd6dd099).
108+
1. Select **Automatic**. When the **UPLOAD SCRIPT** and **TEST SCRIPT** buttons appear, upload and then test the script you'd customized earlier and saved locally.
109109

110110
The new data appears in the **Device Inventory** grid.
111111

0 commit comments

Comments
 (0)