|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2021 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +"""Executable and reusable sample for listing asset- and user-based alerts.""" |
| 18 | + |
| 19 | +import argparse |
| 20 | +import datetime |
| 21 | +import json |
| 22 | +import sys |
| 23 | +from typing import Any, Mapping, Optional, Sequence |
| 24 | + |
| 25 | +from google.auth.transport import requests |
| 26 | + |
| 27 | +from common import chronicle_auth |
| 28 | +from common import datetime_converter |
| 29 | + |
| 30 | +CHRONICLE_API_BASE_URL = "https://backstory.googleapis.com" |
| 31 | + |
| 32 | + |
| 33 | +def initialize_command_line_args( |
| 34 | + args: Optional[Sequence[str]] = None) -> Optional[argparse.Namespace]: |
| 35 | + """Initializes and checks all the command-line arguments.""" |
| 36 | + parser = argparse.ArgumentParser() |
| 37 | + chronicle_auth.add_argument_credentials_file(parser) |
| 38 | + parser.add_argument( |
| 39 | + "-ts", |
| 40 | + "--start_time", |
| 41 | + type=datetime_converter.iso8601_datetime_utc, |
| 42 | + help=("beginning of time range, as an ISO 8601 string " + |
| 43 | + "('yyyy-mm-ddThh:mm:ss')")) |
| 44 | + parser.add_argument( |
| 45 | + "-te", |
| 46 | + "--end_time", |
| 47 | + type=datetime_converter.iso8601_datetime_utc, |
| 48 | + help=("end of time range, as an ISO 8601 string ('yyyy-mm-ddThh:mm:ss')")) |
| 49 | + parser.add_argument( |
| 50 | + "-tl", |
| 51 | + "--local_time", |
| 52 | + action="store_true", |
| 53 | + help=("times are specified in the system's local timezone " + |
| 54 | + "(default = UTC)")) |
| 55 | + |
| 56 | + # Sanity checks for the command-line arguments. |
| 57 | + parsed_args = parser.parse_args(args) |
| 58 | + s, e = parsed_args.start_time, parsed_args.end_time |
| 59 | + if parsed_args.local_time: |
| 60 | + s = s.replace(tzinfo=None).astimezone(datetime.timezone.utc) |
| 61 | + e = e.replace(tzinfo=None).astimezone(datetime.timezone.utc) |
| 62 | + if s > datetime.datetime.now().astimezone(datetime.timezone.utc): |
| 63 | + print("Error: start time should not be in the future") |
| 64 | + return None |
| 65 | + if e > datetime.datetime.now().astimezone(datetime.timezone.utc): |
| 66 | + print("Error: end time should not be in the future") |
| 67 | + return None |
| 68 | + if s >= e: |
| 69 | + print("Error: start time should not be same as or later than end time") |
| 70 | + return None |
| 71 | + |
| 72 | + return parsed_args |
| 73 | + |
| 74 | + |
| 75 | +def list_alerts( |
| 76 | + http_session: requests.AuthorizedSession, |
| 77 | + start_time: datetime.datetime, |
| 78 | + end_time: datetime.datetime, |
| 79 | + page_size: Optional[int] = 100000) -> Mapping[str, Sequence[Any]]: |
| 80 | + """Lists up to 100,000 asset- and user-based alerts in the given time range. |
| 81 | +
|
| 82 | + If you receive the maximum number of results, there might still be more |
| 83 | + discovered within the specified time range. You might want to narrow the time |
| 84 | + range and issue the call again to ensure you have visibility on the results. |
| 85 | +
|
| 86 | + Args: |
| 87 | + http_session: Authorized session for HTTP requests. |
| 88 | + start_time: The inclusive beginning of the time range of alerts to return, |
| 89 | + with any timezone (even a timezone-unaware datetime object, i.e. local |
| 90 | + time). |
| 91 | + end_time: The exclusive end of the time range of alerts to return, with any |
| 92 | + timezone (even a timezone-unaware datetime object, i.e. local time). |
| 93 | + page_size: Maximum number of alerts to return, up to 100,000 (default = |
| 94 | + 100,000). |
| 95 | +
|
| 96 | + Returns: |
| 97 | + { |
| 98 | + "alerts": [ |
| 99 | + ...One or more asset alerts (if zero, no "alerts" field at all)... |
| 100 | + ], |
| 101 | + "userAlerts": [ |
| 102 | + ...One or more user alerts (if zero, no "userAlerts" field at all)... |
| 103 | + ] |
| 104 | + } |
| 105 | +
|
| 106 | + Asset alert structure: |
| 107 | + { |
| 108 | + "asset": { |
| 109 | + "hostname": "..." <-- Or IP address, MAC address, product ID |
| 110 | + }, |
| 111 | + "alertInfos": [ |
| 112 | + ...One or more alert infos... |
| 113 | + ] |
| 114 | + } |
| 115 | +
|
| 116 | + User alert structure: |
| 117 | + { |
| 118 | + "user": { |
| 119 | + "email": "..." <-- Or user name, Windows SID, employee ID, LDAP ID |
| 120 | + }, |
| 121 | + "alertInfos": [ |
| 122 | + ...One or more alert infos... |
| 123 | + ] |
| 124 | + } |
| 125 | +
|
| 126 | + Alert info structure: |
| 127 | + { |
| 128 | + "name": "...", |
| 129 | + "sourceProduct": "...", |
| 130 | + "timestamp": "yyyy-mm-ddThh:mm:ssZ", |
| 131 | + "rawLog": "...", <-- Base64 encoded |
| 132 | + "uri": [ |
| 133 | + "https://customer.backstory.chronicle.security/..." |
| 134 | + ], |
| 135 | + "udmEvent": { |
| 136 | + ... |
| 137 | + } |
| 138 | + } |
| 139 | +
|
| 140 | + Raises: |
| 141 | + requests.exceptions.HTTPError: HTTP request resulted in an error |
| 142 | + (response.status_code >= 400). |
| 143 | + """ |
| 144 | + url = f"{CHRONICLE_API_BASE_URL}/v1/alert/listalerts" |
| 145 | + params = { |
| 146 | + "start_time": datetime_converter.strftime(start_time), |
| 147 | + "end_time": datetime_converter.strftime(end_time), |
| 148 | + "page_size": page_size |
| 149 | + } |
| 150 | + response = http_session.request("GET", url, params=params) |
| 151 | + |
| 152 | + if response.status_code >= 400: |
| 153 | + print(response.text) |
| 154 | + response.raise_for_status() |
| 155 | + return response.json() |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + cli = initialize_command_line_args() |
| 160 | + if not cli: |
| 161 | + sys.exit(1) # A sanity check failed. |
| 162 | + |
| 163 | + start, end = cli.start_time, cli.end_time |
| 164 | + if cli.local_time: |
| 165 | + start, end = start.replace(tzinfo=None), end.replace(tzinfo=None) |
| 166 | + |
| 167 | + session = chronicle_auth.initialize_http_session(cli.credentials_file) |
| 168 | + print(json.dumps(list_alerts(session, start, end), indent=2)) |
0 commit comments