|
| 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). |
0 commit comments